Skip to content

Commit

Permalink
fix(api): resync repo managers to compute template default params (#5260
Browse files Browse the repository at this point in the history
)

* fix(api): resync repo managers to compute template default params

* refactor: move resync to cdsctl workflow init
  • Loading branch information
richardlt authored Jun 23, 2020
1 parent 511cb03 commit 1e8bc3a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
36 changes: 32 additions & 4 deletions cli/cdsctl/workflow_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ func searchRepository(pkey, repoManagerName, repoFullname string) (string, error
return "", fmt.Errorf("unable to find repository %s from %s: please check your credentials", repoFullname, repoManagerName)
}

// Check for given repo URL that it exists in project vcs.
// This func will resync the repositories list if not found at first time.
func checkRepositoryExists(proj sdk.Project, repoURL string) error {
for _, vcs := range proj.VCSServers {
var resync bool
for {
repos, err := client.RepositoriesList(proj.Key, vcs.Name, resync)
if err != nil {
return fmt.Errorf("unable to list repositories from %s: %v", vcs.Name, err)
}
for _, r := range repos {
if repoURL == r.HTTPCloneURL || repoURL == r.SSHCloneURL {
return nil
}
}
if resync {
break
}
resync = true
}
}
return fmt.Errorf("unable to find repository %s for project: please check project vcs configuration", repoURL)
}

func interactiveChoosePipeline(pkey, defaultPipeline string) (string, *sdk.Pipeline, error) {
// Try to find pipeline or create a new pipeline
pips, err := client.PipelineList(pkey)
Expand Down Expand Up @@ -427,9 +451,9 @@ func craftPipelineFile(proj *sdk.Project, existingPip *sdk.Pipeline, pipName, de

func workflowInitRun(c cli.Values) error {
path := "."
gitRepo, errRepo := repo.New(path)
if errRepo != nil {
return errRepo
gitRepo, err := repo.New(path)
if err != nil {
return err
}

pkey, err := interactiveChooseProject(gitRepo, c.GetString(_ProjectKey))
Expand Down Expand Up @@ -476,7 +500,7 @@ func workflowInitRun(c cli.Values) error {

if len(files) > 0 {
if c.GetString("pipeline") != "" {
return errors.New("you can't set a pipeline name while files alerady exists in .cds/ folder")
return errors.New("you can't set a pipeline name while files already exists in .cds/ folder")
}
}

Expand Down Expand Up @@ -536,6 +560,10 @@ func workflowInitRun(c cli.Values) error {
files = append(files, pipFilePath)
}
} else {
// Check that current repository is accessible for CDS, also resync repositories
if err := checkRepositoryExists(*proj, fetchURL); err != nil {
return err
}
fmt.Println("Reading files:")
for _, f := range files {
fmt.Printf(" * %s", cli.Magenta(f))
Expand Down
3 changes: 2 additions & 1 deletion engine/api/router_middleware_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ func (api *API) xsrfMiddleware(ctx context.Context, w http.ResponseWriter, req *
// else if its a read request we want to reuse a cached XSRF token or generate one
if rc.PermissionLevel > sdk.PermissionRead {
if !existXSRFTokenInCache || xsrfToken != existingXSRFToken {
return ctx, sdk.WithStack(sdk.ErrUnauthorized)
// We want to return a forbidden to allow the user to retry with a new token.
return ctx, sdk.WithStack(sdk.ErrForbidden)
}

newXSRFToken, err := authentication.NewSessionXSRFToken(api.Cache, sessionID)
Expand Down

0 comments on commit 1e8bc3a

Please sign in to comment.