Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Davidson committed Nov 6, 2023
1 parent c899896 commit 95f590e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 56 deletions.
64 changes: 16 additions & 48 deletions azuredevops/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func (a *AzureDevOps) GetRepository(projectName string, name string) (*git.GitRe
}

// createRepositoryIfNotExists creates a repository if it does not exist
func (a *AzureDevOps) createRepositoryIfNotExists(projectName string, repoName string, gitEmail string, gitUsername string) (*git.GitRepository, *string, error) {
func (a *AzureDevOps) createRepositoryIfNotExists(projectName string, repoName string, gitEmail string, gitUsername string) (*git.GitRepository, error) {
client, err := git.NewClient(a.ctx, a.connection)
if err != nil {
return nil, nil, err
return nil, err
}

getRepositoryArgs := git.GetRepositoryArgs{
Expand All @@ -52,14 +52,8 @@ func (a *AzureDevOps) createRepositoryIfNotExists(projectName string, repoName s
}

r, err := client.GetRepository(a.ctx, getRepositoryArgs)

if err == nil {
repoPath, err := a.checkoutAndConfigure(*r.RemoteUrl, gitEmail, gitUsername)
if err != nil {
return nil, nil, err
}

return r, repoPath, nil
return r, nil
}

// TODO: Check that err is a GitRepositoryNotFound error
Expand All @@ -72,90 +66,64 @@ func (a *AzureDevOps) createRepositoryIfNotExists(projectName string, repoName s
}

r, err = client.CreateRepository(a.ctx, createRepositoryArgs)
if err != nil {
return nil, nil, err
}

repoPath, err := a.initRepository(*r.RemoteUrl, gitEmail, gitUsername)
if err != nil {
return nil, nil, err
}

return r, repoPath, nil
}

func (a *AzureDevOps) checkoutAndConfigure(remoteUrl string, gitEmail string, gitUsername string) (*string, error) {
repoPath, err := os.MkdirTemp("", "")
if err != nil {
return nil, err
}

repo := egit.NewGit(repoPath)
pat, err := a.GetPAT()
err = a.initRepository(*r.RemoteUrl, gitEmail, gitUsername)
if err != nil {
return nil, err
}

err = repo.CloneOverHttp(remoteUrl, *pat, "x-oauth-basic")
if err != nil {
return nil, err
}

err = repo.Configure(gitEmail, gitUsername)
if err != nil {
return nil, err
}

repoPath = repo.GetRepositoryPath()

return &repoPath, nil
return r, nil
}

// initRepository creates a main branch
func (a *AzureDevOps) initRepository(remoteUrl string, gitEmail string, gitUsername string) (*string, error) {
func (a *AzureDevOps) initRepository(remoteUrl string, gitEmail string, gitUsername string) error {
repoPath, err := os.MkdirTemp("", "")
if err != nil {
return nil, err
return err
}
defer os.RemoveAll(repoPath)

repo := egit.NewGit(repoPath)

pat, err := a.GetPAT()
if err != nil {
return nil, err
return err
}

err = repo.CloneOverHttp(remoteUrl, *pat, "x-oauth-basic")
if err != nil {
return nil, err
return err
}

err = repo.Configure(gitEmail, gitUsername)
if err != nil {
return nil, err
return err
}

file, err := os.Create(repo.GetFilePath("README.md"))
if err != nil {
return nil, err
return err
}
defer file.Close()

err = repo.AddAll()
if err != nil {
return nil, err
return err
}

commitMessage := "Initial commit."
_, err = repo.Commit(commitMessage)
if err != nil {
return nil, err
return err
}

err = repo.Push(false)
if err != nil {
return nil, err
return err
}

return &repoPath, nil
return nil
}
16 changes: 8 additions & 8 deletions azuredevops/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ import (
)

// CreateWikiIfNotExists creates a code wiki if it does not exist.
func (a *AzureDevOps) CreateWikiIfNotExists(projectName string, wikiName string, gitEmail string, gitUsername string) (*wiki.WikiV2, *git.GitRepository, *string, error) {
func (a *AzureDevOps) CreateWikiIfNotExists(projectName string, wikiName string, gitEmail string, gitUsername string) (*wiki.WikiV2, *git.GitRepository, error) {
client, err := wiki.NewClient(a.ctx, a.connection)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

getWikiArgs := wiki.GetWikiArgs{
Project: &projectName,
WikiIdentifier: &wikiName,
}

r, localPath, err := a.createRepositoryIfNotExists(projectName, wikiName, gitEmail, gitUsername)
r, err := a.createRepositoryIfNotExists(projectName, wikiName, gitEmail, gitUsername)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

w, err := client.GetWiki(a.ctx, getWikiArgs)

if err == nil {
return w, r, localPath, nil
return w, r, nil
}

// TODO: Check that this is a WikiNotFoundError

projectId, err := a.getProjectUUID(projectName)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

branch := "main"
Expand All @@ -53,8 +53,8 @@ func (a *AzureDevOps) CreateWikiIfNotExists(projectName string, wikiName string,
}
w, err = client.CreateWiki(a.ctx, wikiCreateArgs)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

return w, r, localPath, nil
return w, r, nil
}
1 change: 1 addition & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Git interface {
Configure(gitEmail string, gitUsername string) error
GetFilePath(filePath string) string
GetRepositoryPath() string
HasChanges() (bool, error)
Push(force bool) error
SetConfig(key string, value string) error
}

0 comments on commit 95f590e

Please sign in to comment.