Skip to content

Commit 17e2435

Browse files
EliiseSNicholas M. Iodice
andauthored
Fix issue with default branch being empty on forked repos (#270)
* Add acceptance test for forked repo * Add branch waiting * Lower waiting time Co-authored-by: Nicholas M. Iodice <niiodice@microsoft.com>
1 parent 2f188f8 commit 17e2435

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

azuredevops/resource_git_repository.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"fmt"
55
"log"
66
"strings"
7+
"time"
78

89
"github.com/google/uuid"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
911
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1012
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
1113
"github.com/microsoft/azure-devops-go-api/azuredevops/core"
@@ -144,11 +146,45 @@ func resourceGitRepositoryCreate(d *schema.ResourceData, m interface{}) error {
144146
return fmt.Errorf("Error initializing repository in Azure DevOps: %+v", err)
145147
}
146148
}
149+
if !(strings.EqualFold(initialization.initType, "uninitialized") && parentRepoRef == nil) {
150+
err := waitForBranch(clients, repo.Name, projectID)
151+
if err != nil {
152+
return err
153+
}
154+
}
147155

148156
d.SetId(createdRepo.Id.String())
149157
return resourceGitRepositoryRead(d, m)
150158
}
151159

160+
func waitForBranch(clients *config.AggregatedClient, repoName *string, projectID fmt.Stringer) error {
161+
stateConf := &resource.StateChangeConf{
162+
Pending: []string{"Waiting"},
163+
Target: []string{"Synched"},
164+
Refresh: func() (interface{}, string, error) {
165+
state := "Waiting"
166+
gitRepo, err := gitRepositoryRead(clients, "", *repoName, projectID.String())
167+
if err != nil {
168+
return nil, "", fmt.Errorf("Error reading repository: %+v", err)
169+
}
170+
171+
if converter.ToString(gitRepo.DefaultBranch, "") != "" {
172+
state = "Synched"
173+
}
174+
175+
return state, state, nil
176+
},
177+
Timeout: 60 * time.Second,
178+
MinTimeout: 2 * time.Second,
179+
Delay: 1 * time.Second,
180+
ContinuousTargetOccurence: 1,
181+
}
182+
if _, err := stateConf.WaitForState(); err != nil {
183+
return fmt.Errorf("Error retrieving expected branch for repository [%s]: %+v", *repoName, err)
184+
}
185+
return nil
186+
}
187+
152188
func createGitRepository(clients *config.AggregatedClient, repoName *string, projectID *uuid.UUID, parentRepo *git.GitRepositoryRef) (*git.GitRepository, error) {
153189
args := git.CreateRepositoryArgs{
154190
GitRepositoryToCreate: &git.GitRepositoryCreateOptions{

azuredevops/resource_git_repository_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,34 @@ func TestAccAzureGitRepo_RepoInitialization_Uninitialized(t *testing.T) {
420420
})
421421
}
422422

423+
// Verifies that a newly forked repo does NOT return an empty branch_name
424+
func TestAccAzureGitRepo_RepoFork_BranchNotEmpty(t *testing.T) {
425+
projectName := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
426+
gitRepoName := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
427+
gitForkedRepoName := testhelper.TestAccResourcePrefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
428+
tfRepoNode := "azuredevops_git_repository.gitrepo"
429+
tfForkedRepoNode := "azuredevops_git_repository.gitforkedrepo"
430+
431+
resource.Test(t, resource.TestCase{
432+
PreCheck: func() { testhelper.TestAccPreCheck(t, nil) },
433+
Providers: testAccProviders,
434+
CheckDestroy: testAccAzureGitRepoCheckDestroy,
435+
Steps: []resource.TestStep{
436+
{
437+
Config: testhelper.TestAccAzureForkedGitRepoResource(projectName, gitRepoName, gitForkedRepoName, "Clean", "Uninitialized"),
438+
Check: resource.ComposeTestCheckFunc(
439+
testAccCheckAzureGitRepoResourceExists(gitRepoName),
440+
resource.TestCheckResourceAttrSet(tfRepoNode, "project_id"),
441+
resource.TestCheckResourceAttr(tfRepoNode, "name", gitRepoName),
442+
resource.TestCheckResourceAttr(tfRepoNode, "default_branch", "refs/heads/master"),
443+
resource.TestCheckResourceAttr(tfForkedRepoNode, "name", gitForkedRepoName),
444+
resource.TestCheckResourceAttr(tfForkedRepoNode, "default_branch", "refs/heads/master"),
445+
),
446+
},
447+
},
448+
})
449+
}
450+
423451
func init() {
424452
InitProvider()
425453
}

azuredevops/utils/testhelper/hcl.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ resource "azuredevops_git_repository" "gitrepo" {
2020
return fmt.Sprintf("%s\n%s", projectResource, azureGitRepoResource)
2121
}
2222

23+
// TestAccAzureForkedGitRepoResource HCL describing an AzDO GIT repository resource
24+
func TestAccAzureForkedGitRepoResource(projectName string, gitRepoName string, gitForkedRepoName string, initType string, forkedInitType string) string {
25+
azureGitRepoResource := fmt.Sprintf(`
26+
resource "azuredevops_git_repository" "gitforkedrepo" {
27+
project_id = azuredevops_project.project.id
28+
parent_repository_id = azuredevops_git_repository.gitrepo.id
29+
name = "%s"
30+
initialization {
31+
init_type = "%s"
32+
}
33+
}`, gitForkedRepoName, forkedInitType)
34+
35+
gitRepoResource := TestAccAzureGitRepoResource(projectName, gitRepoName, initType)
36+
return fmt.Sprintf("%s\n%s", gitRepoResource, azureGitRepoResource)
37+
}
38+
2339
// TestAccGroupDataSource HCL describing an AzDO Group Data Source
2440
func TestAccGroupDataSource(projectName string, groupName string) string {
2541
dataSource := fmt.Sprintf(`

0 commit comments

Comments
 (0)