Skip to content

Commit

Permalink
fix: [PIPE-19752]: Adding the branch to git context (#993)
Browse files Browse the repository at this point in the history
* fix: [PIPE-19752]: Adding the branch to git context

* fix: [PIPE-19752]: Adding the branch to git context

* fix: [PIPE-19752]: Adding the branch to git context

* fix: [PIPE-19752]: Adding the branch to git context

* fix: [PIPE-19752]: Adding the branch to git context
  • Loading branch information
shivamnegi94 committed Jul 4, 2024
1 parent 9b91387 commit 39f3d68
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/999.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
harness_platform_pipeline: Added the branch context when imported the pipeline from non main branch
```
5 changes: 4 additions & 1 deletion examples/resources/harness_platform_pipeline/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Import pipeline
# Import pipeline from default branch
terraform import harness_platform_pipeline.example <org_id>/<project_id>/<pipeline_id>

# Import pipeline from non default branch
terraform import harness_platform_pipeline.example <org_id>/<project_id>/<pipeline_id>/<branch>
5 changes: 5 additions & 0 deletions helpers/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ var UserResourceImporter = &schema.ResourceImporter{

// ProjectResourceImporter defines the importer configuration for all project level resources.
// The id used for the import should be in the format <org_id>/<project_id>/<identifier>
// The id used for the import should be in the format <org_id>/<project_id>/<identifier>/<branch>
var ProjectResourceImporter = &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
Expand All @@ -367,6 +368,10 @@ var ProjectResourceImporter = &schema.ResourceImporter{
d.Set("identifier", parts[2])
d.SetId(parts[2])

if len(parts) == 4 {
d.Set("git_details", []interface{}{map[string]interface{}{"branch_name": parts[3]}})
}

return []*schema.ResourceData{d}, nil
},
}
Expand Down
11 changes: 11 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ func ProjectResourceImportStateIdFunc(resourceName string) resource.ImportStateI
}
}

func ProjectResourceImportStateIdGitFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
primary := s.RootModule().Resources[resourceName].Primary
id := primary.ID
orgId := primary.Attributes["org_id"]
projId := primary.Attributes["project_id"]
branch_name := primary.Attributes["git_details.0.branch_name"]
return fmt.Sprintf("%s/%s/%s/%s", orgId, projId, id, branch_name), nil
}
}

func UserResourceImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
primary := s.RootModule().Resources[resourceName].Primary
Expand Down
2 changes: 1 addition & 1 deletion internal/service/platform/pipeline/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ func resourcePipelineCreateOrUpdate(ctx context.Context, d *schema.ResourceData,
if id == "" {
if d.Get("import_from_git").(bool) {
pipeline_id = d.Get("identifier").(string)

pipeline_import_request_body := createImportFromGitRequest(d)
branch_name = pipeline_import_request_body.GitImportInfo.BranchName

_, httpResp, err = c.PipelinesApi.ImportPipelineFromGit(ctx, org_id, project_id, pipeline_id,
&nextgen.PipelinesApiImportPipelineFromGitOpts{
Expand Down
7 changes: 4 additions & 3 deletions internal/service/platform/pipeline/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccResourcePipeline(t *testing.T) {

resourceName := "harness_platform_pipeline.test"

resource.UnitTest(t, resource.TestCase{
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccPipelineDestroy(resourceName),
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestAccResourcePipelineImportFromGit(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: acctest.ProjectResourceImportStateIdFunc(resourceName),
ImportStateIdFunc: acctest.ProjectResourceImportStateIdGitFunc(resourceName),
ImportStateVerifyIgnore: []string{"git_import_info.0.branch_name", "git_import_info.0.connector_ref", "git_import_info.0.file_path", "git_import_info.0.repo_name", "import_from_git", "pipeline_import_request.0.pipeline_description", "pipeline_import_request.0.pipeline_name", "git_import_info.#", "git_import_info.0.%", "pipeline_import_request.#", "pipeline_import_request.0.%"},
},
},
Expand Down Expand Up @@ -155,8 +155,9 @@ func testAccGetPipeline(resourceName string, state *terraform.State) (*openapi_c
id := r.Primary.ID
orgId := r.Primary.Attributes["org_id"]
projId := r.Primary.Attributes["project_id"]
branch_name := r.Primary.Attributes["git_details.0.branch_name"]

resp, _, err := c.PipelinesApi.GetPipeline(ctx, orgId, projId, id, &openapi_client_nextgen.PipelinesApiGetPipelineOpts{HarnessAccount: optional.NewString(c.AccountId)})
resp, _, err := c.PipelinesApi.GetPipeline(ctx, orgId, projId, id, &openapi_client_nextgen.PipelinesApiGetPipelineOpts{HarnessAccount: optional.NewString(c.AccountId), BranchName: optional.NewString(branch_name)})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 39f3d68

Please sign in to comment.