Skip to content

Commit 35a725d

Browse files
authored
Merge pull request #4 from nedredmond/check-org
- Upgrade dependencies - Implement org check - Restructure code
2 parents 6a1a258 + c084f2a commit 35a725d

File tree

6 files changed

+199
-84
lines changed

6 files changed

+199
-84
lines changed

go.mod

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ go 1.19
55
require github.com/cli/go-gh v1.0.0
66

77
require (
8+
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
89
github.com/cli/safeexec v1.0.0 // indirect
910
github.com/cli/shurcooL-graphql v0.0.2 // indirect
10-
github.com/henvic/httpretty v0.0.6 // indirect
11+
github.com/henvic/httpretty v0.1.0 // indirect
1112
github.com/kr/text v0.2.0 // indirect
1213
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
1314
github.com/mattn/go-isatty v0.0.16 // indirect
14-
github.com/mattn/go-runewidth v0.0.13 // indirect
15-
github.com/muesli/termenv v0.12.0 // indirect
16-
github.com/rivo/uniseg v0.2.0 // indirect
15+
github.com/mattn/go-runewidth v0.0.14 // indirect
16+
github.com/muesli/termenv v0.13.0 // indirect
17+
github.com/rivo/uniseg v0.4.3 // indirect
1718
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
18-
golang.org/x/net v0.0.0-20220923203811-8be639271d50 // indirect
19-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
20-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
19+
golang.org/x/net v0.2.0 // indirect
20+
golang.org/x/sys v0.2.0 // indirect
21+
golang.org/x/term v0.2.0 // indirect
2122
gopkg.in/yaml.v3 v3.0.1 // indirect
2223
)

go.sum

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
2+
github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=
3+
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
24
github.com/cli/go-gh v1.0.0 h1:zE1YUAUYqGXNZuICEBeOkIMJ5F50BS0ftvtoWGlsEFI=
35
github.com/cli/go-gh v1.0.0/go.mod h1:bqxLdCoTZ73BuiPEJx4olcO/XKhVZaFDchFagYRBweE=
46
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
@@ -10,6 +12,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1012
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
1113
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
1214
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
15+
github.com/henvic/httpretty v0.1.0 h1:Htk66UUEbXTD4JR0qJZaw8YAMKw+9I24ZZOnDe/ti+E=
16+
github.com/henvic/httpretty v0.1.0/go.mod h1:ViEsly7wgdugYtymX54pYp6Vv2wqZmNHayJ6q8tlKCc=
1317
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1418
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1519
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
@@ -20,28 +24,62 @@ github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peK
2024
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
2125
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
2226
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
27+
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
28+
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2329
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
2430
github.com/muesli/termenv v0.12.0 h1:KuQRUE3PgxRFWhq4gHvZtPSLCGDqM5q/cYr1pZ39ytc=
2531
github.com/muesli/termenv v0.12.0/go.mod h1:WCCv32tusQ/EEZ5S8oUIIrC/nIuBcxCVqlN4Xfkv+7A=
32+
github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=
33+
github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
2634
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2735
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
2836
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
37+
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
38+
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
2939
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
3040
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8=
3141
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI=
42+
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
43+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
44+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
45+
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
46+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
47+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
48+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
3249
golang.org/x/net v0.0.0-20220923203811-8be639271d50 h1:vKyz8L3zkd+xrMeIaBsQ/MNVPVFSffdaU3ZyYlBGFnI=
3350
golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
51+
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
52+
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
53+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
54+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
55+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
56+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
57+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3458
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3559
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3660
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
61+
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3762
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3863
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3964
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
4065
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
66+
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
67+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
68+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
4169
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
4270
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
71+
golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM=
72+
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
73+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
74+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
75+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
4376
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
4477
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
78+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
79+
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
80+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
81+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
82+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4583
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4684
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
4785
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=

main.go

Lines changed: 9 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"flag"
65
"fmt"
7-
"log"
8-
"os"
9-
"strings"
10-
11-
"github.com/cli/go-gh"
126
)
137

