Skip to content

Commit 183ee8c

Browse files
gsmetbitwiseman
andauthored
Support team, team_add, member and membership payloads (#1818)
* Support team, team_add, member and membership payloads I removed/updated some outdated files that were apparently added in the hope of someone implementing the payloads but I preferred to start fresh with new updated payloads. I did a few adjustments to some existing enums to make sure we don't have an error when a payload arrives with an unknown value. I used the same pattern we decided to implement some time ago. * Apply suggestions from code review * Update src/main/java/org/kohsuke/github/GHTeamChanges.java --------- Co-authored-by: Liam Newman <[email protected]>
1 parent af19581 commit 183ee8c

File tree

17 files changed

+1535
-263
lines changed

17 files changed

+1535
-263
lines changed

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

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ public Date getStarredAt() {
18121812
* A project v2 item was archived, converted, created, edited, restored, deleted, or reordered.
18131813
*
18141814
* @see <a href=
1815-
* "https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#projects_v2_item">star
1815+
* "https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#projects_v2_item">projects_v2_item
18161816
* event</a>
18171817
*/
18181818
public static class ProjectsV2Item extends GHEventPayload {
@@ -1839,4 +1839,169 @@ public GHProjectsV2ItemChanges getChanges() {
18391839
return changes;
18401840
}
18411841
}
1842+
1843+
/**
1844+
* A team_add event was triggered.
1845+
*
1846+
* @see <a href="https://docs.github.com/en/webhooks/webhook-events-and-payloads#team_add">team_add event</a>
1847+
*/
1848+
public static class TeamAdd extends GHEventPayload {
1849+
1850+
private GHTeam team;
1851+
1852+
/**
1853+
* Gets the team.
1854+
*
1855+
* @return the team
1856+
*/
1857+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
1858+
public GHTeam getTeam() {
1859+
return team;
1860+
}
1861+
1862+
/**
1863+
* Late bind.
1864+
*/
1865+
@Override
1866+
void lateBind() {
1867+
if (team == null) {
1868+
throw new IllegalStateException(
1869+
"Expected team payload, but got something else. Maybe we've got another type of event?");
1870+
}
1871+
super.lateBind();
1872+
GHOrganization organization = getOrganization();
1873+
if (organization == null) {
1874+
throw new IllegalStateException("Organization must not be null");
1875+
}
1876+
team.wrapUp(organization);
1877+
}
1878+
}
1879+
1880+
/**
1881+
* A team event was triggered.
1882+
*
1883+
* @see <a href="https://docs.github.com/en/webhooks/webhook-events-and-payloads#team">team event</a>
1884+
*/
1885+
public static class Team extends GHEventPayload {
1886+
1887+
private GHTeam team;
1888+
1889+
private GHTeamChanges changes;
1890+
1891+
/**
1892+
* Gets the team.
1893+
*
1894+
* @return the team
1895+
*/
1896+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
1897+
public GHTeam getTeam() {
1898+
return team;
1899+
}
1900+
1901+
/**
1902+
* Gets the changes made to the team.
1903+
*
1904+
* @return the changes made to the team, null unless action is "edited".
1905+
*/
1906+
public GHTeamChanges getChanges() {
1907+
return changes;
1908+
}
1909+
1910+
/**
1911+
* Late bind.
1912+
*/
1913+
@Override
1914+
void lateBind() {
1915+
if (team == null) {
1916+
throw new IllegalStateException(
1917+
"Expected team payload, but got something else. Maybe we've got another type of event?");
1918+
}
1919+
super.lateBind();
1920+
GHOrganization organization = getOrganization();
1921+
if (organization == null) {
1922+
throw new IllegalStateException("Organization must not be null");
1923+
}
1924+
team.wrapUp(organization);
1925+
}
1926+
}
1927+
1928+
/**
1929+
* A member event was triggered.
1930+
*
1931+
* @see <a href="https://docs.github.com/en/webhooks/webhook-events-and-payloads#member">member event</a>
1932+
*/
1933+
public static class Member extends GHEventPayload {
1934+
1935+
private GHUser member;
1936+
1937+
private GHMemberChanges changes;
1938+
1939+
/**
1940+
* Gets the member.
1941+
*
1942+
* @return the member
1943+
*/
1944+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
1945+
public GHUser getMember() {
1946+
return member;
1947+
}
1948+
1949+
/**
1950+
* Gets the changes made to the member.
1951+
*
1952+
* @return the changes made to the member
1953+
*/
1954+
public GHMemberChanges getChanges() {
1955+
return changes;
1956+
}
1957+
}
1958+
1959+
/**
1960+
* A membership event was triggered.
1961+
*
1962+
* @see <a href="https://docs.github.com/en/webhooks/webhook-events-and-payloads#membership">membership event</a>
1963+
*/
1964+
public static class Membership extends GHEventPayload {
1965+
1966+
private GHTeam team;
1967+
1968+
private GHUser member;
1969+
1970+
/**
1971+
* Gets the team.
1972+
*
1973+
* @return the team
1974+
*/
1975+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
1976+
public GHTeam getTeam() {
1977+
return team;
1978+
}
1979+
1980+
/**
1981+
* Gets the member.
1982+
*
1983+
* @return the member
1984+
*/
1985+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
1986+
public GHUser getMember() {
1987+
return member;
1988+
}
1989+
1990+
/**
1991+
* Late bind.
1992+
*/
1993+
@Override
1994+
void lateBind() {
1995+
if (team == null) {
1996+
throw new IllegalStateException(
1997+
"Expected membership payload, but got something else. Maybe we've got another type of event?");
1998+
}
1999+
super.lateBind();
2000+
GHOrganization organization = getOrganization();
2001+
if (organization == null) {
2002+
throw new IllegalStateException("Organization must not be null");
2003+
}
2004+
team.wrapUp(organization);
2005+
}
2006+
}
18422007
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.kohsuke.github;
2+
3+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4+
import org.kohsuke.github.internal.EnumUtils;
5+
6+
/**
7+
* Changes made to a team.
8+
*/
9+
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
10+
public class GHMemberChanges {
11+
12+
private FromToPermission permission;
13+
14+
/**
15+
* Get changes to permission.
16+
*
17+
* @return changes to permission
18+
*/
19+
public FromToPermission getPermission() {
20+
return permission;
21+
}
22+
23+
/**
24+
* Changes to permission.
25+
*/
26+
public static class FromToPermission {
27+
28+
private String from;
29+
30+
private String to;
31+
32+
/**
33+
* Gets the from.
34+
*
35+
* @return the from
36+
*/
37+
public GHOrganization.Permission getFrom() {
38+
return EnumUtils
39+
.getNullableEnumOrDefault(GHOrganization.Permission.class, from, GHOrganization.Permission.UNKNOWN);
40+
}
41+
42+
/**
43+
* Gets the to.
44+
*
45+
* @return the to
46+
*/
47+
public GHOrganization.Permission getTo() {
48+
return EnumUtils
49+
.getNullableEnumOrDefault(GHOrganization.Permission.class, to, GHOrganization.Permission.UNKNOWN);
50+
}
51+
}
52+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,9 @@ public enum Permission {
487487
/** The triage. */
488488
TRIAGE,
489489
/** The pull. */
490-
PULL
490+
PULL,
491+
/** Unknown, before we add the new permission to the enum */
492+
UNKNOWN
491493
}
492494

493495
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public GHDeploymentStatusBuilder createDeployStatus(int deploymentId, GHDeployme
234234
return getDeployment(deploymentId).createStatus(ghDeploymentState);
235235
}
236236

237-
private static class GHRepoPermission {
237+
static class GHRepoPermission {
238238
boolean pull, push, admin;
239239
}
240240

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
44
import org.apache.commons.lang3.StringUtils;
5+
import org.kohsuke.github.internal.EnumUtils;
56

67
import java.io.IOException;
78
import java.net.URL;
@@ -27,7 +28,7 @@ public class GHTeam extends GHObject implements Refreshable {
2728
private String permission;
2829
private String slug;
2930
private String description;
30-
private Privacy privacy;
31+
private String privacy;
3132

3233
private GHOrganization organization; // populated by GET /user/teams where Teams+Orgs are returned together
3334

@@ -40,7 +41,9 @@ public enum Privacy {
4041
SECRET,
4142
/** The closed. */
4243
// only visible to organization owners and members of this team.
43-
CLOSED // visible to all members of this organization.
44+
CLOSED, // visible to all members of this organization.
45+
/** Unknown privacy value */
46+
UNKNOWN
4447
}
4548

4649
/**
@@ -122,7 +125,7 @@ public String getDescription() {
122125
* @return the privacy state.
123126
*/
124127
public Privacy getPrivacy() {
125-
return privacy;
128+
return EnumUtils.getNullableEnumOrDefault(Privacy.class, privacy, Privacy.UNKNOWN);
126129
}
127130

128131
/**
@@ -473,7 +476,7 @@ public boolean equals(Object o) {
473476
GHTeam ghTeam = (GHTeam) o;
474477
return Objects.equals(name, ghTeam.name) && Objects.equals(getUrl(), ghTeam.getUrl())
475478
&& Objects.equals(permission, ghTeam.permission) && Objects.equals(slug, ghTeam.slug)
476-
&& Objects.equals(description, ghTeam.description) && privacy == ghTeam.privacy;
479+
&& Objects.equals(description, ghTeam.description) && Objects.equals(privacy, ghTeam.privacy);
477480
}
478481

479482
/**

0 commit comments

Comments
 (0)