diff --git a/azuredevops/internal/service/permissions/resource_git_permissions.go b/azuredevops/internal/service/permissions/resource_git_permissions.go index 3900aaa62..c1ebe2f12 100644 --- a/azuredevops/internal/service/permissions/resource_git_permissions.go +++ b/azuredevops/internal/service/permissions/resource_git_permissions.go @@ -49,73 +49,6 @@ func ResourceGitPermissions() *schema.Resource { } } -func createGitToken(clients *client.AggregatedClient, d *schema.ResourceData) (*string, error) { - projectID, ok := d.GetOk("project_id") - if !ok { - return nil, fmt.Errorf("Failed to get 'project_id' from schema") - } - - /* - * Token format - * ACL for ALL Git repositories in a project: repoV2/#ProjectID# - * ACL for a Git repository in a project: repoV2/#ProjectID#/#RepositoryID# - * ACL for all branches inside a Git repository in a project: repoV2/#ProjectID#/#RepositoryID#/refs/heads - * ACL for a branch inside a Git repository in a project: repoV2/#ProjectID#/#RepositoryID#/refs/heads/#BranchID# - */ - aclToken := "repoV2/" + projectID.(string) - repositoryID, repoOk := d.GetOk("repository_id") - if repoOk { - aclToken += "/" + repositoryID.(string) - } - branchName, branchOk := d.GetOk("branch_name") - if branchOk { - if !repoOk { - return nil, fmt.Errorf("Unable to create ACL token for branch %s, because no repository is specified", branchName) - } - branch, err := getBranchByName(clients, - converter.StringFromInterface(repositoryID), - converter.StringFromInterface(branchName)) - if err != nil { - return nil, err - } - branchPath := strings.Split(*branch.Name, "/") - branchName, err = converter.EncodeUtf16HexString(branchPath[len(branchPath)-1]) - if err != nil { - return nil, err - } - aclToken += "/refs/heads/" + branchName.(string) - } - return &aclToken, nil -} - -func getBranchByName(clients *client.AggregatedClient, repositoryID *string, branchName *string) (*git.GitRef, error) { - filter := "heads/" + *branchName - currentToken := "" - args := git.GetRefsArgs{ - RepositoryId: repositoryID, - Filter: &filter, - } - for hasMore := true; hasMore; { - if currentToken != "" { - args.ContinuationToken = ¤tToken - } - res, err := clients.GitReposClient.GetRefs(clients.Ctx, args) - if err != nil { - return nil, err - } - currentToken = res.ContinuationToken - hasMore = currentToken != "" - item := linq.From(res.Value).FirstWith(func(elem interface{}) bool { - return strings.HasSuffix(*(elem.(git.GitRef).Name), *branchName) - }) - if item != nil { - gitRef := item.(git.GitRef) - return &gitRef, nil - } - } - return nil, fmt.Errorf("No branch found with name [%s] in repository with id [%s]", *branchName, *repositoryID) -} - func resourceGitPermissionsCreate(d *schema.ResourceData, m interface{}) error { debug.Wait() @@ -208,3 +141,70 @@ func resourceGitPermissionsImporter(d *schema.ResourceData, m interface{}) ([]*s // repoV2/#ProjectID#/#RepositoryID#/refs/heads/#BranchName#/#SubjectDescriptor# return nil, errors.New("resourceGitPermissionsImporter: Not implemented") } + +func createGitToken(clients *client.AggregatedClient, d *schema.ResourceData) (*string, error) { + projectID, ok := d.GetOk("project_id") + if !ok { + return nil, fmt.Errorf("Failed to get 'project_id' from schema") + } + + /* + * Token format + * ACL for ALL Git repositories in a project: repoV2/#ProjectID# + * ACL for a Git repository in a project: repoV2/#ProjectID#/#RepositoryID# + * ACL for all branches inside a Git repository in a project: repoV2/#ProjectID#/#RepositoryID#/refs/heads + * ACL for a branch inside a Git repository in a project: repoV2/#ProjectID#/#RepositoryID#/refs/heads/#BranchID# + */ + aclToken := "repoV2/" + projectID.(string) + repositoryID, repoOk := d.GetOk("repository_id") + if repoOk { + aclToken += "/" + repositoryID.(string) + } + branchName, branchOk := d.GetOk("branch_name") + if branchOk { + if !repoOk { + return nil, fmt.Errorf("Unable to create ACL token for branch %s, because no repository is specified", branchName) + } + branch, err := getBranchByName(clients, + converter.StringFromInterface(repositoryID), + converter.StringFromInterface(branchName)) + if err != nil { + return nil, err + } + branchPath := strings.Split(*branch.Name, "/") + branchName, err = converter.EncodeUtf16HexString(branchPath[len(branchPath)-1]) + if err != nil { + return nil, err + } + aclToken += "/refs/heads/" + branchName.(string) + } + return &aclToken, nil +} + +func getBranchByName(clients *client.AggregatedClient, repositoryID *string, branchName *string) (*git.GitRef, error) { + filter := "heads/" + *branchName + currentToken := "" + args := git.GetRefsArgs{ + RepositoryId: repositoryID, + Filter: &filter, + } + for hasMore := true; hasMore; { + if currentToken != "" { + args.ContinuationToken = ¤tToken + } + res, err := clients.GitReposClient.GetRefs(clients.Ctx, args) + if err != nil { + return nil, err + } + currentToken = res.ContinuationToken + hasMore = currentToken != "" + item := linq.From(res.Value).FirstWith(func(elem interface{}) bool { + return strings.HasSuffix(*(elem.(git.GitRef).Name), *branchName) + }) + if item != nil { + gitRef := item.(git.GitRef) + return &gitRef, nil + } + } + return nil, fmt.Errorf("No branch found with name [%s] in repository with id [%s]", *branchName, *repositoryID) +} diff --git a/azuredevops/internal/service/permissions/resource_git_permissions_test.go b/azuredevops/internal/service/permissions/resource_git_permissions_test.go index 0ae3b5f88..229977265 100644 --- a/azuredevops/internal/service/permissions/resource_git_permissions_test.go +++ b/azuredevops/internal/service/permissions/resource_git_permissions_test.go @@ -37,25 +37,6 @@ var gitBranchNameValid = "master" var gitTokenBranch = fmt.Sprintf("%s/refs/heads/%s", gitTokenRepository, encodeBranchName(gitBranchNameValid)) var gitBranchNameInValid = "@@invalid@@" -func encodeBranchName(branchName string) string { - ret, _ := converter.EncodeUtf16HexString(branchName) - return ret -} - -func getGitPermissionsResource(t *testing.T, gitProjectID string, repoID string, branchName string) *schema.ResourceData { - d := schema.TestResourceDataRaw(t, ResourceGitPermissions().Schema, nil) - if gitProjectID != "" { - d.Set("project_id", gitProjectID) - } - if repoID != "" { - d.Set("repository_id", repoID) - } - if branchName != "" { - d.Set("branch_name", branchName) - } - return d -} - func TestGitPermissions_CreateGitToken(t *testing.T) { var d *schema.ResourceData var token *string @@ -258,3 +239,22 @@ func TestGitPermissions_GetBranchName_VerifyValue(t *testing.T) { assert.Nil(t, err) assert.Equal(t, gitBranchNameValid, *gitRef.Name) } + +func encodeBranchName(branchName string) string { + ret, _ := converter.EncodeUtf16HexString(branchName) + return ret +} + +func getGitPermissionsResource(t *testing.T, gitProjectID string, repoID string, branchName string) *schema.ResourceData { + d := schema.TestResourceDataRaw(t, ResourceGitPermissions().Schema, nil) + if gitProjectID != "" { + d.Set("project_id", gitProjectID) + } + if repoID != "" { + d.Set("repository_id", repoID) + } + if branchName != "" { + d.Set("branch_name", branchName) + } + return d +} diff --git a/azuredevops/internal/service/permissions/resource_project_permissions.go b/azuredevops/internal/service/permissions/resource_project_permissions.go index 760877b42..931aa6151 100644 --- a/azuredevops/internal/service/permissions/resource_project_permissions.go +++ b/azuredevops/internal/service/permissions/resource_project_permissions.go @@ -32,15 +32,6 @@ func ResourceProjectPermissions() *schema.Resource { } } -func createProjectToken(d *schema.ResourceData) (*string, error) { - projectID, ok := d.GetOk("project_id") - if !ok { - return nil, fmt.Errorf("Failed to get 'project_id' from schema") - } - aclToken := fmt.Sprintf("$PROJECT:vstfs:///Classification/TeamProject/%s", projectID.(string)) - return &aclToken, nil -} - func resourceProjectPermissionsCreate(d *schema.ResourceData, m interface{}) error { debug.Wait() @@ -132,3 +123,12 @@ func resourceProjectPermissionsImporter(d *schema.ResourceData, m interface{}) ( // $PROJECT:vstfs:///Classification/TeamProject// return nil, errors.New("resourceProjectPermissionsImporter: Not implemented") } + +func createProjectToken(d *schema.ResourceData) (*string, error) { + projectID, ok := d.GetOk("project_id") + if !ok { + return nil, fmt.Errorf("Failed to get 'project_id' from schema") + } + aclToken := fmt.Sprintf("$PROJECT:vstfs:///Classification/TeamProject/%s", projectID.(string)) + return &aclToken, nil +} diff --git a/azuredevops/internal/service/permissions/resource_project_permissions_test.go b/azuredevops/internal/service/permissions/resource_project_permissions_test.go index 94096f540..bcb24ec53 100644 --- a/azuredevops/internal/service/permissions/resource_project_permissions_test.go +++ b/azuredevops/internal/service/permissions/resource_project_permissions_test.go @@ -25,14 +25,6 @@ func init() { var projectID = "9083e944-8e9e-405e-960a-c80180aa71e6" var projectToken = fmt.Sprintf("$PROJECT:vstfs:///Classification/TeamProject/%s", projectID) -func getProjecPermissionsResource(t *testing.T, projectID string) *schema.ResourceData { - d := schema.TestResourceDataRaw(t, ResourceProjectPermissions().Schema, nil) - if projectID != "" { - d.Set("project_id", projectID) - } - return d -} - func TestProjectPermissions_CreateProjectToken(t *testing.T) { var d *schema.ResourceData var token *string @@ -49,3 +41,11 @@ func TestProjectPermissions_CreateProjectToken(t *testing.T) { assert.Nil(t, token) assert.NotNil(t, err) } + +func getProjecPermissionsResource(t *testing.T, projectID string) *schema.ResourceData { + d := schema.TestResourceDataRaw(t, ResourceProjectPermissions().Schema, nil) + if projectID != "" { + d.Set("project_id", projectID) + } + return d +}