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

TeamStatus: Fix status posting for PRs and commits #217

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,10 @@ GitStatus.ResponseContributor triggerJob(final GitCodePushedEventArgs gitCodePus
final CauseAction causeAction = new CauseAction(cause);
final Action[] actionArray = ActionHelper.create(actionsWithSafeParams, causeAction);
scmTriggerItem.scheduleBuild2(quietPeriod, actionArray);
if (gitCodePushedEventArgs instanceof PullRequestMergeCommitCreatedEventArgs) {
try {
TeamStatus.createFromJob((PullRequestMergeCommitCreatedEventArgs) gitCodePushedEventArgs, job);
} catch (IOException ex) {
LOGGER.warning("Could not create TeamStatus: " + ex.toString());
}
try {
TeamStatus.createFromJob(gitCodePushedEventArgs, job);
} catch (IOException ex) {
LOGGER.warning("Could not create TeamStatus: " + ex.toString());
}

return new TeamEventsEndpoint.ScheduledResponseContributor(project);
Expand Down
16 changes: 12 additions & 4 deletions tfs/src/main/java/hudson/plugins/tfs/model/GitPushEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.microsoft.teamfoundation.core.webapi.model.TeamProjectReference;
import com.microsoft.teamfoundation.sourcecontrol.webapi.model.GitCommitRef;
import com.microsoft.teamfoundation.sourcecontrol.webapi.model.GitPush;
import com.microsoft.teamfoundation.sourcecontrol.webapi.model.GitRefUpdate;
import com.microsoft.teamfoundation.sourcecontrol.webapi.model.GitRepository;
import com.microsoft.visualstudio.services.webapi.model.IdentityRef;
import hudson.model.Action;
Expand Down Expand Up @@ -102,11 +103,18 @@ static String determineProjectId(final GitRepository repository) {

static String determineCommit(final GitPush gitPush) {
final List<GitCommitRef> commits = gitPush.getCommits();
if (commits == null || commits.size() < 1) {
return null;
if (commits != null && commits.size() > 0) {
final GitCommitRef commit = commits.get(0);
return commit.getCommitId();
}
final GitCommitRef commit = commits.get(0);
return commit.getCommitId();

final List<GitRefUpdate> refs = gitPush.getRefUpdates();
if (refs != null && refs.size() > 0) {
final GitRefUpdate ref = refs.get(0);
return ref.getNewObjectId();
}

return null;
}

static String determinePushedBy(final GitPush gitPush) {
Expand Down
25 changes: 10 additions & 15 deletions tfs/src/main/java/hudson/plugins/tfs/util/TeamStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.tfs.CommitParameterAction;
import hudson.plugins.tfs.PullRequestParameterAction;
import hudson.plugins.tfs.UnsupportedIntegrationAction;
import hudson.plugins.tfs.model.GitCodePushedEventArgs;
import hudson.plugins.tfs.model.PullRequestMergeCommitCreatedEventArgs;
Expand Down Expand Up @@ -38,21 +37,17 @@ public static void createFromRun(@Nonnull final Run<?, ?> run, @Nonnull final Ta

final CommitParameterAction commitParameter = run.getAction(CommitParameterAction.class);
final GitCodePushedEventArgs gitCodePushedEventArgs;
final PullRequestMergeCommitCreatedEventArgs pullRequestMergeCommitCreatedEventArgs;

if (commitParameter != null) {
gitCodePushedEventArgs = commitParameter.getGitCodePushedEventArgs();
if (commitParameter instanceof PullRequestParameterAction) {
final PullRequestParameterAction prpa = (PullRequestParameterAction) commitParameter;
pullRequestMergeCommitCreatedEventArgs = prpa.getPullRequestMergeCommitCreatedEventArgs();
} else {
pullRequestMergeCommitCreatedEventArgs = null;
}
} else {
if (commitParameter == null) {
// TODO: try to guess based on what we _do_ have (i.e. RevisionParameterAction)
return;
}

gitCodePushedEventArgs = commitParameter.getGitCodePushedEventArgs();
if (gitCodePushedEventArgs == null) {
return;
}

final URI collectionUri = gitCodePushedEventArgs.collectionUri;
final TeamGitStatus status = TeamGitStatus.fromRun(run);

Expand All @@ -63,19 +58,19 @@ public static void createFromRun(@Nonnull final Run<?, ?> run, @Nonnull final Ta
.pair("status", status.state.toString())
.build());

addStatus(pullRequestMergeCommitCreatedEventArgs, status);
addStatus(gitCodePushedEventArgs, status);
}

/**
* Create status for a (queued) Job.
*/
public static void createFromJob(final PullRequestMergeCommitCreatedEventArgs pullRequestMergeCommitCreatedEventArgs, final Job job) throws IOException {
public static void createFromJob(final GitCodePushedEventArgs gitCodePushedEventArgs, final Job job) throws IOException {
final TeamGitStatus status = TeamGitStatus.fromJob(job);

addStatus(pullRequestMergeCommitCreatedEventArgs, status);
addStatus(gitCodePushedEventArgs, status);
}

private static void addStatus(final PullRequestMergeCommitCreatedEventArgs gitCodePushedEventArgs, final TeamGitStatus status) throws IOException {
private static void addStatus(final GitCodePushedEventArgs gitCodePushedEventArgs, final TeamGitStatus status) throws IOException {
final PullRequestMergeCommitCreatedEventArgs pullRequestMergeCommitCreatedEventArgs;

if (gitCodePushedEventArgs instanceof PullRequestMergeCommitCreatedEventArgs) {
Expand Down