Skip to content

Commit 5c155dc

Browse files
stianstbitwiseman
andauthored
Support state reason for issues (#1793)
* Support state reason for issues Closes #1792 * Apply suggestions from code review * Update src/main/java/org/kohsuke/github/GHIssueStateReason.java --------- Co-authored-by: Liam Newman <[email protected]>
1 parent 0af15ad commit 5c155dc

File tree

19 files changed

+1203
-1
lines changed

19 files changed

+1203
-1
lines changed

src/main/java/org/kohsuke/github/GHIssue.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
2828
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2929
import org.apache.commons.lang3.StringUtils;
30+
import org.kohsuke.github.internal.EnumUtils;
3031

3132
import java.io.IOException;
3233
import java.net.URL;
@@ -35,8 +36,10 @@
3536
import java.util.Collection;
3637
import java.util.Collections;
3738
import java.util.Date;
39+
import java.util.HashMap;
3840
import java.util.List;
3941
import java.util.Locale;
42+
import java.util.Map;
4043
import java.util.Objects;
4144

4245
import static org.kohsuke.github.internal.Previews.SQUIRREL_GIRL;
@@ -67,6 +70,9 @@ public class GHIssue extends GHObject implements Reactable {
6770
/** The state. */
6871
protected String state;
6972

73+
/** The state reason. */
74+
protected String state_reason;
75+
7076
/** The number. */
7177
protected int number;
7278

@@ -198,6 +204,15 @@ public GHIssueState getState() {
198204
return Enum.valueOf(GHIssueState.class, state.toUpperCase(Locale.ENGLISH));
199205
}
200206

207+
/**
208+
* Gets state reason.
209+
*
210+
* @return the state reason
211+
*/
212+
public GHIssueStateReason getStateReason() {
213+
return EnumUtils.getNullableEnumOrDefault(GHIssueStateReason.class, state_reason, GHIssueStateReason.UNKNOWN);
214+
}
215+
201216
/**
202217
* Gets labels.
203218
*
@@ -273,6 +288,10 @@ private void edit(String key, Object value) throws IOException {
273288
root().createRequest().with(key, value).method("PATCH").withUrlPath(getApiRoute()).send();
274289
}
275290

291+
private void edit(Map<String, Object> map) throws IOException {
292+
root().createRequest().with(map).method("PATCH").withUrlPath(getApiRoute()).send();
293+
}
294+
276295
/**
277296
* Identical to edit(), but allows null for the value.
278297
*/
@@ -294,6 +313,21 @@ public void close() throws IOException {
294313
edit("state", "closed");
295314
}
296315

316+
/**
317+
* Closes this issue.
318+
*
319+
* @param reason
320+
* the reason the issue was closed
321+
* @throws IOException
322+
* the io exception
323+
*/
324+
public void close(GHIssueStateReason reason) throws IOException {
325+
Map<String, Object> map = new HashMap<>();
326+
map.put("state", "closed");
327+
map.put("state_reason", reason.name().toLowerCase(Locale.ENGLISH));
328+
edit(map);
329+
}
330+
297331
/**
298332
* Reopens this issue.
299333
*
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* The enum GHIssueStateReason.
5+
*/
6+
public enum GHIssueStateReason {
7+
8+
/** Completed **/
9+
COMPLETED,
10+
11+
/** Closed as not planned **/
12+
NOT_PLANNED,
13+
14+
/** Uknown **/
15+
UNKNOWN
16+
}

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.hamcrest.Matchers.hasSize;
1919
import static org.hamcrest.Matchers.is;
2020
import static org.hamcrest.Matchers.notNullValue;
21+
import static org.hamcrest.Matchers.nullValue;
2122

2223
// TODO: Auto-generated Javadoc
2324
/**
@@ -158,7 +159,33 @@ public void closeIssue() throws Exception {
158159
assertThat(issue.getTitle(), equalTo(name));
159160
assertThat(getRepository().getIssue(issue.getNumber()).getState(), equalTo(GHIssueState.OPEN));
160161
issue.close();
161-
assertThat(getRepository().getIssue(issue.getNumber()).getState(), equalTo(GHIssueState.CLOSED));
162+
GHIssue closedIssued = getRepository().getIssue(issue.getNumber());
163+
assertThat(closedIssued.getState(), equalTo(GHIssueState.CLOSED));
164+
assertThat(closedIssued.getStateReason(), equalTo(GHIssueStateReason.COMPLETED));
165+
}
166+
167+
/**
168+
* Close issue as not planned.
169+
*
170+
* @throws Exception
171+
* the exception
172+
*/
173+
@Test
174+
public void closeIssueNotPlanned() throws Exception {
175+
String name = "closeIssueNotPlanned";
176+
GHIssue issue = getRepository().createIssue(name).body("## test").create();
177+
assertThat(issue.getTitle(), equalTo(name));
178+
179+
GHIssue createdIssue = issue.getRepository().getIssue(issue.getNumber());
180+
181+
assertThat(createdIssue.getState(), equalTo(GHIssueState.OPEN));
182+
assertThat(createdIssue.getStateReason(), nullValue());
183+
184+
issue.close(GHIssueStateReason.NOT_PLANNED);
185+
186+
GHIssue closedIssued = getRepository().getIssue(issue.getNumber());
187+
assertThat(closedIssued.getState(), equalTo(GHIssueState.CLOSED));
188+
assertThat(closedIssued.getStateReason(), equalTo(GHIssueStateReason.NOT_PLANNED));
162189
}
163190

164191
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "stianst",
3+
"id": 2271511,
4+
"node_id": "MDQ6VXNlcjIyNzE1MTE=",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/2271511?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/stianst",
8+
"html_url": "https://github.com/stianst",
9+
"followers_url": "https://api.github.com/users/stianst/followers",
10+
"following_url": "https://api.github.com/users/stianst/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/stianst/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/stianst/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/stianst/subscriptions",
14+
"organizations_url": "https://api.github.com/users/stianst/orgs",
15+
"repos_url": "https://api.github.com/users/stianst/repos",
16+
"events_url": "https://api.github.com/users/stianst/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/stianst/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Stian Thorgersen",
21+
"company": "Red Hat",
22+
"blog": "",
23+
"location": null,
24+
"email": "[email protected]",
25+
"hireable": null,
26+
"bio": "Keycloak Project Lead",
27+
"twitter_username": null,
28+
"public_repos": 39,
29+
"public_gists": 20,
30+
"followers": 454,
31+
"following": 1,
32+
"created_at": "2012-09-03T14:55:29Z",
33+
"updated_at": "2023-11-20T07:52:25Z"
34+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"login": "hub4j-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/hub4j-test-org",
6+
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
13+
"description": "Hub4j Test Org Description (this could be null or blank too)",
14+
"name": "Hub4j Test Org Name (this could be null or blank too)",
15+
"company": null,
16+
"blog": "https://hub4j.url.io/could/be/null",
17+
"location": "Hub4j Test Org Location (this could be null or blank too)",
18+
"email": "[email protected]",
19+
"twitter_username": null,
20+
"is_verified": false,
21+
"has_organization_projects": true,
22+
"has_repository_projects": true,
23+
"public_repos": 26,
24+
"public_gists": 0,
25+
"followers": 2,
26+
"following": 0,
27+
"html_url": "https://github.com/hub4j-test-org",
28+
"created_at": "2014-05-10T19:39:11Z",
29+
"updated_at": "2020-06-04T05:56:10Z",
30+
"archived_at": null,
31+
"type": "Organization",
32+
"total_private_repos": 6,
33+
"owned_private_repos": 6,
34+
"private_gists": 0,
35+
"disk_usage": 12014,
36+
"collaborators": 1,
37+
"billing_email": "[email protected]",
38+
"default_repository_permission": "none",
39+
"members_can_create_repositories": false,
40+
"two_factor_requirement_enabled": false,
41+
"members_allowed_repository_creation_type": "none",
42+
"members_can_create_public_repositories": false,
43+
"members_can_create_private_repositories": false,
44+
"members_can_create_internal_repositories": false,
45+
"members_can_create_pages": true,
46+
"members_can_fork_private_repositories": false,
47+
"web_commit_signoff_required": false,
48+
"members_can_create_public_pages": true,
49+
"members_can_create_private_pages": true,
50+
"plan": {
51+
"name": "free",
52+
"space": 976562499,
53+
"private_repos": 10000,
54+
"filled_seats": 49,
55+
"seats": 3
56+
},
57+
"advanced_security_enabled_for_new_repositories": false,
58+
"dependabot_alerts_enabled_for_new_repositories": false,
59+
"dependabot_security_updates_enabled_for_new_repositories": false,
60+
"dependency_graph_enabled_for_new_repositories": false,
61+
"secret_scanning_enabled_for_new_repositories": false,
62+
"secret_scanning_push_protection_enabled_for_new_repositories": false,
63+
"secret_scanning_push_protection_custom_link_enabled": false,
64+
"secret_scanning_push_protection_custom_link": null,
65+
"secret_scanning_validity_checks_enabled": false
66+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
{
2+
"id": 539903172,
3+
"node_id": "R_kgDOIC5ExA",
4+
"name": "GHIssueTest",
5+
"full_name": "hub4j-test-org/GHIssueTest",
6+
"private": true,
7+
"owner": {
8+
"login": "hub4j-test-org",
9+
"id": 7544739,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/hub4j-test-org",
14+
"html_url": "https://github.com/hub4j-test-org",
15+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
16+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
20+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
21+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
22+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/hub4j-test-org/GHIssueTest",
28+
"description": "Repository used by GHIssueTest",
29+
"fork": false,
30+
"url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest",
31+
"forks_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/forks",
32+
"keys_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/teams",
35+
"hooks_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/hooks",
36+
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/events",
38+
"assignees_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/tags",
41+
"blobs_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/languages",
47+
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/stargazers",
48+
"contributors_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/contributors",
49+
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/subscribers",
50+
"subscription_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/subscription",
51+
"commits_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/merges",
58+
"archive_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/downloads",
60+
"issues_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/hub4j-test-org/GHIssueTest/deployments",
67+
"created_at": "2022-09-22T09:33:05Z",
68+
"updated_at": "2022-09-22T09:33:16Z",
69+
"pushed_at": "2022-09-22T09:33:05Z",
70+
"git_url": "git://github.com/hub4j-test-org/GHIssueTest.git",
71+
"ssh_url": "[email protected]:hub4j-test-org/GHIssueTest.git",
72+
"clone_url": "https://github.com/hub4j-test-org/GHIssueTest.git",
73+
"svn_url": "https://github.com/hub4j-test-org/GHIssueTest",
74+
"homepage": null,
75+
"size": 0,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": false,
83+
"has_pages": false,
84+
"has_discussions": false,
85+
"forks_count": 0,
86+
"mirror_url": null,
87+
"archived": false,
88+
"disabled": false,
89+
"open_issues_count": 0,
90+
"license": null,
91+
"allow_forking": false,
92+
"is_template": false,
93+
"web_commit_signoff_required": false,
94+
"topics": [],
95+
"visibility": "private",
96+
"forks": 0,
97+
"open_issues": 0,
98+
"watchers": 0,
99+
"default_branch": "main",
100+
"permissions": {
101+
"admin": true,
102+
"maintain": true,
103+
"push": true,
104+
"triage": true,
105+
"pull": true
106+
},
107+
"temp_clone_token": "AARKSFYLQOBOSCZJCPGLQMTFZSKWQ",
108+
"allow_squash_merge": true,
109+
"allow_merge_commit": true,
110+
"allow_rebase_merge": true,
111+
"allow_auto_merge": false,
112+
"delete_branch_on_merge": false,
113+
"allow_update_branch": false,
114+
"use_squash_pr_title_as_default": false,
115+
"squash_merge_commit_message": "COMMIT_MESSAGES",
116+
"squash_merge_commit_title": "COMMIT_OR_PR_TITLE",
117+
"merge_commit_message": "PR_TITLE",
118+
"merge_commit_title": "MERGE_MESSAGE",
119+
"custom_properties": {},
120+
"organization": {
121+
"login": "hub4j-test-org",
122+
"id": 7544739,
123+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
124+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
125+
"gravatar_id": "",
126+
"url": "https://api.github.com/users/hub4j-test-org",
127+
"html_url": "https://github.com/hub4j-test-org",
128+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
129+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
130+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
131+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
132+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
133+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
134+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
135+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
136+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
137+
"type": "Organization",
138+
"site_admin": false
139+
},
140+
"security_and_analysis": {
141+
"secret_scanning": {
142+
"status": "disabled"
143+
},
144+
"secret_scanning_push_protection": {
145+
"status": "disabled"
146+
},
147+
"dependabot_security_updates": {
148+
"status": "disabled"
149+
},
150+
"secret_scanning_validity_checks": {
151+
"status": "disabled"
152+
}
153+
},
154+
"network_count": 0,
155+
"subscribers_count": 15
156+
}

0 commit comments

Comments
 (0)