2
2
3
3
import java .io .IOException ;
4
4
import java .util .HashSet ;
5
+ import java .util .Map ;
5
6
import java .util .Set ;
7
+ import java .util .stream .Collectors ;
6
8
7
- import org .kohsuke .github .GHOrganization ;
8
- import org .kohsuke .github .GHTeam ;
9
- import org .kohsuke .github .GHUser ;
10
- import org .kohsuke .github .GitHub ;
11
- import org .kohsuke .github .GitHubBuilder ;
9
+ import org .kohsuke .github .*;
12
10
13
11
14
12
public class GitHubServiceImpl implements GitHubService {
15
13
private GitHub github ;
16
14
15
+ private static final Map <Role , GHOrganization .Permission > PERMISSIONS_MAP = Map .of (
16
+ Role .READ , GHOrganization .Permission .PULL ,
17
+ Role .TRIAGE , GHOrganization .Permission .TRIAGE ,
18
+ Role .WRITE , GHOrganization .Permission .PUSH ,
19
+ Role .MAINTAIN , GHOrganization .Permission .MAINTAIN ,
20
+ Role .ADMIN , GHOrganization .Permission .ADMIN
21
+ );
22
+
17
23
public GitHubServiceImpl (String oauthToken ) {
18
24
try {
19
25
this .github = new GitHubBuilder ().withOAuthToken (oauthToken ).build ();
@@ -22,11 +28,27 @@ public GitHubServiceImpl(String oauthToken) {
22
28
}
23
29
}
24
30
31
+ @ Override
32
+ public GHTeam getTeamFromRepo (
33
+ String repoName , String orgName , String teamName ) throws IOException {
34
+ GHOrganization org = github .getOrganization (orgName );
35
+ GHRepository repo = org .getRepository (repoName );
36
+ Set <GHTeam > teams = ((GHRepository ) repo ).getTeams ();
37
+
38
+ for (GHTeam team : teams ) {
39
+ if (team .getName ().equals (teamName )) {
40
+ return team ;
41
+ }
42
+ }
43
+ return null ;
44
+ }
45
+
25
46
@ Override
26
47
public GHOrganization getOrganization (String name ) throws IOException {
27
48
return github .getOrganization (name );
28
49
}
29
50
51
+
30
52
@ Override
31
53
public void addDeveloperToTeam (GHTeam team , String developer ) throws IOException {
32
54
GHUser user = github .getUser (developer );
@@ -53,4 +75,32 @@ public GHTeam createTeam(String orgName, String teamName, GHTeam.Privacy privacy
53
75
GHOrganization org = github .getOrganization (orgName );
54
76
return org .createTeam (teamName ).privacy (privacy ).create ();
55
77
}
78
+
79
+ @ Override
80
+ public void updateTeamRole (GHRepository repo , GHTeam ghTeam , Role role ) throws IOException {
81
+ GHOrganization .Permission permission = PERMISSIONS_MAP .get (role );
82
+ GHOrganization .RepositoryRole repoRole = GHOrganization .RepositoryRole .from (permission );
83
+ ghTeam .add (repo , repoRole );
84
+ }
85
+
86
+ @ Override
87
+ public void removeTeamFromRepository (GHTeam team , GHRepository repo ) throws IOException {
88
+ team .remove (repo );
89
+ }
90
+
91
+ /**
92
+ * Retrieves the names of all additional teams associated with the given GitHub repository, excluding the repo team.
93
+ * This method returns only team names because the current Java GitHub API does not support retrieving roles
94
+ * that teams hold within specific repositories. Therefore, role-related information is not available.
95
+ */
96
+ @ Override
97
+ public Set <String > getCurrentTeams (GHRepository repo , GHTeam repoTeam ) throws IOException {
98
+ Set <GHTeam > allTeams = repo .getTeams ();
99
+
100
+ return allTeams .stream ()
101
+ .filter (team -> !team .equals (repoTeam ))
102
+ .map (GHTeam ::getName )
103
+ .collect (Collectors .toSet ());
104
+ }
105
+
56
106
}
0 commit comments