Skip to content

Commit

Permalink
fix: existing projects during rill deploy (#5698)
Browse files Browse the repository at this point in the history
* Handle existing projects for rill deploy

* Update check on connect-github
  • Loading branch information
AdityaHegde committed Sep 17, 2024
1 parent 612795a commit 201fa83
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
60 changes: 45 additions & 15 deletions cli/cmd/project/connect_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ func GitPushCmd(ch *cmdutil.Helper) *cobra.Command {
}

func ConnectGithubFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployOpts) error {
// Set a default org for the user if necessary
// (If user is not in an org, we'll create one based on their Github account later in the flow.)
if ch.Org == "" {
if err := org.SetDefaultOrg(ctx, ch); err != nil {
return err
}
}

// The gitPath can be either a local path or a remote .git URL.
// Determine which it is.
var isLocalGitPath bool
Expand All @@ -84,17 +92,47 @@ func ConnectGithubFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployOpts
}

// If the Git path is local, we'll do some extra steps to infer the githubURL.
var localGitPath, localProjectPath string
if isLocalGitPath {
var err error
localGitPath, localProjectPath, err = ValidateLocalProject(ch, opts.GitPath, opts.SubPath)
localGitPath, localProjectPath, err := ValidateLocalProject(ch, opts.GitPath, opts.SubPath)
if err != nil {
if errors.Is(err, ErrInvalidProject) {
return nil
}
return err
}

if ch.Org != "" {
adminClient, err := ch.Client()
if err != nil {
if errors.Is(err, ErrInvalidProject) {
return nil
}
return err
}

var proj *adminv1.Project

if opts.Name == "" {
// Try loading the project from the .rillcloud directory
proj, err = ch.LoadProject(ctx, localProjectPath)
if err != nil {
return err
}
} else {
projResp, err := adminClient.GetProject(ctx, &adminv1.GetProjectRequest{OrganizationName: ch.Org, Name: opts.Name})
if err != nil {
if st, ok := status.FromError(err); !ok || st.Code() != codes.NotFound {
return err
}
}
if projResp != nil {
proj = projResp.Project
}
}

if proj != nil && proj.GithubUrl != "" {
ch.PrintfError("Found existing project. But it is already connected to a github repo.\nPlease visit %s to update the github repo.\n", proj.FrontendUrl)
return nil
}
}

if isLocalGitPath {
// Extract the Git remote and infer the githubURL.
var remote *gitutil.Remote
remote, githubURL, err = gitutil.ExtractGitRemote(localGitPath, opts.RemoteName, false)
Expand Down Expand Up @@ -160,14 +198,6 @@ func ConnectGithubFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployOpts
opts.Name = ghRepo
}

// Set a default org for the user if necessary
// (If user is not in an org, we'll create one based on their Github account later in the flow.)
if ch.Org == "" {
if err := org.SetDefaultOrg(ctx, ch); err != nil {
return err
}
}

// If no default org is set by now, it means the user is not in an org yet.
// We create a default org based on their Github account name.
if ch.Org == "" {
Expand Down
16 changes: 12 additions & 4 deletions cli/cmd/project/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,20 @@ func DeployWithUploadFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployO
return err
}

// check if the project with name already exists
projectExists, err := projectExists(ctx, ch, ch.Org, opts.Name)
projResp, err := adminClient.GetProject(ctx, &adminv1.GetProjectRequest{OrganizationName: ch.Org, Name: opts.Name})
if err != nil {
return err
if st, ok := status.FromError(err); !ok || st.Code() != codes.NotFound {
return err
}
}
if projectExists {

// check if the project with name already exists
if projResp != nil {
if projResp.Project.GithubUrl != "" {
ch.PrintfError("Found existing project. But it is connected to a github repo.\nPush any changes to %q to deploy.\n", projResp.Project.GithubUrl)
return nil
}

ch.Printer.Println("Found existing project. Starting re-upload.")
assetID, err := cmdutil.UploadRepo(ctx, repo, ch, ch.Org, opts.Name)
if err != nil {
Expand Down

0 comments on commit 201fa83

Please sign in to comment.