From 95f590e14b41e282dd5e5b00c97476520b601f49 Mon Sep 17 00:00:00 2001 From: Fraser Davidson Date: Mon, 6 Nov 2023 15:03:01 +0000 Subject: [PATCH] WIP --- azuredevops/repository.go | 64 ++++++++++----------------------------- azuredevops/wiki.go | 16 +++++----- git/git.go | 1 + 3 files changed, 25 insertions(+), 56 deletions(-) diff --git a/azuredevops/repository.go b/azuredevops/repository.go index f9fb049..61d1f36 100644 --- a/azuredevops/repository.go +++ b/azuredevops/repository.go @@ -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{ @@ -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 @@ -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 } diff --git a/azuredevops/wiki.go b/azuredevops/wiki.go index d804ed0..70392fa 100644 --- a/azuredevops/wiki.go +++ b/azuredevops/wiki.go @@ -6,10 +6,10 @@ 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{ @@ -17,22 +17,22 @@ func (a *AzureDevOps) CreateWikiIfNotExists(projectName string, wikiName string, 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" @@ -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 } diff --git a/git/git.go b/git/git.go index a199038..0c71976 100644 --- a/git/git.go +++ b/git/git.go @@ -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 }