-
Notifications
You must be signed in to change notification settings - Fork 0
/
teams.go
57 lines (53 loc) · 1.66 KB
/
teams.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
. "github.com/google/go-github/v43/github"
"golang.org/x/net/context"
"log"
)
func isTeam(ctx context.Context, client *Client, org, entityId string) bool {
_, resp, err := client.Organizations.Get(ctx, org)
if err != nil || resp.StatusCode == 404 {
return false
}
_, resp, err = client.Teams.GetTeamBySlug(ctx, org, entityId)
if resp.StatusCode == 200 {
return true
}
return false
}
// get a team by name (using the generated slug)
func getTeamByName(ctx context.Context, client *Client, org, teamName string) *Team {
team, _, err := client.Teams.GetTeamBySlug(ctx, org, slugify(teamName))
if err != nil {
log.Fatal("Unable to find team ", teamName, " - ", err)
}
return team
}
// check the prerequisites and if satisfied add the user to the team
func checkAndAddMember(ctx context.Context, client *Client, team *Team, ghUser *User) {
var teamMembership *Membership
teamMembership, response, err := client.Teams.GetTeamMembershipByID(ctx,
*team.Organization.ID,
*team.ID,
*ghUser.Login)
// check for 404
if err != nil && response.StatusCode != 404 {
log.Fatal("Unable to check team membership: ", err)
}
if teamMembership == nil {
opts := TeamAddTeamMembershipOptions{Role: "member"}
_, _, err = client.Teams.AddTeamMembershipByID(ctx,
*team.Organization.ID,
*team.ID,
*ghUser.Login,
&opts)
if err != nil {
log.Fatal("Error adding user", *ghUser.Login, " to Team", *team.Name, ": ", err)
}
prompt(fmt.Sprintf("User %s added to %s", *ghUser.Login, *team.Name))
log.Println("User", *ghUser.Login, "added to", *team.Name)
} else {
log.Println("User", *ghUser.Login, "is already a member of", *team.Name)
}
}