Skip to content

Commit ea3e3e2

Browse files
authored
Merge pull request #1069 from timofurrer/cleanup/jira-id
resource/gitlab_service_jira: several cleanup of tech debt
2 parents 9a9a4fd + 98ea574 commit ea3e3e2

File tree

3 files changed

+41
-73
lines changed

3 files changed

+41
-73
lines changed

docs/resources/service_jira.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ resource "gitlab_service_jira" "jira" {
4646
- `comment_on_event_enabled` (Boolean) Enable comments inside Jira issues on each GitLab event (commit / merge request)
4747
- `commit_events` (Boolean) Enable notifications for commit events
4848
- `issues_events` (Boolean) Enable notifications for issues events.
49-
- `jira_issue_transition_id` (String) The ID of a transition that moves issues to a closed state. You can find this number under the JIRA workflow administration (Administration > Issues > Workflows) by selecting View under Operations of the desired workflow of your project. By default, this ID is set to 2.
49+
- `jira_issue_transition_id` (String) The ID of a transition that moves issues to a closed state. You can find this number under the JIRA workflow administration (Administration > Issues > Workflows) by selecting View under Operations of the desired workflow of your project. By default, this ID is set to 2. **Note**: importing this field is currently not supported.
5050
- `job_events` (Boolean) Enable notifications for job events.
5151
- `merge_requests_events` (Boolean) Enable notifications for merge request events
5252
- `note_events` (Boolean) Enable notifications for note events.

internal/provider/resource_gitlab_service_jira.go

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var _ = registerResource("gitlab_service_jira", func() *schema.Resource {
2020
UpdateContext: resourceGitlabServiceJiraUpdate,
2121
DeleteContext: resourceGitlabServiceJiraDelete,
2222
Importer: &schema.ResourceImporter{
23-
StateContext: resourceGitlabServiceJiraImportState,
23+
StateContext: schema.ImportStatePassthroughContext,
2424
},
2525

2626
Schema: map[string]*schema.Schema{
@@ -81,7 +81,7 @@ var _ = registerResource("gitlab_service_jira", func() *schema.Resource {
8181
Sensitive: true,
8282
},
8383
"jira_issue_transition_id": {
84-
Description: "The ID of a transition that moves issues to a closed state. You can find this number under the JIRA workflow administration (Administration > Issues > Workflows) by selecting View under Operations of the desired workflow of your project. By default, this ID is set to 2.",
84+
Description: "The ID of a transition that moves issues to a closed state. You can find this number under the JIRA workflow administration (Administration > Issues > Workflows) by selecting View under Operations of the desired workflow of your project. By default, this ID is set to 2. **Note**: importing this field is currently not supported.",
8585
Type: schema.TypeString,
8686
Optional: true,
8787
},
@@ -166,46 +166,31 @@ func resourceGitlabServiceJiraCreate(ctx context.Context, d *schema.ResourceData
166166

167167
func resourceGitlabServiceJiraRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
168168
client := meta.(*gitlab.Client)
169-
project := d.Get("project").(string)
170-
171-
p, _, err := client.Projects.GetProject(project, nil, gitlab.WithContext(ctx))
172-
if err != nil {
173-
if is404(err) {
174-
log.Printf("[DEBUG] Removing Gitlab Jira service %s because project %s not found", d.Id(), p.Name)
175-
d.SetId("")
176-
return nil
177-
}
178-
return diag.FromErr(err)
179-
}
169+
project := d.Id()
180170

181-
log.Printf("[DEBUG] Read Gitlab Jira service %s", d.Id())
171+
log.Printf("[DEBUG] Read Gitlab Jira service %s", project)
182172

183173
jiraService, _, err := client.Services.GetJiraService(project, gitlab.WithContext(ctx))
184174
if err != nil {
185175
if is404(err) {
186-
log.Printf("[DEBUG] gitlab jira service not found %s", project)
176+
log.Printf("[DEBUG] gitlab jira service not found %s, removing from state", project)
187177
d.SetId("")
188178
return nil
189179
}
190180
return diag.FromErr(err)
191181
}
192182

193-
if v := jiraService.Properties.URL; v != "" {
194-
d.Set("url", v)
183+
d.Set("project", project)
184+
d.Set("url", jiraService.Properties.URL)
185+
d.Set("api_url", jiraService.Properties.APIURL)
186+
d.Set("username", jiraService.Properties.Username)
187+
d.Set("project_key", jiraService.Properties.ProjectKey)
188+
// FIXME: The API or go-gitlab doesn't return `jira_issue_transition_id` properly,
189+
// therefore we don't overwrite the value set in the config, in case it's not set in the API
190+
// response. This currently impacts the import of this attribute.
191+
if jiraService.Properties.JiraIssueTransitionID != "" {
192+
d.Set("jira_issue_transition_id", jiraService.Properties.JiraIssueTransitionID)
195193
}
196-
if v := jiraService.Properties.APIURL; v != "" {
197-
d.Set("api_url", v)
198-
}
199-
if v := jiraService.Properties.Username; v != "" {
200-
d.Set("username", v)
201-
}
202-
if v := jiraService.Properties.ProjectKey; v != "" {
203-
d.Set("project_key", v)
204-
}
205-
if v := jiraService.Properties.JiraIssueTransitionID; v != "" {
206-
d.Set("jira_issue_transition_id", v)
207-
}
208-
209194
d.Set("title", jiraService.Title)
210195
d.Set("created_at", jiraService.CreatedAt.String())
211196
d.Set("updated_at", jiraService.UpdatedAt.String())
@@ -264,9 +249,3 @@ func expandJiraOptions(d *schema.ResourceData) (*gitlab.SetJiraServiceOptions, e
264249

265250
return &setJiraServiceOptions, nil
266251
}
267-
268-
func resourceGitlabServiceJiraImportState(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
269-
d.Set("project", d.Id())
270-
271-
return []*schema.ResourceData{d}, nil
272-
}

internal/provider/resource_gitlab_service_jira_test.go

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ func TestAccGitlabServiceJira_basic(t *testing.T) {
3535
resource.TestCheckResourceAttr(jiraResourceName, "comment_on_event_enabled", "false"),
3636
),
3737
},
38+
// Verify Import
39+
{
40+
ResourceName: jiraResourceName,
41+
ImportState: true,
42+
ImportStateVerify: true,
43+
// FIXME: there is a bug in the GitLab API which causes the `jira_issue_transition_id` field
44+
// to be empty in the response. Therefore, we ignore it for now in the import.
45+
// See https://gitlab.com/gitlab-org/gitlab/-/issues/362437
46+
ImportStateVerifyIgnore: []string{"password", "jira_issue_transition_id"},
47+
},
3848
// Update the jira service
3949
{
4050
Config: testAccGitlabServiceJiraUpdateConfig(rInt),
@@ -50,6 +60,16 @@ func TestAccGitlabServiceJira_basic(t *testing.T) {
5060
resource.TestCheckResourceAttr(jiraResourceName, "comment_on_event_enabled", "true"),
5161
),
5262
},
63+
// Verify Import
64+
{
65+
ResourceName: jiraResourceName,
66+
ImportState: true,
67+
ImportStateVerify: true,
68+
// FIXME: there is a bug in the GitLab API which causes the `jira_issue_transition_id` field
69+
// to be empty in the response. Therefore, we ignore it for now in the import.
70+
// See https://gitlab.com/gitlab-org/gitlab/-/issues/362437
71+
ImportStateVerifyIgnore: []string{"password", "jira_issue_transition_id"},
72+
},
5373
// Update the jira service to get back to previous settings
5474
{
5575
Config: testAccGitlabServiceJiraConfig(rInt),
@@ -64,30 +84,15 @@ func TestAccGitlabServiceJira_basic(t *testing.T) {
6484
resource.TestCheckResourceAttr(jiraResourceName, "comment_on_event_enabled", "false"),
6585
),
6686
},
67-
},
68-
})
69-
}
70-
71-
// lintignore: AT002 // TODO: Resolve this tfproviderlint issue
72-
func TestAccGitlabServiceJira_import(t *testing.T) {
73-
jiraResourceName := "gitlab_service_jira.jira"
74-
rInt := acctest.RandInt()
75-
76-
resource.Test(t, resource.TestCase{
77-
ProviderFactories: providerFactories,
78-
CheckDestroy: testAccCheckGitlabServiceJiraDestroy,
79-
Steps: []resource.TestStep{
80-
{
81-
Config: testAccGitlabServiceJiraConfig(rInt),
82-
},
87+
// Verify Import
8388
{
8489
ResourceName: jiraResourceName,
85-
ImportStateIdFunc: getJiraProjectID(jiraResourceName),
8690
ImportState: true,
8791
ImportStateVerify: true,
88-
ImportStateVerifyIgnore: []string{
89-
"password",
90-
},
92+
// FIXME: there is a bug in the GitLab API which causes the `jira_issue_transition_id` field
93+
// to be empty in the response. Therefore, we ignore it for now in the import.
94+
// See https://gitlab.com/gitlab-org/gitlab/-/issues/362437
95+
ImportStateVerifyIgnore: []string{"password", "jira_issue_transition_id"},
9196
},
9297
},
9398
})
@@ -136,22 +141,6 @@ func testAccCheckGitlabServiceJiraDestroy(s *terraform.State) error {
136141
return nil
137142
}
138143

139-
func getJiraProjectID(n string) resource.ImportStateIdFunc {
140-
return func(s *terraform.State) (string, error) {
141-
rs, ok := s.RootModule().Resources[n]
142-
if !ok {
143-
return "", fmt.Errorf("Not Found: %s", n)
144-
}
145-
146-
project := rs.Primary.Attributes["project"]
147-
if project == "" {
148-
return "", fmt.Errorf("No project ID is set")
149-
}
150-
151-
return project, nil
152-
}
153-
}
154-
155144
func testAccGitlabServiceJiraConfig(rInt int) string {
156145
return fmt.Sprintf(`
157146
resource "gitlab_project" "foo" {

0 commit comments

Comments
 (0)