Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/azure devops - #186

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *RepositoriesClient) Get(ctx context.Context, ref gitprovider.OrgReposit
if err != nil {
return nil, err
}
return newUserRepository(c.clientContext, *apiObj, ref), nil
return newRepository(c.clientContext, *apiObj, ref), nil
}

func (c *RepositoriesClient) List(ctx context.Context, o gitprovider.OrganizationRef) ([]gitprovider.OrgRepository, error) {
Expand Down
61 changes: 61 additions & 0 deletions azuredevops/client_repository_branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2023 The Flux CD contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package azuredevops

import (
"context"
"github.com/fluxcd/go-git-providers/gitprovider"
"github.com/microsoft/azure-devops-go-api/azuredevops/v6/git"
)

// BranchClient implements the gitprovider.BranchClient interface.
var _ gitprovider.BranchClient = &BranchClient{}

// BranchClient operates on the branch for a specific repository.
type BranchClient struct {
*clientContext
ref gitprovider.RepositoryRef
}

// Create creates a branch with the given specifications.

func (b BranchClient) Create(ctx context.Context, branch, sha string) error {
ref := "refs/heads/" + branch
repositoryId := b.ref.GetRepository()
project := b.ref.GetIdentity()
oldObjectId := "0000000000000000000000000000000000000000"
opts := []git.GitRefUpdate{
{
Name: &ref,
NewObjectId: &sha,
OldObjectId: &oldObjectId,
},
}
reference := git.UpdateRefsArgs{
RefUpdates: &opts,
RepositoryId: &repositoryId,
Project: &project,
}

if _, err := b.g.UpdateRefs(
ctx,
reference,
); err != nil {
return err
}
return nil
}
99 changes: 99 additions & 0 deletions azuredevops/client_repository_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright 2023 The Flux CD contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package azuredevops

import (
"context"
"github.com/fluxcd/go-git-providers/gitprovider"
"github.com/microsoft/azure-devops-go-api/azuredevops/v6/git"
)

// TreeClient implements the gitprovider.TreeClient interface.
var _ gitprovider.TreeClient = &TreeClient{}

// TreeClient operates on the trees in a specific repository.
type TreeClient struct {
*clientContext
ref gitprovider.RepositoryRef
}

// Get returns a tree
func (c *TreeClient) Get(ctx context.Context, sha string, recursive bool) (*gitprovider.TreeInfo, error) {
repoName := c.ref.GetRepository()
projectName := c.ref.GetIdentity()
opts := git.GetTreeArgs{
RepositoryId: &repoName,
Project: &projectName,
Sha1: &sha,
Recursive: &recursive,
}
apiObj, err := c.g.GetTree(ctx, opts)
if err != nil {
return nil, err
}
treeEntries := make([]*gitprovider.TreeEntry, len(*apiObj.TreeEntries))
for ind, treeEntry := range *apiObj.TreeEntries {
size := 0
if *treeEntry.GitObjectType != "tree" {
size = int(*treeEntry.Size)
}
treeEntries[ind] = &gitprovider.TreeEntry{
Path: *treeEntry.RelativePath,
Mode: *treeEntry.Mode,
Type: string(*treeEntry.GitObjectType),
Size: size,
SHA: *treeEntry.ObjectId,
URL: *treeEntry.Url,
}
}
treeInfo := gitprovider.TreeInfo{
SHA: *apiObj.ObjectId,
Tree: treeEntries,
}
return &treeInfo, nil
}

// List files (blob) in a tree
func (c *TreeClient) List(ctx context.Context, sha string, path string, recursive bool) ([]*gitprovider.TreeEntry, error) {
repoName := c.ref.GetRepository()
projectName := c.ref.GetIdentity()
opts := git.GetTreeArgs{
RepositoryId: &repoName,
Project: &projectName,
Sha1: &sha,
Recursive: &recursive,
}
apiObj, err := c.g.GetTree(ctx, opts)
if err != nil {
return nil, err
}
treeEntries := make([]*gitprovider.TreeEntry, 0, len(*apiObj.TreeEntries))
for _, treeEntry := range *apiObj.TreeEntries {
if *treeEntry.GitObjectType == "blob" {
treeEntries = append(treeEntries, &gitprovider.TreeEntry{
Path: *treeEntry.RelativePath,
Mode: *treeEntry.Mode,
Type: string(*treeEntry.GitObjectType),
Size: int(*treeEntry.Size),
SHA: *treeEntry.ObjectId,
URL: *treeEntry.Url,
})
}
}

return treeEntries, nil
}
52 changes: 30 additions & 22 deletions azuredevops/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,45 @@ import (
"github.com/microsoft/azure-devops-go-api/azuredevops/v6/git"
)

var _ gitprovider.UserRepository = &userRepository{}
var _ gitprovider.OrgRepository = &repository{}

func newUserRepository(ctx *clientContext, apiObj git.GitRepository, ref gitprovider.RepositoryRef) *userRepository {
return &userRepository{
func newRepository(ctx *clientContext, apiObj git.GitRepository, ref gitprovider.RepositoryRef) *repository {
return &repository{
clientContext: ctx,
r: apiObj,
ref: ref,
pullRequests: &PullRequestClient{
clientContext: ctx,
ref: ref,
},
trees: &TreeClient{
clientContext: ctx,
ref: ref,
},
branches: &BranchClient{
clientContext: ctx,
ref: ref,
},
}
}

type userRepository struct {
type repository struct {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since within Azure Devops it doesn't have a concept of user repositories vs org repositories

*clientContext
pr git.GitPullRequest
r git.GitRepository
ref gitprovider.RepositoryRef

pullRequests *PullRequestClient
trees *TreeClient
branches *BranchClient
}

func (r *userRepository) TeamAccess() gitprovider.TeamAccessClient {
func (r *repository) TeamAccess() gitprovider.TeamAccessClient {
//TODO implement me
panic("implement me")
}

func (r *userRepository) Get() gitprovider.RepositoryInfo {
func (r *repository) Get() gitprovider.RepositoryInfo {
return repositoryFromAPI(&r.r)
}

Expand All @@ -61,61 +71,59 @@ func repositoryFromAPI(apiObj *git.GitRepository) gitprovider.RepositoryInfo {
}
return repo
}
func (r userRepository) APIObject() interface{} {
//TODO implement me
panic("implement me")
func (r *repository) Trees() gitprovider.TreeClient {
return r.trees
}

func (r userRepository) Update(ctx context.Context) error {
func (r *repository) APIObject() interface{} {
//TODO implement me
panic("implement me")
}

func (r userRepository) Reconcile(ctx context.Context) (actionTaken bool, err error) {
func (r *repository) Update(ctx context.Context) error {
//TODO implement me
panic("implement me")
}

func (r userRepository) Delete(ctx context.Context) error {
func (r *repository) Reconcile(ctx context.Context) (actionTaken bool, err error) {
//TODO implement me
panic("implement me")
}

func (r userRepository) Repository() gitprovider.RepositoryRef {
func (r *repository) Delete(ctx context.Context) error {
//TODO implement me
panic("implement me")
}

func (r userRepository) Set(info gitprovider.RepositoryInfo) error {
func (r *repository) Repository() gitprovider.RepositoryRef {
//TODO implement me
panic("implement me")
}

func (r userRepository) DeployKeys() gitprovider.DeployKeyClient {
func (r *repository) Set(info gitprovider.RepositoryInfo) error {
//TODO implement me
panic("implement me")
}

func (r userRepository) Commits() gitprovider.CommitClient {
func (r *repository) DeployKeys() gitprovider.DeployKeyClient {
//TODO implement me
panic("implement me")
}

func (r userRepository) Branches() gitprovider.BranchClient {
func (r *repository) Commits() gitprovider.CommitClient {
//TODO implement me
panic("implement me")
}

func (r userRepository) PullRequests() gitprovider.PullRequestClient {
panic("implement me")
func (r *repository) Branches() gitprovider.BranchClient {
return r.branches
}

func (r userRepository) Files() gitprovider.FileClient {
//TODO implement me
func (r *repository) PullRequests() gitprovider.PullRequestClient {
panic("implement me")
}

func (r userRepository) Trees() gitprovider.TreeClient {
func (r *repository) Files() gitprovider.FileClient {
//TODO implement me
panic("implement me")
}