Skip to content

Commit 6bb4b30

Browse files
committed
gitea support
1 parent 7a5fab0 commit 6bb4b30

File tree

10 files changed

+248
-89
lines changed

10 files changed

+248
-89
lines changed

commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/gitea/GiteaBuildStatus.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
public enum GiteaBuildStatus {
2828
PENDING("pending"),
29-
RUNNING("running"),
3029
SUCCESS("success"),
31-
FAILED("failed"),
32-
CANCELED("canceled");
30+
ERROR("error"),
31+
FAILURE("failure"),
32+
WARNING("warning");
3333

3434
private static final Map<String, GiteaBuildStatus> INDEX = Arrays.stream(values()).collect(Collectors.toMap(GiteaBuildStatus::getName, Function.identity()));
3535

commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/gitea/GiteaPublisher.java

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.google.gson.Gson;
2020
import jetbrains.buildServer.BuildType;
2121
import jetbrains.buildServer.commitPublisher.*;
22-
import jetbrains.buildServer.commitPublisher.gitea.GiteaBuildStatus;
23-
import jetbrains.buildServer.commitPublisher.gitea.GiteaSettings;
2422
import jetbrains.buildServer.commitPublisher.gitea.data.GiteaCommitStatus;
2523
import jetbrains.buildServer.serverSide.*;
2624
import jetbrains.buildServer.serverSide.impl.LogUtil;
@@ -30,16 +28,13 @@
3028
import org.jetbrains.annotations.NotNull;
3129
import org.jetbrains.annotations.Nullable;
3230

33-
import java.util.Collections;
3431
import java.util.LinkedHashMap;
3532
import java.util.Map;
3633

3734
import static jetbrains.buildServer.commitPublisher.LoggerUtil.LOG;
3835

3936
class GiteaPublisher extends HttpBasedCommitStatusPublisher {
4037

41-
private static final String REFS_HEADS = "refs/heads/";
42-
private static final String REFS_TAGS = "refs/tags/";
4338
private final Gson myGson = new Gson();
4439
private static final GitRepositoryParser VCS_URL_PARSER = new GitRepositoryParser();
4540

@@ -76,41 +71,41 @@ public boolean buildQueued(@NotNull BuildPromotion buildPromotion, @NotNull Buil
7671

7772
@Override
7873
public boolean buildRemovedFromQueue(@NotNull BuildPromotion buildPromotion, @NotNull BuildRevision revision, @NotNull AdditionalTaskInfo additionalTaskInfo) throws PublisherException {
79-
publish(buildPromotion, revision, GiteaBuildStatus.CANCELED, additionalTaskInfo);
74+
publish(buildPromotion, revision, GiteaBuildStatus.PENDING, additionalTaskInfo);
8075
return true;
8176
}
8277

8378
@Override
8479
public boolean buildStarted(@NotNull SBuild build, @NotNull BuildRevision revision) throws PublisherException {
85-
publish(build, revision, GiteaBuildStatus.RUNNING, DefaultStatusMessages.BUILD_STARTED);
80+
publish(build, revision, GiteaBuildStatus.PENDING, DefaultStatusMessages.BUILD_STARTED);
8681
return true;
8782
}
8883

8984

9085
@Override
9186
public boolean buildFinished(@NotNull SBuild build, @NotNull BuildRevision revision) throws PublisherException {
92-
GiteaBuildStatus status = build.getBuildStatus().isSuccessful() ? GiteaBuildStatus.SUCCESS : GiteaBuildStatus.FAILED;
87+
GiteaBuildStatus status = build.getBuildStatus().isSuccessful() ? GiteaBuildStatus.SUCCESS : GiteaBuildStatus.FAILURE;
9388
publish(build, revision, status, build.getStatusDescriptor().getText());
9489
return true;
9590
}
9691

9792

9893
@Override
9994
public boolean buildFailureDetected(@NotNull SBuild build, @NotNull BuildRevision revision) throws PublisherException {
100-
publish(build, revision, GiteaBuildStatus.FAILED, build.getStatusDescriptor().getText());
95+
publish(build, revision, GiteaBuildStatus.FAILURE, build.getStatusDescriptor().getText());
10196
return true;
10297
}
10398

10499
@Override
105100
public boolean buildMarkedAsSuccessful(@NotNull SBuild build, @NotNull BuildRevision revision, boolean buildInProgress) throws PublisherException {
106-
publish(build, revision, buildInProgress ? GiteaBuildStatus.RUNNING : GiteaBuildStatus.SUCCESS, DefaultStatusMessages.BUILD_MARKED_SUCCESSFULL);
101+
publish(build, revision, buildInProgress ? GiteaBuildStatus.PENDING : GiteaBuildStatus.SUCCESS, DefaultStatusMessages.BUILD_MARKED_SUCCESSFULL);
107102
return true;
108103
}
109104

110105

111106
@Override
112107
public boolean buildInterrupted(@NotNull SBuild build, @NotNull BuildRevision revision) throws PublisherException {
113-
publish(build, revision, GiteaBuildStatus.CANCELED, build.getStatusDescriptor().getText());
108+
publish(build, revision, GiteaBuildStatus.WARNING, build.getStatusDescriptor().getText());
114109
return true;
115110
}
116111

@@ -137,8 +132,9 @@ public RevisionStatus getRevisionStatus(@NotNull BuildPromotion buildPromotion,
137132

138133
private GiteaCommitStatus getLatestCommitStatusForBuild(@NotNull BuildRevision revision, @Nullable SBuildType buildType) throws PublisherException {
139134
String url = buildRevisionStatusesUrl(revision, buildType);
135+
url += "?access_token=" + getPrivateToken();
140136
ResponseEntityProcessor<GiteaCommitStatus[]> processor = new ResponseEntityProcessor<>(GiteaCommitStatus[].class);
141-
GiteaCommitStatus[] commitStatuses = get(url, null, null, Collections.singletonMap("PRIVATE-TOKEN", getPrivateToken()), processor);
137+
GiteaCommitStatus[] commitStatuses = get(url, null, null, null, processor);
142138
if (commitStatuses == null || commitStatuses.length == 0) {
143139
return null;
144140
}
@@ -164,11 +160,7 @@ private String buildRevisionStatusesUrl(@NotNull BuildRevision revision, @Nullab
164160
Repository repository = parseRepository(root, pathPrefix);
165161
if (repository == null)
166162
throw new PublisherException("Cannot parse repository URL from VCS root " + root.getName());
167-
String statusesUrl = GiteaSettings.getProjectsUrl(getApiUrl(), repository.owner(), repository.repositoryName()) + "/repository/commits/" + revision.getRevision() + "/statuses";
168-
if (buildType != null) {
169-
statusesUrl += ("?" + encodeParameter("name", buildType.getName()));
170-
}
171-
return statusesUrl;
163+
return GiteaSettings.getProjectsUrl(getApiUrl(), repository.owner(), repository.repositoryName()) + "/statuses/" + revision.getRevision();
172164
}
173165

174166
private Event getTriggeredEvent(GiteaCommitStatus commitStatus) {
@@ -183,16 +175,18 @@ private Event getTriggeredEvent(GiteaCommitStatus commitStatus) {
183175
}
184176

185177
switch (status) {
186-
case CANCELED:
178+
case WARNING:
179+
return Event.INTERRUPTED;
180+
case PENDING:
187181
if (commitStatus.description != null && commitStatus.description.contains(DefaultStatusMessages.BUILD_REMOVED_FROM_QUEUE)) {
188182
return Event.REMOVED_FROM_QUEUE;
189183
}
190-
return null;
191-
case PENDING:
192-
return Event.QUEUED;
193-
case RUNNING:
184+
if (commitStatus.description != null && commitStatus.description.contains(DefaultStatusMessages.BUILD_QUEUED)) {
185+
return Event.QUEUED;
186+
}
194187
case SUCCESS:
195-
case FAILED:
188+
case FAILURE:
189+
case ERROR:
196190
return null;
197191
default:
198192
LOG.warn("No event is assosiated with Gitea build status \"" + status + "\". Related event can not be defined");
@@ -255,7 +249,8 @@ private void publish(@NotNull String message,
255249
private void publish(@NotNull String commit, @NotNull String data, @NotNull Repository repository, @NotNull String buildDescription) {
256250
String url = GiteaSettings.getProjectsUrl(getApiUrl(), repository.owner(), repository.repositoryName()) + "/statuses/" + commit;
257251
LOG.debug("Request url: " + url + ", message: " + data);
258-
postJson(url, null, null, data, Collections.singletonMap("PRIVATE-TOKEN", getPrivateToken()), buildDescription);
252+
url += "?access_token=" + getPrivateToken();
253+
postJson(url, null, null, data, null, buildDescription);
259254
}
260255

261256
@Override
@@ -279,25 +274,11 @@ private String createMessage(@NotNull GiteaBuildStatus status,
279274
@NotNull String url,
280275
@NotNull String description) {
281276

282-
RepositoryVersion repositoryVersion = revision.getRepositoryVersion();
283-
String ref = repositoryVersion.getVcsBranch();
284-
if (ref != null) {
285-
if (ref.startsWith(REFS_HEADS)) {
286-
ref = ref.substring(REFS_HEADS.length());
287-
} else if (ref.startsWith(REFS_TAGS)) {
288-
ref = ref.substring(REFS_TAGS.length());
289-
} else {
290-
ref = null;
291-
}
292-
}
293-
294277
final Map<String, String> data = new LinkedHashMap<String, String>();
295278
data.put("state", status.getName());
296-
data.put("name", name);
297-
data.put("target_url", url);
279+
data.put("context", name);
298280
data.put("description", description);
299-
if (ref != null)
300-
data.put("ref", ref);
281+
data.put("target_url", url);
301282
return myGson.toJson(data);
302283
}
303284

commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/gitea/GiteaSettings.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import jetbrains.buildServer.commitPublisher.*;
2020
import jetbrains.buildServer.commitPublisher.CommitStatusPublisher.Event;
21-
import jetbrains.buildServer.commitPublisher.gitea.GiteaPublisher;
2221
import jetbrains.buildServer.commitPublisher.gitea.data.GiteaRepoInfo;
2322
import jetbrains.buildServer.commitPublisher.gitea.data.GiteaUserInfo;
2423
import jetbrains.buildServer.serverSide.*;
@@ -104,12 +103,15 @@ public void testConnection(@NotNull BuildTypeIdentity buildTypeOrTemplate, @NotN
104103
try {
105104
IOGuard.allowNetworkCall(() -> {
106105
ProjectInfoResponseProcessor processorPrj = new ProjectInfoResponseProcessor();
107-
HttpHelper.get(getProjectsUrl(apiUrl, repository.owner(), repository.repositoryName()),
108-
null, null, Collections.singletonMap("PRIVATE-TOKEN", token),
106+
String url = getProjectsUrl(apiUrl, repository.owner(), repository.repositoryName());
107+
url += "?access_token=" + token;
108+
HttpHelper.get(url, null, null, null,
109109
BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, trustStore(), processorPrj);
110-
if (processorPrj.getAccessLevel() < 30) {
110+
if (!processorPrj.hasPushAccess()) {
111111
UserInfoResponseProcessor processorUser = new UserInfoResponseProcessor();
112-
HttpHelper.get(getUserUrl(apiUrl), null, null, Collections.singletonMap("PRIVATE-TOKEN", token),
112+
url = getUserUrl(apiUrl);
113+
url += "?access_token=" + token;
114+
HttpHelper.get(url, null, null, null,
113115
BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, trustStore(), processorUser);
114116
if (!processorUser.isAdmin()) {
115117
throw new HttpPublisherException("Gitea does not grant enough permissions to publish a commit status");
@@ -160,7 +162,7 @@ public Collection<InvalidProperty> process(Map<String, String> params) {
160162

161163
@NotNull
162164
public static String getProjectsUrl(@NotNull String apiUrl, @NotNull String owner, @NotNull String repo) {
163-
return apiUrl + "/projects/" + owner.replace(".", "%2E").replace("/", "%2F") + "%2F" + repo.replace(".", "%2E");
165+
return apiUrl + "/repos/" + owner.replace(".", "%2E").replace("/", "%2F") + "/" + repo.replace(".", "%2E");
164166
}
165167

166168
@NotNull
@@ -206,28 +208,25 @@ public void processResponse(HttpHelper.HttpResponse response) throws HttpPublish
206208

207209
private class ProjectInfoResponseProcessor extends JsonResponseProcessor<GiteaRepoInfo> {
208210

209-
private int myAccessLevel;
211+
private boolean myHasPushAccess;
210212

211213
ProjectInfoResponseProcessor() {
212214
super(GiteaRepoInfo.class);
213215
}
214216

215-
int getAccessLevel() {
216-
return myAccessLevel;
217+
boolean hasPushAccess() {
218+
return myHasPushAccess;
217219
}
218220

219221
@Override
220222
public void processResponse(HttpHelper.HttpResponse response) throws HttpPublisherException, IOException {
221-
myAccessLevel = 0;
223+
myHasPushAccess = false;
222224
super.processResponse(response);
223225
GiteaRepoInfo repoInfo = getInfo();
224226
if (null == repoInfo || null == repoInfo.id || null == repoInfo.permissions) {
225227
throw new HttpPublisherException("Gitea publisher has received a malformed response");
226228
}
227-
if (null != repoInfo.permissions.project_access)
228-
myAccessLevel = repoInfo.permissions.project_access.access_level;
229-
if (null != repoInfo.permissions.group_access && myAccessLevel < repoInfo.permissions.group_access.access_level)
230-
myAccessLevel = repoInfo.permissions.group_access.access_level;
229+
myHasPushAccess = repoInfo.permissions.push;
231230
}
232231
}
233232

commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/gitea/data/GiteaAccessLevel.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/gitea/data/GiteaCommitStatus.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ public class GiteaCommitStatus {
77
public final Long id;
88
public final String status;
99
public final String description;
10-
public final String name;
10+
public final String context;
1111
public final String target_url;
1212

13-
public GiteaCommitStatus(Long id, String status, String description, String name, String target_url) {
13+
public GiteaCommitStatus(Long id, String status, String description, String context, String target_url) {
1414
this.id = id;
1515
this.status = status;
1616
this.description = description;
17-
this.name = name;
17+
this.context = context;
1818
this.target_url = target_url;
1919
}
2020
}

commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/gitea/data/GiteaPermissions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@
2020
* @author Felix Heim, 21/02/22.
2121
*/
2222
public class GiteaPermissions {
23-
public GiteaAccessLevel project_access;
24-
public GiteaAccessLevel group_access;
23+
public boolean push;
2524
}

commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/gitea/data/GiteaUserInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
public class GiteaUserInfo {
2323
public String id;
24-
public String name;
25-
public String username;
24+
public String full_name;
25+
public String login;
2626
public boolean is_admin;
2727
}

commit-status-publisher-server/src/main/resources/buildServerResources/gitea/giteaSettings.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@
4545
</script>
4646
</c:if>
4747
</td>
48-
</tr>
48+
</tr>

0 commit comments

Comments
 (0)