Skip to content

Commit

Permalink
Changes applied according to first review
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeckel committed Jun 29, 2020
1 parent 35d9884 commit 1f9fccc
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 103 deletions.
134 changes: 67 additions & 67 deletions azuredevops/internal/service/permissions/resource_git_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = &currentToken
}
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()

Expand Down Expand Up @@ -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 = &currentToken
}
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -132,3 +123,12 @@ func resourceProjectPermissionsImporter(d *schema.ResourceData, m interface{}) (
// $PROJECT:vstfs:///Classification/TeamProject/<ProjectId>/<SubjectDescriptor>
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 1f9fccc

Please sign in to comment.