Skip to content

Commit

Permalink
show number of selected repositories in secret list
Browse files Browse the repository at this point in the history
  • Loading branch information
vilmibm committed Dec 10, 2020
1 parent 408d5c6 commit 2e6639f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
56 changes: 44 additions & 12 deletions pkg/cmd/secret/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package list
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -69,7 +70,7 @@ func listRun(opts *ListOptions) error {
}
}

var secrets []Secret
var secrets []*Secret
if orgName == "" {
secrets, err = getRepoSecrets(client, baseRepo)
} else {
Expand All @@ -90,7 +91,7 @@ func listRun(opts *ListOptions) error {
tp.AddField(updatedAt, nil, nil)
if secret.Visibility != "" {
if opts.IO.IsStdoutTTY() {
tp.AddField(fmtVisibility(secret), nil, nil)
tp.AddField(fmtVisibility(*secret), nil, nil)
} else {
tp.AddField(strings.ToUpper(secret.Visibility), nil, nil)
}
Expand All @@ -107,9 +108,11 @@ func listRun(opts *ListOptions) error {
}

type Secret struct {
Name string
UpdatedAt time.Time `json:"updated_at"`
Visibility string
Name string
UpdatedAt time.Time `json:"updated_at"`
Visibility string
SelectedReposURL string `json:"selected_repositories_url"`
NumSelectedRepos int
}

func fmtVisibility(s Secret) string {
Expand All @@ -119,27 +122,56 @@ func fmtVisibility(s Secret) string {
case shared.VisPrivate:
return "Visible to private repositories"
case shared.VisSelected:
// TODO print how many? print which ones?
return "Visible to selected repositories"
if s.NumSelectedRepos == 1 {
return "Visible to 1 selected repository"
} else {
return fmt.Sprintf("Visible to %d selected repositories", s.NumSelectedRepos)
}
}
return ""
}

func getOrgSecrets(client *api.Client, orgName string) ([]Secret, error) {
func getOrgSecrets(client *api.Client, orgName string) ([]*Secret, error) {
host := ghinstance.OverridableDefault()
return getSecrets(client, host, fmt.Sprintf("orgs/%s/actions/secrets", orgName))
secrets, err := getSecrets(client, host, fmt.Sprintf("orgs/%s/actions/secrets", orgName))
if err != nil {
return nil, err
}

type responseData struct {
TotalCount int `json:"total_count"`
}

for _, secret := range secrets {
if secret.SelectedReposURL == "" {
continue
}
u, err := url.Parse(secret.SelectedReposURL)
if err != nil {
return nil, fmt.Errorf("failed determining selected repositories for %s: %w", secret.Name, err)
}

var result responseData
err = client.REST(u.Host, "GET", u.Path[1:], nil, &result)
if err != nil {
return nil, fmt.Errorf("failed determining selected repositories for %s: %w", secret.Name, err)
}
secret.NumSelectedRepos = result.TotalCount
}

return secrets, nil
}

func getRepoSecrets(client *api.Client, repo ghrepo.Interface) ([]Secret, error) {
func getRepoSecrets(client *api.Client, repo ghrepo.Interface) ([]*Secret, error) {
return getSecrets(client, repo.RepoHost(), fmt.Sprintf("repos/%s/actions/secrets",
ghrepo.FullName(repo)))
}

type secretsPayload struct {
Secrets []Secret
Secrets []*Secret
}

func getSecrets(client *api.Client, host, path string) ([]Secret, error) {
func getSecrets(client *api.Client, host, path string) ([]*Secret, error) {
result := secretsPayload{}

err := client.REST(host, "GET", path, nil, &result)
Expand Down
21 changes: 13 additions & 8 deletions pkg/cmd/secret/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ func Test_NewCmdList(t *testing.T) {
}
}

// TODO run tests

func Test_listRun(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -106,7 +104,7 @@ func Test_listRun(t *testing.T) {
wantOut: []string{
"SECRET_ONE.*Updated 1988-10-11.*Visible to all repositories",
"SECRET_TWO.*Updated 2020-12-04.*Visible to private repositories",
"SECRET_THREE.*Updated 1975-11-30.*Visible to selected repositories",
"SECRET_THREE.*Updated 1975-11-30.*Visible to 2 selected repositories",
},
},
{
Expand All @@ -132,7 +130,7 @@ func Test_listRun(t *testing.T) {
t2, _ := time.Parse("2006-01-02", "1975-11-30")
path := "repos/owner/repo/actions/secrets"
payload := secretsPayload{}
payload.Secrets = []Secret{
payload.Secrets = []*Secret{
{
Name: "SECRET_ONE",
UpdatedAt: t0,
Expand All @@ -147,7 +145,7 @@ func Test_listRun(t *testing.T) {
},
}
if tt.opts.OrgName != "" {
payload.Secrets = []Secret{
payload.Secrets = []*Secret{
{
Name: "SECRET_ONE",
UpdatedAt: t0,
Expand All @@ -159,12 +157,19 @@ func Test_listRun(t *testing.T) {
Visibility: shared.VisPrivate,
},
{
Name: "SECRET_THREE",
UpdatedAt: t2,
Visibility: shared.VisSelected,
Name: "SECRET_THREE",
UpdatedAt: t2,
Visibility: shared.VisSelected,
SelectedReposURL: fmt.Sprintf("https://api.github.com/orgs/%s/actions/secrets/SECRET_THREE/repositories", tt.opts.OrgName),
},
}
path = fmt.Sprintf("orgs/%s/actions/secrets", tt.opts.OrgName)

reg.Register(
httpmock.REST("GET", fmt.Sprintf("orgs/%s/actions/secrets/SECRET_THREE/repositories", tt.opts.OrgName)),
httpmock.JSONResponse(struct {
TotalCount int `json:"total_count"`
}{2}))
}

reg.Register(httpmock.REST("GET", path), httpmock.JSONResponse(payload))
Expand Down

0 comments on commit 2e6639f

Please sign in to comment.