Fix zero required approvals being ignored

This commit is contained in:
Martin Litvaj
2020-07-21 19:16:35 +02:00
parent f7ff5040fa
commit f11b275f56
2 changed files with 42 additions and 15 deletions

View File

@@ -36,7 +36,7 @@ type DefaultReviewersConditionPayload struct {
SourceMatcher Matcher `json:"sourceMatcher,omitempty"`
TargetMatcher Matcher `json:"targetMatcher,omitempty"`
Reviewers []Reviewer `json:"reviewers,omitempty"`
RequiredApprovals int `json:"requiredApprovals,omitempty"`
RequiredApprovals string `json:"requiredApprovals,omitempty"`
}
type DefaultReviewersConditionResp struct {
@@ -251,26 +251,29 @@ func resourceDefaultReviewersConditionCreate(d *schema.ResourceData, m interface
projectKey := d.Get("project_key").(string)
repositorySlug := d.Get("repository_slug").(string)
condition := &DefaultReviewersConditionPayload{
SourceMatcher: expandMatcher(d.Get("source_matcher").(map[string]interface{})),
TargetMatcher: expandMatcher(d.Get("target_matcher").(map[string]interface{})),
Reviewers: expandReviewers(d.Get("reviewers").(*schema.Set)),
RequiredApprovals: d.Get("required_approvals").(int),
sourceMatcher := expandMatcher(d.Get("source_matcher").(map[string]interface{}))
targetMatcher := expandMatcher(d.Get("target_matcher").(map[string]interface{}))
reviewers := expandReviewers(d.Get("reviewers").(*schema.Set))
requiredApprovals := d.Get("required_approvals").(int)
if contains(validMatcherTypeIDs, sourceMatcher.Type.ID) == false {
return fmt.Errorf("source_matcher.type_id %s must be one of %v", sourceMatcher.Type.ID, validMatcherTypeIDs)
}
if contains(validMatcherTypeIDs, condition.SourceMatcher.Type.ID) == false {
return fmt.Errorf("source_matcher.type_id %s must be one of %v", condition.SourceMatcher.Type.ID, validMatcherTypeIDs)
if contains(validMatcherTypeIDs, targetMatcher.Type.ID) == false {
return fmt.Errorf("target_matcher.type_id %s must be one of %v", targetMatcher.Type.ID, validMatcherTypeIDs)
}
if contains(validMatcherTypeIDs, condition.TargetMatcher.Type.ID) == false {
return fmt.Errorf("target_matcher.type_id %s must be one of %v", condition.TargetMatcher.Type.ID, validMatcherTypeIDs)
if requiredApprovals > len(reviewers) {
return fmt.Errorf("required_approvals %d cannot be more than length of reviewers %d", requiredApprovals, len(reviewers))
}
if condition.RequiredApprovals > len(condition.Reviewers) {
return fmt.Errorf("required_approvals %d cannot be more than length of reviewers %d", condition.RequiredApprovals, len(condition.Reviewers))
}
bytedata, err := json.Marshal(condition)
bytedata, err := json.Marshal(&DefaultReviewersConditionPayload{
SourceMatcher: sourceMatcher,
TargetMatcher: targetMatcher,
Reviewers: reviewers,
RequiredApprovals: strconv.Itoa(requiredApprovals),
})
if err != nil {
return err

View File

@@ -35,6 +35,30 @@ func TestAccBitbucketDefaultReviewersCondition_forProject(t *testing.T) {
})
}
func TestAccBitbucketDefaultReviewersCondition_noRequiredApprovals(t *testing.T) {
key := fmt.Sprintf("%v", rand.New(rand.NewSource(time.Now().UnixNano())).Int())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBitbucketDefaultReviewersConditionDestroy,
Steps: []resource.TestStep{
{
Config: testAccBitbucketDefaultReviewersConditionResourceForProject(key, 0),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("bitbucketserver_default_reviewers_condition.test", "project_key", "TEST"+key),
resource.TestCheckResourceAttr("bitbucketserver_default_reviewers_condition.test", "source_matcher.id", "any"),
resource.TestCheckResourceAttr("bitbucketserver_default_reviewers_condition.test", "source_matcher.type_id", "ANY_REF"),
resource.TestCheckResourceAttr("bitbucketserver_default_reviewers_condition.test", "target_matcher.id", "any"),
resource.TestCheckResourceAttr("bitbucketserver_default_reviewers_condition.test", "target_matcher.type_id", "ANY_REF"),
resource.TestCheckResourceAttr("bitbucketserver_default_reviewers_condition.test", "reviewers.#", "1"),
resource.TestCheckResourceAttr("bitbucketserver_default_reviewers_condition.test", "required_approvals", "0"),
),
},
},
})
}
func TestAccBitbucketDefaultReviewersCondition_forRepository(t *testing.T) {
key := fmt.Sprintf("%v", rand.New(rand.NewSource(time.Now().UnixNano())).Int())