show number of selected repositories in secret list
This commit is contained in:
parent
408d5c6d96
commit
2e6639fe78
2 changed files with 57 additions and 20 deletions
|
|
@ -3,6 +3,7 @@ package list
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -69,7 +70,7 @@ func listRun(opts *ListOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
var secrets []Secret
|
||||
var secrets []*Secret
|
||||
if orgName == "" {
|
||||
secrets, err = getRepoSecrets(client, baseRepo)
|
||||
} else {
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -68,8 +68,6 @@ func Test_NewCmdList(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO run tests
|
||||
|
||||
func Test_listRun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
@ -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",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -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,
|
||||
|
|
@ -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,
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue