Skip to content

Commit 1839bf6

Browse files
committed
test: implement test for enable pull request auto merge
1 parent e20a6b1 commit 1839bf6

File tree

25 files changed

+2365
-0
lines changed

25 files changed

+2365
-0
lines changed

src/main/resources/META-INF/native-image/org.kohsuke/github-api/reflect-config.json

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6703,5 +6703,125 @@
67036703
"allDeclaredMethods": true,
67046704
"allPublicClasses": true,
67056705
"allDeclaredClasses": true
6706+
},
6707+
{
6708+
"name": "org.kohsuke.github.GHPullRequest$EnablePullRequestAutoMergeResponse",
6709+
"allPublicFields": true,
6710+
"allDeclaredFields": true,
6711+
"queryAllPublicConstructors": true,
6712+
"queryAllDeclaredConstructors": true,
6713+
"allPublicConstructors": true,
6714+
"allDeclaredConstructors": true,
6715+
"queryAllPublicMethods": true,
6716+
"queryAllDeclaredMethods": true,
6717+
"allPublicMethods": true,
6718+
"allDeclaredMethods": true,
6719+
"allPublicClasses": true,
6720+
"allDeclaredClasses": true
6721+
},
6722+
{
6723+
"name": "org.kohsuke.github.GHPullRequest$EnablePullRequestAutoMergeResponse$EnablePullRequestAutoMerge",
6724+
"allPublicFields": true,
6725+
"allDeclaredFields": true,
6726+
"queryAllPublicConstructors": true,
6727+
"queryAllDeclaredConstructors": true,
6728+
"allPublicConstructors": true,
6729+
"allDeclaredConstructors": true,
6730+
"queryAllPublicMethods": true,
6731+
"queryAllDeclaredMethods": true,
6732+
"allPublicMethods": true,
6733+
"allDeclaredMethods": true,
6734+
"allPublicClasses": true,
6735+
"allDeclaredClasses": true
6736+
},
6737+
{
6738+
"name": "org.kohsuke.github.GHPullRequest$EnablePullRequestAutoMergeResponse$EnablePullRequestAutoMerge$EnablePullRequestAutoMergePullRequest",
6739+
"allPublicFields": true,
6740+
"allDeclaredFields": true,
6741+
"queryAllPublicConstructors": true,
6742+
"queryAllDeclaredConstructors": true,
6743+
"allPublicConstructors": true,
6744+
"allDeclaredConstructors": true,
6745+
"queryAllPublicMethods": true,
6746+
"queryAllDeclaredMethods": true,
6747+
"allPublicMethods": true,
6748+
"allDeclaredMethods": true,
6749+
"allPublicClasses": true,
6750+
"allDeclaredClasses": true
6751+
},
6752+
{
6753+
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse",
6754+
"allPublicFields": true,
6755+
"allDeclaredFields": true,
6756+
"queryAllPublicConstructors": true,
6757+
"queryAllDeclaredConstructors": true,
6758+
"allPublicConstructors": true,
6759+
"allDeclaredConstructors": true,
6760+
"queryAllPublicMethods": true,
6761+
"queryAllDeclaredMethods": true,
6762+
"allPublicMethods": true,
6763+
"allDeclaredMethods": true,
6764+
"allPublicClasses": true,
6765+
"allDeclaredClasses": true
6766+
},
6767+
{
6768+
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse$PullRequestOwnerRepository",
6769+
"allPublicFields": true,
6770+
"allDeclaredFields": true,
6771+
"queryAllPublicConstructors": true,
6772+
"queryAllDeclaredConstructors": true,
6773+
"allPublicConstructors": true,
6774+
"allDeclaredConstructors": true,
6775+
"queryAllPublicMethods": true,
6776+
"queryAllDeclaredMethods": true,
6777+
"allPublicMethods": true,
6778+
"allDeclaredMethods": true,
6779+
"allPublicClasses": true,
6780+
"allDeclaredClasses": true
6781+
},
6782+
{
6783+
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse$PullRequestOwnerRepository$GraphQLPullRequest",
6784+
"allPublicFields": true,
6785+
"allDeclaredFields": true,
6786+
"queryAllPublicConstructors": true,
6787+
"queryAllDeclaredConstructors": true,
6788+
"allPublicConstructors": true,
6789+
"allDeclaredConstructors": true,
6790+
"queryAllPublicMethods": true,
6791+
"queryAllDeclaredMethods": true,
6792+
"allPublicMethods": true,
6793+
"allDeclaredMethods": true,
6794+
"allPublicClasses": true,
6795+
"allDeclaredClasses": true
6796+
},
6797+
{
6798+
"name": "org.kohsuke.github.graphql.response.GHGraphQLResponse",
6799+
"allPublicFields": true,
6800+
"allDeclaredFields": true,
6801+
"queryAllPublicConstructors": true,
6802+
"queryAllDeclaredConstructors": true,
6803+
"allPublicConstructors": true,
6804+
"allDeclaredConstructors": true,
6805+
"queryAllPublicMethods": true,
6806+
"queryAllDeclaredMethods": true,
6807+
"allPublicMethods": true,
6808+
"allDeclaredMethods": true,
6809+
"allPublicClasses": true,
6810+
"allDeclaredClasses": true
6811+
},
6812+
{
6813+
"name": "org.kohsuke.github.graphql.response.GHGraphQLResponse$GHGraphQLError",
6814+
"allPublicFields": true,
6815+
"allDeclaredFields": true,
6816+
"queryAllPublicConstructors": true,
6817+
"queryAllDeclaredConstructors": true,
6818+
"allPublicConstructors": true,
6819+
"allDeclaredConstructors": true,
6820+
"queryAllPublicMethods": true,
6821+
"queryAllDeclaredMethods": true,
6822+
"allPublicMethods": true,
6823+
"allDeclaredMethods": true,
6824+
"allPublicClasses": true,
6825+
"allDeclaredClasses": true
67066826
}
67076827
]

