Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project template ID #377

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.formatTool": "gofmt"
}
44 changes: 44 additions & 0 deletions gitlab/resource_gitlab_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"template_name": {
Type: schema.TypeString,
Optional: true,
},
"template_project_id": {
Type: schema.TypeInt,
Optional: true,
},
"use_custom_template": {
Type: schema.TypeBool,
Optional: true,
},
"group_with_project_templates_id": {
Type: schema.TypeInt,
Optional: true,
},
}

func resourceGitlabProject() *schema.Resource {
Expand Down Expand Up @@ -259,6 +275,10 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
OnlyAllowMergeIfAllDiscussionsAreResolved: gitlab.Bool(d.Get("only_allow_merge_if_all_discussions_are_resolved").(bool)),
SharedRunnersEnabled: gitlab.Bool(d.Get("shared_runners_enabled").(bool)),
RemoveSourceBranchAfterMerge: gitlab.Bool(d.Get("remove_source_branch_after_merge").(bool)),
TemplateName: gitlab.String(d.Get("template_name").(string)),
TemplateProjectID: gitlab.Int(d.Get("template_project_id").(int)),
UseCustomTemplate: gitlab.Bool(d.Get("use_custom_template").(bool)),
GroupWithProjectTemplatesID: gitlab.Int(d.Get("group_with_project_templates_id").(int)),
}

// need to manage partial state since project creation may require
Expand All @@ -282,6 +302,10 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
"only_allow_merge_if_all_discussions_are_resolved",
"shared_runners_enabled",
"remove_source_branch_after_merge",
"template_name",
"template_project_id",
"use_custom_template",
"group_with_project_templates_id",
}

if v, ok := d.GetOk("path"); ok {
Expand Down Expand Up @@ -309,6 +333,26 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
setProperties = append(setProperties, "initialize_with_readme")
}

if v, ok := d.GetOk("template_name"); ok {
options.TemplateName = gitlab.String(v.(string))
setProperties = append(setProperties, "template_name")
}

if v, ok := d.GetOk("template_project_id"); ok {
options.TemplateProjectID = gitlab.Int(v.(int))
setProperties = append(setProperties, "template_project_id")
}

if v, ok := d.GetOk("use_custom_template"); ok {
options.UseCustomTemplate = gitlab.Bool(v.(bool))
setProperties = append(setProperties, "use_custom_template")
}

if v, ok := d.GetOk("group_with_project_templates_id"); ok {
options.GroupWithProjectTemplatesID = gitlab.Int(v.(int))
setProperties = append(setProperties, "group_with_project_templates_id")
}

log.Printf("[DEBUG] create gitlab project %q", *options.Name)

project, _, err := client.Projects.CreateProject(options)
Expand Down
56 changes: 37 additions & 19 deletions gitlab/resource_gitlab_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,29 @@ func TestAccGitlabProject_initializeWithReadme(t *testing.T) {
Config: testAccGitlabProjectConfigInitializeWithReadme(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
testAccCheckGitlabProjectInitializeWithReadme(&project, &testAccGitlabProjectExpectedAttributes{
testAccCheckGitlabProjectDefaultBranch(&project, &testAccGitlabProjectExpectedAttributes{
DefaultBranch: "master",
}),
),
},
},
})
}

func TestAccGitlabProject_templateName(t *testing.T) {
var project gitlab.Project
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccGitlabProjectConfigTemplateName(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
testAccCheckGitlabProjectDefaultBranch(&project, &testAccGitlabProjectExpectedAttributes{
DefaultBranch: "master",
}),
),
Expand Down Expand Up @@ -431,10 +453,10 @@ func testAccCheckAggregateGitlabProject(expected, received *gitlab.Project) reso
return resource.ComposeAggregateTestCheckFunc(checks...)
}

