mirror of
https://github.com/ysoftdevs/terraform-provider-bitbucketserver.git
synced 2026-04-18 06:49:44 +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"`
|
SourceMatcher Matcher `json:"sourceMatcher,omitempty"`
|
||||||
TargetMatcher Matcher `json:"targetMatcher,omitempty"`
|
TargetMatcher Matcher `json:"targetMatcher,omitempty"`
|
||||||
Reviewers []Reviewer `json:"reviewers,omitempty"`
|
Reviewers []Reviewer `json:"reviewers,omitempty"`
|
||||||
RequiredApprovals int `json:"requiredApprovals,omitempty"`
|
RequiredApprovals string `json:"requiredApprovals,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultReviewersConditionResp struct {
|
type DefaultReviewersConditionResp struct {
|
||||||
@@ -251,26 +251,29 @@ func resourceDefaultReviewersConditionCreate(d *schema.ResourceData, m interface
|
|||||||
projectKey := d.Get("project_key").(string)
|
projectKey := d.Get("project_key").(string)
|
||||||
repositorySlug := d.Get("repository_slug").(string)
|
repositorySlug := d.Get("repository_slug").(string)
|
||||||
|
|
||||||
condition := &DefaultReviewersConditionPayload{
|
sourceMatcher := expandMatcher(d.Get("source_matcher").(map[string]interface{}))
|
||||||
SourceMatcher: expandMatcher(d.Get("source_matcher").(map[string]interface{})),
|
targetMatcher := expandMatcher(d.Get("target_matcher").(map[string]interface{}))
|
||||||
TargetMatcher: expandMatcher(d.Get("target_matcher").(map[string]interface{})),
|
reviewers := expandReviewers(d.Get("reviewers").(*schema.Set))
|
||||||
Reviewers: expandReviewers(d.Get("reviewers").(*schema.Set)),
|
requiredApprovals := d.Get("required_approvals").(int)
|
||||||
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 {
|
if contains(validMatcherTypeIDs, targetMatcher.Type.ID) == false {
|
||||||
return fmt.Errorf("source_matcher.type_id %s must be one of %v", condition.SourceMatcher.Type.ID, validMatcherTypeIDs)
|
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 {
|
if requiredApprovals > len(reviewers) {
|
||||||
return fmt.Errorf("target_matcher.type_id %s must be one of %v", condition.TargetMatcher.Type.ID, validMatcherTypeIDs)
|
return fmt.Errorf("required_approvals %d cannot be more than length of reviewers %d", requiredApprovals, len(reviewers))
|
||||||
}
|
}
|
||||||
|
|
||||||
if condition.RequiredApprovals > len(condition.Reviewers) {
|
bytedata, err := json.Marshal(&DefaultReviewersConditionPayload{
|
||||||
return fmt.Errorf("required_approvals %d cannot be more than length of reviewers %d", condition.RequiredApprovals, len(condition.Reviewers))
|
SourceMatcher: sourceMatcher,
|
||||||
}
|
TargetMatcher: targetMatcher,
|
||||||
|
Reviewers: reviewers,
|
||||||
bytedata, err := json.Marshal(condition)
|
RequiredApprovals: strconv.Itoa(requiredApprovals),
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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) {
|
func TestAccBitbucketDefaultReviewersCondition_forRepository(t *testing.T) {
|
||||||
key := fmt.Sprintf("%v", rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
key := fmt.Sprintf("%v", rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user