summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Vink <mike1994vink@gmail.com>2023-02-23 22:07:12 +0100
committerMike Vink <mike1994vink@gmail.com>2023-02-28 10:14:28 +0100
commit27d80129ec8d959ec375873090bb2eec825ec671 (patch)
tree85c76a657b74d0305c9c1ae3e699228677d55738
parent65973b80cf0b8b073449ae7b8ac6b07c614e8f5c (diff)
fix: Incorporate comments
1. Change field source_ref to ref, tag, and commit_id. 2. Remove creation of branch without existing history 3. Remove unnecessary branch exists in acceptance test 4. Replace custom validation with suggested TestCheckResourceAttr 5. Remove Sensitive: false mistake 6. Remove unnecessary call to read in import 7. Remove is_default_branch from computed fields, since the api https://learn.microsoft.com/en-us/rest/api/azure/devops/git/stats/get?view=azure-devops-rest-7.1&tabs=HTTP returns true when non-default branch is on same head commit.
-rw-r--r--azuredevops/internal/acceptancetests/resource_git_repository_branch_test.go191
-rw-r--r--azuredevops/internal/service/git/resource_git_repository_branch.go130
-rw-r--r--azuredevops/internal/service/git/resource_git_repository_branch_test.go82
3 files changed, 122 insertions, 281 deletions
diff --git a/azuredevops/internal/acceptancetests/resource_git_repository_branch_test.go b/azuredevops/internal/acceptancetests/resource_git_repository_branch_test.go
index 4ca47695..2e34eca0 100644
--- a/azuredevops/internal/acceptancetests/resource_git_repository_branch_test.go
+++ b/azuredevops/internal/acceptancetests/resource_git_repository_branch_test.go
@@ -10,19 +10,12 @@ import (
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
- "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
- "github.com/microsoft/azure-devops-go-api/azuredevops/v6/git"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/acceptancetests/testutils"
- "github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/client"
- "github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils/tfhelper"
)
// TestAccGitRepoBranch_CreateUpdateDelete verifies that a branch can
// be added to a repository and that it can be replaced
func TestAccGitRepoBranch_CreateAndUpdate(t *testing.T) {
- var gotBranch git.GitBranchStats
- var gotBranch2 git.GitBranchStats
- var gotBranch3 git.GitBranchStats
projectName := testutils.GenerateResourceName()
gitRepoName := testutils.GenerateResourceName()
branchName := testutils.GenerateResourceName()
@@ -33,77 +26,41 @@ func TestAccGitRepoBranch_CreateAndUpdate(t *testing.T) {
Providers: testutils.GetProviders(),
Steps: []resource.TestStep{
{
- Config: hclGitRepoBranches(projectName, gitRepoName, "Uninitialized", branchName),
+ Config: hclGitRepoBranches(projectName, gitRepoName, "Clean", branchName),
Check: resource.ComposeTestCheckFunc(
- testAccGitRepoBranchExists("foo_orphan", &gotBranch),
- testAccGitRepoBranchExists("foo_from_ref", &gotBranch2),
- testAccGitRepoBranchExists("foo_from_sha", &gotBranch3),
- testAccGitRepoBranchAttributes("foo_orphan", &gotBranch, &testAccGitRepoBranchExpectedAttributes{
- Name: fmt.Sprintf("testbranch-%s", branchName),
- }, &testAccGitRepoBranchExpectedStateAttrs{
- source_ref: "",
- source_sha: false,
- is_default_branch: true,
- ref: fmt.Sprintf("refs/heads/testbranch-%s", branchName),
- sha: true,
- }),
- testAccGitRepoBranchAttributes("foo_from_ref", &gotBranch2, &testAccGitRepoBranchExpectedAttributes{
- Name: fmt.Sprintf("testbranch2-%s", branchName),
- }, &testAccGitRepoBranchExpectedStateAttrs{
- source_ref: fmt.Sprintf("refs/heads/testbranch-%s", branchName),
- source_sha: true,
- ref: fmt.Sprintf("refs/heads/testbranch2-%s", branchName),
- sha: true,
- }),
- testAccGitRepoBranchAttributes("foo_from_sha", &gotBranch3, &testAccGitRepoBranchExpectedAttributes{
- Name: fmt.Sprintf("testbranch3-%s", branchName),
- }, &testAccGitRepoBranchExpectedStateAttrs{
- source_ref: "",
- source_sha: true,
- ref: fmt.Sprintf("refs/heads/testbranch3-%s", branchName),
- sha: true,
- }),
+ // test-branch
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_master", "name", fmt.Sprintf("testbranch-%s", branchName)),
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_master", "ref", "master"),
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_master", "branch_reference", fmt.Sprintf("refs/heads/testbranch-%s", branchName)),
+ resource.TestCheckResourceAttrSet("azuredevops_git_repository_branch.from_master", "branch_head"),
+ // test-branch2
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_commit_id", "name", fmt.Sprintf("testbranch2-%s", branchName)),
+ resource.TestCheckResourceAttrSet("azuredevops_git_repository_branch.from_commit_id", "commit_id"),
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_commit_id", "branch_reference", fmt.Sprintf("refs/heads/testbranch2-%s", branchName)),
+ resource.TestCheckResourceAttrSet("azuredevops_git_repository_branch.from_commit_id", "branch_head"),
),
},
- // Test import branch created from ref
+ // Test import branch created from ref, ignore fields set only on create
{
- ResourceName: "azuredevops_git_repository_branch.foo_from_ref",
+ ResourceName: "azuredevops_git_repository_branch.from_master",
ImportState: true,
ImportStateVerify: true,
- ImportStateVerifyIgnore: []string{"source_ref", "source_sha"},
+ ImportStateVerifyIgnore: []string{"ref", "tag", "commit_id"},
},
// Test replace/update branch when name changes
{
- Config: hclGitRepoBranches(projectName, gitRepoName, "Uninitialized", branchNameChanged),
+ Config: hclGitRepoBranches(projectName, gitRepoName, "Clean", branchNameChanged),
Check: resource.ComposeTestCheckFunc(
- testAccGitRepoBranchExists("foo_orphan", &gotBranch),
- testAccGitRepoBranchExists("foo_from_ref", &gotBranch2),
- testAccGitRepoBranchExists("foo_from_sha", &gotBranch3),
- testAccGitRepoBranchAttributes("foo_orphan", &gotBranch, &testAccGitRepoBranchExpectedAttributes{
- Name: fmt.Sprintf("testbranch-%s", branchNameChanged),
- }, &testAccGitRepoBranchExpectedStateAttrs{
- source_ref: "",
- source_sha: false,
- is_default_branch: true,
- ref: fmt.Sprintf("refs/heads/testbranch-%s", branchNameChanged),
- sha: true,
- }),
- testAccGitRepoBranchAttributes("foo_from_ref", &gotBranch2, &testAccGitRepoBranchExpectedAttributes{
- Name: fmt.Sprintf("testbranch2-%s", branchNameChanged),
- }, &testAccGitRepoBranchExpectedStateAttrs{
- source_ref: fmt.Sprintf("refs/heads/testbranch-%s", branchNameChanged),
- source_sha: true,
- ref: fmt.Sprintf("refs/heads/testbranch2-%s", branchNameChanged),
- sha: true,
- }),
- testAccGitRepoBranchAttributes("foo_from_sha", &gotBranch3, &testAccGitRepoBranchExpectedAttributes{
- Name: fmt.Sprintf("testbranch3-%s", branchNameChanged),
- }, &testAccGitRepoBranchExpectedStateAttrs{
- source_ref: "",
- source_sha: true,
- ref: fmt.Sprintf("refs/heads/testbranch3-%s", branchNameChanged),
- sha: true,
- }),
+ // test-branch
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_master", "name", fmt.Sprintf("testbranch-%s", branchNameChanged)),
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_master", "ref", "master"),
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_master", "branch_reference", fmt.Sprintf("refs/heads/testbranch-%s", branchNameChanged)),
+ resource.TestCheckResourceAttrSet("azuredevops_git_repository_branch.from_master", "branch_head"),
+ // test-branch2
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_commit_id", "name", fmt.Sprintf("testbranch2-%s", branchNameChanged)),
+ resource.TestCheckResourceAttrSet("azuredevops_git_repository_branch.from_commit_id", "commit_id"),
+ resource.TestCheckResourceAttr("azuredevops_git_repository_branch.from_commit_id", "branch_reference", fmt.Sprintf("refs/heads/testbranch2-%s", branchNameChanged)),
+ resource.TestCheckResourceAttrSet("azuredevops_git_repository_branch.from_commit_id", "branch_head"),
),
},
// Test invalid ref
@@ -111,115 +68,33 @@ func TestAccGitRepoBranch_CreateAndUpdate(t *testing.T) {
Config: fmt.Sprintf(`
%s
-resource "azuredevops_git_repository_branch" "foo_nonexistent_tag" {
+resource "azuredevops_git_repository_branch" "from_nonexistent_tag" {
repository_id = azuredevops_git_repository.repository.id
- name = "testbranch2-non-existent-tag"
- source_ref = "refs/tags/non-existent"
+ name = "testbranch-non-existent-tag"
+ tag = "0.0.0"
}
`, hclGitRepoBranches(projectName, gitRepoName, "Clean", branchNameChanged)),
- ExpectError: regexp.MustCompile(`No refs found that match source_ref "refs/tags/non-existent"`),
+ ExpectError: regexp.MustCompile(`No refs found that match ref "refs/tags/0.0.0"`),
},
},
},
)
}
-func testAccGitRepoBranchAttributes(node string, branch *git.GitBranchStats, want *testAccGitRepoBranchExpectedAttributes, wantState *testAccGitRepoBranchExpectedStateAttrs) resource.TestCheckFunc {
- return func(s *terraform.State) error {
- if *branch.Name != want.Name {
- return fmt.Errorf("Error got name %s, want %s", *branch.Name, want.Name)
- }
-
- rs, ok := s.RootModule().Resources[fmt.Sprintf("azuredevops_git_repository_branch.%s", node)]
- if !ok {
- return fmt.Errorf("Not found: %s", node)
- }
-
- sourceRef := rs.Primary.Attributes["source_ref"]
- if wantState.source_ref != sourceRef {
- return fmt.Errorf("azuredevops_git_repository_branch.%s.source_ref = %s, want %s", node, sourceRef, wantState.source_ref)
- }
-
- sourceSha := rs.Primary.Attributes["source_sha"]
- if wantState.source_sha && sourceSha == "" {
- return fmt.Errorf("azuredevops_git_repository_branch.%s.source_sha is not set", node)
- }
-
- isDefaultBranch := rs.Primary.Attributes["is_default_branch"]
- if wantState.is_default_branch && isDefaultBranch != "true" {
- return fmt.Errorf("azuredevops_git_repository_branch.%s.is_default_branch = %s, want %v", node, isDefaultBranch, wantState.is_default_branch)
- }
-
- ref := rs.Primary.Attributes["ref"]
- if wantState.ref != ref {
- return fmt.Errorf("azuredevops_git_repository_branch.%s.ref = %s, want %s", node, ref, wantState.ref)
- }
-
- sha := rs.Primary.Attributes["sha"]
- if wantState.sha && sha == "" {
- return fmt.Errorf("azuredevops_git_repository_branch.%s.ref = %s, want %s", node, ref, wantState.ref)
- }
-
- return nil
- }
-}
-
-func testAccGitRepoBranchExists(node string, gotBranch *git.GitBranchStats) resource.TestCheckFunc {
- return func(s *terraform.State) error {
- rs, ok := s.RootModule().Resources[fmt.Sprintf("azuredevops_git_repository_branch.%s", node)]
- if !ok {
- return fmt.Errorf("Not found: %s", node)
- }
-
- repoID, branchName, err := tfhelper.ParseGitRepoBranchID(rs.Primary.ID)
- if err != nil {
- return fmt.Errorf("Error in parsing branch ID: %w", err)
- }
-
- clients := testutils.GetProvider().Meta().(*client.AggregatedClient)
- branch, err := clients.GitReposClient.GetBranch(clients.Ctx, git.GetBranchArgs{
- RepositoryId: &repoID,
- Name: &branchName,
- })
- if err != nil {
- return err
- }
- *gotBranch = *branch
-
- return nil
- }
-}
-
func hclGitRepoBranches(projectName, gitRepoName, initType, branchName string) string {
gitRepoResource := testutils.HclGitRepoResource(projectName, gitRepoName, initType)
return fmt.Sprintf(`
%[1]s
-resource "azuredevops_git_repository_branch" "foo_orphan" {
+resource "azuredevops_git_repository_branch" "from_master" {
repository_id = azuredevops_git_repository.repository.id
name = "testbranch-%[2]s"
+ ref = "master"
}
-resource "azuredevops_git_repository_branch" "foo_from_ref" {
+resource "azuredevops_git_repository_branch" "from_commit_id" {
repository_id = azuredevops_git_repository.repository.id
name = "testbranch2-%[2]s"
- source_ref = azuredevops_git_repository_branch.foo_orphan.ref
-}
-resource "azuredevops_git_repository_branch" "foo_from_sha" {
- repository_id = azuredevops_git_repository.repository.id
- name = "testbranch3-%[2]s"
- source_sha = azuredevops_git_repository_branch.foo_orphan.sha
+ commit_id = azuredevops_git_repository_branch.from_master.branch_head
}
`, gitRepoResource, branchName)
}
-
-type testAccGitRepoBranchExpectedStateAttrs struct {
- source_ref string
- source_sha bool
- is_default_branch bool
- ref string
- sha bool
-}
-
-type testAccGitRepoBranchExpectedAttributes struct {
- Name string
-}
diff --git a/azuredevops/internal/service/git/resource_git_repository_branch.go b/azuredevops/internal/service/git/resource_git_repository_branch.go
index ef200e2f..362694d9 100644
--- a/azuredevops/internal/service/git/resource_git_repository_branch.go
+++ b/azuredevops/internal/service/git/resource_git_repository_branch.go
@@ -30,7 +30,6 @@ func ResourceGitRepositoryBranch() *schema.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
- Sensitive: false,
},
"repository_id": {
Type: schema.TypeString,
@@ -38,28 +37,30 @@ func ResourceGitRepositoryBranch() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.IsUUID,
},
- "source_ref": {
+ "ref": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
- "source_sha": {
+ "tag": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
- Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},
- "is_default_branch": {
- Type: schema.TypeBool,
- Computed: true,
+ "commit_id": {
+ Type: schema.TypeString,
+ Optional: true,
+ ForceNew: true,
+ Computed: true,
+ ValidateFunc: validation.StringIsNotEmpty,
},
- "ref": {
+ "branch_reference": {
Type: schema.TypeString,
Computed: true,
},
- "sha": {
+ "branch_head": {
Type: schema.TypeString,
Computed: true,
},
@@ -71,31 +72,28 @@ func resourceGitRepositoryBranchCreate(ctx context.Context, d *schema.ResourceDa
clients := m.(*client.AggregatedClient)
repoId := d.Get("repository_id").(string)
branchName := d.Get("name").(string)
- branchRef := withRefsHeadsPrefix(branchName)
- sourceRef, hasSourceRef := d.GetOk("source_ref")
- _, hasSourceSha := d.GetOk("source_sha")
+ branchRefHeadName := withPrefix("refs/heads/", branchName)
+ ref, hasRef := d.GetOk("ref")
+ tag, hasTag := d.GetOk("tag")
+ _, hasCommitId := d.GetOk("commit_id")
- // Initialise new orphan branch
- if !hasSourceRef && !hasSourceSha {
- args := branchCreatePushArgs(branchRef, repoId)
+ if !hasRef && !hasTag && !hasCommitId {
+ return diag.Errorf("One of 'ref' or 'tag' or 'commit_id' must be provided.")
+ }
- _, err := clients.GitReposClient.CreatePush(clients.Ctx, args)
- if err != nil {
- return diag.FromErr(fmt.Errorf("Error initialising new branch: %w", err))
+ // Get a commitId from a head or tag if it is not provided in the resource
+ if !hasCommitId {
+ var rs string
+ if hasRef {
+ rs = withPrefix("refs/heads/", ref.(string))
+ }
+ if hasTag {
+ rs = withPrefix("refs/tags/", tag.(string))
}
- d.SetId(fmt.Sprintf("%s:%s", repoId, branchName))
-
- return resourceGitRepositoryBranchRead(ctx, d, m)
- }
-
- // Get sha from source ref which can be a branch or a tag
- if !hasSourceSha {
// Azuredevops GetRefs api returns refs whose "prefix" matches Filter sorted from shortest to longest
// Top1 should return best match
- sourceRefName := sourceRef.(string)
- filter := strings.TrimPrefix(sourceRefName, "refs/")
-
+ filter := strings.TrimPrefix(rs, "refs/")
gotRefs, err := clients.GitReposClient.GetRefs(clients.Ctx, git.GetRefsArgs{
RepositoryId: converter.String(repoId),
Filter: converter.String(filter),
@@ -107,7 +105,7 @@ func resourceGitRepositoryBranchCreate(ctx context.Context, d *schema.ResourceDa
}
if len(gotRefs.Value) == 0 {
- return diag.FromErr(fmt.Errorf("No refs found that match source_ref %q.", sourceRefName))
+ return diag.FromErr(fmt.Errorf("No refs found that match ref %q.", rs))
}
gotRef := gotRefs.Value[0]
@@ -116,7 +114,7 @@ func resourceGitRepositoryBranchCreate(ctx context.Context, d *schema.ResourceDa
}
// Check for complete match. Sometimes refs exist that match prefix with Ref, but do not match completely.
- if *gotRef.Name != sourceRefName {
+ if *gotRef.Name != rs {
return diag.FromErr(fmt.Errorf("Ref %q not found, closest match is %q.", filter, *gotRef.Name))
}
@@ -129,13 +127,13 @@ func resourceGitRepositoryBranchCreate(ctx context.Context, d *schema.ResourceDa
} else {
return diag.FromErr(fmt.Errorf("GetRefs response doesn't have a valid commit id."))
}
- d.Set("source_sha", *refObjectIdSha)
+ d.Set("commit_id", *refObjectIdSha)
}
- newObjectId := d.Get("source_sha").(string)
+ newObjectId := d.Get("commit_id").(string)
_, err := updateRefs(clients, git.UpdateRefsArgs{
RefUpdates: &[]git.GitRefUpdate{{
- Name: &branchRef,
+ Name: &branchRefHeadName,
NewObjectId: &newObjectId,
OldObjectId: converter.String("0000000000000000000000000000000000000000"),
}},
@@ -158,7 +156,7 @@ func resourceGitRepositoryBranchRead(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}
- branchStats, err := clients.GitReposClient.GetBranch(clients.Ctx, git.GetBranchArgs{
+ gotBranch, err := clients.GitReposClient.GetBranch(clients.Ctx, git.GetBranchArgs{
RepositoryId: converter.String(repoId),
Name: converter.String(name),
})
@@ -173,9 +171,8 @@ func resourceGitRepositoryBranchRead(ctx context.Context, d *schema.ResourceData
d.SetId(fmt.Sprintf("%s:%s", repoId, name))
d.Set("name", name)
d.Set("repository_id", repoId)
- d.Set("is_default_branch", *branchStats.IsBaseVersion)
- d.Set("ref", converter.String(withRefsHeadsPrefix(name)))
- d.Set("sha", *branchStats.Commit.CommitId)
+ d.Set("branch_reference", converter.String(withPrefix("refs/heads/", name)))
+ d.Set("branch_head", *gotBranch.Commit.CommitId)
return nil
}
@@ -188,7 +185,7 @@ func resourceGitRepositoryBranchDelete(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(err)
}
- branchStats, err := clients.GitReposClient.GetBranch(clients.Ctx, git.GetBranchArgs{
+ gotBranch, err := clients.GitReposClient.GetBranch(clients.Ctx, git.GetBranchArgs{
RepositoryId: converter.String(repoId),
Name: converter.String(name),
})
@@ -198,8 +195,8 @@ func resourceGitRepositoryBranchDelete(ctx context.Context, d *schema.ResourceDa
_, err = updateRefs(clients, git.UpdateRefsArgs{
RefUpdates: &[]git.GitRefUpdate{{
- Name: converter.String(withRefsHeadsPrefix(name)),
- OldObjectId: branchStats.Commit.CommitId,
+ Name: converter.String(withPrefix("refs/heads/", name)),
+ OldObjectId: gotBranch.Commit.CommitId,
NewObjectId: converter.String("0000000000000000000000000000000000000000"),
}},
RepositoryId: converter.String(repoId),
@@ -212,55 +209,13 @@ func resourceGitRepositoryBranchDelete(ctx context.Context, d *schema.ResourceDa
}
func resourceGitRepositoryBranchImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
- _, branchName, err := tfhelper.ParseGitRepoBranchID(d.Id())
+ _, _, err := tfhelper.ParseGitRepoBranchID(d.Id())
if err != nil {
return nil, err
}
-
- diags := resourceGitRepositoryBranchRead(ctx, d, m)
- if diags.HasError() {
- return nil, fmt.Errorf(diags[0].Summary)
- }
-
- if d.Id() == "" {
- return nil, fmt.Errorf("Branch %q not found", branchName)
- }
-
return []*schema.ResourceData{d}, nil
}
-func branchCreatePushArgs(name, repoId string) git.CreatePushArgs {
- args := git.CreatePushArgs{
- RepositoryId: converter.String(repoId),
- Push: &git.GitPush{
- RefUpdates: &[]git.GitRefUpdate{
- {
- Name: converter.String(name),
- OldObjectId: converter.String("0000000000000000000000000000000000000000"),
- },
- },
- Commits: &[]git.GitCommitRef{
- {
- Comment: converter.String("Initial commit."),
- Changes: &[]interface{}{
- git.Change{
- ChangeType: &git.VersionControlChangeTypeValues.Add,
- Item: git.GitItem{
- Path: converter.String("/readme.md"),
- },
- NewContent: &git.ItemContent{
- ContentType: &git.ItemContentTypeValues.RawText,
- Content: converter.String("Branch initialized with azuredevops terraform provider"),
- },
- },
- },
- },
- },
- },
- }
- return args
-}
-
func updateRefs(clients *client.AggregatedClient, args git.UpdateRefsArgs) (*[]git.GitRefUpdateResult, error) {
updateRefResults, err := clients.GitReposClient.UpdateRefs(clients.Ctx, args)
if err != nil {
@@ -276,10 +231,9 @@ func updateRefs(clients *client.AggregatedClient, args git.UpdateRefsArgs) (*[]g
return updateRefResults, nil
}
-func withRefsHeadsPrefix(branchName string) string {
- prefix := "refs/heads/"
- if strings.HasPrefix(branchName, prefix) {
- return branchName
+func withPrefix(prefix, name string) string {
+ if strings.HasPrefix(name, prefix) {
+ return name
}
- return prefix + branchName
+ return prefix + name
}
diff --git a/azuredevops/internal/service/git/resource_git_repository_branch_test.go b/azuredevops/internal/service/git/resource_git_repository_branch_test.go
index 51a90441..7a16fd5e 100644
--- a/azuredevops/internal/service/git/resource_git_repository_branch_test.go
+++ b/azuredevops/internal/service/git/resource_git_repository_branch_test.go
@@ -28,19 +28,44 @@ func TestGitRepositoryBranch_Create(t *testing.T) {
want diag.Diagnostics
}{
{
- "When source_ref is not given, create push does not swallow error",
+ "When ref is given, refs update does not swallow error",
func(g *azdosdkmocks.MockGitClient) args {
clients := &client.AggregatedClient{
GitReposClient: g,
Ctx: context.Background(),
}
- expectedArgs := branchCreatePushArgs(withRefsHeadsPrefix("a-branch"), "a-repo")
d := schema.TestResourceDataRaw(t, ResourceGitRepositoryBranch().Schema, nil)
- d.Set("name", "a-branch")
- d.Set("repository_id", "a-repo")
+ ref := "refs/heads/another-branch"
+ fakeCommitId := "a-commit"
+ branchName := "a-branch"
+ repoId := "a-repo"
+ d.Set("ref", ref)
+ d.Set("name", branchName)
+ d.Set("repository_id", repoId)
+
+ g.EXPECT().
+ GetRefs(clients.Ctx, git.GetRefsArgs{
+ RepositoryId: &repoId,
+ Filter: converter.String(strings.TrimPrefix(ref, "refs/")),
+ Top: converter.Int(1),
+ PeelTags: converter.Bool(true),
+ }).
+ Return(&git.GetRefsResponseValue{
+ Value: []git.GitRef{{
+ Name: &ref,
+ ObjectId: &fakeCommitId,
+ }},
+ }, nil)
g.EXPECT().
- CreatePush(clients.Ctx, expectedArgs).
+ UpdateRefs(clients.Ctx, git.UpdateRefsArgs{
+ RefUpdates: &[]git.GitRefUpdate{{
+ Name: converter.String(withPrefix("refs/heads/", "a-branch")),
+ NewObjectId: &fakeCommitId,
+ OldObjectId: converter.String("0000000000000000000000000000000000000000"),
+ }},
+ RepositoryId: converter.String("a-repo"),
+ }).
Return(nil, fmt.Errorf("an-error"))
return args{
context.Background(),
@@ -48,43 +73,30 @@ func TestGitRepositoryBranch_Create(t *testing.T) {
clients,
}
},
- diag.FromErr(fmt.Errorf("Error initialising new branch: an-error")),
+ diag.FromErr(fmt.Errorf("Error creating branch \"a-branch\": an-error")),
},
{
- "When source_ref is given, refs update does not swallow error",
+ "When more than one of commitId, tag, or ref are given, the first one from left to right wins",
func(g *azdosdkmocks.MockGitClient) args {
clients := &client.AggregatedClient{
GitReposClient: g,
Ctx: context.Background(),
}
d := schema.TestResourceDataRaw(t, ResourceGitRepositoryBranch().Schema, nil)
- source_ref := "refs/heads/another-branch"
- commit := "a-commit"
+ tag := "refs/tag/v1.0.0"
+ fakeCommitId := "a-commit"
branchName := "a-branch"
repoId := "a-repo"
- d.Set("source_ref", source_ref)
+ d.Set("commit_id", fakeCommitId)
+ d.Set("tag", tag)
d.Set("name", branchName)
d.Set("repository_id", repoId)
g.EXPECT().
- GetRefs(clients.Ctx, git.GetRefsArgs{
- RepositoryId: &repoId,
- Filter: converter.String(strings.TrimPrefix(source_ref, "refs/")),
- Top: converter.Int(1),
- PeelTags: converter.Bool(true),
- }).
- Return(&git.GetRefsResponseValue{
- Value: []git.GitRef{{
- Name: &source_ref,
- ObjectId: &commit,
- }},
- }, nil)
-
- g.EXPECT().
UpdateRefs(clients.Ctx, git.UpdateRefsArgs{
RefUpdates: &[]git.GitRefUpdate{{
- Name: converter.String(withRefsHeadsPrefix("a-branch")),
- NewObjectId: &commit,
+ Name: converter.String(withPrefix("refs/heads/", "a-branch")),
+ NewObjectId: &fakeCommitId,
OldObjectId: converter.String("0000000000000000000000000000000000000000"),
}},
RepositoryId: converter.String("a-repo"),
@@ -106,33 +118,33 @@ func TestGitRepositoryBranch_Create(t *testing.T) {
Ctx: context.Background(),
}
d := schema.TestResourceDataRaw(t, ResourceGitRepositoryBranch().Schema, nil)
- source_ref := "refs/heads/another-branch"
- commit := "a-commit"
+ ref := "refs/heads/another-branch"
+ fakeCommitId := "a-commit"
branchName := "a-branch"
repoId := "a-repo"
- d.Set("source_ref", source_ref)
+ d.Set("ref", ref)
d.Set("name", branchName)
d.Set("repository_id", repoId)
g.EXPECT().
GetRefs(clients.Ctx, git.GetRefsArgs{
RepositoryId: &repoId,
- Filter: converter.String(strings.TrimPrefix(source_ref, "refs/")),
+ Filter: converter.String(strings.TrimPrefix(ref, "refs/")),
Top: converter.Int(1),
PeelTags: converter.Bool(true),
}).
Return(&git.GetRefsResponseValue{
Value: []git.GitRef{{
- Name: &source_ref,
- ObjectId: &commit,
+ Name: &ref,
+ ObjectId: &fakeCommitId,
}},
}, nil)
g.EXPECT().
UpdateRefs(clients.Ctx, git.UpdateRefsArgs{
RefUpdates: &[]git.GitRefUpdate{{
- Name: converter.String(withRefsHeadsPrefix("a-branch")),
- NewObjectId: &commit,
+ Name: converter.String(withPrefix("refs/heads/", "a-branch")),
+ NewObjectId: &fakeCommitId,
OldObjectId: converter.String("0000000000000000000000000000000000000000"),
}},
RepositoryId: converter.String("a-repo"),
@@ -260,7 +272,7 @@ func TestGitRepositoryBranch_Delete(t *testing.T) {
g.EXPECT().
UpdateRefs(clients.Ctx, git.UpdateRefsArgs{
RefUpdates: &[]git.GitRefUpdate{{
- Name: converter.String(withRefsHeadsPrefix("a-branch")),
+ Name: converter.String(withPrefix("refs/heads/", "a-branch")),
OldObjectId: converter.String("a-commit"),
NewObjectId: converter.String("0000000000000000000000000000000000000000"),
}},