Skip to content

Commit 092d254

Browse files
committed
Be a lot more flexible as for GHMemberChanges values
Unfortunately, things are not consistent between added and edited. added provide a permission of some sort and a role name (sometimes...) edited only provides permission and it's not exactly the same ones For now, I think we need to return strings to avoid issues until GitHub adjusts the API.
1 parent 4f44fee commit 092d254

4 files changed

Lines changed: 261 additions & 10 deletions

File tree

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

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.kohsuke.github;
22

3+
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
34
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
45
import org.kohsuke.github.internal.EnumUtils;
56

@@ -11,6 +12,8 @@ public class GHMemberChanges {
1112

1213
private FromToPermission permission;
1314

15+
private FromRoleName roleName;
16+
1417
/**
1518
* Get changes to permission.
1619
*
@@ -20,6 +23,18 @@ public FromToPermission getPermission() {
2023
return permission;
2124
}
2225

26+
/**
27+
* Get changes to the role name.
28+
* <p>
29+
* Apparently, it is recommended to use this rather than permission if defined. But it will only be defined when
30+
* adding and not when editing.
31+
*
32+
* @return changes to role name
33+
*/
34+
public FromRoleName getRoleName() {
35+
return roleName;
36+
}
37+
2338
/**
2439
* Changes to permission.
2540
*/
@@ -34,19 +49,52 @@ public static class FromToPermission {
3449
*
3550
* @return the from
3651
*/
37-
public GHOrganization.Permission getFrom() {
38-
return EnumUtils
39-
.getNullableEnumOrDefault(GHOrganization.Permission.class, from, GHOrganization.Permission.UNKNOWN);
52+
@WithBridgeMethods(value = GHOrganization.Permission.class, adapterMethod = "stringToOrgPermission")
53+
public String getFrom() {
54+
return from;
4055
}
4156

4257
/**
4358
* Gets the to.
4459
*
4560
* @return the to
4661
*/
47-
public GHOrganization.Permission getTo() {
48-
return EnumUtils
49-
.getNullableEnumOrDefault(GHOrganization.Permission.class, to, GHOrganization.Permission.UNKNOWN);
62+
@WithBridgeMethods(value = GHOrganization.Permission.class, adapterMethod = "stringToOrgPermission")
63+
public String getTo() {
64+
return to;
65+
}
66+
67+
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getFrom and getTo")
68+
private Object stringToOrgPermission(String permissionType, Class type) {
69+
switch (permissionType) {
70+
case "admin" :
71+
return GHOrganization.Permission.ADMIN;
72+
case "none" :
73+
return GHOrganization.Permission.UNKNOWN;
74+
case "read" :
75+
return GHOrganization.Permission.PULL;
76+
case "write" :
77+
return GHOrganization.Permission.PUSH;
78+
default :
79+
return EnumUtils.getNullableEnumOrDefault(GHPermissionType.class, to, GHPermissionType.UNKNOWN);
80+
}
81+
}
82+
}
83+
84+
/**
85+
* Changes to role name.
86+
*/
87+
public static class FromRoleName {
88+
89+
private String to;
90+
91+
/**
92+
* Gets the to.
93+
*
94+
* @return the to
95+
*/
96+
public String getTo() {
97+
return to;
5098
}
5199
}
52100
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public void testBridgeMethods() throws IOException {
6060

6161
verifyBridgeMethods(GHTeam.class, "getId", int.class, long.class, String.class);
6262

63+
verifyBridgeMethods(GHMemberChanges.FromToPermission.class,
64+
"getTo",
65+
String.class,
66+
GHOrganization.Permission.class);
67+
verifyBridgeMethods(GHMemberChanges.FromToPermission.class,
68+
"getFrom",
69+
String.class,
70+
GHOrganization.Permission.class);
71+
6372
// verifyBridgeMethods(GitHub.class, "getMyself", GHMyself.class, GHUser.class);
6473

6574
}

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.junit.Test;
55
import org.kohsuke.github.GHCheckRun.Conclusion;
66
import org.kohsuke.github.GHCheckRun.Status;
7-
import org.kohsuke.github.GHOrganization.Permission;
87
import org.kohsuke.github.GHProjectsV2Item.ContentType;
98
import org.kohsuke.github.GHProjectsV2ItemChanges.FieldType;
109
import org.kohsuke.github.GHTeam.Privacy;
@@ -1715,8 +1714,8 @@ public void member_edited() throws Exception {
17151714
assertThat(member.getLogin(), is("yrodiere"));
17161715

17171716
GHMemberChanges changes = memberPayload.getChanges();
1718-
assertThat(changes.getPermission().getFrom(), is(Permission.ADMIN));
1719-
assertThat(changes.getPermission().getTo(), is(Permission.TRIAGE));
1717+
assertThat(changes.getPermission().getFrom(), is("admin"));
1718+
assertThat(changes.getPermission().getTo(), is("triage"));
17201719
}
17211720

17221721
/**
@@ -1741,7 +1740,33 @@ public void member_added() throws Exception {
17411740

17421741
GHMemberChanges changes = memberPayload.getChanges();
17431742
assertThat(changes.getPermission().getFrom(), is(nullValue()));
1744-
assertThat(changes.getPermission().getTo(), is(Permission.ADMIN));
1743+
assertThat(changes.getPermission().getTo(), is("admin"));
1744+
}
1745+
1746+
/**
1747+
* Member added with role name defined.
1748+
*
1749+
* @throws Exception
1750+
* the exception
1751+
*/
1752+
@Test
1753+
public void member_added_role_name() throws Exception {
1754+
final GHEventPayload.Member memberPayload = GitHub.offline()
1755+
.parseEventPayload(payload.asReader(), GHEventPayload.Member.class);
1756+
1757+
assertThat(memberPayload.getAction(), is("added"));
1758+
1759+
assertThat(memberPayload.getOrganization().getLogin(), is("gsmet-bot-playground"));
1760+
assertThat(memberPayload.getRepository().getName(), is("github-automation-with-quarkus-demo-playground"));
1761+
1762+
GHUser member = memberPayload.getMember();
1763+
assertThat(member.getId(), is(412878L));
1764+
assertThat(member.getLogin(), is("yrodiere"));
1765+
1766+
GHMemberChanges changes = memberPayload.getChanges();
1767+
assertThat(changes.getPermission().getFrom(), is(nullValue()));
1768+
assertThat(changes.getPermission().getTo(), is("write"));
1769+
assertThat(changes.getRoleName().getTo(), is("maintain"));
17451770
}
17461771

17471772
/**
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
{
2+
"action": "added",
3+
"member": {
4+
"login": "yrodiere",
5+
"id": 412878,
6+
"node_id": "MDQ6VXNlcjQxMjg3OA==",
7+
"avatar_url": "https://avatars.githubusercontent.com/u/412878?v=4",
8+
"gravatar_id": "",
9+
"url": "https://api.github.com/users/yrodiere",
10+
"html_url": "https://github.com/yrodiere",
11+
"followers_url": "https://api.github.com/users/yrodiere/followers",
12+
"following_url": "https://api.github.com/users/yrodiere/following{/other_user}",
13+
"gists_url": "https://api.github.com/users/yrodiere/gists{/gist_id}",
14+
"starred_url": "https://api.github.com/users/yrodiere/starred{/owner}{/repo}",
15+
"subscriptions_url": "https://api.github.com/users/yrodiere/subscriptions",
16+
"organizations_url": "https://api.github.com/users/yrodiere/orgs",
17+
"repos_url": "https://api.github.com/users/yrodiere/repos",
18+
"events_url": "https://api.github.com/users/yrodiere/events{/privacy}",
19+
"received_events_url": "https://api.github.com/users/yrodiere/received_events",
20+
"type": "User",
21+
"site_admin": false
22+
},
23+
"changes": { "permission": { "to": "write" }, "role_name": { "to": "maintain" } },
24+
"repository": {
25+
"id": 493568123,
26+
"node_id": "R_kgDOHWtAew",
27+
"name": "github-automation-with-quarkus-demo-playground",
28+
"full_name": "gsmet-bot-playground/github-automation-with-quarkus-demo-playground",
29+
"private": false,
30+
"owner": {
31+
"login": "gsmet-bot-playground",
32+
"id": 81260024,
33+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjgxMjYwMDI0",
34+
"avatar_url": "https://avatars.githubusercontent.com/u/81260024?v=4",
35+
"gravatar_id": "",
36+
"url": "https://api.github.com/users/gsmet-bot-playground",
37+
"html_url": "https://github.com/gsmet-bot-playground",
38+
"followers_url": "https://api.github.com/users/gsmet-bot-playground/followers",
39+
"following_url": "https://api.github.com/users/gsmet-bot-playground/following{/other_user}",
40+
"gists_url": "https://api.github.com/users/gsmet-bot-playground/gists{/gist_id}",
41+
"starred_url": "https://api.github.com/users/gsmet-bot-playground/starred{/owner}{/repo}",
42+
"subscriptions_url": "https://api.github.com/users/gsmet-bot-playground/subscriptions",
43+
"organizations_url": "https://api.github.com/users/gsmet-bot-playground/orgs",
44+
"repos_url": "https://api.github.com/users/gsmet-bot-playground/repos",
45+
"events_url": "https://api.github.com/users/gsmet-bot-playground/events{/privacy}",
46+
"received_events_url": "https://api.github.com/users/gsmet-bot-playground/received_events",
47+
"type": "Organization",
48+
"site_admin": false
49+
},
50+
"html_url": "https://github.com/gsmet-bot-playground/github-automation-with-quarkus-demo-playground",
51+
"description": "Example repository with a demo GitHub app (github-automation-with-quarkus-demo-app) installed",
52+
"fork": false,
53+
"url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground",
54+
"forks_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/forks",
55+
"keys_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/keys{/key_id}",
56+
"collaborators_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/collaborators{/collaborator}",
57+
"teams_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/teams",
58+
"hooks_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/hooks",
59+
"issue_events_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/issues/events{/number}",
60+
"events_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/events",
61+
"assignees_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/assignees{/user}",
62+
"branches_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/branches{/branch}",
63+
"tags_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/tags",
64+
"blobs_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/git/blobs{/sha}",
65+
"git_tags_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/git/tags{/sha}",
66+
"git_refs_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/git/refs{/sha}",
67+
"trees_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/git/trees{/sha}",
68+
"statuses_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/statuses/{sha}",
69+
"languages_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/languages",
70+
"stargazers_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/stargazers",
71+
"contributors_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/contributors",
72+
"subscribers_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/subscribers",
73+
"subscription_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/subscription",
74+
"commits_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/commits{/sha}",
75+
"git_commits_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/git/commits{/sha}",
76+
"comments_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/comments{/number}",
77+
"issue_comment_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/issues/comments{/number}",
78+
"contents_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/contents/{+path}",
79+
"compare_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/compare/{base}...{head}",
80+
"merges_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/merges",
81+
"archive_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/{archive_format}{/ref}",
82+
"downloads_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/downloads",
83+
"issues_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/issues{/number}",
84+
"pulls_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/pulls{/number}",
85+
"milestones_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/milestones{/number}",
86+
"notifications_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/notifications{?since,all,participating}",
87+
"labels_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/labels{/name}",
88+
"releases_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/releases{/id}",
89+
"deployments_url": "https://api.github.com/repos/gsmet-bot-playground/github-automation-with-quarkus-demo-playground/deployments",
90+
"created_at": "2022-05-18T08:07:30Z",
91+
"updated_at": "2022-05-19T10:46:46Z",
92+
"pushed_at": "2023-07-07T08:22:15Z",
93+
"git_url": "git://github.com/gsmet-bot-playground/github-automation-with-quarkus-demo-playground.git",
94+
"ssh_url": "[email protected]:gsmet-bot-playground/github-automation-with-quarkus-demo-playground.git",
95+
"clone_url": "https://github.com/gsmet-bot-playground/github-automation-with-quarkus-demo-playground.git",
96+
"svn_url": "https://github.com/gsmet-bot-playground/github-automation-with-quarkus-demo-playground",
97+
"homepage": null,
98+
"size": 5,
99+
"stargazers_count": 0,
100+
"watchers_count": 0,
101+
"language": null,
102+
"has_issues": true,
103+
"has_projects": true,
104+
"has_downloads": true,
105+
"has_wiki": true,
106+
"has_pages": false,
107+
"has_discussions": false,
108+
"forks_count": 0,
109+
"mirror_url": null,
110+
"archived": false,
111+
"disabled": false,
112+
"open_issues_count": 0,
113+
"license": {
114+
"key": "apache-2.0",
115+
"name": "Apache License 2.0",
116+
"spdx_id": "Apache-2.0",
117+
"url": "https://api.github.com/licenses/apache-2.0",
118+
"node_id": "MDc6TGljZW5zZTI="
119+
},
120+
"allow_forking": true,
121+
"is_template": false,
122+
"web_commit_signoff_required": false,
123+
"topics": [],
124+
"visibility": "public",
125+
"forks": 0,
126+
"open_issues": 0,
127+
"watchers": 0,
128+
"default_branch": "main",
129+
"custom_properties": {}
130+
},
131+
"organization": {
132+
"login": "gsmet-bot-playground",
133+
"id": 81260024,
134+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjgxMjYwMDI0",
135+
"url": "https://api.github.com/orgs/gsmet-bot-playground",
136+
"repos_url": "https://api.github.com/orgs/gsmet-bot-playground/repos",
137+
"events_url": "https://api.github.com/orgs/gsmet-bot-playground/events",
138+
"hooks_url": "https://api.github.com/orgs/gsmet-bot-playground/hooks",
139+
"issues_url": "https://api.github.com/orgs/gsmet-bot-playground/issues",
140+
"members_url": "https://api.github.com/orgs/gsmet-bot-playground/members{/member}",
141+
"public_members_url": "https://api.github.com/orgs/gsmet-bot-playground/public_members{/member}",
142+
"avatar_url": "https://avatars.githubusercontent.com/u/81260024?v=4",
143+
"description": null
144+
},
145+
"sender": {
146+
"login": "gsmet",
147+
"id": 1279749,
148+
"node_id": "MDQ6VXNlcjEyNzk3NDk=",
149+
"avatar_url": "https://avatars.githubusercontent.com/u/1279749?v=4",
150+
"gravatar_id": "",
151+
"url": "https://api.github.com/users/gsmet",
152+
"html_url": "https://github.com/gsmet",
153+
"followers_url": "https://api.github.com/users/gsmet/followers",
154+
"following_url": "https://api.github.com/users/gsmet/following{/other_user}",
155+
"gists_url": "https://api.github.com/users/gsmet/gists{/gist_id}",
156+
"starred_url": "https://api.github.com/users/gsmet/starred{/owner}{/repo}",
157+
"subscriptions_url": "https://api.github.com/users/gsmet/subscriptions",
158+
"organizations_url": "https://api.github.com/users/gsmet/orgs",
159+
"repos_url": "https://api.github.com/users/gsmet/repos",
160+
"events_url": "https://api.github.com/users/gsmet/events{/privacy}",
161+
"received_events_url": "https://api.github.com/users/gsmet/received_events",
162+
"type": "User",
163+
"site_admin": false
164+
},
165+
"installation": {
166+
"id": 16779846,
167+
"node_id": "MDIzOkludGVncmF0aW9uSW5zdGFsbGF0aW9uMTY3Nzk4NDY="
168+
}
169+
}

0 commit comments

Comments
 (0)