Skip to content

Commit ced50eb

Browse files
committed
update gitea integration for 2023.11
(cherry picked from commit 88f47a1) (cherry picked from commit 0e39136)
1 parent fccf333 commit ced50eb

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public boolean buildQueued(@NotNull BuildPromotion buildPromotion, @NotNull Buil
7777

7878
@Override
7979
public boolean buildRemovedFromQueue(@NotNull BuildPromotion buildPromotion, @NotNull BuildRevision revision, @NotNull AdditionalTaskInfo additionalTaskInfo) throws PublisherException {
80-
if (!additionalTaskInfo.isBuildManuallyRemoved()) return false;
8180
publish(buildPromotion, revision, GiteaBuildStatus.WARNING , additionalTaskInfo);
8281
return true;
8382
}
@@ -129,7 +128,7 @@ RevisionStatus getRevisionStatusForRemovedBuild(@NotNull SQueuedBuild removedBui
129128
}
130129
Event event = getTriggeredEvent(commitStatus);
131130
boolean isSameBuildType = StringUtil.areEqual(getBuildName(removedBuild.getBuildPromotion()), commitStatus.context);
132-
return new RevisionStatus(event, commitStatus.description, isSameBuildType);
131+
return new RevisionStatus(event, commitStatus.description, isSameBuildType, getBuildIdFromViewUrl(commitStatus.target_url));
133132
}
134133

135134
private String getBuildName(BuildPromotion promotion) {
@@ -182,13 +181,13 @@ RevisionStatus getRevisionStatus(@NotNull BuildPromotion buildPromotion, @Nullab
182181
}
183182
Event event = getTriggeredEvent(commitStatus);
184183
boolean isSameBuildType = StringUtil.areEqual(getBuildName(buildPromotion), commitStatus.context);
185-
return new RevisionStatus(event, commitStatus.description, isSameBuildType);
184+
return new RevisionStatus(event, commitStatus.description, isSameBuildType, getBuildIdFromViewUrl(commitStatus.target_url));
186185
}
187186

