From f37d989bfc21df160be60c03598b08c1643857be Mon Sep 17 00:00:00 2001 From: Claystation Date: Tue, 31 Oct 2023 16:12:18 +0100 Subject: [PATCH 1/4] Added RequiredWorkflows structs --- github/repos_rules.go | 35 +++++++++++++++++++++++++++++++++++ github/repos_rules_test.go | 11 +++++++++++ 2 files changed, 46 insertions(+) diff --git a/github/repos_rules.go b/github/repos_rules.go index 2c24f8c27b3..656b1a293a9 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -97,6 +97,19 @@ type RequiredStatusChecksRuleParameters struct { StrictRequiredStatusChecksPolicy bool `json:"strict_required_status_checks_policy"` } +// RuleRequiredWorkflow represents the Workflow for the RequireWorkflowsRuleParameters object. +type RuleRequiredWorkflow struct { + Path string `json:"path"` + Ref string `json:"ref,omitempty"` + RepositoryID *int64 `json:"repository_id"` + Sha string `json:"sha,omitempty"` +} + +// RequiredWorkflowsRuleParameters represents the workflows rule parameters. +type RequiredWorkflowsRuleParameters struct { + RequiredWorkflows []RuleRequiredWorkflow `json:"workflows"` +} + // RepositoryRule represents a GitHub Rule. type RepositoryRule struct { Type string `json:"type"` @@ -171,6 +184,16 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error { bytes, _ := json.Marshal(params) rawParams := json.RawMessage(bytes) + r.Parameters = &rawParams + case "workflows": + params := RequiredWorkflowsRuleParameters{} + if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { + return err + } + + bytes, _ := json.Marshal(params) + rawParams := json.RawMessage(bytes) + r.Parameters = &rawParams default: r.Type = "" @@ -329,6 +352,18 @@ func NewTagNamePatternRule(params *RulePatternParameters) (rule *RepositoryRule) } } +// NewRequiredStatusChecksRule creates a rule to require which status checks must pass before branches can be merged into a branch rule. +func NewRequiredWorkflowsRule(params *RequiredWorkflowsRuleParameters) (rule *RepositoryRule) { + bytes, _ := json.Marshal(params) + + rawParams := json.RawMessage(bytes) + + return &RepositoryRule{ + Type: "workflows", + Parameters: &rawParams, + } +} + // Ruleset represents a GitHub ruleset object. type Ruleset struct { ID *int64 `json:"id,omitempty"` diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go index a57137c688d..eaf580a43be 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -215,6 +215,17 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) { }, wantErr: true, }, + "Required workflows params": { + data: `{"type":"workflows","parameters":{"workflows":[{"path": ".github/workflows/test.yml", "repository_id": 1}]}}`, + want: NewRequiredWorkflowsRule(&RequiredWorkflowsRuleParameters{ + RequiredWorkflows: []RuleRequiredWorkflow{ + { + Path: ".github/workflows/test.yml", + RepositoryID: Int64(1), + }, + }, + }), + }, "Invalid type": { data: `{"type":"unknown"}`, want: &RepositoryRule{ From 80b9405fdc18cf0dd94fcefc097090624aeecc61 Mon Sep 17 00:00:00 2001 From: Claystation Date: Fri, 3 Nov 2023 16:51:57 +0100 Subject: [PATCH 2/4] PR review fixes --- github/repos_rules.go | 6 +++--- github/repos_rules_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/github/repos_rules.go b/github/repos_rules.go index 656b1a293a9..dec07afd147 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -101,13 +101,13 @@ type RequiredStatusChecksRuleParameters struct { type RuleRequiredWorkflow struct { Path string `json:"path"` Ref string `json:"ref,omitempty"` - RepositoryID *int64 `json:"repository_id"` + RepositoryID *int64 `json:"repository_id,omitempty"` Sha string `json:"sha,omitempty"` } // RequiredWorkflowsRuleParameters represents the workflows rule parameters. type RequiredWorkflowsRuleParameters struct { - RequiredWorkflows []RuleRequiredWorkflow `json:"workflows"` + RequiredWorkflows []*RuleRequiredWorkflow `json:"workflows"` } // RepositoryRule represents a GitHub Rule. @@ -352,7 +352,7 @@ func NewTagNamePatternRule(params *RulePatternParameters) (rule *RepositoryRule) } } -// NewRequiredStatusChecksRule creates a rule to require which status checks must pass before branches can be merged into a branch rule. +// NewRequiredWorkflowsRule creates a rule to require which status checks must pass before branches can be merged into a branch rule. func NewRequiredWorkflowsRule(params *RequiredWorkflowsRuleParameters) (rule *RepositoryRule) { bytes, _ := json.Marshal(params) diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go index eaf580a43be..cd8e49c2e07 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -218,7 +218,7 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) { "Required workflows params": { data: `{"type":"workflows","parameters":{"workflows":[{"path": ".github/workflows/test.yml", "repository_id": 1}]}}`, want: NewRequiredWorkflowsRule(&RequiredWorkflowsRuleParameters{ - RequiredWorkflows: []RuleRequiredWorkflow{ + RequiredWorkflows: []*RuleRequiredWorkflow{ { Path: ".github/workflows/test.yml", RepositoryID: Int64(1), From 65b32762d965f8922b28511165c035d2cceee42f Mon Sep 17 00:00:00 2001 From: Claystation Date: Mon, 6 Nov 2023 09:33:04 +0100 Subject: [PATCH 3/4] Pointer for omit empty fields Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/repos_rules.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github/repos_rules.go b/github/repos_rules.go index 2381b274b7e..1af6f1d8c32 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -100,9 +100,9 @@ type RequiredStatusChecksRuleParameters struct { // RuleRequiredWorkflow represents the Workflow for the RequireWorkflowsRuleParameters object. type RuleRequiredWorkflow struct { Path string `json:"path"` - Ref string `json:"ref,omitempty"` + Ref *string `json:"ref,omitempty"` RepositoryID *int64 `json:"repository_id,omitempty"` - Sha string `json:"sha,omitempty"` + Sha *string `json:"sha,omitempty"` } // RequiredWorkflowsRuleParameters represents the workflows rule parameters. From 0574bb8897936e79513f6c791e96d9a935e3ecbb Mon Sep 17 00:00:00 2001 From: Claystation Date: Tue, 7 Nov 2023 10:29:23 +0100 Subject: [PATCH 4/4] gofmt and generate files --- github/github-accessors.go | 24 ++++++++++++++++++++++++ github/github-accessors_test.go | 30 ++++++++++++++++++++++++++++++ github/repos_rules.go | 4 ++-- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index a489d66cd43..bcae7636d0a 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -20358,6 +20358,30 @@ func (r *RuleRequiredStatusChecks) GetIntegrationID() int64 { return *r.IntegrationID } +// GetRef returns the Ref field if it's non-nil, zero value otherwise. +func (r *RuleRequiredWorkflow) GetRef() string { + if r == nil || r.Ref == nil { + return "" + } + return *r.Ref +} + +// GetRepositoryID returns the RepositoryID field if it's non-nil, zero value otherwise. +func (r *RuleRequiredWorkflow) GetRepositoryID() int64 { + if r == nil || r.RepositoryID == nil { + return 0 + } + return *r.RepositoryID +} + +// GetSha returns the Sha field if it's non-nil, zero value otherwise. +func (r *RuleRequiredWorkflow) GetSha() string { + if r == nil || r.Sha == nil { + return "" + } + return *r.Sha +} + // GetConditions returns the Conditions field. func (r *Ruleset) GetConditions() *RulesetConditions { if r == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 2c26fb6ba79..36feed7fe8e 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -23678,6 +23678,36 @@ func TestRuleRequiredStatusChecks_GetIntegrationID(tt *testing.T) { r.GetIntegrationID() } +func TestRuleRequiredWorkflow_GetRef(tt *testing.T) { + var zeroValue string + r := &RuleRequiredWorkflow{Ref: &zeroValue} + r.GetRef() + r = &RuleRequiredWorkflow{} + r.GetRef() + r = nil + r.GetRef() +} + +func TestRuleRequiredWorkflow_GetRepositoryID(tt *testing.T) { + var zeroValue int64 + r := &RuleRequiredWorkflow{RepositoryID: &zeroValue} + r.GetRepositoryID() + r = &RuleRequiredWorkflow{} + r.GetRepositoryID() + r = nil + r.GetRepositoryID() +} + +func TestRuleRequiredWorkflow_GetSha(tt *testing.T) { + var zeroValue string + r := &RuleRequiredWorkflow{Sha: &zeroValue} + r.GetSha() + r = &RuleRequiredWorkflow{} + r.GetSha() + r = nil + r.GetSha() +} + func TestRuleset_GetConditions(tt *testing.T) { r := &Ruleset{} r.GetConditions() diff --git a/github/repos_rules.go b/github/repos_rules.go index 1af6f1d8c32..479806c2ee9 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -99,9 +99,9 @@ type RequiredStatusChecksRuleParameters struct { // RuleRequiredWorkflow represents the Workflow for the RequireWorkflowsRuleParameters object. type RuleRequiredWorkflow struct { - Path string `json:"path"` + Path string `json:"path"` Ref *string `json:"ref,omitempty"` - RepositoryID *int64 `json:"repository_id,omitempty"` + RepositoryID *int64 `json:"repository_id,omitempty"` Sha *string `json:"sha,omitempty"` }