Skip to content

Commit 1df88ac

Browse files
authored
Reverse badState logic. (#144)
* Reverse badState logic. Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com> * Test case Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com>
1 parent 5a6c1aa commit 1df88ac

10 files changed

Lines changed: 738 additions & 19 deletions

File tree

github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/trigger/check/SkipPRInBadState.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ public static SkipPRInBadState badState(GitHubPRRepository localRepo, LoggingTas
3232
@Override
3333
public boolean apply(@Nullable GHPullRequest remotePR) {
3434
if (remotePR == null) {
35-
return false;
35+
return true;
3636
}
3737

3838
@CheckForNull GitHubPRPullRequest localPR = localRepo.getPulls().get(remotePR.getNumber());
3939

4040
if (localPR != null && localPR.isInBadState()) {
4141
logger.error("local PR [#{} {}] is in bad state", remotePR.getNumber(), remotePR.getTitle());
42-
return true;
42+
return false;
4343
}
4444

45-
return false;
45+
return true;
4646
}
4747
}

github-pullrequest-plugin/src/test/java/com/github/kostyasha/github/integration/branch/GitHubBranchTriggerTest.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.kostyasha.github.integration.branch;
22

3-
import antlr.ANTLRException;
4-
import com.cloudbees.jenkins.GitHubPushTrigger;
53
import com.coravy.hudson.plugins.github.GithubProjectProperty;
64
import com.github.kostyasha.github.integration.branch.events.GitHubBranchEvent;
75
import com.github.kostyasha.github.integration.branch.events.impl.GitHubBranchCreatedEvent;
@@ -12,20 +10,14 @@
1210
import com.github.tomakehurst.wiremock.junit.WireMockRule;
1311
import hudson.model.FreeStyleBuild;
1412
import hudson.model.FreeStyleProject;
15-
import hudson.model.Result;
16-
import org.eclipse.jgit.lib.ObjectId;
1713
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
18-
import org.jenkinsci.plugins.github.pullrequest.GitHubPRTriggerMode;
1914
import org.junit.Rule;
2015
import org.junit.Test;
21-
import org.junit.rules.ExternalResource;
2216
import org.junit.rules.RuleChain;
2317
import org.jvnet.hudson.test.JenkinsRule;
2418
import org.jvnet.hudson.test.TestExtension;
2519

2620
import javax.inject.Inject;
27-
28-
import java.io.IOException;
2921
import java.util.ArrayList;
3022
import java.util.List;
3123

@@ -34,10 +26,7 @@
3426
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
3527
import static org.hamcrest.core.Is.is;
3628
import static org.jenkinsci.plugins.github.pullrequest.GitHubPRTriggerMode.CRON;
37-
import static org.junit.Assert.*;
38-
import static org.mockito.Matchers.isNotNull;
39-
import static org.mockito.Matchers.notNull;
40-
import static org.mockito.Mockito.when;
29+
import static org.junit.Assert.assertThat;
4130

4231
/**
4332
* @author Kanstantsin Shautsou

github-pullrequest-plugin/src/test/java/com/github/kostyasha/github/integration/branch/test/GHMockRule.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,57 @@ public void run() {
169169
});
170170
}
171171

172+
173+
public GHMockRule stubPulls() {
174+
return addSetup(new Runnable() {
175+
@Override
176+
public void run() {
177+
service().stubFor(
178+
get(urlEqualTo(
179+
format("/repos/%s/%s/pulls?state=open", REPO.getUserName(), REPO.getRepositoryName()))
180+
).willReturn(aResponse()
181+
.withStatus(200)
182+
.withHeader("Content-Type", "application/json; charset=utf-8")
183+
.withBody(classpath(GHMockRule.class, "pulls.json"))
184+
)
185+
);
186+
}
187+
});
188+
}
189+
public GHMockRule stubIssues1() {
190+
return addSetup(new Runnable() {
191+
@Override
192+
public void run() {
193+
service().stubFor(
194+
get(urlEqualTo(
195+
format("/repos/%s/%s/issues/1", REPO.getUserName(), REPO.getRepositoryName()))
196+
).willReturn(aResponse()
197+
.withStatus(200)
198+
.withHeader("Content-Type", "application/json; charset=utf-8")
199+
.withBody(classpath(GHMockRule.class, "issues1.json"))
200+
)
201+
);
202+
}
203+
});
204+
}
205+
206+
public GHMockRule stubComments1() {
207+
return addSetup(new Runnable() {
208+
@Override
209+
public void run() {
210+
service().stubFor(
211+
get(urlEqualTo(
212+
format("/repos/%s/%s/issues/1/comments", REPO.getUserName(), REPO.getRepositoryName()))
213+
).willReturn(aResponse()
214+
.withStatus(200)
215+
.withHeader("Content-Type", "application/json; charset=utf-8")
216+
.withBody(classpath(GHMockRule.class, "comments1.json"))
217+
)
218+
);
219+
}
220+
});
221+
}
222+
172223
/**
173224
* When we call one of predefined stub* methods, wiremock is not not started yet, so we need to create a closure
174225
*
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.jenkinsci.plugins.github.pullrequest;
2+
3+
import com.github.kostyasha.github.integration.branch.test.GHMockRule;
4+
import com.github.kostyasha.github.integration.branch.test.InjectJenkinsMembersRule;
5+
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
6+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
7+
import hudson.model.FreeStyleProject;
8+
import hudson.model.TopLevelItem;
9+
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.rules.RuleChain;
13+
import org.jvnet.hudson.test.JenkinsRule;
14+
import org.jvnet.hudson.test.TestExtension;
15+
import org.jvnet.hudson.test.recipes.LocalData;
16+
17+
import javax.inject.Inject;
18+
import java.io.IOException;
19+
import java.util.Map;
20+
21+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
22+
import static org.hamcrest.CoreMatchers.not;
23+
import static org.hamcrest.Matchers.containsString;
24+
import static org.hamcrest.Matchers.is;
25+
import static org.hamcrest.Matchers.notNullValue;
26+
import static org.junit.Assert.assertThat;
27+
28+
/**
29+
* @author Kanstantsin Shautsou
30+
*/
31+
public class GitHubPRTriggerMockTest {
32+
@Inject
33+
public GitHubPluginConfig config;
34+
35+
public JenkinsRule jRule = new JenkinsRule();
36+
37+
@Rule
38+
public RuleChain chain = RuleChain.outerRule(jRule)
39+
.around(new InjectJenkinsMembersRule(jRule, this));
40+
41+
@Rule
42+
public GHMockRule github = new GHMockRule(
43+
new WireMockRule(
44+
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
45+
))
46+
.stubRateLimit()
47+
.stubPulls()
48+
.stubComments1()
49+
.stubIssues1()
50+
.stubUser()
51+
.stubRepo();
52+
53+
@TestExtension
54+
public static final GHMockRule.FixedGHRepoNameTestContributor CONTRIBUTOR = new GHMockRule.FixedGHRepoNameTestContributor();
55+
56+
57+
@LocalData
58+
@Test
59+
public void badStatePR() throws InterruptedException, IOException {
60+
config.getConfigs().add(github.serverConfig());
61+
config.save();
62+
63+
Thread.sleep(1000);
64+
65+
final TopLevelItem item = jRule.getInstance().getItem("test-job");
66+
assertThat(item, notNullValue());
67+
final FreeStyleProject project = (FreeStyleProject) item;
68+
69+
GitHubPRRepository prRepository = project.getAction(GitHubPRRepository.class);
70+
assertThat(project, notNullValue());
71+
72+
assertThat(prRepository.getFullName(), is("org/repo"));
73+
74+
final Map<Integer, GitHubPRPullRequest> pulls = prRepository.getPulls();
75+
assertThat(pulls.size(), is(1));
76+
final GitHubPRPullRequest pullRequest = pulls.get(1);
77+
assertThat(pullRequest, notNullValue());
78+
assertThat(pullRequest.isInBadState(), is(true));
79+
80+
81+
final GitHubPRTrigger trigger = project.getTrigger(GitHubPRTrigger.class);
82+
assertThat(trigger, notNullValue());
83+
84+
trigger.run();
85+
86+
GitHubPRPollingLogAction logAction = project.getAction(GitHubPRPollingLogAction.class);
87+
assertThat(logAction, notNullValue());
88+
89+
assertThat(logAction.getLog(), containsString("ERROR: local PR [#1 new-feature] is in bad state"));
90+
91+
trigger.run();
92+
93+
logAction = project.getAction(GitHubPRPollingLogAction.class);
94+
95+
assertThat(logAction.getLog(), not(containsString("ERROR: local PR [#1 new-feature] is in bad state")));
96+
assertThat(logAction.getLog(), containsString("PR [#1 new-feature] not changed"));
97+
}
98+
}

github-pullrequest-plugin/src/test/java/org/jenkinsci/plugins/github/pullrequest/trigger/check/SkipPRInBadStateTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class SkipPRInBadStateTest {
4040
@Test
4141
public void ignoreNullRemotePR() {
4242
assertThat(badState(null, null).apply(null),
43-
is(false));
43+
is(true));
4444
}
4545

4646
@Test
@@ -52,7 +52,7 @@ public void ignoreNullLocalPR() {
5252
when(localRepo.getPulls()).thenReturn(pulls);
5353

5454
assertThat(badState(localRepo, tlRule.getListener()).apply(remotePR),
55-
is(false));
55+
is(true));
5656
}
5757

