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 b47b37038ed..479806c2ee9 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,omitempty"` + 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) } } +// 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) + + 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..cd8e49c2e07 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{