Skip to content
Closed
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
26 changes: 6 additions & 20 deletions github/orgs_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T)
"type": "creation"
},
{
"type": "update",
"parameters": {
"update_allows_fetch_and_merge": true
}
"type": "update"
},
{
"type": "deletion"
Expand Down Expand Up @@ -234,9 +231,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T)
},
Rules: []*RepositoryRule{
NewCreationRule(),
NewUpdateRule(&UpdateAllowsFetchAndMergeRuleParameters{
UpdateAllowsFetchAndMerge: true,
}),
NewUpdateRule(),
NewDeletionRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
Expand Down Expand Up @@ -320,9 +315,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T)
},
Rules: []*RepositoryRule{
NewCreationRule(),
NewUpdateRule(&UpdateAllowsFetchAndMergeRuleParameters{
UpdateAllowsFetchAndMerge: true,
}),
NewUpdateRule(),
NewDeletionRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
Expand Down Expand Up @@ -429,10 +422,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) {
"type": "creation"
},
{
"type": "update",
"parameters": {
"update_allows_fetch_and_merge": true
}
"type": "update"
},
{
"type": "deletion"
Expand Down Expand Up @@ -546,9 +536,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) {
},
Rules: []*RepositoryRule{
NewCreationRule(),
NewUpdateRule(&UpdateAllowsFetchAndMergeRuleParameters{
UpdateAllowsFetchAndMerge: true,
}),
NewUpdateRule(),
NewDeletionRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
Expand Down Expand Up @@ -630,9 +618,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) {
},
Rules: []*RepositoryRule{
NewCreationRule(),
NewUpdateRule(&UpdateAllowsFetchAndMergeRuleParameters{
UpdateAllowsFetchAndMerge: true,
}),
NewUpdateRule(),
NewDeletionRule(),
NewRequiredLinearHistoryRule(),
NewRequiredDeploymentsRule(&RequiredDeploymentEnvironmentsRuleParameters{
Expand Down
26 changes: 3 additions & 23 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ type RulePatternParameters struct {
Pattern string `json:"pattern"`
}

// UpdateAllowsFetchAndMergeRuleParameters represents the update rule parameters.
type UpdateAllowsFetchAndMergeRuleParameters struct {
UpdateAllowsFetchAndMerge bool `json:"update_allows_fetch_and_merge"`
}

// RequiredDeploymentEnvironmentsRuleParameters represents the required_deployments rule parameters.
type RequiredDeploymentEnvironmentsRuleParameters struct {
RequiredDeploymentEnvironments []string `json:"required_deployment_environments"`
Expand Down Expand Up @@ -115,18 +110,8 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error {
r.Type = RepositoryRule.Type

switch RepositoryRule.Type {
case "creation", "deletion", "required_linear_history", "required_signatures", "non_fast_forward":
case "creation", "update", "deletion", "required_linear_history", "required_signatures", "non_fast_forward":
r.Parameters = nil
case "update":
params := UpdateAllowsFetchAndMergeRuleParameters{}
if err := json.Unmarshal(*RepositoryRule.Parameters, &params); err != nil {
return err
}

bytes, _ := json.Marshal(params)
rawParams := json.RawMessage(bytes)

r.Parameters = &rawParams
case "required_deployments":
params := RequiredDeploymentEnvironmentsRuleParameters{}
if err := json.Unmarshal(*RepositoryRule.Parameters, &params); err != nil {
Expand Down Expand Up @@ -184,14 +169,9 @@ func NewCreationRule() (rule *RepositoryRule) {
}

// NewUpdateRule creates a rule to only allow users with bypass permission to update matching refs.
func NewUpdateRule(params *UpdateAllowsFetchAndMergeRuleParameters) (rule *RepositoryRule) {
bytes, _ := json.Marshal(params)

rawParams := json.RawMessage(bytes)

func NewUpdateRule() (rule *RepositoryRule) {
return &RepositoryRule{
Type: "update",
Parameters: &rawParams,
Type: "update",
}
}

Expand Down
18 changes: 4 additions & 14 deletions github/repos_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,12 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) {
Parameters: nil,
},
},
"Valid update params": {
data: `{"type":"update","parameters":{"update_allows_fetch_and_merge":true}}`,
want: NewUpdateRule(&UpdateAllowsFetchAndMergeRuleParameters{UpdateAllowsFetchAndMerge: true}),
},
"Invalid update params": {
data: `{"type":"update","parameters":{"update_allows_fetch_and_merge":"true"}}`,
"Valid update": {
data: `{"type":"update"}`,
want: &RepositoryRule{
Type: "update",
Parameters: nil,
},
wantErr: true,
},
"Valid required_deployments params": {
data: `{"type":"required_deployments","parameters":{"required_deployment_environments":["test"]}}`,
Expand Down Expand Up @@ -254,10 +249,7 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
"type": "creation"
},
{
"type": "update",
"parameters": {
"update_allows_fetch_and_merge": true
}
"type": "update"
}
]`)
})
Expand All @@ -269,9 +261,7 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
}

creationRule := NewCreationRule()
updateRule := NewUpdateRule(&UpdateAllowsFetchAndMergeRuleParameters{
UpdateAllowsFetchAndMerge: true,
})
updateRule := NewUpdateRule()

want := []*RepositoryRule{
creationRule,
Expand Down