Skip to content

Commit 14dccc2

Browse files
authored
Add support for Required Workflows (#2979)
Fixes: #2978.
1 parent 630bfdb commit 14dccc2

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

github/github-accessors.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos_rules.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ type RequiredStatusChecksRuleParameters struct {
9797
StrictRequiredStatusChecksPolicy bool `json:"strict_required_status_checks_policy"`
9898
}
9999

100+
// RuleRequiredWorkflow represents the Workflow for the RequireWorkflowsRuleParameters object.
101+
type RuleRequiredWorkflow struct {
102+
Path string `json:"path"`
103+
Ref *string `json:"ref,omitempty"`
104+
RepositoryID *int64 `json:"repository_id,omitempty"`
105+
Sha *string `json:"sha,omitempty"`
106+
}
107+
108+
// RequiredWorkflowsRuleParameters represents the workflows rule parameters.
109+
type RequiredWorkflowsRuleParameters struct {
110+
RequiredWorkflows []*RuleRequiredWorkflow `json:"workflows"`
111+
}
112+
100113
// RepositoryRule represents a GitHub Rule.
101114
type RepositoryRule struct {
102115
Type string `json:"type"`
@@ -171,6 +184,16 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error {
171184
bytes, _ := json.Marshal(params)
172185
rawParams := json.RawMessage(bytes)
173186

187+
r.Parameters = &rawParams
188+
case "workflows":
189+
params := RequiredWorkflowsRuleParameters{}
190+
if err := json.Unmarshal(*RepositoryRule.Parameters, &params); err != nil {
191+
return err
192+
}
193+
194+
bytes, _ := json.Marshal(params)
195+
rawParams := json.RawMessage(bytes)
196+
174197
r.Parameters = &rawParams
175198
default:
176199
r.Type = ""
@@ -329,6 +352,18 @@ func NewTagNamePatternRule(params *RulePatternParameters) (rule *RepositoryRule)
329352
}
330353
}
331354

355+
// NewRequiredWorkflowsRule creates a rule to require which status checks must pass before branches can be merged into a branch rule.
356+
func NewRequiredWorkflowsRule(params *RequiredWorkflowsRuleParameters) (rule *RepositoryRule) {
357+
bytes, _ := json.Marshal(params)
358+
359+
rawParams := json.RawMessage(bytes)
360+
361+
return &RepositoryRule{
362+
Type: "workflows",
363+
Parameters: &rawParams,
364+
}
365+
}
366+
332367
// Ruleset represents a GitHub ruleset object.
333368
type Ruleset struct {
334369
ID *int64 `json:"id,omitempty"`

github/repos_rules_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) {
215215
},
216216
wantErr: true,
217217
},
218+
"Required workflows params": {
219+
data: `{"type":"workflows","parameters":{"workflows":[{"path": ".github/workflows/test.yml", "repository_id": 1}]}}`,
220+
want: NewRequiredWorkflowsRule(&RequiredWorkflowsRuleParameters{
221+
RequiredWorkflows: []*RuleRequiredWorkflow{
222+
{
223+
Path: ".github/workflows/test.yml",
224+
RepositoryID: Int64(1),
225+
},
226+
},
227+
}),
228+
},
218229
"Invalid type": {
219230
data: `{"type":"unknown"}`,
220231
want: &RepositoryRule{

0 commit comments

Comments
 (0)