5858
@Test
@@ -65,7 +65,7 @@ public void ignoreLocalPRinBadState() {
6565
when(localRepo.getPulls()).thenReturn(pulls);
6666

6767
assertThat(badState(localRepo, tlRule.getListener()).apply(remotePR),
68-
is(true));
68+
is(false));
6969
}
7070

7171
@Test
@@ -78,7 +78,7 @@ public void ignoreLocalPRNotinBadState() {
7878
when(localRepo.getPulls()).thenReturn(pulls);
7979

8080
assertThat(badState(localRepo, tlRule.getListener()).apply(remotePR),
81-
is(false));
81+
is(true));
8282
}
8383

8484
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[
2+
{
3+
"url": "https://localhost/repos/org/repo/pulls/comments/1",
4+
"id": 1,
5+
"diff_hunk": "@@ -16,33 +16,40 @@ public class Connection : IConnection...",
6+
"path": "file1.txt",
7+
"position": 1,
8+
"original_position": 4,
9+
"commit_id": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
10+
"original_commit_id": "9c48853fa3dc5c1c3d6f1f1cd1f2743e72652840",
11+
"user": {
12+
"login": "org",
13+
"id": 1,
14+
"avatar_url": "https://github.com/images/error/org_happy.gif",
15+
"gravatar_id": "",
16+
"url": "https://localhost/users/org",
17+
"html_url": "https://github.com/org",
18+
"followers_url": "https://localhost/users/org/followers",
19+
"following_url": "https://localhost/users/org/following{/other_user}",
20+
"gists_url": "https://localhost/users/org/gists{/gist_id}",
21+
"starred_url": "https://localhost/users/org/starred{/owner}{/repo}",
22+
"subscriptions_url": "https://localhost/users/org/subscriptions",
23+
"organizations_url": "https://localhost/users/org/orgs",
24+
"repos_url": "https://localhost/users/org/repos",
25+
"events_url": "https://localhost/users/org/events{/privacy}",
26+
"received_events_url": "https://localhost/users/org/received_events",
27+
"type": "User",
28+
"site_admin": false
29+
},
30+
"body": "Great stuff",
31+
"created_at": "2011-04-14T16:00:49Z",
32+
"updated_at": "2011-04-14T16:00:49Z",
33+
"html_url": "https://github.com/org/repo/pull/1#discussion-diff-1",
34+
"pull_request_url": "https://localhost/repos/org/repo/pulls/1",
35+
"_links": {
36+
"self": {
37+
"href": "https://localhost/repos/org/repo/pulls/comments/1"
38+
},
39+
"html": {
40+
"href": "https://github.com/org/repo/pull/1#discussion-diff-1"
41+
},
42+
"pull_request": {
43+
"href": "https://localhost/repos/org/repo/pulls/1"
44+
}
45+
}
46+
}
47+
]

0 commit comments

Comments
 (0)