From 166687f118a6bb3c47d97218374323c3b12e9d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20B=C3=A4uerle?= Date: Wed, 28 Nov 2018 10:46:20 +0100 Subject: [PATCH 1/2] TeamStatus: Fix status posting for PRs and commits Due to some misasumptions about the ways the tfs-plugin can trigger a job, there was a too early (and duplicated) check that caused calling TeamStatus.addStatus with a null-pointer. This also fixes the status posting for commits. Fixes: JENKINS-54569 --- .../plugins/tfs/model/AbstractHookEvent.java | 10 +++----- .../hudson/plugins/tfs/util/TeamStatus.java | 25 ++++++++----------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/tfs/src/main/java/hudson/plugins/tfs/model/AbstractHookEvent.java b/tfs/src/main/java/hudson/plugins/tfs/model/AbstractHookEvent.java index 5a7cd93a3..17980e5b3 100644 --- a/tfs/src/main/java/hudson/plugins/tfs/model/AbstractHookEvent.java +++ b/tfs/src/main/java/hudson/plugins/tfs/model/AbstractHookEvent.java @@ -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); diff --git a/tfs/src/main/java/hudson/plugins/tfs/util/TeamStatus.java b/tfs/src/main/java/hudson/plugins/tfs/util/TeamStatus.java index 4e7c3e904..ba8e0e32b 100644 --- a/tfs/src/main/java/hudson/plugins/tfs/util/TeamStatus.java +++ b/tfs/src/main/java/hudson/plugins/tfs/util/TeamStatus.java @@ -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; @@ -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); @@ -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) { From ba80438f0a9e48ea8992d949129f3c9093b0b63c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20B=C3=A4uerle?= Date: Wed, 5 Dec 2018 15:21:15 +0100 Subject: [PATCH 2/2] GitPushEvent: determin git commit from refUpdates This is required for a Code pushed event to work with a generic build trigger. --- .../hudson/plugins/tfs/model/GitPushEvent.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tfs/src/main/java/hudson/plugins/tfs/model/GitPushEvent.java b/tfs/src/main/java/hudson/plugins/tfs/model/GitPushEvent.java index dce6258c4..e899b5171 100644 --- a/tfs/src/main/java/hudson/plugins/tfs/model/GitPushEvent.java +++ b/tfs/src/main/java/hudson/plugins/tfs/model/GitPushEvent.java @@ -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; @@ -102,11 +103,18 @@ static String determineProjectId(final GitRepository repository) { static String determineCommit(final GitPush gitPush) { final List 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 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) {