Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions github/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
// Branch or tag target rules.
RulesetRuleTypeBranchNamePattern RepositoryRuleType = "branch_name_pattern"
RulesetRuleTypeCodeCoverage RepositoryRuleType = "code_coverage"
RulesetRuleTypeCodeQuality RepositoryRuleType = "code_quality"
RulesetRuleTypeCodeScanning RepositoryRuleType = "code_scanning"
RulesetRuleTypeCommitAuthorEmailPattern RepositoryRuleType = "commit_author_email_pattern"
RulesetRuleTypeCommitMessagePattern RepositoryRuleType = "commit_message_pattern"
Expand Down Expand Up @@ -321,6 +322,7 @@
Workflows *WorkflowsRuleParameters
CodeScanning *CodeScanningRuleParameters
CodeCoverage *CodeCoverageRuleParameters
CodeQuality *CodeQualityRuleParameters
CopilotCodeReview *CopilotCodeReviewRuleParameters

// Push target rules.
Expand Down Expand Up @@ -358,6 +360,7 @@
TagNamePattern []*PatternBranchRule
Workflows []*WorkflowsBranchRule
CodeScanning []*CodeScanningBranchRule
CodeQuality []*CodeQualityBranchRule
CopilotCodeReview []*CopilotCodeReviewBranchRule

// Push target rules.
Expand Down Expand Up @@ -446,6 +449,12 @@
Parameters CodeScanningRuleParameters `json:"parameters"`
}

// CodeQualityBranchRule represents a code quality branch rule.
type CodeQualityBranchRule struct {
BranchRuleMetadata
Parameters CodeQualityRuleParameters `json:"parameters"`
}

// CopilotCodeReviewBranchRule represents a copilot code review branch rule.
type CopilotCodeReviewBranchRule struct {
BranchRuleMetadata
Expand Down Expand Up @@ -611,6 +620,11 @@
MinimumCoverage *float64 `json:"minimum_coverage,omitempty"`
}

// CodeQualityRuleParameters represents the code_quality rule parameters.
type CodeQualityRuleParameters struct {
Severity string `json:"severity,omitempty"`

Check failure on line 625 in github/rules.go

View workflow job for this annotation

GitHub Actions / golangci-lint

change the "Severity" field type to "*string" in the struct "CodeQualityRuleParameters" because its json tag uses "omitempty" (structfield)
}

// CopilotCodeReviewRuleParameters represents the copilot_code_review rule parameters.
type CopilotCodeReviewRuleParameters struct {
ReviewOnPush bool `json:"review_on_push"`
Expand Down Expand Up @@ -823,6 +837,14 @@
rawRules = append(rawRules, json.RawMessage(bytes))
}

if r.CodeQuality != nil {
bytes, err := marshalRepositoryRulesetRule(RulesetRuleTypeCodeQuality, r.CodeQuality)
if err != nil {
return nil, err
}
rawRules = append(rawRules, json.RawMessage(bytes))
}

if r.CopilotCodeReview != nil {
bytes, err := marshalRepositoryRulesetRule(RulesetRuleTypeCopilotCodeReview, r.CopilotCodeReview)
if err != nil {
Expand Down Expand Up @@ -1070,6 +1092,14 @@
return err
}
}
case RulesetRuleTypeCodeQuality:
r.CodeQuality = &CodeQualityRuleParameters{}

if w.Parameters != nil {
if err := json.Unmarshal(w.Parameters, r.CodeQuality); err != nil {
return err
}
}
case RulesetRuleTypeCopilotCodeReview:
r.CopilotCodeReview = &CopilotCodeReviewRuleParameters{}

Expand Down Expand Up @@ -1293,6 +1323,16 @@
}

r.CodeScanning = append(r.CodeScanning, &CodeScanningBranchRule{BranchRuleMetadata: w.BranchRuleMetadata, Parameters: *params})
case RulesetRuleTypeCodeQuality:
params := &CodeQualityRuleParameters{}

if w.Parameters != nil {
if err := json.Unmarshal(w.Parameters, params); err != nil {
return err
}
}

r.CodeQuality = append(r.CodeQuality, &CodeQualityBranchRule{BranchRuleMetadata: w.BranchRuleMetadata, Parameters: *params})
case RulesetRuleTypeCopilotCodeReview:
params := &CopilotCodeReviewRuleParameters{}

Expand Down Expand Up @@ -1462,6 +1502,16 @@
}
}

r.Parameters = p
case RulesetRuleTypeCodeQuality:
p := &CodeQualityRuleParameters{}

if w.Parameters != nil {
if err := json.Unmarshal(w.Parameters, p); err != nil {
return err
}
}

r.Parameters = p
case RulesetRuleTypeCopilotCodeReview:
p := &CopilotCodeReviewRuleParameters{}
Expand Down
61 changes: 61 additions & 0 deletions github/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,67 @@ func TestRepositoryRulesetRules_CodeCoverageMarshalError(t *testing.T) {
}
}

func TestCodeQualityRuleRoundTrip(t *testing.T) {
t.Parallel()
tests := []struct {
name string
params CodeQualityRuleParameters
json string
}{
{
"with_severity",
CodeQualityRuleParameters{Severity: "errors"},
`{"severity":"errors"}`,
},
{
"empty_parameters",
CodeQualityRuleParameters{},
`{}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ruleJSON := `{"type":"code_quality","parameters":` + tt.json + `}`
testJSONMarshal(t, &RepositoryRule{
Type: RulesetRuleTypeCodeQuality,
Parameters: &tt.params,
}, ruleJSON)
testJSONMarshal(t, &RepositoryRulesetRules{CodeQuality: &tt.params}, `[`+ruleJSON+`]`)
})
}

t.Run("missing_parameters", func(t *testing.T) {
t.Parallel()
testJSONUnmarshalOnly(t, &RepositoryRule{
Type: RulesetRuleTypeCodeQuality,
Parameters: &CodeQualityRuleParameters{},
}, `{"type":"code_quality"}`)
testJSONUnmarshalOnly(t, &RepositoryRulesetRules{
CodeQuality: &CodeQualityRuleParameters{},
}, `[{"type":"code_quality"}]`)
})
}

func TestCodeQualityRuleInvalidParameters(t *testing.T) {
t.Parallel()
for _, params := range []string{
`{"severity":123}`,
`"not_an_object"`,
} {
t.Run(params, func(t *testing.T) {
t.Parallel()
data := `{"type":"code_quality","parameters":` + params + `}`
if err := json.Unmarshal([]byte(data), &RepositoryRule{}); err == nil {
t.Errorf("Expected error unmarshaling %q, got nil", data)
}
if err := json.Unmarshal([]byte(`[`+data+`]`), &RepositoryRulesetRules{}); err == nil {
t.Errorf("Expected error unmarshaling [%v], got nil", data)
}
})
}
}

func TestRepositoryRulesetRules(t *testing.T) {
t.Parallel()
tests := []struct {
Expand Down
Loading