-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nedredmond/check-org
- Upgrade dependencies - Implement org check - Restructure code
- Loading branch information
Showing
6 changed files
with
199 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/cli/go-gh" | ||
"github.com/cli/go-gh/pkg/api" | ||
) | ||
|
||
type LoginQuery struct { | ||
Viewer struct { | ||
Login string | ||
} | ||
} | ||
|
||
func orgRole(org string) (orgRole string) { | ||
return getOrgRole(org, getLogin()) | ||
} | ||
|
||
func getOrgRole(org string, login string) string { | ||
restClient, err := gh.RESTClient(nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
path := fmt.Sprintf("orgs/%s/memberships/%s", org, login) | ||
var resp map[string]interface{} | ||
err = restClient.Get(path, &resp) | ||
if err != nil { | ||
if err.(api.HTTPError).StatusCode == 404 { | ||
log.Fatal(fmt.Errorf("user has no role in %s", org)) | ||
} | ||
log.Fatal(err) | ||
} | ||
|
||
return resp["role"].(string) | ||
} | ||
|
||
func getLogin() string { | ||
// GitHub CLI doesn't make it easy to get the current user's login. | ||
// I could either parse it from the verbose `gh auth status` or make an API call. | ||
// Since the status is subject to change, I'll just make the API call. | ||
gqlClient, err := gh.GQLClient(nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var query LoginQuery | ||
err = gqlClient.Query("LoginQuery", &query, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return query.Viewer.Login | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
|
||
"github.com/cli/go-gh" | ||
) | ||
|
||
func currentRepoName() *string { | ||
repository, err := gh.CurrentRepository() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
repoName := repository.Name() | ||
return &repoName | ||
} | ||
|
||
func repoRole(repo string) (repoRole string) { | ||
// Build the command | ||
ghArgs := []string{"repo", "view", repo, "--json", "viewerPermission"} | ||
|
||
// Execute the command | ||
stdOut, _, err := gh.Exec(ghArgs...) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Parse the output | ||
var result struct { | ||
Role string `json:"viewerPermission"` | ||
} | ||
err = json.Unmarshal(stdOut.Bytes(), &result) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return result.Role | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func evaluate(entity string, userRole string, roles []string, friendly bool) { | ||
// If no roles were specified, print the role | ||
if len(roles) == 0 { | ||
succeed(entity, userRole, friendly) | ||
} | ||
|
||
// Otherwise, check if the user has one of the specified roles | ||
for _, role := range roles { | ||
if strings.EqualFold(userRole, role) { | ||
succeed(entity, userRole, friendly) | ||
} | ||
} | ||
|
||
// If we got here, the user doesn't have any of the specified roles | ||
fail(entity, userRole, roles) | ||
} | ||
|
||
func succeed(entity string, userRole string, friendly bool) { | ||
roleString := strings.ToLower(userRole) | ||
if friendly { | ||
fmt.Printf("Current user has %s role in %s.\n", roleString, entity) | ||
} else { | ||
fmt.Println(roleString) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
func fail(entity string, userRole string, checkedRoles []string) { | ||
s := "" | ||
if len(checkedRoles) > 1 { | ||
s = "s" | ||
} | ||
|
||
log.Fatal( | ||
fmt.Errorf( | ||
"user does not have role%s in %s: %s; found %s", | ||
s, entity, strings.Join(checkedRoles, ", "), strings.ToLower(userRole), | ||
), | ||
) | ||
} |