|
| 1 | +package in.ashwanthkumar.gocd.github.provider.gitlab; |
| 2 | + |
| 3 | +import com.thoughtworks.go.plugin.api.GoPluginIdentifier; |
| 4 | +import com.tw.go.plugin.model.GitConfig; |
| 5 | +import com.tw.go.plugin.util.StringUtil; |
| 6 | +import in.ashwanthkumar.gocd.github.provider.Provider; |
| 7 | +import in.ashwanthkumar.gocd.github.provider.github.GHUtils; |
| 8 | +import in.ashwanthkumar.gocd.github.provider.github.model.PullRequestStatus; |
| 9 | +import in.ashwanthkumar.gocd.github.settings.general.DefaultGeneralPluginConfigurationView; |
| 10 | +import in.ashwanthkumar.gocd.github.settings.general.GeneralPluginConfigurationView; |
| 11 | +import in.ashwanthkumar.gocd.github.settings.scm.DefaultScmPluginConfigurationView; |
| 12 | +import in.ashwanthkumar.gocd.github.settings.scm.ScmPluginConfigurationView; |
| 13 | +import in.ashwanthkumar.gocd.github.util.URLUtils; |
| 14 | +import in.ashwanthkumar.utils.func.Function; |
| 15 | +import in.ashwanthkumar.utils.lang.StringUtils; |
| 16 | +import org.gitlab4j.api.GitLabApi; |
| 17 | +import org.gitlab4j.api.GitLabApiException; |
| 18 | +import org.gitlab4j.api.models.Author; |
| 19 | +import org.gitlab4j.api.models.MergeRequest; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Properties; |
| 26 | + |
| 27 | +public class GitLabProvider implements Provider { |
| 28 | + private static final Logger LOG = LoggerFactory.getLogger(GitLabProvider.class); |
| 29 | + public static final String REF_SPEC = "+refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*"; |
| 30 | + public static final String REF_PATTERN = "refs/remotes/origin/merge-requests/"; |
| 31 | + private static String LOGIN = "login"; |
| 32 | + private static String ACCESS_TOKEN = "accessToken"; |
| 33 | + |
| 34 | + @Override |
| 35 | + public GoPluginIdentifier getPluginId() { |
| 36 | + return new GoPluginIdentifier("gitlab.pr", Arrays.asList("1.0")); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String getName() { |
| 41 | + return "Gitlab"; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void addConfigData(GitConfig gitConfig) { |
| 46 | + try { |
| 47 | + Properties props = GitLabUtils.readPropertyFile(); |
| 48 | + if (StringUtil.isEmpty(gitConfig.getUsername())) { |
| 49 | + gitConfig.setUsername(props.getProperty(LOGIN)); |
| 50 | + } |
| 51 | + if (StringUtil.isEmpty(gitConfig.getPassword())) { |
| 52 | + gitConfig.setPassword(props.getProperty(ACCESS_TOKEN)); |
| 53 | + } |
| 54 | + } catch (Exception e) { |
| 55 | + LOG.error(String.format("Failed to add config data. %s", e.getMessage()), e); |
| 56 | + throw new RuntimeException(String.format("Failed to add config data. %s", e.getMessage()), e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean isValidURL(String url) { |
| 62 | + return new URLUtils().isValidURL(url); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void checkConnection(GitConfig gitConfig) { |
| 67 | + try { |
| 68 | + loginWith(gitConfig).getProjectApi().getProject(GHUtils.parseGithubUrl(gitConfig.getEffectiveUrl())); |
| 69 | + } catch (Exception e) { |
| 70 | + LOG.error(String.format("Check connection failed. %s", e.getMessage()), e); |
| 71 | + throw new RuntimeException(String.format("Check connection failed. %s", e.getMessage()), e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String getRefSpec() { |
| 77 | + return REF_SPEC; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String getRefPattern() { |
| 82 | + return REF_PATTERN; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void populateRevisionData(GitConfig gitConfig, String prId, String prSHA, Map<String, String> data) { |
| 87 | + data.put("PR_ID", prId); |
| 88 | + |
| 89 | + PullRequestStatus prStatus = null; |
| 90 | + boolean isDisabled = System.getProperty("go.plugin.gitlab.pr.populate-details", "Y").equals("N"); |
| 91 | + if (isDisabled) { |
| 92 | + LOG.debug("Populating PR details is disabled"); |
| 93 | + } else { |
| 94 | + prStatus = getPullRequestStatus(gitConfig, prId, prSHA); |
| 95 | + } |
| 96 | + |
| 97 | + if (prStatus != null) { |
| 98 | + data.put("PR_BRANCH", String.valueOf(prStatus.getPrBranch())); |
| 99 | + data.put("TARGET_BRANCH", String.valueOf(prStatus.getToBranch())); |
| 100 | + data.put("PR_URL", String.valueOf(prStatus.getUrl())); |
| 101 | + data.put("PR_AUTHOR", prStatus.getAuthor()); |
| 102 | + data.put("PR_AUTHOR_EMAIL", prStatus.getAuthorEmail()); |
| 103 | + data.put("PR_DESCRIPTION", prStatus.getDescription()); |
| 104 | + data.put("PR_TITLE", prStatus.getTitle()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public ScmPluginConfigurationView getScmConfigurationView() { |
| 110 | + return new DefaultScmPluginConfigurationView(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public GeneralPluginConfigurationView getGeneralConfigurationView() { |
| 115 | + return new DefaultGeneralPluginConfigurationView(); |
| 116 | + } |
| 117 | + |
| 118 | + private PullRequestStatus getPullRequestStatus(GitConfig gitConfig, String prId, String prSHA) { |
| 119 | + try { |
| 120 | + MergeRequest currentPR = pullRequestFrom(gitConfig, Integer.parseInt(prId)); |
| 121 | + return transformMergeRequestToPullRequestStatus(prSHA).apply(currentPR); |
| 122 | + } catch (Exception e) { |
| 123 | + LOG.error(String.format("Failed to fetch PR status. %s", e.getMessage()), e); |
| 124 | + throw new RuntimeException(String.format("Failed to fetch PR status. %s", e.getMessage()), e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private MergeRequest pullRequestFrom(GitConfig gitConfig, int currentPullRequestID) throws GitLabApiException { |
| 129 | + return loginWith(gitConfig) |
| 130 | + .getMergeRequestApi() |
| 131 | + .getMergeRequest(GHUtils.parseGithubUrl(gitConfig.getEffectiveUrl()), currentPullRequestID); |
| 132 | + } |
| 133 | + |
| 134 | + private Function<MergeRequest, PullRequestStatus> transformMergeRequestToPullRequestStatus(final String mergedSHA) { |
| 135 | + return new Function<MergeRequest, PullRequestStatus>() { |
| 136 | + @Override |
| 137 | + public PullRequestStatus apply(MergeRequest input) { |
| 138 | + int prID = input.getId(); |
| 139 | + Author user = input.getAuthor(); |
| 140 | + return new PullRequestStatus(prID, GitLabProvider.REF_PATTERN, input.getSha(), mergedSHA, |
| 141 | + input.getSourceBranch(), input.getTargetBranch(), input.getWebUrl(), user.getName(), |
| 142 | + user.getEmail(), input.getDescription(), input.getTitle()); |
| 143 | + } |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + private GitLabApi loginWith(GitConfig gitConfig) throws RuntimeException { |
| 148 | + if (hasCredentials(gitConfig)) |
| 149 | + return new GitLabApi(GitLabUtils.getServerUrl(gitConfig.getEffectiveUrl()), |
| 150 | + gitConfig.getPassword()); |
| 151 | + else { |
| 152 | + LOG.error("No gitlab credentials found"); |
| 153 | + throw new RuntimeException("No gitlab credentials found"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private boolean hasCredentials(GitConfig gitConfig) { |
| 158 | + return StringUtils.isNotEmpty(gitConfig.getUsername()) && StringUtils.isNotEmpty(gitConfig.getPassword()); |
| 159 | + } |
| 160 | +} |
0 commit comments