Skip to content

Commit

Permalink
Merge pull request #189 from pnag90/feature/support-project-webhook
Browse files Browse the repository at this point in the history
Added field project for sonarqube_webhook resource
  • Loading branch information
jdamata authored Aug 14, 2023
2 parents 786557d + 3da92f7 commit 792271d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/resources/sonarqube_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,29 @@ resource "sonarqube_webhook" "webhook" {
}
```

## Example: create a webhook owned by a project

```terraform
resource "sonarqube_project" "project" {
name = "project"
project = "project"
visibility = "public"
}
resource "sonarqube_webhook" "webhook" {
name = "terraform-webhook"
url = "https://my-webhook-destination.example.com"
project = sonarqube_project.project.name
}
```

## Argument Reference

The following arguments are supported:

- name - (Required) The name of the webhook to create. This will be displayed in the Sonarqube administration console.
- url - (Required) The URL to send event payloads to. This must begin with either `https://` or `http://`.
- project - (Optional) The key of the project that will own the webhook.
- secret - (Optional) The secret to send with the event payload.


Expand Down
25 changes: 25 additions & 0 deletions sonarqube/resource_sonarqube_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func resourceSonarqubeWebhook() *schema.Resource {
Optional: true,
Computed: true,
},
"project": {
Type: schema.TypeString,
Description: "The key of the project that will own the webhook.",
Optional: true,
ForceNew: true,
},
},
}
}
Expand All @@ -67,6 +73,10 @@ func resourceSonarqubeWebhookCreate(d *schema.ResourceData, m interface{}) error
if secret, ok := d.GetOk("secret"); ok {
params.Set("secret", secret.(string))
}
if project, ok := d.GetOk("project"); ok {
params.Set("project", project.(string))
}

sonarQubeURL.RawQuery = params.Encode()

resp, err := httpRequestHelper(
Expand Down Expand Up @@ -98,6 +108,13 @@ func resourceSonarqubeWebhookRead(d *schema.ResourceData, m interface{}) error {
sonarQubeURL := m.(*ProviderConfiguration).sonarQubeURL
sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/webhooks/list"

if project, ok := d.GetOk("project"); ok {
rawQuery := url.Values{
"project": []string{string(project.(string))},
}
sonarQubeURL.RawQuery = rawQuery.Encode()
}

resp, err := httpRequestHelper(
m.(*ProviderConfiguration).httpClient,
"GET",
Expand All @@ -120,6 +137,10 @@ func resourceSonarqubeWebhookRead(d *schema.ResourceData, m interface{}) error {
if webhook.Key == d.Id() {
d.Set("name", webhook.Name)
d.Set("url", webhook.Url)
// Field 'project' is not included in the webhook response object, so it is imported from the parameter.
if project, ok := d.GetOk("project"); ok {
d.Set("project", project.(string))
}
// Version 10.1 of sonarqube does not return the secret in the api response anymore. Field 'secret' replaced by flag 'hasSecret' in response
// Instead we just set the secret in state to the value being passed in to avoid constant drifts
if secret, ok := d.GetOk("secret"); ok {
Expand All @@ -141,6 +162,10 @@ func resourceSonarqubeWebhookUpdate(d *schema.ResourceData, m interface{}) error
"name": []string{d.Get("name").(string)},
"url": []string{d.Get("url").(string)},
}
project := d.Get("project").(string)
if project != "" {
params.Set("project", project)
}
if secret, ok := d.GetOk("secret"); ok {
params.Set("secret", secret.(string))
}
Expand Down
39 changes: 39 additions & 0 deletions sonarqube/resource_sonarqube_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,42 @@ resource "sonarqube_webhook" "%s" {
}
`, rnd, name, url, secret)
}

func TestAccSonarqubeWebhookProjectBasic(t *testing.T) {
rnd := generateRandomResourceName()
resourceName := "sonarqube_webhook." + rnd

name := acctest.RandString(10)
url := fmt.Sprintf("https://%s.com", acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
project := "testAccSonarqubeWebhookProject"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccSonarqubeWebhookProjectBasicConfig(rnd, name, url, project),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "url", url),
resource.TestCheckResourceAttr(resourceName, "project", project),
),
},
},
})
}

func testAccSonarqubeWebhookProjectBasicConfig(rnd string, name string, url string, project string) string {
return fmt.Sprintf(`
resource "sonarqube_project" "%[1]s" {
name = "%[4]s"
project = "%[4]s"
visibility = "public"
}
resource "sonarqube_webhook" "%[1]s" {
name = "%[2]s"
url = "%[3]s"
project = sonarqube_project.%[1]s.project
}`, rnd, name, url, project)
}

0 comments on commit 792271d

Please sign in to comment.