split ruleset types by API type

This commit is contained in:
vaindil 2023-05-02 11:55:37 -04:00
parent 3e6419237d
commit e91670edcc
6 changed files with 33 additions and 44 deletions

View file

@ -19,7 +19,7 @@ const (
authorization = "Authorization"
cacheTTL = "X-GH-CACHE-TTL"
graphqlFeatures = "GraphQL-Features"
features = "merge_queue,push_policies"
features = "merge_queue"
userAgent = "User-Agent"
)

View file

@ -149,7 +149,7 @@ func listRun(opts *ListOptions) error {
tp.HeaderRow("ID", "NAME", "SOURCE", "STATUS", "TARGET", "RULES")
for _, rs := range result.Rulesets {
tp.AddField(strconv.Itoa(rs.Id))
tp.AddField(strconv.Itoa(rs.DatabaseId))
tp.AddField(rs.Name, tableprinter.WithColor(cs.Bold))
var ownerString string
if rs.Source.RepoOwner != "" {
@ -162,7 +162,7 @@ func listRun(opts *ListOptions) error {
tp.AddField(ownerString)
tp.AddField(strings.ToLower(rs.Enforcement))
tp.AddField(strings.ToLower(rs.Target))
tp.AddField(strconv.Itoa(rs.RulesGql.TotalCount))
tp.AddField(strconv.Itoa(rs.Rules.TotalCount))
tp.EndRow()
}

View file

@ -12,7 +12,7 @@ type RulesetResponse struct {
Level struct {
Rulesets struct {
TotalCount int
Nodes []Ruleset
Nodes []RulesetGraphQL
PageInfo struct {
HasNextPage bool
EndCursor string
@ -23,7 +23,7 @@ type RulesetResponse struct {
type RulesetList struct {
TotalCount int
Rulesets []Ruleset
Rulesets []RulesetGraphQL
}
func ListRepoRulesets(httpClient *http.Client, repo ghrepo.Interface, limit int, includeParents bool) (*RulesetList, error) {
@ -49,7 +49,7 @@ func listRulesets(httpClient *http.Client, query string, variables map[string]in
pageLimit := min(limit, 100)
res := RulesetList{
Rulesets: []Ruleset{},
Rulesets: []RulesetGraphQL{},
}
client := api.NewClientFromHTTP(httpClient)
@ -97,7 +97,7 @@ func rulesetsQuery(org bool) string {
rulesets(first: $limit, after: $endCursor, includeParents: $includeParents) {
totalCount
nodes {
id: databaseId
databaseId
name
target
enforcement
@ -105,18 +105,7 @@ func rulesetsQuery(org bool) string {
... on Repository { repoOwner: nameWithOwner }
... on Organization { orgOwner: login }
}
# conditions {
# refName {
# include
# exclude
# }
# repositoryName {
# include
# exclude
# protected
# }
# }
rulesGql: rules {
rules {
totalCount
}
}

View file

@ -1,6 +1,20 @@
package shared
type Ruleset struct {
type RulesetGraphQL struct {
DatabaseId int
Name string
Target string
Enforcement string
Source struct {
RepoOwner string
OrgOwner string
}
Rules struct {
TotalCount int
}
}
type RulesetREST struct {
Id int
Name string
Target string
@ -10,23 +24,9 @@ type Ruleset struct {
ActorId int `json:"actor_id"`
ActorType string `json:"actor_type"`
} `json:"bypass_actors"`
Conditions map[string]map[string]interface {
// RefName struct {
// Include []string
// Exclude []string
// } `json:"ref_name"`
// RepositoryName struct {
// Include []string
// Exclude []string
// Protected bool
// } `json:"repository_name"`
}
Source struct {
RepoOwner string
OrgOwner string
}
RulesGql struct {
TotalCount int
}
Rules []interface{}
Conditions map[string]map[string]interface{}
// TODO is this source field used?
SourceType string `json:"source_type"`
Source string
Rules []struct{}
}

View file

@ -9,19 +9,19 @@ import (
"github.com/cli/cli/v2/pkg/cmd/ruleset/shared"
)
func viewRepoRuleset(httpClient *http.Client, repo ghrepo.Interface, databaseId string) (*shared.Ruleset, error) {
func viewRepoRuleset(httpClient *http.Client, repo ghrepo.Interface, databaseId string) (*shared.RulesetREST, error) {
path := fmt.Sprintf("repos/%s/%s/rulesets/%s", repo.RepoOwner(), repo.RepoName(), databaseId)
return viewRuleset(httpClient, repo.RepoHost(), path)
}
func viewOrgRuleset(httpClient *http.Client, orgLogin string, databaseId string, host string) (*shared.Ruleset, error) {
func viewOrgRuleset(httpClient *http.Client, orgLogin string, databaseId string, host string) (*shared.RulesetREST, error) {
path := fmt.Sprintf("orgs/%s/rulesets/%s", orgLogin, databaseId)
return viewRuleset(httpClient, host, path)
}
func viewRuleset(httpClient *http.Client, hostname string, path string) (*shared.Ruleset, error) {
func viewRuleset(httpClient *http.Client, hostname string, path string) (*shared.RulesetREST, error) {
apiClient := api.NewClientFromHTTP(httpClient)
result := shared.Ruleset{}
result := shared.RulesetREST{}
err := apiClient.REST(hostname, "GET", path, nil, &result)
if err != nil {

View file

@ -104,7 +104,7 @@ func viewRun(opts *ViewOptions) error {
hostname, _ := cfg.DefaultHost()
var rs *shared.Ruleset
var rs *shared.RulesetREST
if opts.Organization != "" {
rs, err = viewOrgRuleset(httpClient, opts.Organization, opts.ID, hostname)
} else {