|
| 1 | +package stashpullrequestbuilder.stashpullrequestbuilder; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import hudson.model.ParameterValue; |
| 7 | +import hudson.model.ParametersAction; |
| 8 | +import hudson.model.Run; |
| 9 | +import stashpullrequestbuilder.stashpullrequestbuilder.stash.StashPullRequestResponseValue; |
| 10 | +import stashpullrequestbuilder.stashpullrequestbuilder.stash.StashPullRequestResponseValueRepository; |
| 11 | + |
| 12 | +public class ExternalLaunchSupport { |
| 13 | + private static final String PULL_REQUEST_ID = "pullRequestId"; |
| 14 | + private static final String PULL_REQUEST_VERSION = "pullRequestVersion"; |
| 15 | + private static final String PULL_REQUEST_TITLE = "pullRequestTitle"; |
| 16 | + private static final String TARGET_BRANCH = "targetBranch"; |
| 17 | + private static final String DESTINATION_COMMIT_HASH = "destinationCommitHash"; |
| 18 | + private static final String DESTINATION_REPOSITORY_NAME = "destinationRepositoryName"; |
| 19 | + private static final String DESTINATION_REPOSITORY_OWNER = "destinationRepositoryOwner"; |
| 20 | + private static final String SOURCE_BRANCH = "sourceBranch"; |
| 21 | + private static final String SOURCE_COMMIT_HASH = "sourceCommitHash"; |
| 22 | + private static final String SOURCE_REPOSITORY_NAME = "sourceRepositoryName"; |
| 23 | + private static final String SOURCE_REPOSITORY_OWNER = "sourceRepositoryOwner"; |
| 24 | + |
| 25 | + private static Map<String, String> PULL_REQUEST_NOTIFIER_FOR_BITBUCKET_MAP = new HashMap<String, String>() { |
| 26 | + { |
| 27 | + put(PULL_REQUEST_ID, "PULL_REQUEST_ID"); |
| 28 | + put(PULL_REQUEST_TITLE, "PULL_REQUEST_TITLE"); |
| 29 | + put(PULL_REQUEST_VERSION, "PULL_REQUEST_VERSION"); |
| 30 | + put(SOURCE_BRANCH, "PULL_REQUEST_FROM_BRANCH"); |
| 31 | + put(SOURCE_REPOSITORY_OWNER, "PULL_REQUEST_FROM_REPO_PROJECT_KEY"); |
| 32 | + put(SOURCE_REPOSITORY_NAME, "PULL_REQUEST_FROM_REPO_SLUG"); |
| 33 | + put(SOURCE_COMMIT_HASH, "PULL_REQUEST_FROM_HASH"); |
| 34 | + put(TARGET_BRANCH, "PULL_REQUEST_TO_BRANCH"); |
| 35 | + put(DESTINATION_REPOSITORY_OWNER, "PULL_REQUEST_TO_REPO_PROJECT_KEY"); |
| 36 | + put(DESTINATION_REPOSITORY_NAME, "PULL_REQUEST_TO_REPO_SLUG"); |
| 37 | + put(DESTINATION_COMMIT_HASH, "PULL_REQUEST_TO_HASH"); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + private static Map<String, String> STASH_PULLREQUEST_BUILDER_PLUGIN_MAP = new HashMap<String, String>() { |
| 42 | + { |
| 43 | + put(PULL_REQUEST_ID, PULL_REQUEST_ID); |
| 44 | + put(PULL_REQUEST_TITLE, PULL_REQUEST_TITLE); |
| 45 | + put(PULL_REQUEST_VERSION, PULL_REQUEST_VERSION); |
| 46 | + put(SOURCE_BRANCH, SOURCE_BRANCH); |
| 47 | + put(SOURCE_REPOSITORY_OWNER, SOURCE_REPOSITORY_OWNER); |
| 48 | + put(SOURCE_REPOSITORY_NAME, SOURCE_REPOSITORY_NAME); |
| 49 | + put(SOURCE_COMMIT_HASH, SOURCE_COMMIT_HASH); |
| 50 | + put(TARGET_BRANCH, TARGET_BRANCH); |
| 51 | + put(DESTINATION_REPOSITORY_OWNER, DESTINATION_REPOSITORY_OWNER); |
| 52 | + put(DESTINATION_REPOSITORY_NAME, DESTINATION_REPOSITORY_NAME); |
| 53 | + put(DESTINATION_COMMIT_HASH, DESTINATION_COMMIT_HASH); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + private static String getStringVariableValue(ParametersAction parametersAction, Map<String, String> keymap, String key) { |
| 58 | + ParameterValue value = parametersAction.getParameter(keymap.get(key)); |
| 59 | + return value == null || value.getValue() == null ? null : value.getValue().toString(); |
| 60 | + } |
| 61 | + |
| 62 | + public static StashCause resolveOnStartedStashCause(final StashCause cause, final StashRepository repository, |
| 63 | + final StashBuildTrigger trigger, |
| 64 | + final Run<?, ?> run) { |
| 65 | + if (cause != null) |
| 66 | + return cause; |
| 67 | + |
| 68 | + ParametersAction parametersAction = run.getAction(ParametersAction.class); |
| 69 | + if (parametersAction==null) |
| 70 | + return null; |
| 71 | + |
| 72 | + final Map<String, String> keymap; |
| 73 | + if (getStringVariableValue(parametersAction, |
| 74 | + STASH_PULLREQUEST_BUILDER_PLUGIN_MAP, |
| 75 | + DESTINATION_COMMIT_HASH) != null) { |
| 76 | + keymap = STASH_PULLREQUEST_BUILDER_PLUGIN_MAP; |
| 77 | + } else if (getStringVariableValue(parametersAction, |
| 78 | + PULL_REQUEST_NOTIFIER_FOR_BITBUCKET_MAP, |
| 79 | + DESTINATION_COMMIT_HASH) != null) { |
| 80 | + keymap = PULL_REQUEST_NOTIFIER_FOR_BITBUCKET_MAP; |
| 81 | + } else |
| 82 | + return null; |
| 83 | + |
| 84 | + StashPullRequestResponseValue pullRequest = new StashPullRequestResponseValue(); |
| 85 | + pullRequest.setToRef(new StashPullRequestResponseValueRepository()); |
| 86 | + pullRequest.getToRef() |
| 87 | + .setLatestCommit(getStringVariableValue(parametersAction, keymap, DESTINATION_COMMIT_HASH)); |
| 88 | + pullRequest.setFromRef(new StashPullRequestResponseValueRepository()); |
| 89 | + pullRequest.getFromRef().setLatestCommit(getStringVariableValue(parametersAction, keymap, SOURCE_COMMIT_HASH)); |
| 90 | + pullRequest.setId(getStringVariableValue(parametersAction, keymap, PULL_REQUEST_ID)); |
| 91 | + repository.init(); |
| 92 | + String commentId = repository.postBuildStartCommentTo(pullRequest); |
| 93 | + |
| 94 | + StashCause newCause = new StashCause(trigger.getStashHost(), |
| 95 | + getStringVariableValue(parametersAction, keymap, SOURCE_BRANCH), |
| 96 | + getStringVariableValue(parametersAction, keymap, TARGET_BRANCH), |
| 97 | + getStringVariableValue(parametersAction, keymap, SOURCE_REPOSITORY_OWNER), |
| 98 | + getStringVariableValue(parametersAction, keymap, SOURCE_REPOSITORY_NAME), |
| 99 | + getStringVariableValue(parametersAction, keymap, PULL_REQUEST_ID), |
| 100 | + getStringVariableValue(parametersAction, keymap, DESTINATION_REPOSITORY_OWNER), |
| 101 | + getStringVariableValue(parametersAction, keymap, DESTINATION_REPOSITORY_NAME), |
| 102 | + getStringVariableValue(parametersAction, keymap, PULL_REQUEST_TITLE), |
| 103 | + getStringVariableValue(parametersAction, keymap, SOURCE_COMMIT_HASH), |
| 104 | + getStringVariableValue(parametersAction, keymap, DESTINATION_COMMIT_HASH), |
| 105 | + commentId, |
| 106 | + getStringVariableValue(parametersAction, keymap, PULL_REQUEST_VERSION), |
| 107 | + new HashMap<String, String>()); |
| 108 | + |
| 109 | + run.addAction(new StashCauseWrapperAction(newCause)); |
| 110 | + |
| 111 | + return newCause; |
| 112 | + } |
| 113 | + |
| 114 | + public static StashCause resolveOnCompletedStashCause(final StashCause cause, final Run<?, ?> run) { |
| 115 | + if (cause != null) |
| 116 | + return cause; |
| 117 | + StashCauseWrapperAction causeWrapperAction = run.getAction(StashCauseWrapperAction.class); |
| 118 | + return causeWrapperAction == null ? cause : causeWrapperAction.getStashCause(); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments