Skip to content

Add support for gitea pr status update #168

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ github-pr-status/build
gitlab-mr-status/build
gerrit-cs-status/build
stash-pr-status/build
gitea-pr-status/build
common/out
github-pr-status/out
gitlab-mr-status/out
gerrit-cs-status/out
stash-pr-status/out
gitea-pr-status/out


# App #
Expand Down
15 changes: 14 additions & 1 deletion 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
* Gitea Pull Request status

## Requirements
These plugins require GoCD version >= v15.x or above
Expand All @@ -20,7 +21,7 @@ These plugins require GoCD version >= v15.x or above

## Configuration

- You will see `Github Pull Requests status notifier` / `Stash Pull Requests status notifier` / `Gerrit Change Set status notifier` / `GitLab Feature Branch status notifier` on plugin listing page
- You will see `Github Pull Requests status notifier` / `Stash Pull Requests status notifier` / `Gerrit Change Set status notifier` / `GitLab Feature Branch status notifier` / `Gitea Pull Requests status notifier` on plugin listing page
![Plugins listing page][1]

- You can configure the plugin (this feature requires GoCD version >= v15.2, use system properties to configure the plugin). The details should be as follows:
Expand Down Expand Up @@ -115,6 +116,18 @@ Eg:
-Dgo.plugin.build.status.gitlab.oauth=XXXX
```

#### Gitea
**Setup:**
- You need to provide `GoCD Base URL`, `Gitea Base URL`, `Username`, `Password` using the plugin configuration view.
- (or) through system property `go.plugin.build.status.go-server`, `go.plugin.build.status.gitea.endpoint`, `go.plugin.build.status.gitea.username`, `go.plugin.build.status.gitea.password`.
Eg:
```
-Dgo.plugin.build.status.go-server=https://gitea.com
-Dgo.plugin.build.status.gitea.endpoint=https://gitea.com
-Dgo.plugin.build.status.gitea.username=johndoe
-Dgo.plugin.build.status.gitea.password=thisaintapassword
```

## FAQs

[1]: images/list-plugin.png "List Plugin"
Expand Down
40 changes: 40 additions & 0 deletions gitea-pr-status/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

repositories {
mavenCentral()
}

dependencies {
compile project(":common")

testCompile 'org.mockito:mockito-core:3.8.0'
testCompile 'junit:junit:4.13.2'
}

processResources {
doFirst {
project.pluginDesc.id = "gitea.pr.status"
project.pluginDesc.name = "Gitea Pull Requests status notifier"
project.pluginDesc.description = "Updates build status for Gitea pull request"
}

from("src/main/resources") {
filesMatching('plugin.xml') {
expand project.pluginDesc
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tw.go.plugin;

import com.thoughtworks.go.plugin.api.annotation.Extension;
import com.tw.go.plugin.provider.Provider;
import com.tw.go.plugin.provider.GiteaProvider;

@Extension
public class GiteaBuildStatusNotifierPlugin extends BuildStatusNotifierPlugin {
@Override
protected Provider loadProvider() {
try {
return new GiteaProvider();
} catch (Exception e) {
throw new RuntimeException("could not create provider", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tw.go.plugin.provider;

import com.tw.go.plugin.setting.PluginConfigurationView;

import java.util.HashMap;
import java.util.Map;

import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_SERVER_BASE_URL;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_END_POINT;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_USERNAME;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_PASSWORD;
import static com.tw.go.plugin.util.ConfigurationUtils.createField;

public class GiteaConfigurationView implements PluginConfigurationView {

@Override
public String templateName() {
return "plugin-settings-gitea.template.html";
}

@Override
public Map<String, Object> fields() {
Map<String, Object> response = new HashMap<String, Object>();
response.put(PLUGIN_SETTINGS_SERVER_BASE_URL, createField("GoCD Base URL", null, true, false, "0"));
response.put(PLUGIN_SETTINGS_END_POINT, createField("Gitea Base URL", null, true, false, "1"));
response.put(PLUGIN_SETTINGS_USERNAME, createField("Username", null, true, false, "2"));
response.put(PLUGIN_SETTINGS_PASSWORD, createField("Password", null, true, true, "3"));
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tw.go.plugin.provider;

import com.tw.go.plugin.setting.PluginSettings;
import com.tw.go.plugin.util.AuthenticationType;
import com.tw.go.plugin.util.HTTPClient;
import com.tw.go.plugin.util.JSONUtils;
import org.apache.commons.lang3.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_SERVER_BASE_URL;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_END_POINT;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_USERNAME;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_PASSWORD;
import static com.tw.go.plugin.util.ValidationUtils.getValidationError;

public class GiteaProvider extends DefaultProvider {
public static final String PLUGIN_ID = "gitea.pr.status";
public static final String GIT_FB_POLLER_PLUGIN_ID = "git.fb";
public static final String STATUS_CONTEXT = "GoCD";

private final HTTPClient httpClient;

public GiteaProvider() {
super(new GiteaConfigurationView());
httpClient = new HTTPClient();
}

public GiteaProvider(HTTPClient httpClient) {
super(new GiteaConfigurationView());
this.httpClient = httpClient;
}

@Override
public String pluginId() {
return PLUGIN_ID;
}

@Override
public String pollerPluginId() {
return GIT_FB_POLLER_PLUGIN_ID;
}

@Override
public List<Map<String, Object>> validateConfig(Map<String, Object> fields) {
List<Map<String, Object>> errors = new ArrayList<>();

if (!validateUrl(getValueFromPluginSettings(fields.get(PLUGIN_SETTINGS_SERVER_BASE_URL)))) {
errors.add(getValidationError(
PLUGIN_SETTINGS_SERVER_BASE_URL,
"GoCD base url not set correctly"
));
}
if (!validateUrl(getValueFromPluginSettings(fields.get(PLUGIN_SETTINGS_END_POINT)))) {
errors.add(getValidationError(
PLUGIN_SETTINGS_END_POINT,
"Gitea base url not set correctly"
));
}
if (getValueFromPluginSettings(fields.get(PLUGIN_SETTINGS_USERNAME)).equals("")) {
errors.add(getValidationError(
PLUGIN_SETTINGS_USERNAME,
"Gitea Username not set correctly"
));
}
if (getValueFromPluginSettings(fields.get(PLUGIN_SETTINGS_PASSWORD)).equals("")) {
errors.add(getValidationError(
PLUGIN_SETTINGS_PASSWORD,
"Gitea Password not set correctly"
));
}

return errors;
}

@Override
public void updateStatus(String url, PluginSettings pluginSettings, String branch, String revision, String pipelineStage,
String result, String trackbackURL) throws Exception {
String giteaBaseURL = pluginSettings.getEndPoint();
String username = pluginSettings.getUsername();
String password = pluginSettings.getPassword();

if (StringUtils.isEmpty(giteaBaseURL)) {
giteaBaseURL = System.getProperty("go.plugin.build.status.gitea.endpoint");
}
if (StringUtils.isEmpty(username)) {
username = System.getProperty("go.plugin.build.status.gitea.username");
}
if (StringUtils.isEmpty(password)) {
password = System.getProperty("go.plugin.build.status.gitea.password");
}

String updateURL = formUpdateURL(giteaBaseURL, url, revision);
Map<String, String> request = new HashMap<>();
request.put("context", STATUS_CONTEXT);
request.put("description", GiteaState.descriptionFor(result));
request.put("state", GiteaState.stateFor(result));
request.put("target_url", trackbackURL);
String requestBody = JSONUtils.toJSON(request);

httpClient.postRequest(updateURL, AuthenticationType.BASIC, username, password, requestBody);
}

private String getValueFromPluginSettings(Object values) {
if (values == null) {
return "";
}
Map<String, String> vals = (Map<String, String>) values;
String ret = vals.get("value");
if (ret == null) {
return "";
}
return ret;
}

private boolean validateUrl(String uri) {
try {
new URL(uri);
} catch (MalformedURLException e) {
return false;
}
return true;
}

public String formUpdateURL(String gitServerBaseURL, String url, String revision) {
if (gitServerBaseURL.endsWith("/")) {
gitServerBaseURL = gitServerBaseURL.substring(0, gitServerBaseURL.length() - 1);
}
return String.format("%s/api/v1/repos/%s/statuses/%s", gitServerBaseURL, getRepository(url), revision);
}

public String getRepository(String url) {
String repoPath = null;

String sshProtocolString = "(.*)@(.*):(.*?)(/*)$";
Pattern sshPattern = Pattern.compile(sshProtocolString);
Matcher sshMatcher = sshPattern.matcher(url);
if (sshMatcher.find()) {
repoPath = sshMatcher.group(3);
}

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

if (!StringUtils.isEmpty(repoPath) && repoPath.endsWith(".git")) {
repoPath = repoPath.substring(0, repoPath.length() - 4);
}
return repoPath;
}
}
Loading