-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (179 loc) · 4.99 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"context"
"flag"
"fmt"
"github.com/google/go-github/v43/github"
"github.com/jdxcode/netrc"
"golang.org/x/oauth2"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"rsc.io/getopt"
)
var DOMAINS = []string{"mdsol.com", "shyftanalytics.com", "3ds.com"}
// Default values
const ORG = "mdsol"
const TeamMedidata = "Team Medidata"
const TokenEnvVar = "GITHUB_AUTH_TOKEN"
// Helper function
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// creates the initial contact with GitHub - uses the users netrc to get the
// token
func connect() (context.Context, *http.Client, *github.Client) {
usr, err := user.Current()
if err != nil {
log.Fatal("Unable to get User")
}
var token string
// check the environment variable
token = os.Getenv(TokenEnvVar)
if token == "" {
n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
if err != nil {
log.Fatal("Unable to load token")
}
token = n.Machine("api.github.com").Get("password")
}
if token == "" {
log.Fatal("Unable to find a token for access")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return ctx, tc, client
}
func userIsValid(ctx context.Context, client *github.Client, tc *http.Client, userLogin string) (bool, *github.User) {
ghUser := userPrerequisites(ctx, client, &userLogin)
// check membership of org
result, code := meetsOrgPrequisites(ctx, client, ghUser)
if !result && code == 1 {
if code == 1 {
prompt(fmt.Sprintf("User %s is not a member of organisation %s", *ghUser.Login, ORG))
log.Println("User ", *ghUser.Login, " is not a member of organization ", ORG)
} else {
log.Printf("Unable to determine organization membership")
}
return false, ghUser
}
result, code = meetsSSOPrequisites(ctx, tc, ghUser)
if !result {
prompt(
fmt.Sprintf("User %s is not SSO Enabled", *ghUser.Login),
)
log.Printf("User %s is not SSO enabled", *ghUser.Login)
return false, ghUser
}
return true, ghUser
}
// Go time!
func main() {
var teamName = flag.String("team", TeamMedidata, "Specified Team")
var resetFlag = flag.Bool("reset", false, "Generate the Reset link")
var entityTeams = flag.Bool("teams", false, "List User/Repo Teams")
var addToTM = flag.Bool("add", false, "Add User to Team Medidata")
var help = flag.Bool("help", false, "Print help")
getopt.Alias("s", "team")
getopt.Alias("a", "add")
getopt.Alias("t", "teams")
getopt.Alias("r", "reset")
getopt.Alias("h", "help")
getopt.Parse()
if *help == true {
fmt.Println("Usage is: ghMdsol <options> <logins or repository names>")
fmt.Println("where options are:")
getopt.PrintDefaults()
os.Exit(0)
}
var userOrRepoList = flag.Args()
if len(userOrRepoList) == 0 {
log.Fatal("Usage is: ghMdsol <options> <logins or repository names>")
}
// create a connection
ctx, tc, client := connect()
for i := 0; i < len(userOrRepoList); i++ {
entitySlug := userOrRepoList[i]
if entitySlug == "" {
// skip empty
continue
}
if isRepository(ctx, client, ORG, entitySlug) {
// check the repo exists and we have permission
_, err := checkRepository(ctx, client, ORG, entitySlug)
if err != nil {
log.Printf("Can't resolve Repository")
continue
}
// get the teams
teams, err := getRepositoryTeams(ctx, client, ORG, entitySlug)
if err != nil {
log.Printf("Unable to resolve teams for Repostory %s: %s", entitySlug, err)
continue
}
log.Printf("Repository %s has the following teams with access:", entitySlug)
for _, team := range teams {
log.Printf("* %s (%s) %s", team.name, team.url, team.access)
}
} else {
login, err := resolveLogin(ctx, tc, &entitySlug)
if err != nil {
log.Printf("Unable to resolve %s: %s", entitySlug, err)
continue
}
if login == "" {
continue
}
log.Printf("Processing %s", login)
// check the user exists
if isUser(ctx, client, &login) {
// check the user is valid
valid, ghUser := userIsValid(ctx, client, tc, login)
if !valid {
continue
}
// Supply the reset URL
if *resetFlag {
// copy the reset URL to clipboard
prompt(fmt.Sprintf("https://github.com/orgs/mdsol/people/%s/sso", login))
log.Printf(
"Reset Link: https://github.com/orgs/mdsol/people/%s/sso",
login,
)
continue
}
if *addToTM {
team := getTeamByName(ctx, client, ORG, *teamName)
checkAndAddMember(ctx, client, team, ghUser)
continue
}
// check membership of team
if *entityTeams {
teams, err := getUserTeams(ctx, tc, ORG, login)
if err == nil {
log.Printf("User %s is a member of the following teams", login)
for _, team := range teams {
log.Printf("* %s (%s)", team.name, team.url)
}
} else {
log.Println("Unable to get teams: ", err)
}
continue
}
} else {
prompt(fmt.Sprintf("Account or Repository %s not found.", login))
}
}
}
}