Skip to content

Commit 4b96220

Browse files
committed
New Datasource gitlab_project_issue
Refs #746
1 parent 9f8fe2d commit 4b96220

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

docs/data-sources/project_issue.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_project_issue Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
This data source allows you to access GitLab Project Issues.
7+
Upstream API: GitLab API docs https://docs.gitlab.com/ee/api/issues.html
8+
---
9+
10+
# gitlab_project_issue (Data Source)
11+
12+
This data source allows you to access GitLab Project Issues.
13+
14+
**Upstream API:** [GitLab API docs](https://docs.gitlab.com/ee/api/issues.html)
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "gitlab_project" "foo" {
20+
id = "foo/bar/baz"
21+
}
22+
23+
data "gitlab_project_issue" "welcome_issue" {
24+
project = data.gitlab_project.foo.id
25+
iid = 1
26+
}
27+
28+
output "welcome_issue_web_url" {
29+
value = data.gitlab_project_issue.web_url
30+
}
31+
```
32+
33+
<!-- schema generated by tfplugindocs -->
34+
## Schema
35+
36+
### Required
37+
38+
- **iid** (Number) The internal ID of the project's issue.
39+
- **project** (String) The name or ID of the project.
40+
41+
### Optional
42+
43+
- **id** (String) The ID of this resource.
44+
45+
### Read-Only
46+
47+
- **assignee_ids** (Set of Number) The IDs of the users to assign the issue to.
48+
- **author_id** (Number) The ID of the author of the issue. Use `gitlab_user` data source to get more information about the user.
49+
- **closed_at** (String) When the issue was closed. Date time string, ISO 8601 formatted, for example 2016-03-11T03:45:40Z.
50+
- **closed_by_user_id** (Number) The ID of the user that closed the issue. Use `gitlab_user` data source to get more information about the user.
51+
- **confidential** (Boolean) Set an issue to be confidential.
52+
- **created_at** (String) When the issue was created. Date time string, ISO 8601 formatted, for example 2016-03-11T03:45:40Z. Requires administrator or project/group owner rights.
53+
- **description** (String) The description of an issue. Limited to 1,048,576 characters.
54+
- **discussion_locked** (Boolean) Whether the issue is locked for discussions or not.
55+
- **discussion_to_resolve** (String) The ID of a discussion to resolve. This fills out the issue with a default description and mark the discussion as resolved. Use in combination with merge_request_to_resolve_discussions_of.
56+
- **downvotes** (Number) The number of downvotes the issue has received.
57+
- **due_date** (String) The due date. Date time string in the format YYYY-MM-DD, for example 2016-03-11.
58+
**Note:** removing a due date is currently not supported, see https://github.com/xanzy/go-gitlab/issues/1384 for details.
59+
- **epic_id** (Number) ID of the epic to add the issue to. Valid values are greater than or equal to 0.
60+
- **epic_issue_id** (Number) The ID of the epic issue.
61+
- **external_id** (String) The external ID of the issue.
62+
- **human_time_estimate** (String) The human-readable time estimate of the issue.
63+
- **human_total_time_spent** (String) The human-readable total time spent of the issue.
64+
- **issue_id** (Number) The instance-wide ID of the issue.
65+
- **issue_link_id** (Number) The ID of the issue link.
66+
- **issue_type** (String) The type of issue. Valid values are: `issue`, `incident`, `test_case`.
67+
- **labels** (Set of String) The labels of an issue.
68+
- **links** (Map of String) The links of the issue.
69+
- **merge_request_to_resolve_discussions_of** (Number) The IID of a merge request in which to resolve all issues. This fills out the issue with a default description and mark all discussions as resolved. When passing a description or title, these values take precedence over the default values.
70+
- **merge_requests_count** (Number) The number of merge requests associated with the issue.
71+
- **milestone_id** (Number) The global ID of a milestone to assign issue. To find the milestone_id associated with a milestone, view an issue with the milestone assigned and use the API to retrieve the issue's details.
72+
- **moved_to_id** (Number) The ID of the issue that was moved to.
73+
- **references** (Map of String) The references of the issue.
74+
- **state** (String) The state of the issue. Valid values are: `opened`, `closed`.
75+
- **subscribed** (Boolean) Whether the authenticated user is subscribed to the issue or not.
76+
- **task_completion_status** (List of Object) The task completion status. It's always a one element list. (see [below for nested schema](#nestedatt--task_completion_status))
77+
- **time_estimate** (Number) The time estimate of the issue.
78+
- **title** (String) The title of the issue.
79+
- **total_time_spent** (Number) The total time spent of the issue.
80+
- **updated_at** (String) When the issue was updated. Date time string, ISO 8601 formatted, for example 2016-03-11T03:45:40Z.
81+
- **upvotes** (Number) The number of upvotes the issue has received.
82+
- **user_notes_count** (Number) The number of user notes on the issue.
83+
- **web_url** (String) The web URL of the issue.
84+
- **weight** (Number) The weight of the issue. Valid values are greater than or equal to 0.
85+
86+
<a id="nestedatt--task_completion_status"></a>
87+
### Nested Schema for `task_completion_status`
88+
89+
Read-Only:
90+
91+
- **completed_count** (Number)
92+
- **count** (Number)
93+
94+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
data "gitlab_project" "foo" {
2+
id = "foo/bar/baz"
3+
}
4+
5+
data "gitlab_project_issue" "welcome_issue" {
6+
project = data.gitlab_project.foo.id
7+
iid = 1
8+
}
9+
10+
output "welcome_issue_web_url" {
11+
value = data.gitlab_project_issue.web_url
12+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
var _ = registerDataSource("gitlab_project_issue", func() *schema.Resource {
12+
return &schema.Resource{
13+
Description: `This data source allows you to access GitLab Project Issues.
14+
15+
**Upstream API:** [GitLab API docs](https://docs.gitlab.com/ee/api/issues.html)`,
16+
17+
ReadContext: dataSourceGitlabProjectIssueRead,
18+
Schema: datasourceSchemaFromResourceSchema(gitlabProjectIssueGetSchema(), "project", "iid"),
19+
}
20+
})
21+
22+
func dataSourceGitlabProjectIssueRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
23+
client := meta.(*gitlab.Client)
24+
project := d.Get("project").(string)
25+
issueIID := d.Get("iid").(int)
26+
27+
issue, _, err := client.Issues.GetIssue(project, issueIID, gitlab.WithContext(ctx))
28+
if err != nil {
29+
return diag.FromErr(err)
30+
}
31+
d.SetId(resourceGitLabProjectIssueBuildId(project, issueIID))
32+
stateMap := gitlabProjectIssueToStateMap(project, issue)
33+
34+
if err := setStateMapInResourceData(stateMap, d); err != nil {
35+
return diag.FromErr(err)
36+
}
37+
return nil
38+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
)
10+
11+
func TestAccDataSourceGitlabProjectIssue_basic(t *testing.T) {
12+
testAccCheck(t)
13+
14+
testProject := testAccCreateProject(t)
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: providerFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataGitlabProjectIssueConfig(testProject.ID),
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccDataSourceGitlabProjectIssue("gitlab_project_issue.this", "data.gitlab_project_issue.this"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccDataSourceGitlabProjectIssue(src, n string) resource.TestCheckFunc {
31+
return func(s *terraform.State) error {
32+
33+
resource := s.RootModule().Resources[src]
34+
resourceAttributes := resource.Primary.Attributes
35+
36+
datasource := s.RootModule().Resources[n]
37+
datasourceAttributes := datasource.Primary.Attributes
38+
39+
testAttributes := attributeNamesFromSchema(gitlabProjectIssueGetSchema())
40+
41+
for _, attribute := range testAttributes {
42+
if datasourceAttributes[attribute] != resourceAttributes[attribute] {
43+
return fmt.Errorf("Expected issue's attribute `%s` to be: %s, but got: `%s`", attribute, resourceAttributes[attribute], datasourceAttributes[attribute])
44+
}
45+
}
46+
47+
return nil
48+
}
49+
}
50+
51+
func testAccDataGitlabProjectIssueConfig(projectID int) string {
52+
return fmt.Sprintf(`
53+
resource "gitlab_project_issue" "this" {
54+
project = %d
55+
title = "Terraform acceptance tests"
56+
description = "Some description"
57+
due_date = "1994-02-21"
58+
}
59+
60+
data "gitlab_project_issue" "this" {
61+
project = %d
62+
iid = gitlab_project_issue.this.iid
63+
}
64+
`, projectID, projectID)
65+
}

0 commit comments

Comments
 (0)