Skip to content
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

Adding (rebased) support for GitLab PR Poller plugin #249

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Supported:
* GitHub Pull Request status
* Stash Pull Request status
* Gerrit Change Set status
* Gitlab Merge Request Status

## Requirements
These plugins require GoCD version >= v15.x or above
Expand All @@ -15,7 +16,7 @@ These plugins require GoCD version >= v15.x or above
- Download the latest plugin jar from [Releases](https://github.com/srinivasupadhya/gocd-build-status-notifier/releases) section. Place it in `<go-server-location>/plugins/external` & restart Go Server.

## Behavior
- Go Server notifies the plugin on every `Stage Status Change` with relevant details. The plugin scans the `build-cause` to see if the `github.pr` / `stash.pr` / `gerrit.cs` material is present.
- Go Server notifies the plugin on every `Stage Status Change` with relevant details. The plugin scans the `build-cause` to see if the `github.pr` / `stash.pr` / `gerrit.cs` / `gitlab.pr` material is present.
- If it is, then Pull Request/Change Set status is updated with `status=stage-result`, `context=pipeline-name/stage-name` & `target-url=trackback-url`.

## Configuration
Expand Down Expand Up @@ -104,9 +105,10 @@ Eg:

#### Gitlab
**Setup:**
- This works with git.fb poller plugin that can be found here https://github.com/ashwanthkumar/gocd-build-github-pull-requests/releases
- This works with `git.fb` and `gitlab.pr` poller plugins that can be found here https://github.com/ashwanthkumar/gocd-build-github-pull-requests/releases
- You will need a Gitlab Oauth token: https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html
- You need to provide `server_base_url`, `endpoint`, `oauth_token` using the plugin configuration view
- When using `gitlab.pr` plugin, please make sure the populate fields are enabled
![Configure Gitlab Plugin][5]
- Alternatively you can pass them through GoCD system property `go.plugin.build.status.gitlab.endpoint`, `go.plugin.build.status.gitlab.oauth`.
Eg:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {

apply from: 'plugin-helpers.gradle'

project.ext.pluginVersion = '1.7.3'
project.ext.pluginVersion = '1.8.0'
project.ext.fullVersion = project.git.distVersion() ? "${project.pluginVersion}-${project.git.distVersion()}" : project.pluginVersion

project.ext.pluginDesc = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,21 @@ GoPluginApiResponse handleStageNotification(GoPluginApiRequest goPluginApiReques
List<Map<String, Object>> materialRevisions = (List<Map<String, Object>>) pipeline.get("build-cause");
for (Map<String, Object> materialRevision : materialRevisions) {
Map<String, Object> material = (Map<String, Object>) materialRevision.get("material");
if (isMaterialOfType(material, provider.pollerPluginId())) {
if (isMaterialFromTypes(material, provider.pollerPluginIds())) {
Map<String, Object> materialConfiguration = (Map<String, Object>) material.get("scm-configuration");
String url = (String) materialConfiguration.get("url");

List<Map<String, Object>> modifications = (List<Map<String, Object>>) materialRevision.get("modifications");
String revision = (String) modifications.get(0).get("revision");
Map<String, Object> modificationData = (Map<String, Object>) modifications.get(0).get("data");
String prId = (String) modificationData.get("PR_ID");
String prBranch = (String) modificationData.getOrDefault("PR_BRANCH", modificationData.get("PR_ID"));

if (ValidationUtils.isEmpty(prId)) {
prId = (String) modificationData.get("CURRENT_BRANCH");
if (ValidationUtils.isEmpty(prBranch)) {
prBranch = (String) modificationData.get("CURRENT_BRANCH");
}

try {
provider.updateStatus(url, pluginSettings, prId, revision, pipelineStage, result, trackbackURL);
provider.updateStatus(url, pluginSettings, prBranch, revision, pipelineStage, result, trackbackURL);
} catch (Exception e) {
LOGGER.error(String.format("Error occurred. Could not update build status - URL: %s Revision: %s Build: %s Result: %s", url, revision, pipelineInstance, result), e);
}
Expand All @@ -193,8 +193,12 @@ GoPluginApiResponse handleStageNotification(GoPluginApiRequest goPluginApiReques
return renderJSON(responseCode, response);
}

private boolean isMaterialOfType(Map<String, Object> material, String pollerPluginId) {
return "scm".equals(material.get("type")) && ((String) material.get("plugin-id")).equalsIgnoreCase(pollerPluginId);
private boolean isMaterialFromTypes(Map<String, Object> material, Collection<String> pollerPluginIds) {
if ("scm".equals(material.get("type"))) {
return pollerPluginIds.contains((String) material.get("plugin-id"));
}

return false;
}

private GoPluginIdentifier getGoPluginIdentifier() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public interface Provider {
public String pluginId();

public String pollerPluginId();
public List<String> pollerPluginIds();

public void updateStatus(String url, PluginSettings pluginSettings, String branch, String revision, String pipelineStage,
String result, String trackbackURL) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -44,8 +45,8 @@ public String pluginId() {
}

@Override
public String pollerPluginId() {
return null;
public List<String> pollerPluginIds() {
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public String pluginId() {
}

@Override
public String pollerPluginId() {
return GERRIT_CS_POLLER_PLUGIN_ID;
public List<String> pollerPluginIds() {
return List.of(GERRIT_CS_POLLER_PLUGIN_ID);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setUp() {
pluginSettingsResponse.setResponseBody(JSONUtils.toJSON(new HashMap<String, String>()));
when(goApplicationAccessor.submit(any(GoApiRequest.class))).thenReturn(pluginSettingsResponse);
when(provider.pluginId()).thenReturn(PLUGIN_ID);
when(provider.pollerPluginId()).thenReturn(POLLER_PLUGIN_ID);
when(provider.pollerPluginIds()).thenReturn(List.of(POLLER_PLUGIN_ID));

plugin.initializeGoApplicationAccessor(goApplicationAccessor);
plugin.setProvider(provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public String pluginId() {
}

@Override
public String pollerPluginId() {
return GITHUB_PR_POLLER_PLUGIN_ID;
public List<String> pollerPluginIds() {
return List.of(GITHUB_PR_POLLER_PLUGIN_ID);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setUp() {
pluginSettingsResponse.setResponseBody(JSONUtils.toJSON(new HashMap<String, String>()));
when(goApplicationAccessor.submit(any(GoApiRequest.class))).thenReturn(pluginSettingsResponse);
when(provider.pluginId()).thenReturn(PLUGIN_ID);
when(provider.pollerPluginId()).thenReturn(POLLER_PLUGIN_ID);
when(provider.pollerPluginIds()).thenReturn(List.of(POLLER_PLUGIN_ID));

plugin.initializeGoApplicationAccessor(goApplicationAccessor);
plugin.setProvider(provider);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tw.go.plugin.provider;

import com.thoughtworks.go.plugin.api.logging.Logger;
import com.tw.go.plugin.setting.PluginSettings;
import com.tw.go.plugin.util.ValidationUtils;
import org.gitlab.api.GitlabAPI;
Expand All @@ -17,9 +18,11 @@
import static com.tw.go.plugin.util.ValidationUtils.getValidationError;

public class GitLabProvider extends DefaultProvider {
private static final Logger LOGGER = Logger.getLoggerFor(GitLabProvider.class);

private static final String PLUGIN_ID = "gitlab.mr.status";
private static final String GITLAB_FB_POLLER_PLUGIN_ID = "git.fb";
private static final String GITLAB_POLLER_PLUGIN_ID = "gitlab.pr";

public GitLabProvider() {
super(new GitlLabConfigurationView());
Expand All @@ -31,8 +34,8 @@ public String pluginId() {
}

@Override
public String pollerPluginId() {
return GITLAB_FB_POLLER_PLUGIN_ID;
public List<String> pollerPluginIds() {
return List.of(GITLAB_FB_POLLER_PLUGIN_ID, GITLAB_POLLER_PLUGIN_ID);
}

@Override
Expand Down Expand Up @@ -62,8 +65,9 @@ public List<Map<String, Object>> validateConfig(Map<String, Object> fields) {
}

@Override
public void updateStatus(String url, PluginSettings pluginSettings, String prIdStr, String revision, String pipelineStage,
public void updateStatus(String url, PluginSettings pluginSettings, String branch, String revision, String pipelineStage,
String result, String trackbackURL) throws Exception {
LOGGER.info(String.format("Updating commit status for %s on %s", revision, pipelineStage));

String endPointToUse = pluginSettings.getEndPoint();
String oauthAccessTokenToUse = pluginSettings.getOauthToken();
Expand All @@ -77,7 +81,8 @@ public void updateStatus(String url, PluginSettings pluginSettings, String prIdS

GitlabAPI api = GitlabAPI.connect(endPointToUse, oauthAccessTokenToUse);
GitlabProject project = api.getProject(getRepository(url));
api.createCommitStatus(project, revision, GitLabState.stateFor(result), prIdStr, "GoCD", trackbackURL, "");
String state = GitLabState.stateFor(result);
api.createCommitStatus(project, revision, state, branch, pipelineStage, trackbackURL, "");
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -108,14 +113,14 @@ public String getRepository(String url) {
String sshProtocolString = "(.*)@(.*):(.*?)(/*)$";
Pattern sshPattern = Pattern.compile(sshProtocolString);
Matcher sshMatcher = sshPattern.matcher(url);
if(sshMatcher.find()) {
if (sshMatcher.find()) {
repoPath = sshMatcher.group(3);
}

String httpProtocolString = "http(.?)://(.*?)/(.*?)(/*)$";
Pattern httpPattern = Pattern.compile(httpProtocolString);
Matcher httpMatcher = httpPattern.matcher(url);
if(httpMatcher.find()) {
if (httpMatcher.find()) {
repoPath = httpMatcher.group(3);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void setUp() throws Exception {
@Test
public void checkIdsMatch () throws Exception {
assertEquals("gitlab.mr.status", provider.pluginId());
assertEquals("git.fb", provider.pollerPluginId());
assertThat(provider.pollerPluginIds()).containsExactly("git.fb", "gitlab.pr");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public String pluginId() {
}

@Override
public String pollerPluginId() {
return STASH_PR_POLLER_PLUGIN_ID;
public List<String> pollerPluginIds() {
return List.of(STASH_PR_POLLER_PLUGIN_ID);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void setUp() {
pluginSettingsResponse.setResponseBody(JSONUtils.toJSON(new HashMap<String, String>()));
when(goApplicationAccessor.submit(any(GoApiRequest.class))).thenReturn(pluginSettingsResponse);
when(provider.pluginId()).thenReturn(PLUGIN_ID);
when(provider.pollerPluginId()).thenReturn(POLLER_PLUGIN_ID);
when(provider.pollerPluginIds()).thenReturn(List.of(POLLER_PLUGIN_ID));

plugin.initializeGoApplicationAccessor(goApplicationAccessor);
plugin.setProvider(provider);
Expand Down