mirror of
https://github.com/ysoftdevs/terraform-provider-bitbucketserver.git
synced 2026-03-30 14:12:01 +02:00
Fix zero required approvals being ignored
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user