148
func main() {
159
// Flags for the command
1610
repo := flag.String("r", "", "The repo for which to check roles. If blank, the current repo is used.")
17-
friendly := flag.Bool("f", false, "Prints a friendly message instead of the role constant.")
11+
org := flag.String("o", "", "The org for which to check roles. If blank, defaults to repo check. If present, `-r` flag will be ignored.")
12+
friendly := flag.Bool("f", false, "Prints a friendly message. Otherwise, prints a machine-readable string.")
1813
// Overrides default help message to inform about args
1914
defaultUsage := flag.Usage
2015
flag.Usage = func() {
@@ -25,79 +20,16 @@ func main() {
2520
flag.Parse()
2621
var roles = flag.Args()
2722

28-
// Start building the command
29-
ghArgs := []string{"repo", "view"}
30-
if repo != nil && *repo != "" {
31-
// If repo provided, add it to the command
32-
ghArgs = append(ghArgs, *repo)
33-
} else {
34-
// Write the current repo to the variable but we don't add it to the command
35-
// It's for later
36-
repo = currentRepoName()
37-
}
38-
ghArgs = append(ghArgs, "--json", "viewerPermission")
39-
40-
// Execute the command
41-
stdOut, _, err := gh.Exec(ghArgs...)
42-
if err != nil {
43-
log.Fatal(err)
44-
os.Exit(1)
45-
}
46-
47-
// Parse the output
48-
var result struct {
49-
Role string `json:"viewerPermission"`
50-
}
51-
err = json.Unmarshal(stdOut.Bytes(), &result)
52-
if err != nil {
53-
log.Fatal(err)
54-
os.Exit(1)
55-
}
56-
57-
success := func() {
58-
if *friendly {
59-
fmt.Printf("Current user has %s role on %s.\n", result.Role, *repo)
60-
} else {
61-
fmt.Println(result.Role)
62-
}
23+
if *org != "" {
24+
// Check org roles
25+
evaluate(*org, orgRole(*org), roles, *friendly)
6326
}
6427

65-
// If no roles were specified, print the role
66-
if len(roles) == 0 {
67-
success()
68-
os.Exit(0)
69-
}
70-
// Otherwise, check if the user has one of the specified roles
71-
for _, role := range roles {
72-
if strings.EqualFold(result.Role, role) {
73-
success()
74-
os.Exit(0)
75-
}
76-
}
77-
// If we got here, the user doesn't have any of the specified roles
78-
79-
s := ""
80-
if len(roles) > 1 {
81-
s = "s"
82-
}
83-
84-
log.Fatal(
85-
fmt.Errorf(
86-
"User does not have role%s on %s: %s; found %s",
87-
s, *repo, strings.Join(roles, ", "), result.Role,
88-
),
89-
)
90-
os.Exit(1)
91-
}
92-
93-
func currentRepoName() *string {
94-
repository, err := gh.CurrentRepository()
95-
if err != nil {
96-
log.Fatal(err)
97-
os.Exit(1)
28+
// Check repo roles
29+
if *repo == "" {
30+
repo = currentRepoName()
9831
}
99-
repoName := repository.Name()
100-
return &repoName
32+
evaluate(*repo, repoRole(*repo), roles, *friendly)
10133
}
10234

10335
// For more examples of using go-gh, see:

org.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/cli/go-gh"
8+
"github.com/cli/go-gh/pkg/api"
9+
)
10+
11+
type LoginQuery struct {
12+
Viewer struct {
13+
Login string
14+
}
15+
}
16+
17+
func orgRole(org string) (orgRole string) {
18+
return getOrgRole(org, getLogin())
19+
}
20+
21+
func getOrgRole(org string, login string) string {
22+
restClient, err := gh.RESTClient(nil)
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
27+
path := fmt.Sprintf("orgs/%s/memberships/%s", org, login)
28+
var resp map[string]interface{}
29+
err = restClient.Get(path, &resp)
30+
if err != nil {
31+
if err.(api.HTTPError).StatusCode == 404 {
32+
log.Fatal(fmt.Errorf("user has no role in %s", org))
33+
}
34+
log.Fatal(err)
35+
}
36+
37+
return resp["role"].(string)
38+
}
39+
40+
func getLogin() string {
41+
// GitHub CLI doesn't make it easy to get the current user's login.
42+
// I could either parse it from the verbose `gh auth status` or make an API call.
43+
// Since the status is subject to change, I'll just make the API call.
44+
gqlClient, err := gh.GQLClient(nil)
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
49+
var query LoginQuery
50+
err = gqlClient.Query("LoginQuery", &query, nil)
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
55+
return query.Viewer.Login
56+
}

repo.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
7+
"github.com/cli/go-gh"
8+
)
9+
10+
func currentRepoName() *string {
11+
repository, err := gh.CurrentRepository()
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
repoName := repository.Name()
16+
return &repoName
17+
}
18+
19+
func repoRole(repo string) (repoRole string) {
20+
// Build the command
21+
ghArgs := []string{"repo", "view", repo, "--json", "viewerPermission"}
22+
23+
// Execute the command
24+
stdOut, _, err := gh.Exec(ghArgs...)
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
29+
// Parse the output
30+
var result struct {
31+
Role string `json:"viewerPermission"`
32+
}
33+
err = json.Unmarshal(stdOut.Bytes(), &result)
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
38+
return result.Role
39+
}

utils.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strings"
8+
)
9+
10+
func evaluate(entity string, userRole string, roles []string, friendly bool) {
11+
// If no roles were specified, print the role
12+
if len(roles) == 0 {
13+
succeed(entity, userRole, friendly)
14+
}
15+
16+
// Otherwise, check if the user has one of the specified roles
17+
for _, role := range roles {
18+
if strings.EqualFold(userRole, role) {
19+
succeed(entity, userRole, friendly)
20+
}
21+
}
22+
23+
// If we got here, the user doesn't have any of the specified roles
24+
fail(entity, userRole, roles)
25+
}
26+
27+
func succeed(entity string, userRole string, friendly bool) {
28+
roleString := strings.ToLower(userRole)
29+
if friendly {
30+
fmt.Printf("Current user has %s role in %s.\n", roleString, entity)
31+
} else {
32+
fmt.Println(roleString)
33+
}
34+
os.Exit(0)
35+
}
36+
37+
func fail(entity string, userRole string, checkedRoles []string) {
38+
s := ""
39+
if len(checkedRoles) > 1 {
40+
s = "s"
41+
}
42+
43+
log.Fatal(
44+
fmt.Errorf(
45+
"user does not have role%s in %s: %s; found %s",
46+
s, entity, strings.Join(checkedRoles, ", "), strings.ToLower(userRole),
47+
),
48+
)
49+
}

0 commit comments

Comments
 (0)