|
| 1 | +/* |
| 2 | +
|
| 3 | + Copyright 2015-2016 Artem Stasiuk |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +*/ |
| 18 | +package com.github.terma.jenkins.githubprcoveragestatus; |
| 19 | + |
| 20 | +import hudson.EnvVars; |
| 21 | +import hudson.model.Run; |
| 22 | +import hudson.model.TaskListener; |
| 23 | +import org.kohsuke.github.GHPullRequest; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.PrintStream; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +public class PrIdAndUrlUtils { |
| 30 | + |
| 31 | + /** |
| 32 | + * Injected by Git plugin |
| 33 | + */ |
| 34 | + public static final String GIT_URL_PROPERTY = "GIT_URL"; |
| 35 | + |
| 36 | + /** |
| 37 | + * Injected by |
| 38 | + * https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin |
| 39 | + */ |
| 40 | + public static final String GIT_PR_ID_ENV_PROPERTY = "ghprbPullId"; |
| 41 | + public static final String CHANGE_ID_PROPERTY = "CHANGE_ID"; |
| 42 | + public static final String CHANGE_URL_PROPERTY = "CHANGE_URL"; |
| 43 | + |
| 44 | + private PrIdAndUrlUtils() { |
| 45 | + throw new UnsupportedOperationException("Util class!"); |
| 46 | + } |
| 47 | + |
| 48 | + private static Integer getPullRequestBuilder(Run build, TaskListener listener) throws IOException, InterruptedException { |
| 49 | + final EnvVars envVars = build.getEnvironment(listener); |
| 50 | + final String gitPrId = envVars.get(GIT_PR_ID_ENV_PROPERTY); |
| 51 | + final String changeId = envVars.get(CHANGE_ID_PROPERTY); |
| 52 | + final String idString = gitPrId != null ? gitPrId : changeId; |
| 53 | + return idString != null ? Integer.parseInt(idString) : null; |
| 54 | + } |
| 55 | + |
| 56 | + private static Integer getMultiBranch(Map<String, String> scmVars, TaskListener listener) throws IOException { |
| 57 | + if (scmVars == null) return null; |
| 58 | + final PrintStream buildLog = listener.getLogger(); |
| 59 | + final String url = scmVars.get(GIT_URL_PROPERTY); |
| 60 | + final String branch = scmVars.get("GIT_BRANCH"); |
| 61 | + final String sha = scmVars.get("GIT_COMMIT"); |
| 62 | + buildLog.println(CompareCoverageAction.BUILD_LOG_PREFIX + String.format("Attempt to discover PR for %s @ %s", branch, sha)); |
| 63 | + GHPullRequest gitPr = ServiceRegistry.getPullRequestRepository().getPullRequestFor(url, branch, sha); |
| 64 | + int id = gitPr.getNumber(); |
| 65 | + buildLog.println(CompareCoverageAction.BUILD_LOG_PREFIX + String.format("Discovered PR %d", id)); |
| 66 | + return id; |
| 67 | + } |
| 68 | + |
| 69 | + public static int getPrId( |
| 70 | + final Map<String, String> scmVars, final Run build, final TaskListener listener) throws IOException, InterruptedException { |
| 71 | + Integer id = getPullRequestBuilder(build, listener); |
| 72 | + if (id == null) id = getMultiBranch(scmVars, listener); |
| 73 | + if (id == null) throw new UnsupportedOperationException( |
| 74 | + "Can't find " + GIT_PR_ID_ENV_PROPERTY + " or scmVars in build variables!"); |
| 75 | + return id; |
| 76 | + } |
| 77 | + |
| 78 | + public static String getGitUrl(final Map<String, String> scmVars, final Run build, final TaskListener listener) throws IOException, InterruptedException { |
| 79 | + Map<String, String> envVars = build.getEnvironment(listener); |
| 80 | + final String gitUrl = envVars.get(GIT_URL_PROPERTY); |
| 81 | + final String changeUrl = envVars.get(CHANGE_URL_PROPERTY); |
| 82 | + if (gitUrl != null) return gitUrl; |
| 83 | + else if (changeUrl != null) return changeUrl; |
| 84 | + else if (scmVars != null && scmVars.containsKey(GIT_URL_PROPERTY)) return scmVars.get(GIT_URL_PROPERTY); |
| 85 | + else throw new UnsupportedOperationException("Can't find " + GIT_URL_PROPERTY |
| 86 | + + " or " + CHANGE_URL_PROPERTY + " in envs: " + envVars); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments