Skip to content

Commit c5a2f34

Browse files
authored
Merge pull request #796 from timofurrer/feature/issue-mr-templates
Support `issues_template` and `merge_requests_template` in project resource
2 parents a915ccb + be9fab0 commit c5a2f34

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

docs/resources/project.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ consult the [gitlab documentation](https://docs.gitlab.com/ee/user/project/repos
109109
Valid values are `disabled`, `private`, `enabled`, `public`.
110110
`private` is the default.
111111

112+
* `issues_template` - (Optional) Sets the template for new issues in the project.
113+
114+
* `merge_requests_template` - (Optional) Sets the template for new merge requests in the project.
115+
112116
* `build_coverage_regex` - (Optional) Test coverage parsing for the project.
113117

114118
* `ci_config_path` - (Optional) Custom Path to CI config file.

gitlab/resource_gitlab_project.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
290290
Type: schema.TypeString,
291291
Optional: true,
292292
},
293+
"issues_template": {
294+
Type: schema.TypeString,
295+
Optional: true,
296+
},
297+
"merge_requests_template": {
298+
Type: schema.TypeString,
299+
Optional: true,
300+
},
293301
"ci_config_path": {
294302
Type: schema.TypeString,
295303
Optional: true,
@@ -349,6 +357,8 @@ func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Pro
349357
d.Set("mirror_overwrites_diverged_branches", project.MirrorOverwritesDivergedBranches)
350358
d.Set("only_mirror_protected_branches", project.OnlyMirrorProtectedBranches)
351359
d.Set("build_coverage_regex", project.BuildCoverageRegex)
360+
d.Set("issues_template", project.IssuesTemplate)
361+
d.Set("merge_requests_template", project.MergeRequestsTemplate)
352362
d.Set("ci_config_path", project.CIConfigPath)
353363
return nil
354364
}
@@ -551,6 +561,14 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
551561
editProjectOptions.ImportURL = gitlab.String(d.Get("import_url").(string))
552562
}
553563

564+
if v, ok := d.GetOk("issues_template"); ok {
565+
editProjectOptions.IssuesTemplate = gitlab.String(v.(string))
566+
}
567+
568+
if v, ok := d.GetOk("merge_requests_template"); ok {
569+
editProjectOptions.MergeRequestsTemplate = gitlab.String(v.(string))
570+
}
571+
554572
if (editProjectOptions != gitlab.EditProjectOptions{}) {
555573
if _, _, err := client.Projects.EditProject(d.Id(), &editProjectOptions, gitlab.WithContext(ctx)); err != nil {
556574
return diag.Errorf("Could not update project %q: %s", d.Id(), err)
@@ -724,6 +742,14 @@ func resourceGitlabProjectUpdate(ctx context.Context, d *schema.ResourceData, me
724742
options.BuildCoverageRegex = gitlab.String(d.Get("build_coverage_regex").(string))
725743
}
726744

745+
if d.HasChange("issues_template") {
746+
options.IssuesTemplate = gitlab.String(d.Get("issues_template").(string))
747+
}
748+
749+
if d.HasChange("merge_requests_template") {
750+
options.MergeRequestsTemplate = gitlab.String(d.Get("merge_requests_template").(string))
751+
}
752+
727753
if d.HasChange("ci_config_path") {
728754
options.CIConfigPath = gitlab.String(d.Get("ci_config_path").(string))
729755
}

gitlab/resource_gitlab_project_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func TestAccGitlabProject_basic(t *testing.T) {
5050
PackagesEnabled: true,
5151
PagesAccessLevel: gitlab.PublicAccessControl,
5252
BuildCoverageRegex: "foo",
53+
IssuesTemplate: "",
54+
MergeRequestsTemplate: "",
5355
CIConfigPath: ".gitlab-ci.yml@mynamespace/myproject",
5456
}
5557

@@ -381,6 +383,37 @@ func TestAccGitlabProject_initializeWithoutReadme(t *testing.T) {
381383
})
382384
}
383385

386+
func TestAccGitlabProject_IssueMergeRequestTemplates(t *testing.T) {
387+
var project gitlab.Project
388+
rInt := acctest.RandInt()
389+
390+
resource.Test(t, resource.TestCase{
391+
PreCheck: func() { testAccPreCheck(t) },
392+
Providers: testAccProviders,
393+
CheckDestroy: testAccCheckGitlabProjectDestroy,
394+
Steps: []resource.TestStep{
395+
{
396+
SkipFunc: isRunningInCE,
397+
Config: testAccGitlabProjectConfigIssueMergeRequestTemplates(rInt),
398+
Check: resource.ComposeTestCheckFunc(
399+
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
400+
func(s *terraform.State) error {
401+
if project.IssuesTemplate != "foo" {
402+
return fmt.Errorf("expected issues template to be 'foo'; got '%s'", project.IssuesTemplate)
403+
}
404+
405+
if project.MergeRequestsTemplate != "bar" {
406+
return fmt.Errorf("expected merge requests template to be 'bar'; got '%s'", project.MergeRequestsTemplate)
407+
}
408+
409+
return nil
410+
},
411+
),
412+
},
413+
},
414+
})
415+
}
416+
384417
func TestAccGitlabProject_willError(t *testing.T) {
385418
var received, defaults gitlab.Project
386419
rInt := acctest.RandInt()
@@ -1298,3 +1331,15 @@ resource "gitlab_project" "template-mutual-exclusive" {
12981331
}
12991332
`, rInt, rInt)
13001333
}
1334+
1335+
func testAccGitlabProjectConfigIssueMergeRequestTemplates(rInt int) string {
1336+
return fmt.Sprintf(`
1337+
resource "gitlab_project" "foo" {
1338+
name = "foo-%d"
1339+
path = "foo.%d"
1340+
description = "Terraform acceptance tests"
1341+
issues_template = "foo"
1342+
merge_requests_template = "bar"
1343+
}
1344+
`, rInt, rInt)
1345+
}

0 commit comments

Comments
 (0)