1919import com .google .gson .Gson ;
2020import jetbrains .buildServer .MockBuildPromotion ;
2121import jetbrains .buildServer .commitPublisher .*;
22- import jetbrains .buildServer .commitPublisher .gitea .GiteaBuildStatus ;
23- import jetbrains .buildServer .commitPublisher .gitea .GiteaPublisher ;
24- import jetbrains .buildServer .commitPublisher .gitea .GiteaSettings ;
2522import jetbrains .buildServer .commitPublisher .gitea .data .GiteaCommitStatus ;
2623import jetbrains .buildServer .commitPublisher .gitea .data .GiteaPermissions ;
2724import jetbrains .buildServer .commitPublisher .gitea .data .GiteaRepoInfo ;
3128import org .apache .http .HttpRequest ;
3229import org .apache .http .HttpResponse ;
3330import org .apache .http .entity .StringEntity ;
31+ import org .jetbrains .annotations .NotNull ;
3432import org .jmock .Mock ;
3533import org .testng .annotations .BeforeMethod ;
3634import 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
4240import static org .assertj .core .api .BDDAssertions .then ;
4341
4846public 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