Skip to content

Commit

Permalink
chore: bump go-repo 1.8.0 (#5386)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored Aug 21, 2020
1 parent d28bb81 commit e2bee0a
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 74 deletions.
40 changes: 21 additions & 19 deletions cli/cdsctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -195,12 +196,12 @@ func withAutoConf() cli.CommandModifier {
)
}

func discoverConf(ctx []cli.Arg) ([]string, error) {
func discoverConf(ctxArg []cli.Arg) ([]string, error) {
var needProject, needApplication, needWorkflow bool

// populates needs from ctx
mctx := make(map[string]cli.Arg, len(ctx))
for _, arg := range ctx {
// populates needs from ctxArg
mctx := make(map[string]cli.Arg, len(ctxArg))
for _, arg := range ctxArg {
mctx[arg.Name] = arg
switch arg.Name {
case _ProjectKey:
Expand All @@ -217,29 +218,30 @@ func discoverConf(ctx []cli.Arg) ([]string, error) {
}

var projectKey, applicationName, workflowName string
ctx := context.Background()

// try to find existing .git repository
var repoExists bool
r, err := repo.New(".")
r, err := repo.New(ctx, ".")
if err == nil {
repoExists = true
}

// if repo exists ask for usage
if repoExists {
gitProjectKey, _ := r.LocalConfigGet("cds", "project")
gitApplicationName, _ := r.LocalConfigGet("cds", "application")
gitWorkflowName, _ := r.LocalConfigGet("cds", "workflow")
gitProjectKey, _ := r.LocalConfigGet(ctx, "cds", "project")
gitApplicationName, _ := r.LocalConfigGet(ctx, "cds", "application")
gitWorkflowName, _ := r.LocalConfigGet(ctx, "cds", "workflow")

// if all needs were found in git do not ask for confirmation and use the config
needConfirmation := (needProject && gitProjectKey == "") || (needApplication && gitApplicationName == "") || (needWorkflow && gitWorkflowName == "")

if needConfirmation {
fetchURL, err := r.FetchURL()
fetchURL, err := r.FetchURL(ctx)
if err != nil {
return nil, errors.Wrap(err, "cannot get url from local git repository")
}
name, err := r.Name()
name, err := r.Name(ctx)
if err != nil {
return nil, errors.Wrap(err, "cannot get name from local git repository")
}
Expand All @@ -250,13 +252,13 @@ func discoverConf(ctx []cli.Arg) ([]string, error) {
// if repo exists and is correct get existing config from it's config
if repoExists {
if needProject {
projectKey, _ = r.LocalConfigGet("cds", "project")
projectKey, _ = r.LocalConfigGet(ctx, "cds", "project")
}
if needApplication {
applicationName, _ = r.LocalConfigGet("cds", "application")
applicationName, _ = r.LocalConfigGet(ctx, "cds", "application")
}
if needWorkflow {
workflowName, _ = r.LocalConfigGet("cds", "workflow")
workflowName, _ = r.LocalConfigGet(ctx, "cds", "workflow")
}
}

Expand All @@ -270,7 +272,7 @@ func discoverConf(ctx []cli.Arg) ([]string, error) {
var projects []sdk.Project

if repoExists {
name, err := r.Name()
name, err := r.Name(ctx)
if err != nil {
return nil, errors.Wrap(err, "cannot get name from current repository")
}
Expand Down Expand Up @@ -340,7 +342,7 @@ func discoverConf(ctx []cli.Arg) ([]string, error) {
// set project key and override repository config if exists
projectKey = project.Key
if repoExists {
if err := r.LocalConfigSet("cds", "project", projectKey); err != nil {
if err := r.LocalConfigSet(ctx, "cds", "project", projectKey); err != nil {
return nil, errors.Wrap(err, "cannot set local git configuration")
}
}
Expand Down Expand Up @@ -377,7 +379,7 @@ func discoverConf(ctx []cli.Arg) ([]string, error) {
if application != nil {
applicationName = application.Name
if repoExists {
if err := r.LocalConfigSet("cds", "application", applicationName); err != nil {
if err := r.LocalConfigSet(ctx, "cds", "application", applicationName); err != nil {
return nil, errors.Wrap(err, "cannot set local git configuration")
}
}
Expand Down Expand Up @@ -416,7 +418,7 @@ func discoverConf(ctx []cli.Arg) ([]string, error) {
if workflow != nil {
workflowName = workflow.Name
if repoExists {
if err := r.LocalConfigSet("cds", "workflow", workflowName); err != nil {
if err := r.LocalConfigSet(ctx, "cds", "workflow", workflowName); err != nil {
return nil, errors.Wrap(err, "cannot set local git configuration")
}
}
Expand All @@ -425,8 +427,8 @@ func discoverConf(ctx []cli.Arg) ([]string, error) {
}

// for all required context args override or add the value in cli args
preargs := make([]string, len(ctx))
for i, arg := range ctx {
preargs := make([]string, len(ctxArg))
for i, arg := range ctxArg {
switch arg.Name {
case _ProjectKey:
preargs[i] = projectKey
Expand Down
10 changes: 6 additions & 4 deletions cli/cdsctl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"fmt"
"io"
"path"
Expand Down Expand Up @@ -93,19 +94,20 @@ func shellRun(v cli.Values) error {

home := "/"

ctx := context.Background()
// try to discover conf for existing .git repository
r, errR := repo.New(".")
r, errR := repo.New(ctx, ".")
if errR == nil {
if _, err := discoverConf([]cli.Arg{
{Name: _ProjectKey},
{Name: _ApplicationName, AllowEmpty: true},
{Name: _WorkflowName, AllowEmpty: true},
}); err == nil {
if proj, _ := r.LocalConfigGet("cds", "project"); proj != "" {
if proj, _ := r.LocalConfigGet(ctx, "cds", "project"); proj != "" {
home = "/project/" + proj
if wf, _ := r.LocalConfigGet("cds", "workflow"); wf != "" {
if wf, _ := r.LocalConfigGet(ctx, "cds", "workflow"); wf != "" {
home += "/workflow/" + wf
} else if app, _ := r.LocalConfigGet("cds", "application"); app != "" {
} else if app, _ := r.LocalConfigGet(ctx, "cds", "application"); app != "" {
home += "/application/" + app
}
}
Expand Down
10 changes: 6 additions & 4 deletions cli/cdsctl/template_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -180,13 +181,14 @@ func templateApplyRun(v cli.Values) error {
// try to find existing .git repository
var localRepoURL string
var localRepoName string
r, err := repo.New(".")
ctx := context.Background()
r, err := repo.New(ctx, ".")
if err == nil {
localRepoURL, err = r.FetchURL()
localRepoURL, err = r.FetchURL(ctx)
if err != nil {
return err
}
localRepoName, err = r.Name()
localRepoName, err = r.Name(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -364,7 +366,7 @@ func templateApplyRun(v cli.Values) error {

// store the chosen workflow name to git config
if localRepoName != "" {
if err := r.LocalConfigSet("cds", "workflow", workflowName); err != nil {
if err := r.LocalConfigSet(ctx, "cds", "workflow", workflowName); err != nil {
return err
}
}
Expand Down
6 changes: 4 additions & 2 deletions cli/cdsctl/workflow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"

repo "github.com/fsamin/go-repo"
Expand Down Expand Up @@ -41,12 +42,13 @@ func workflow() *cobra.Command {

func workflowNodeForCurrentRepo(projectKey, workflowName string) (int64, error) {
//Try to get the latest commit
r, err := repo.New("")
ctx := context.Background()
r, err := repo.New(ctx, "")
if err != nil {
return 0, nil
}

latestCommit, err := r.LatestCommit()
latestCommit, err := r.LatestCommit(ctx)
if err != nil {
return 0, fmt.Errorf("unable to get latest commit: %v", err)
}
Expand Down
17 changes: 9 additions & 8 deletions cli/cdsctl/workflow_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -90,7 +91,7 @@ func interactiveChooseProject(gitRepo repo.Repo, defaultValue string) (string, e
selected := cli.AskChoice("Choose the CDS project:", opts...)
chosenProj = &projs[selected]

if err := gitRepo.LocalConfigSet("cds", "project", chosenProj.Key); err != nil {
if err := gitRepo.LocalConfigSet(context.Background(), "cds", "project", chosenProj.Key); err != nil {
return "", err
}

Expand All @@ -105,8 +106,7 @@ func interactiveChooseVCSServer(proj *sdk.Project, gitRepo repo.Repo) (string, e
case 1:
return proj.VCSServers[0].Name, nil
default:

fetchURL, err := gitRepo.FetchURL()
fetchURL, err := gitRepo.FetchURL(context.Background())
if err != nil {
return "", fmt.Errorf("Unable to get remote URL: %v", err)
}
Expand Down Expand Up @@ -451,7 +451,8 @@ func craftPipelineFile(proj *sdk.Project, existingPip *sdk.Pipeline, pipName, de

func workflowInitRun(c cli.Values) error {
path := "."
gitRepo, err := repo.New(path)
ctx := context.Background()
gitRepo, err := repo.New(ctx, path)
if err != nil {
return err
}
Expand All @@ -469,7 +470,7 @@ func workflowInitRun(c cli.Values) error {

repoFullname := c.GetString("repository-fullname")
if repoFullname == "" {
repoFullname, err = gitRepo.Name()
repoFullname, err = gitRepo.Name(ctx)
if err != nil {
return fmt.Errorf("unable to retrieve repository name: %v", err)
}
Expand All @@ -480,7 +481,7 @@ func workflowInitRun(c cli.Values) error {

fetchURL := c.GetString("repository-url")
if fetchURL == "" {
fetchURL, err = gitRepo.FetchURL()
fetchURL, err = gitRepo.FetchURL(ctx)
if err != nil {
return fmt.Errorf("unable to retrieve origin URL: %v", err)
}
Expand Down Expand Up @@ -600,11 +601,11 @@ func workflowInitRun(c cli.Values) error {
}

//Configure local git
if err := gitRepo.LocalConfigSet("cds", "project", proj.Key); err != nil {
if err := gitRepo.LocalConfigSet(ctx, "cds", "project", proj.Key); err != nil {
fmt.Printf("error: unable to setup git local config to store cds project key: %v\n", err)
}

if err := gitRepo.LocalConfigSet("cds", "workflow", workflowName); err != nil {
if err := gitRepo.LocalConfigSet(ctx, "cds", "workflow", workflowName); err != nil {
fmt.Printf("error: unable to setup git local config to store cds workflow name: %v\n", err)
}

Expand Down
8 changes: 5 additions & 3 deletions cli/cdsctl/workflow_run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -106,10 +107,11 @@ func workflowRunManualRun(v cli.Values) error {
return fmt.Errorf("Unable to get current path: %s", err)
}
var gitBranch, currentBranch, remoteURL string
r, err := repo.New(dir)
ctx := context.Background()
r, err := repo.New(ctx, dir)
if err == nil { // If the directory is a git repository
currentBranch, _ = r.CurrentBranch()
remoteURL, err = r.FetchURL()
currentBranch, _ = r.CurrentBranch(ctx)
remoteURL, err = r.FetchURL(ctx)
if err != nil {
return sdk.WrapError(err, "cannot fetch the remote url")
}
Expand Down
12 changes: 4 additions & 8 deletions cli/cdsctl/workflow_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func workflowStatusRunWithTrack(v cli.Values) (interface{}, error) {
var currentDisplay = new(cli.Display)

// try to get the latest commit
r, err := repo.New(".")
ctx := context.Background()
r, err := repo.New(ctx, ".")
if err != nil {
return nil, fmt.Errorf("unable to get latest commit: %v", err)
}
latestCommit, err := r.LatestCommit()
latestCommit, err := r.LatestCommit(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get latest commit: %v", err)
}
Expand All @@ -74,13 +75,8 @@ func workflowStatusRunWithTrack(v cli.Values) (interface{}, error) {
runNumber = runs[0].Number
}

run, err := client.WorkflowRunGet(v.GetString(_ProjectKey), v.GetString(_WorkflowName), runNumber)
if err != nil {
return nil, err
}

for {
run, err = client.WorkflowRunGet(v.GetString(_ProjectKey), v.GetString(_WorkflowName), runNumber)
run, err := client.WorkflowRunGet(v.GetString(_ProjectKey), v.GetString(_WorkflowName), runNumber)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions engine/repositories/processor_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func (s *Service) processCheckout(ctx context.Context, op *sdk.Operation) error
log.Debug("processCheckout> repo cloned with current branch: %s", currentBranch)

// Clean no commited changes if exists
if err := gitRepo.ResetHard("HEAD"); err != nil {
if err := gitRepo.ResetHard(ctx, "HEAD"); err != nil {
return sdk.WithStack(err)
}
log.Debug("processCheckout> repo reset to HEAD")

if op.Setup.Checkout.Tag != "" {
log.Debug("processCheckout> fetching tag %s from %s", op.Setup.Checkout.Tag, op.URL)
if err := gitRepo.FetchRemoteTag("origin", op.Setup.Checkout.Tag); err != nil {
if err := gitRepo.FetchRemoteTag(ctx, "origin", op.Setup.Checkout.Tag); err != nil {
return sdk.WithStack(err)
}
log.Info(ctx, "processCheckout> repository %s ready on tag '%s'", op.URL, op.Setup.Checkout.Tag)
Expand All @@ -33,31 +33,31 @@ func (s *Service) processCheckout(ctx context.Context, op *sdk.Operation) error
op.Setup.Checkout.Branch = op.RepositoryInfo.DefaultBranch
}
log.Debug("processCheckout> fetching branch %s from %s", op.Setup.Checkout.Branch, op.URL)
if err := gitRepo.FetchRemoteBranch("origin", op.Setup.Checkout.Branch); err != nil {
if err := gitRepo.FetchRemoteBranch(ctx, "origin", op.Setup.Checkout.Branch); err != nil {
return sdk.WithStack(err)
}

// Check commit
if op.Setup.Checkout.Commit == "" {
// Reset HARD to the latest commit of the remote branch (don't use pull because there can be conflicts if the remote was forced)
log.Debug("processCheckout> resetting the branch %s from remote", op.Setup.Checkout.Branch)
if err := gitRepo.ResetHard("origin/" + op.Setup.Checkout.Branch); err != nil {
if err := gitRepo.ResetHard(ctx, "origin/"+op.Setup.Checkout.Branch); err != nil {
return sdk.WithStack(err)
}
} else {
currentCommit, err := gitRepo.LatestCommit()
currentCommit, err := gitRepo.LatestCommit(ctx)
if err != nil {
return sdk.WithStack(err)
}
if currentCommit.LongHash != op.Setup.Checkout.Commit {
// Not the same commit, pull and reset HARD the commit
log.Debug("processCheckout> resetting the branch %s from remote", op.Setup.Checkout.Branch)
if err := gitRepo.ResetHard("origin/" + op.Setup.Checkout.Branch); err != nil {
if err := gitRepo.ResetHard(ctx, "origin/"+op.Setup.Checkout.Branch); err != nil {
return sdk.WithStack(err)
}

log.Debug("Repositories> processCheckout> resetting commit %s", op.Setup.Checkout.Commit)
if err := gitRepo.ResetHard(op.Setup.Checkout.Commit); err != nil {
if err := gitRepo.ResetHard(ctx, op.Setup.Checkout.Commit); err != nil {
return sdk.WithStack(err)
}
}
Expand Down
Loading

0 comments on commit e2bee0a

Please sign in to comment.