func testAccCheckGitlabProjectInitializeWithReadme(project *gitlab.Project, want *testAccGitlabProjectExpectedAttributes) resource.TestCheckFunc {
func testAccCheckGitlabProjectDefaultBranch(project *gitlab.Project, want *testAccGitlabProjectExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {
if project.DefaultBranch != want.DefaultBranch {
return fmt.Errorf("got description %q; want %q", project.DefaultBranch, want.DefaultBranch)
return fmt.Errorf("got default branch %q; want %q", project.DefaultBranch, want.DefaultBranch)
}

return nil
Expand Down Expand Up @@ -470,12 +492,10 @@ resource "gitlab_group" "foo" {
path = "foogroup-%d"
visibility_level = "public"
}

resource "gitlab_project" "foo" {
name = "foo-%d"
description = "Terraform acceptance tests"
namespace_id = "${gitlab_group.foo.id}"

# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
Expand All @@ -490,18 +510,15 @@ resource "gitlab_group" "foo" {
path = "foogroup-%d"
visibility_level = "public"
}

resource "gitlab_group" "foo2" {
name = "foo2group-%d"
path = "foo2group-%d"
visibility_level = "public"
}

resource "gitlab_project" "foo" {
name = "foo-%d"
description = "Terraform acceptance tests"
namespace_id = "${gitlab_group.foo2.id}"

# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
Expand All @@ -521,13 +538,10 @@ resource "gitlab_project" "foo" {
name = "foo-%d"
path = "foo.%d"
description = "Terraform acceptance tests"

%s

tags = [
"tag1",
]

# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
Expand Down Expand Up @@ -566,19 +580,16 @@ resource "gitlab_project" "foo" {
name = "foo-%d"
path = "foo.%d"
description = "Terraform acceptance tests!"

tags = [
"tag1",
"tag2",
]

# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
merge_method = "ff"
only_allow_merge_if_pipeline_succeeds = true
only_allow_merge_if_all_discussions_are_resolved = true

request_access_enabled = false
issues_enabled = false
merge_requests_enabled = false
Expand All @@ -604,13 +615,11 @@ resource "gitlab_project" "foo" {
merge_method = "ff"
only_allow_merge_if_pipeline_succeeds = false
only_allow_merge_if_all_discussions_are_resolved = false

shared_with_groups {
group_id = "${gitlab_group.foo.id}"
group_access_level = "developer"
}
}

resource "gitlab_group" "foo" {
name = "foo-name-%d"
path = "foo-path-%d"
Expand All @@ -630,7 +639,6 @@ resource "gitlab_project" "foo" {
merge_method = "ff"
only_allow_merge_if_pipeline_succeeds = false
only_allow_merge_if_all_discussions_are_resolved = false

shared_with_groups {
group_id = "${gitlab_group.foo.id}"
group_access_level = "guest"
Expand All @@ -640,14 +648,12 @@ resource "gitlab_project" "foo" {
group_access_level = "developer"
}
}

resource "gitlab_group" "foo" {
name = "foo-name-%d"
path = "foo-path-%d"
description = "Terraform acceptance tests!"
visibility_level = "public"
}

resource "gitlab_group" "foo2" {
name = "foo2-name-%d"
path = "foo2-path-%d"
Expand All @@ -667,3 +673,15 @@ resource "gitlab_project" "foo" {
}
`, rInt, rInt)
}

func testAccGitlabProjectConfigTemplateName(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "foo" {
name = "foo-%d"
path = "foo.%d"
description = "Terraform acceptance tests"
template_name = "spring"
default_branch = "master"
}
`, rInt, rInt)
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ github.com/posener/complete v1.2.1/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DK
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sfang97/go-gitlab v0.33.1-0.20200806213917-abe9c5791dd4 h1:2nl6802voI5d3rNDr19rbXTc5U3PcTBxr2cDCsayuDA=
github.com/sfang97/go-gitlab v0.33.1-0.20200806213917-abe9c5791dd4/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/xanzy/go-gitlab/projects.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,4 @@ google.golang.org/grpc/stats
google.golang.org/grpc/status
google.golang.org/grpc/tap
google.golang.org/grpc/test/bufconn
# github.com/xanzy/go-gitlab v0.32.1 => github.com/sfang97/go-gitlab v0.33.1-0.20200806213917-abe9c5791dd4
Loading