188187
private String buildRevisionStatusesUrl(@NotNull BuildRevision revision, @Nullable BuildType buildType) throws PublisherException {
189188
VcsRootInstance root = revision.getRoot();
190189
String apiUrl = getApiUrl();
191-
if (null == apiUrl || apiUrl.length() == 0)
190+
if (null == apiUrl || apiUrl.isEmpty())
192191
throw new PublisherException("Missing Gitea API URL parameter");
193192
String pathPrefix = GiteaSettings.getPathPrefix(apiUrl);
194193
Repository repository = parseRepository(root, pathPrefix);
@@ -210,14 +209,20 @@ private Event getTriggeredEvent(GiteaCommitStatus commitStatus) {
210209

211210
switch (status) {
212211
case WARNING:
213-
return Event.INTERRUPTED;
212+
return commitStatus.description != null
213+
&& (commitStatus.description.contains(DefaultStatusMessages.BUILD_REMOVED_FROM_QUEUE)
214+
|| commitStatus.description.contains(DefaultStatusMessages.BUILD_REMOVED_FROM_QUEUE_AS_CANCELED))
215+
? Event.REMOVED_FROM_QUEUE : null;
214216
case PENDING:
215-
if (commitStatus.description != null && commitStatus.description.contains(DefaultStatusMessages.BUILD_REMOVED_FROM_QUEUE)) {
216-
return Event.REMOVED_FROM_QUEUE;
217-
}
218-
if (commitStatus.description != null && commitStatus.description.contains(DefaultStatusMessages.BUILD_QUEUED)) {
217+
if (commitStatus.description == null || commitStatus.description.contains(DefaultStatusMessages.BUILD_QUEUED)) {
219218
return Event.QUEUED;
220219
}
220+
if (commitStatus.description.contains(DefaultStatusMessages.BUILD_STARTED)) {
221+
return Event.STARTED;
222+
}
223+
if (commitStatus.description.contains(DefaultStatusMessages.BUILD_MARKED_SUCCESSFULL)) {
224+
return null;
225+
}
221226
case SUCCESS:
222227
case FAILURE:
223228
case ERROR:
@@ -261,7 +266,7 @@ private void publish(@NotNull String message,
261266
@NotNull String buildDescription) throws PublisherException {
262267
VcsRootInstance root = revision.getRoot();
263268
String apiUrl = getApiUrl();
264-
if (null == apiUrl || apiUrl.length() == 0)
269+
if (null == apiUrl || apiUrl.isEmpty())
265270
throw new PublisherException("Missing Gitea API URL parameter");
266271
String pathPrefix = GiteaSettings.getPathPrefix(apiUrl);
267272
Repository repository = parseRepository(root, pathPrefix);
@@ -276,7 +281,10 @@ private void publish(@NotNull String message,
276281
}
277282
}
278283

279-
private void publish(@NotNull String commit, @NotNull String data, @NotNull Repository repository, @NotNull String buildDescription) {
284+
private void publish(@NotNull String commit,
285+
@NotNull String data,
286+
@NotNull Repository repository,
287+
@NotNull String buildDescription) throws PublisherException {
280288
String url = GiteaSettings.getProjectsUrl(getApiUrl(), repository.owner(), repository.repositoryName()) + "/statuses/" + commit;
281289
LOG.debug("Request url: " + url + ", message: " + data);
282290
url += "?access_token=" + getPrivateToken();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public boolean isPublishingForVcsRoot(final VcsRoot vcsRoot) {
187187

188188
@Override
189189
protected Set<Event> getSupportedEvents(final SBuildType buildType, final Map<String, String> params) {
190-
return isBuildQueuedSupported(buildType, params) ? mySupportedEventsWithQueued : mySupportedEvents;
190+
return isBuildQueuedSupported(buildType) ? mySupportedEventsWithQueued : mySupportedEvents;
191191
}
192192

193193
private abstract class JsonResponseProcessor<T> extends DefaultHttpResponseProcessor {

commit-status-publisher-server/src/test/java/jetbrains/buildServer/commitPublisher/gitea/GiteaPublisherTest.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
import com.google.gson.Gson;
2020
import jetbrains.buildServer.MockBuildPromotion;
2121
import jetbrains.buildServer.commitPublisher.*;
22-
import jetbrains.buildServer.commitPublisher.gitea.GiteaBuildStatus;
23-
import jetbrains.buildServer.commitPublisher.gitea.GiteaPublisher;
24-
import jetbrains.buildServer.commitPublisher.gitea.GiteaSettings;
2522
import jetbrains.buildServer.commitPublisher.gitea.data.GiteaCommitStatus;
2623
import jetbrains.buildServer.commitPublisher.gitea.data.GiteaPermissions;
2724
import jetbrains.buildServer.commitPublisher.gitea.data.GiteaRepoInfo;
@@ -31,13 +28,14 @@
3128
import org.apache.http.HttpRequest;
3229
import org.apache.http.HttpResponse;
3330
import org.apache.http.entity.StringEntity;
31+
import org.jetbrains.annotations.NotNull;
3432
import org.jmock.Mock;
3533
import org.testng.annotations.BeforeMethod;
3634
import org.testng.annotations.Test;
3735

38-
import java.util.Collections;
39-
import java.util.HashMap;
40-
import java.util.Map;
36+
import java.nio.charset.StandardCharsets;
37+
import java.util.*;
38+
import java.util.stream.Collectors;
4139

4240
import static org.assertj.core.api.BDDAssertions.then;
4341

@@ -48,6 +46,7 @@
4846
public class GiteaPublisherTest extends HttpPublisherTest {
4947

5048
private static final String GROUP_REPO = "group_repo";
49+
private final Map<String, List<GiteaCommitStatus>> myRevisionToStatuses = new HashMap<>();
5150

5251
public GiteaPublisherTest() {
5352
myExpectedRegExps.put(EventToTest.QUEUED, String.format(".*/repos/owner/project/statuses/%s.*ENTITY:.*pending.*%s.*", REVISION, DefaultStatusMessages.BUILD_QUEUED));
@@ -131,15 +130,16 @@ public void should_calculate_correct_revision_status() {
131130
assertNull(publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.FAILURE.getName(), null, null, null)).getTriggeredEvent());
132131
assertNull(publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.ERROR.getName(), null, null, null)).getTriggeredEvent());
133132
assertNull(publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_MARKED_SUCCESSFULL, null, null)).getTriggeredEvent());
134-
assertNull(publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), null, null, null)).getTriggeredEvent());
135-
assertNull(publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_STARTED, null, null)).getTriggeredEvent());
136-
assertEquals(CommitStatusPublisher.Event.REMOVED_FROM_QUEUE, publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_REMOVED_FROM_QUEUE, null, null)).getTriggeredEvent());
133+
assertEquals(CommitStatusPublisher.Event.QUEUED, publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), null, null, null)).getTriggeredEvent());
134+
assertEquals(CommitStatusPublisher.Event.STARTED, publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_STARTED, null, null)).getTriggeredEvent());
135+
assertEquals(CommitStatusPublisher.Event.REMOVED_FROM_QUEUE, publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.WARNING.getName(), DefaultStatusMessages.BUILD_REMOVED_FROM_QUEUE, null, null)).getTriggeredEvent());
136+
assertEquals(CommitStatusPublisher.Event.REMOVED_FROM_QUEUE, publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.WARNING.getName(), DefaultStatusMessages.BUILD_REMOVED_FROM_QUEUE_AS_CANCELED, null, null)).getTriggeredEvent());
137137
assertNull(publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), "", null, null)).getTriggeredEvent());
138138
assertEquals(CommitStatusPublisher.Event.QUEUED, publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_QUEUED, null, null)).getTriggeredEvent());
139-
assertEquals(CommitStatusPublisher.Event.INTERRUPTED, publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.WARNING.getName(), null, null, null)).getTriggeredEvent());
139+
assertNull(publisher.getRevisionStatus(promotion, new GiteaCommitStatus(null, GiteaBuildStatus.WARNING.getName(), null, null, null)).getTriggeredEvent());
140140
}
141141