src/main/resources/META-INF/native-image/org.kohsuke/github-api/serialization-config.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,5 +1342,29 @@
13421342
},
13431343
{
13441344
"name": "org.kohsuke.github.SkipFromToString"
1345+
},
1346+
{
1347+
"name": "org.kohsuke.github.GHPullRequest$EnablePullRequestAutoMergeResponse"
1348+
},
1349+
{
1350+
"name": "org.kohsuke.github.GHPullRequest$EnablePullRequestAutoMergeResponse$EnablePullRequestAutoMerge"
1351+
},
1352+
{
1353+
"name": "org.kohsuke.github.GHPullRequest$EnablePullRequestAutoMergeResponse$EnablePullRequestAutoMerge$EnablePullRequestAutoMergePullRequest"
1354+
},
1355+
{
1356+
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse"
1357+
},
1358+
{
1359+
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse$PullRequestOwnerRepository"
1360+
},
1361+
{
1362+
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse$PullRequestOwnerRepository$GraphQLPullRequest"
1363+
},
1364+
{
1365+
"name": "org.kohsuke.github.graphql.response.GHGraphQLResponse"
1366+
},
1367+
{
1368+
"name": "org.kohsuke.github.graphql.response.GHGraphQLResponse$GHGraphQLError"
13451369
}
13461370
]

src/test/java/org/kohsuke/github/GHPullRequestMockTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66

77
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.hamcrest.Matchers.containsString;
89
import static org.mockito.Mockito.mock;
910
import static org.mockito.Mockito.when;
1011

@@ -34,4 +35,14 @@ public void shouldMockGHPullRequest() throws IOException {
3435
assertThat("Mock should return true", pullRequest.isDraft());
3536
}
3637

38+
@Test
39+
public void getGraphQLPullRequestIdFailure() throws IOException {
40+
GHPullRequest pullRequest = new GHPullRequest();
41+
42+
try {
43+
pullRequest.getGraphqlPullRequestId();
44+
} catch (IllegalStateException e) {
45+
assertThat(e.getMessage(), containsString("Repository owner is required to get the pull request ID"));
46+
}
47+
}
3748
}

src/test/java/org/kohsuke/github/GHPullRequestTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,65 @@ public void refreshFromSearchResults() throws Exception {
10361036
pullRequestFromSearchResults.close();
10371037
}
10381038

