-
Notifications
You must be signed in to change notification settings - Fork 385
JENKINS-58313 Add source commit and branch name #250
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
Changes from 1 commit
c559689
f11fcb4
ff8e92d
fc051d0
2817466
139cae8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import jenkins.model.Jenkins; | ||
| import jenkins.plugins.git.AbstractGitSCMSource; | ||
| import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl; | ||
| import jenkins.scm.api.SCMHead; | ||
| import jenkins.scm.api.SCMHeadObserver; | ||
|
|
@@ -73,6 +74,9 @@ private static void createBuildCommitStatus(Run<?, ?> build, TaskListener listen | |
| SCMSource src = SCMSource.SourceByItem.findSource(build.getParent()); | ||
| SCMRevision revision = src != null ? SCMRevisionAction.getRevision(src, build) : null; | ||
| if (revision != null) { // only notify if we have a revision to notify | ||
| GitHubEnvAction gitHubEnvAction = getGitHubEnvAction(revision); | ||
|
||
| build.getParent().addAction(gitHubEnvAction); | ||
|
|
||
| try { | ||
| GitHub gitHub = lookUpGitHub(build.getParent()); | ||
| try { | ||
|
|
@@ -127,6 +131,31 @@ private static void createBuildCommitStatus(Run<?, ?> build, TaskListener listen | |
| } | ||
| } | ||
|
|
||
| private static GitHubEnvAction getGitHubEnvAction(SCMRevision revision) { | ||
|
||
| GitHubEnvAction action; | ||
| if (revision instanceof PullRequestSCMRevision) { | ||
| PullRequestSCMRevision pullRequestSCMRevision = (PullRequestSCMRevision) revision; | ||
|
|
||
| action = new GitHubEnvAction( | ||
| pullRequestSCMRevision.getPullHash(), | ||
| pullRequestSCMRevision.getMergeHash(), | ||
| pullRequestSCMRevision.getBaseHash() | ||
| ); | ||
|
|
||
|
|
||
| } else if (revision instanceof SCMRevisionImpl) { | ||
| SCMRevisionImpl gitRevision = (SCMRevisionImpl) revision; | ||
|
|
||
| action = new GitHubEnvAction( | ||
| gitRevision.getHash(), | ||
| revision.getHead().getName() | ||
| ); | ||
| } else { | ||
| throw new IllegalArgumentException("did not recognize " + revision); | ||
| } | ||
| return action; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the GitHub Repository associated to a Job. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright 2019 Tim Jacomb | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.jenkinsci.plugins.github_branch_source; | ||
|
|
||
| import hudson.model.InvisibleAction; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
|
||
| /** | ||
| * Caches SCM values for a Run | ||
| */ | ||
| @Restricted(NoExternalUse.class) | ||
| public class GitHubEnvAction extends InvisibleAction { | ||
|
||
|
|
||
| private static final String CHANGE_SOURCE_COMMIT_ID = "CHANGE_SOURCE_COMMIT_ID"; | ||
| private static final String CHANGE_MERGE_COMMIT_ID = "CHANGE_MERGE_COMMIT_ID"; | ||
| private static final String CHANGE_BASE_COMMIT_ID = "CHANGE_BASE_COMMIT_ID"; | ||
| private static final String CHANGE_BRANCH = "CHANGE_BRANCH"; | ||
|
|
||
|
|
||
| private final Map<String, String> values; | ||
|
|
||
| public GitHubEnvAction(String sourceCommit, String branch) { | ||
| HashMap<String, String> theValues = new HashMap<>(); | ||
| theValues.put(CHANGE_SOURCE_COMMIT_ID, sourceCommit); | ||
| theValues.put(CHANGE_BRANCH, branch); | ||
|
|
||
| this.values = theValues; | ||
| } | ||
|
|
||
| public GitHubEnvAction(String sourceCommit, String mergeCommit, String baseCommit) { | ||
| Map<String, String> theValues = new HashMap<>(); | ||
| theValues.put(CHANGE_SOURCE_COMMIT_ID, sourceCommit); | ||
| theValues.put(CHANGE_MERGE_COMMIT_ID, mergeCommit); | ||
| theValues.put(CHANGE_BASE_COMMIT_ID, baseCommit); | ||
|
|
||
| this.values = theValues; | ||
| } | ||
|
|
||
| public Map<String, String> getValues() { | ||
| return values; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| GitHubEnvAction action = (GitHubEnvAction) o; | ||
| return Objects.equals(values, action.values); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(values); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("GitHubEnvAction{values=%s}", values); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright 2019 Tim Jacomb | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.jenkinsci.plugins.github_branch_source; | ||
|
|
||
| import hudson.EnvVars; | ||
| import hudson.Extension; | ||
| import hudson.model.EnvironmentContributor; | ||
| import hudson.model.ItemGroup; | ||
| import hudson.model.Job; | ||
| import hudson.model.TaskListener; | ||
| import javax.annotation.Nonnull; | ||
| import jenkins.branch.MultiBranchProject; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
|
||
| @Extension | ||
| @Restricted(NoExternalUse.class) | ||
| public class GitHubEnvContributor extends EnvironmentContributor { | ||
|
|
||
| public void buildEnvironmentFor(Job job, @Nonnull EnvVars envs, @Nonnull TaskListener listener) { | ||
| ItemGroup parent = job.getParent(); | ||
| if (parent instanceof MultiBranchProject) { | ||
| GitHubEnvAction action = job.getAction(GitHubEnvAction.class); | ||
| if (action != null) { | ||
| envs.putAll(action.getValues()); | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That you need to add a hard dependency on branch-api suggests this change should be an extension plugin and not in the core plugin. SCM implementations shouldn't be tied to orthogonal consumer concepts like branch-api