Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PR iteration status for generic build trigger #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tfs/src/main/java/hudson/plugins/tfs/model/BuildCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import hudson.plugins.tfs.model.servicehooks.Event;
import hudson.plugins.tfs.util.ActionHelper;
import hudson.plugins.tfs.util.MediaType;
import hudson.plugins.tfs.util.TeamRestClient;
import hudson.util.RunList;
import jenkins.model.Jenkins;
import jenkins.util.TimeDuration;
Expand Down Expand Up @@ -143,6 +144,14 @@ else if ("git.pullrequest.merged".equals(eventType)) {
final String message = event.getMessage().getText();
final String detailedMessage = event.getDetailedMessage().getText();
final Action teamPullRequestMergedDetailsAction = new TeamPullRequestMergedDetailsAction(gitPullRequest, message, detailedMessage, args.collectionUri.toString());
if (gitPullRequest.getSupportsIterations()) {
try {
final TeamRestClient client = new TeamRestClient(args.collectionUri);
args.iterationId = client.getPullRequestIteration(args);
} catch (IOException e) {
e.printStackTrace();
}
}
actions.add(teamPullRequestMergedDetailsAction);
TeamGlobalStatusAction.addIfApplicable(actions);
}
Expand Down
15 changes: 15 additions & 0 deletions tfs/src/main/java/hudson/plugins/tfs/model/GitPullRequestEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@
* by extending {@link GitPullRequest}.
*/
public class GitPullRequestEx extends GitPullRequest {
private boolean supportsIterations;
private ResourceRef[] workItemRefs;

/**
* Returns iteration support.
*/
public boolean getSupportsIterations() {
return supportsIterations;
}

/**
* Sets if iterations are supported.
*/
public void setSupportsIterations(final boolean supportsIterations) {
this.supportsIterations = supportsIterations;
}

/**
* Returns the work item references.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//CHECKSTYLE:OFF
package hudson.plugins.tfs.model;

import com.microsoft.teamfoundation.sourcecontrol.webapi.model.GitCommitRef;

public class GitPullRequestIteration {
public int id;
public GitCommitRef sourceRefCommit;
public GitCommitRef targetRefCommit;
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ static PullRequestMergeCommitCreatedEventArgs decodeGitPullRequest(final GitPull
final String pushedBy = determineCreatedBy(gitPullRequest);
final int pullRequestId = gitPullRequest.getPullRequestId();
final String targetBranch = determineTargetBranch(gitPullRequest);
final String targetCommit = gitPullRequest.getLastMergeTargetCommit().getCommitId();
final String sourceCommit = gitPullRequest.getLastMergeSourceCommit().getCommitId();

final PullRequestMergeCommitCreatedEventArgs args = new PullRequestMergeCommitCreatedEventArgs();
args.collectionUri = collectionUri;
Expand All @@ -120,6 +122,8 @@ static PullRequestMergeCommitCreatedEventArgs decodeGitPullRequest(final GitPull
args.pushedBy = pushedBy;
args.pullRequestId = pullRequestId;
args.targetBranch = targetBranch;
args.targetCommit = targetCommit;
args.sourceCommit = sourceCommit;
return args;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//CHECKSTYLE:OFF
package hudson.plugins.tfs.model;

import java.util.List;

public class ListOfGitPullRequestIteration {
public int count;
public List<GitPullRequestIteration> value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package hudson.plugins.tfs.model;

public class PullRequestMergeCommitCreatedEventArgs extends GitCodePushedEventArgs {
private static final long serialVersionUID = 1L;

public int pullRequestId;
public int iterationId = -1;

public String sourceCommit;
public String targetCommit;
}
22 changes: 22 additions & 0 deletions tfs/src/main/java/hudson/plugins/tfs/util/TeamRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.plugins.tfs.TeamCollectionConfiguration;
import hudson.plugins.tfs.model.GitCodePushedEventArgs;
import hudson.plugins.tfs.model.GitPullRequestIteration;
import hudson.plugins.tfs.model.HttpMethod;
import hudson.plugins.tfs.model.JobCompletionEventArgs;
import hudson.plugins.tfs.model.JsonPatchOperation;
import hudson.plugins.tfs.model.Link;
import hudson.plugins.tfs.model.ListOfGitPullRequestIteration;
import hudson.plugins.tfs.model.ListOfGitRepositories;
import hudson.plugins.tfs.model.PullRequestMergeCommitCreatedEventArgs;
import hudson.plugins.tfs.model.Server;
Expand Down Expand Up @@ -329,4 +331,24 @@ public void sendJobCompletionEvent(final JobCompletionEventArgs args) throws IOE
request(Void.class, HttpMethod.POST, requestUri, json, headers);
}

public int getPullRequestIteration(PullRequestMergeCommitCreatedEventArgs args) throws IOException {
final QueryString qs = new QueryString(API_VERSION, "4.1");
final URI requestUri = UriHelper.join(
collectionUri, args.projectId,
"_apis", "git",
"repositories", args.repoId,
"pullRequests", args.pullRequestId,
"iterations",
qs);

ListOfGitPullRequestIteration list = request(ListOfGitPullRequestIteration.class, HttpMethod.GET, requestUri, null);

for (GitPullRequestIteration iteration : list.value) {
if (iteration.sourceRefCommit.getCommitId().equals(args.sourceCommit)) {
return iteration.id;
}
}

return -1;
}
}