1039+
/**
1040+
*
1041+
* Test enabling auto merge for pull request
1042+
*
1043+
* @throws IOException
1044+
* the Exception
1045+
*/
1046+
@Test
1047+
public void enablePullRequestAutoMerge() throws IOException {
1048+
String authorEmail = "sa20207@naver.com";
1049+
String clientMutationId = "github-api";
1050+
String commitBody = "This is commit body.";
1051+
String commitTitle = "This is commit title.";
1052+
String expectedCommitHeadOid = "4888b44d7204dd05680e90159af839c8b1194b6d";
1053+
1054+
GHPullRequest pullRequest = gitHub.getRepository("seate/for-test").getPullRequest(9);
1055+
1056+
pullRequest.requestEnableAutoMerge(authorEmail,
1057+
clientMutationId,
1058+
commitBody,
1059+
commitTitle,
1060+
expectedCommitHeadOid,
1061+
GHPullRequest.MergeMethod.MERGE);
1062+
1063+
AutoMerge autoMerge = pullRequest.getAutoMerge();
1064+
assertThat(autoMerge.getEnabledBy().getEmail(), is(authorEmail));
1065+
assertThat(autoMerge.getCommitMessage(), is(commitBody));
1066+
assertThat(autoMerge.getCommitTitle(), is(commitTitle));
1067+
assertThat(autoMerge.getMergeMethod(), is(GHPullRequest.MergeMethod.MERGE));
1068+
}
1069+
1070+
/**
1071+
* Test enabling auto merge for pull request with no verified email throws GraphQL exception
1072+
*
1073+
* @throws IOException
1074+
* the io exception
1075+
*/
1076+
@Test
1077+
public void enablePullRequestAutoMergeFailure() throws IOException {
1078+
String authorEmail = "failureEmail@gmail.com";
1079+
String clientMutationId = "github-api";
1080+
String commitBody = "This is commit body.";
1081+
String commitTitle = "This is commit title.";
1082+
String expectedCommitHeadOid = "4888b44d7204dd05680e90159af839c8b1194b6d";
1083+
1084+
GHPullRequest pullRequest = gitHub.getRepository("seate/for-test").getPullRequest(9);
1085+
1086+
try {
1087+
pullRequest.requestEnableAutoMerge(authorEmail,
1088+
clientMutationId,
1089+
commitBody,
1090+
commitTitle,
1091+
expectedCommitHeadOid,
1092+
GHPullRequest.MergeMethod.MERGE);
1093+
} catch (IOException e) {
1094+
assertThat(e.getMessage(), containsString("does not have a verified email"));
1095+
}
1096+
}
1097+
10391098
/**
10401099
* Gets the repository.
10411100
*
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.kohsuke.github.graphql.response;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.JavaType;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.ObjectReader;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
import static org.hamcrest.Matchers.containsString;
12+
13+
/**
14+
* Test GHGraphQLResponse's methods
15+
*/
16+
class GHGraphQLResponseMockTest {
17+
18+
/**
19+
* Test get data throws exception when response means error
20+
*
21+
* @throws JsonProcessingException
22+
*/
23+
@Test
24+
void getDataFailure() throws JsonProcessingException {
25+
String graphQLErrorResponse = "{\"data\": {\"enablePullRequestAutoMerge\": null},\"errors\": [{\"type\": "
26+
+ "\"UNPROCESSABLE\",\"path\": [\"enablePullRequestAutoMerge\"],\"locations\": [{\"line\": 2,"
27+
+ "\"column\": 5}],\"message\": \"hub4j does not have a verified email, which is required to enable "
28+
+ "auto-merging.\"}]}";
29+
30+
GHGraphQLResponse<Object> response = convertJsonToGraphQLResponse(graphQLErrorResponse);
31+
32+
try {
33+
response.getData();
34+
} catch (RuntimeException e) {
35+
assertThat(e.getMessage(), containsString("This response is Errors occurred response"));
36+
}
37+
}
38+
39+
/**
40+
* Test getErrorMessages throws exception when response means not error
41+
*
42+
* @throws JsonProcessingException
43+
*/
44+
@Test
45+
void getErrorMessagesFailure() throws JsonProcessingException {
46+
String graphQLSuccessResponse = "{\"data\": {\"repository\": {\"pullRequest\": {\"id\": "
47+
+ "\"PR_TEMP_GRAPHQL_ID\"}}}}";
48+
49+
GHGraphQLResponse<Object> response = convertJsonToGraphQLResponse(graphQLSuccessResponse);
50+
51+
try {
52+
response.getErrorMessages();
53+
} catch (RuntimeException e) {
54+
assertThat(e.getMessage(), containsString("No errors occurred"));
55+
}
56+
}
57+
58+
private GHGraphQLResponse<Object> convertJsonToGraphQLResponse(String json) throws JsonProcessingException {
59+
ObjectMapper objectMapper = new ObjectMapper();
60+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
61+
62+
ObjectReader objectReader = objectMapper.reader();
63+
JavaType javaType = objectReader.getTypeFactory()
64+
.constructParametricType(GHGraphQLResponse.class, Object.class);
65+
66+
return objectReader.forType(javaType).readValue(json);
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"login": "seate",
3+
"id": 86824703,
4+
"node_id": "MDQ6VXNlcjg2ODI0NzAz",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/86824703?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/seate",
8+
"html_url": "https://github.com/seate",
9+
"followers_url": "https://api.github.com/users/seate/followers",
10+
"following_url": "https://api.github.com/users/seate/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/seate/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/seate/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/seate/subscriptions",
14+
"organizations_url": "https://api.github.com/users/seate/orgs",
15+
"repos_url": "https://api.github.com/users/seate/repos",
16+
"events_url": "https://api.github.com/users/seate/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/seate/received_events",
18+
"type": "User",
19+
"user_view_type": "public",
20+
"site_admin": false,
21+
"name": "KIMSIWOO",
22+
"company": "Inha university",
23+
"blog": "",
24+
"location": null,
25+
"email": "sa20207@naver.com",
26+
"hireable": null,
27+
"bio": null,
28+
"twitter_username": null,
29+
"notification_email": "sa20207@naver.com",
30+
"public_repos": 34,
31+
"public_gists": 0,
32+
"followers": 2,
33+
"following": 2,
34+
"created_at": "2021-07-02T07:40:16Z",
35+
"updated_at": "2025-03-03T13:26:53Z"
36+
}

0 commit comments

Comments
 (0)