142-
public void should_define_correctly_if_event_allowed() {
142+
public void should_allow_queued_depending_on_build_type() {
143143
Mock removedBuildMock = new Mock(SQueuedBuild.class);
144144
removedBuildMock.stubs().method("getBuildTypeId").withNoArguments().will(returnValue("buildType"));
145145
removedBuildMock.stubs().method("getItemId").withNoArguments().will(returnValue("123"));
@@ -151,8 +151,8 @@ public void should_define_correctly_if_event_allowed() {
151151
SQueuedBuild removedBuild = (SQueuedBuild)removedBuildMock.proxy();
152152

153153
GiteaPublisher publisher = (GiteaPublisher)myPublisher;
154-
assertTrue(publisher.getRevisionStatusForRemovedBuild(removedBuild, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_QUEUED, "typeFullName", "http://localhost:8111/viewQueued.html?itemId=123")).isEventAllowed(CommitStatusPublisher.Event.REMOVED_FROM_QUEUE));
155-
assertFalse(publisher.getRevisionStatusForRemovedBuild(removedBuild, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_QUEUED, "anotherTypeFullName", "http://localhost:8111/viewQueued.html?itemId=321")).isEventAllowed(CommitStatusPublisher.Event.REMOVED_FROM_QUEUE));
154+
assertTrue(publisher.getRevisionStatusForRemovedBuild(removedBuild, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_QUEUED, "typeFullName", "http://localhost:8111/viewQueued.html?itemId=123")).isEventAllowed(CommitStatusPublisher.Event.REMOVED_FROM_QUEUE, Long.MAX_VALUE));
155+
assertFalse(publisher.getRevisionStatusForRemovedBuild(removedBuild, new GiteaCommitStatus(null, GiteaBuildStatus.PENDING.getName(), DefaultStatusMessages.BUILD_QUEUED, "anotherTypeFullName", "http://localhost:8111/viewQueued.html?itemId=321")).isEventAllowed(CommitStatusPublisher.Event.REMOVED_FROM_QUEUE, Long.MAX_VALUE));
156156
}
157157

158158
@BeforeMethod
@@ -177,7 +177,10 @@ protected Map<String, String> getPublisherParams() {
177177

178178
@Override
179179
protected boolean respondToGet(String url, HttpResponse httpResponse) {
180-
if (url.contains("/repos" + "/" + OWNER + "/" + CORRECT_REPO)) {
180+
if (url.contains("/repos/" + OWNER + "/" + CORRECT_REPO + "/statuses")) {
181+
String revision = getRevision(url, "/api/v1/repos/" + OWNER + "/" + CORRECT_REPO + "/statuses/");
182+
respondWithStatuses(httpResponse, revision);
183+
} else if (url.contains("/repos" + "/" + OWNER + "/" + CORRECT_REPO)) {
181184
respondWithRepoInfo(httpResponse, CORRECT_REPO, false, true);
182185
} else if (url.contains("/repos" + "/" + OWNER + "/" + GROUP_REPO)) {
183186
respondWithRepoInfo(httpResponse, GROUP_REPO, true, true);
@@ -190,8 +193,19 @@ protected boolean respondToGet(String url, HttpResponse httpResponse) {
190193
return true;
191194
}
192195

196+
private void respondWithStatuses(HttpResponse httpResponse, String revision) {
197+
List<GiteaCommitStatus> statuses = myRevisionToStatuses.getOrDefault(revision, new ArrayList<>());
198+
String json = gson.toJson(statuses.stream().map(s -> new GiteaCommitStatus(0L, s.status, s.description, s.context, s.target_url)).collect(Collectors.toList()));
199+
httpResponse.setEntity(new StringEntity(json, StandardCharsets.UTF_8));
200+
}
201+
193202
@Override
194203
protected boolean respondToPost(String url, String requestData, final HttpRequest httpRequest, HttpResponse httpResponse) {
204+
String revision = getRevision(url, "/api/v1/repos/" + OWNER + "/" + CORRECT_REPO + "/statuses/");
205+
if (revision != null) {
206+
GiteaCommitStatus status = gson.fromJson(requestData, GiteaCommitStatus.class);
207+
myRevisionToStatuses.computeIfAbsent(revision, k -> new ArrayList<>()).add(status);
208+
}
195209
return isUrlExpected(url, httpResponse);
196210
}
197211

@@ -206,4 +220,8 @@ private void respondWithRepoInfo(HttpResponse httpResponse, String repoName, boo
206220
httpResponse.setEntity(new StringEntity(jsonResponse, "UTF-8"));
207221
}
208222

223+
@Override
224+
protected boolean checkEventFinished(@NotNull String requestString, boolean isSuccessful) {
225+
return requestString.contains(isSuccessful ? "success" : "failure");
226+
}
209227
}

0 commit comments

Comments
 (0)