From f4c4300778cff0ddf94108aecab660d571880716 Mon Sep 17 00:00:00 2001 From: Shawna Monero Date: Thu, 18 Apr 2024 20:29:52 -0700 Subject: [PATCH] fix all issues and remove all unused code --- cmd/adhoc.go | 2 + .../adhoc_execution_params.go | 57 ++++++++----- .../adhoc_github_helpers.go | 50 ------------ server/neptune/adhoc/config/config.go | 2 + server/neptune/adhoc/server.go | 80 ++++++++++--------- 5 files changed, 82 insertions(+), 109 deletions(-) delete mode 100644 server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go diff --git a/cmd/adhoc.go b/cmd/adhoc.go index 197f645e1..0a4d82cec 100644 --- a/cmd/adhoc.go +++ b/cmd/adhoc.go @@ -67,6 +67,8 @@ func (a *Adhoc) NewServer(userConfig legacy.UserConfig, config legacy.Config) (S GithubAppKeyFile: userConfig.GithubAppKeyFile, GithubAppSlug: userConfig.GithubAppSlug, GlobalCfg: globalCfg, + GithubUser: userConfig.GithubUser, + GithubToken: userConfig.GithubToken, } return adhoc.NewServer(cfg) } diff --git a/server/neptune/adhoc/adhocexecutionhelpers/adhoc_execution_params.go b/server/neptune/adhoc/adhocexecutionhelpers/adhoc_execution_params.go index b9202879f..2b6b49141 100644 --- a/server/neptune/adhoc/adhocexecutionhelpers/adhoc_execution_params.go +++ b/server/neptune/adhoc/adhocexecutionhelpers/adhoc_execution_params.go @@ -4,13 +4,12 @@ import ( "context" "github.com/pkg/errors" - "github.com/runatlantis/atlantis/server/models" - "github.com/runatlantis/atlantis/server/neptune/adhoc/adhocgithubhelpers" "github.com/runatlantis/atlantis/server/neptune/gateway/config" root_config "github.com/runatlantis/atlantis/server/neptune/gateway/config" "github.com/runatlantis/atlantis/server/neptune/workflows/activities/github" "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform" internal_gh "github.com/runatlantis/atlantis/server/vcs/provider/github" + "github.com/runatlantis/atlantis/server/vcs/provider/github/converter" ) type AdhocTerraformWorkflowExecutionParams struct { @@ -20,11 +19,30 @@ type AdhocTerraformWorkflowExecutionParams struct { // Note that deploymentID is used in NewWorkflowStore(), but we don't care about that in adhoc mode so can leave it blank } -func ConstructAdhocExecParamsWithRootCfgBuilderAndRepoRetriever(ctx context.Context, repoName string, revision string, githubRetriever *adhocgithubhelpers.AdhocGithubRetriever, rootCfgBuilder *root_config.Builder) (AdhocTerraformWorkflowExecutionParams, error) { +func ConstructAdhocExecParamsWithRootCfgBuilderAndRepoRetriever( + ctx context.Context, + repoName string, + PRNum int, + pullFetcher *internal_gh.PRFetcher, + pullConverter converter.PullConverter, + installationRetriever *internal_gh.InstallationRetriever, + rootCfgBuilder *root_config.Builder) (AdhocTerraformWorkflowExecutionParams, error) { + + orgName := "lyft" + installationToken, err := installationRetriever.FindOrganizationInstallation(ctx, orgName) + if err != nil { + return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "finding organization installation") + } + // TODO: in the future, could potentially pass in the owner instead of hardcoding lyft - repo, err := githubRetriever.GetRepository(ctx, "lyft", repoName) + ghCommit, err := pullFetcher.Fetch(ctx, installationToken.Token, orgName, repoName, PRNum) if err != nil { - return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "getting repo") + return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "fetching commit") + } + + actualCommit, err := pullConverter.Convert(ghCommit) + if err != nil { + return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "converting commit") } opts := config.BuilderOptions{ @@ -35,20 +53,11 @@ func ConstructAdhocExecParamsWithRootCfgBuilderAndRepoRetriever(ctx context.Cont } rootCfgs, err := rootCfgBuilder.Build(ctx, &root_config.RepoCommit{ - Repo: models.Repo{ - FullName: repo.GetFullName(), - Owner: repo.Owner, - Name: repoName, - CloneURL: repo.URL, - VCSHost: models.VCSHost{ - Hostname: "github.com", - Type: models.Github, - }, - DefaultBranch: repo.DefaultBranch, - }, - Branch: repo.DefaultBranch, - Sha: revision, - }, repo.Credentials.InstallationToken, opts) + Repo: actualCommit.HeadRepo, + Branch: actualCommit.HeadBranch, + Sha: actualCommit.HeadCommit, + OptionalPRNum: actualCommit.Num, + }, installationToken.Token, opts) if err != nil { return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "building root cfgs") } @@ -58,8 +67,14 @@ func ConstructAdhocExecParamsWithRootCfgBuilderAndRepoRetriever(ctx context.Cont rootCfgBuilder.Logger.Info("returning adhocexecution params") return AdhocTerraformWorkflowExecutionParams{ - Revision: revision, - GithubRepo: repo, + Revision: actualCommit.HeadCommit, + GithubRepo: github.Repo{ + Owner: orgName, + Name: repoName, + URL: actualCommit.HeadRepo.CloneURL, + DefaultBranch: actualCommit.HeadRepo.DefaultBranch, + Credentials: github.AppCredentials{InstallationToken: installationToken.Token}, + }, TerraformRoots: roots, }, nil } diff --git a/server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go b/server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go deleted file mode 100644 index fb0854c57..000000000 --- a/server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go +++ /dev/null @@ -1,50 +0,0 @@ -package adhocgithubhelpers - -import ( - "context" - "fmt" - - "github.com/pkg/errors" - "github.com/runatlantis/atlantis/server/models" - "github.com/runatlantis/atlantis/server/neptune/workflows/activities/github" - internal "github.com/runatlantis/atlantis/server/vcs/provider/github" -) - -type repoRetriever interface { - Get(ctx context.Context, installationToken int64, owner, repo string) (models.Repo, error) -} - -type installationRetriever interface { - FindOrganizationInstallation(ctx context.Context, org string) (internal.Installation, error) -} - -type AdhocGithubRetriever struct { - RepoRetriever repoRetriever - InstallationRetriever installationRetriever -} - -func (r *AdhocGithubRetriever) GetRepository(ctx context.Context, owner string, repoName string) (github.Repo, error) { - installation, err := r.InstallationRetriever.FindOrganizationInstallation(ctx, owner) - if err != nil { - return github.Repo{}, errors.Wrap(err, "finding installation") - } - - repo, err := r.RepoRetriever.Get(ctx, installation.Token, owner, repoName) - if err != nil { - return github.Repo{}, errors.Wrap(err, "getting repo") - } - - if len(repo.DefaultBranch) == 0 { - return github.Repo{}, fmt.Errorf("default branch was nil, this is a bug on github's side") - } - - return github.Repo{ - Owner: repo.Owner, - Name: repo.Name, - URL: repo.CloneURL, - DefaultBranch: repo.DefaultBranch, - Credentials: github.AppCredentials{ - InstallationToken: installation.Token, - }, - }, nil -} diff --git a/server/neptune/adhoc/config/config.go b/server/neptune/adhoc/config/config.go index ac01acff5..eb52a8293 100644 --- a/server/neptune/adhoc/config/config.go +++ b/server/neptune/adhoc/config/config.go @@ -27,6 +27,8 @@ type Config struct { GithubAppKeyFile string GithubAppSlug string GithubHostname string + GithubUser string + GithubToken string GlobalCfg valid.GlobalCfg } diff --git a/server/neptune/adhoc/server.go b/server/neptune/adhoc/server.go index 8e721206b..783eb9e6b 100644 --- a/server/neptune/adhoc/server.go +++ b/server/neptune/adhoc/server.go @@ -19,6 +19,7 @@ import ( ghClient "github.com/runatlantis/atlantis/server/neptune/workflows/activities/github" "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform" "github.com/runatlantis/atlantis/server/vcs/provider/github" + "github.com/runatlantis/atlantis/server/vcs/provider/github/converter" assetfs "github.com/elazarl/go-bindata-assetfs" "github.com/gorilla/mux" @@ -26,7 +27,6 @@ import ( "github.com/runatlantis/atlantis/server/logging" "github.com/runatlantis/atlantis/server/metrics" adhoc "github.com/runatlantis/atlantis/server/neptune/adhoc/adhocexecutionhelpers" - adhocGithubHelpers "github.com/runatlantis/atlantis/server/neptune/adhoc/adhocgithubhelpers" adhocconfig "github.com/runatlantis/atlantis/server/neptune/adhoc/config" root_config "github.com/runatlantis/atlantis/server/neptune/gateway/config" "github.com/runatlantis/atlantis/server/neptune/gateway/deploy" @@ -38,7 +38,6 @@ import ( "github.com/runatlantis/atlantis/server/neptune/workflows" "github.com/runatlantis/atlantis/server/neptune/workflows/activities" "github.com/runatlantis/atlantis/server/static" - github_converter "github.com/runatlantis/atlantis/server/vcs/provider/github/converter" "github.com/uber-go/tally/v4" "github.com/urfave/negroni" "go.temporal.io/sdk/client" @@ -47,22 +46,23 @@ import ( ) type Server struct { - Logger logging.Logger - CronScheduler *internalSync.CronScheduler - Crons []*internalSync.Cron - HTTPServerProxy *neptune_http.ServerProxy - Port int - StatsScope tally.Scope - StatsCloser io.Closer - TemporalClient *temporal.ClientWrapper - TerraformActivities *activities.Terraform - GithubActivities *activities.Github - TerraformTaskQueue string - RootConfigBuilder *root_config.Builder - GithubRetriever *adhocGithubHelpers.AdhocGithubRetriever - Repo string - Root string - Revision string + Logger logging.Logger + CronScheduler *internalSync.CronScheduler + Crons []*internalSync.Cron + HTTPServerProxy *neptune_http.ServerProxy + Port int + StatsScope tally.Scope + StatsCloser io.Closer + TemporalClient *temporal.ClientWrapper + TerraformActivities *activities.Terraform + GithubActivities *activities.Github + TerraformTaskQueue string + RootConfigBuilder *root_config.Builder + Repo string + PRNum int + InstallationRetriever *github.InstallationRetriever + PullFetcher *github.PRFetcher + PullConverter converter.PullConverter } func NewServer(config *adhocconfig.Config) (*Server, error) { @@ -183,17 +183,19 @@ func NewServer(config *adhocconfig.Config) (*Server, error) { Scope: scope.SubScope("event.filters.root"), } - repoConverter := github_converter.RepoConverter{} - repoRetriever := &github.RepoRetriever{ + pullFetcher := &github.PRFetcher{ ClientCreator: clientCreator, - RepoConverter: repoConverter, } - // This exists to convert a repo name to a repo object - githubRetriever := &adhocGithubHelpers.AdhocGithubRetriever{ - RepoRetriever: repoRetriever, - InstallationRetriever: installationFetcher, + repoConverter := converter.RepoConverter{ + GithubUser: config.GithubUser, + GithubToken: config.GithubToken, } + + pullConverter := converter.PullConverter{ + RepoConverter: repoConverter, + } + config.CtxLogger.Info(fmt.Sprintf("Starting adhoc server, params are: repo: %s, root: %s, revision: %s", config.GlobalCfg.AdhocMode.Repo, config.GlobalCfg.AdhocMode.Root, config.GlobalCfg.AdhocMode.Revision)) server := Server{ @@ -205,18 +207,20 @@ func NewServer(config *adhocconfig.Config) (*Server, error) { Frequency: 1 * time.Minute, }, }, - HTTPServerProxy: httpServerProxy, - Port: config.ServerCfg.Port, - StatsScope: scope, - StatsCloser: statsCloser, - TemporalClient: temporalClient, - TerraformActivities: terraformActivities, - TerraformTaskQueue: config.TemporalCfg.TerraformTaskQueue, - GithubActivities: githubActivities, - RootConfigBuilder: rootConfigBuilder, - GithubRetriever: githubRetriever, - Repo: config.GlobalCfg.AdhocMode.Repo, - Revision: config.GlobalCfg.AdhocMode.Revision, + HTTPServerProxy: httpServerProxy, + Port: config.ServerCfg.Port, + StatsScope: scope, + StatsCloser: statsCloser, + TemporalClient: temporalClient, + TerraformActivities: terraformActivities, + TerraformTaskQueue: config.TemporalCfg.TerraformTaskQueue, + GithubActivities: githubActivities, + RootConfigBuilder: rootConfigBuilder, + Repo: config.GlobalCfg.AdhocMode.Repo, + PRNum: config.GlobalCfg.AdhocMode.PRNum, + InstallationRetriever: installationFetcher, + PullFetcher: pullFetcher, + PullConverter: pullConverter, } return &server, nil } @@ -275,7 +279,7 @@ func (s Server) Start() error { go func() { defer wg.Done() - adhocExecutionParams, err := adhoc.ConstructAdhocExecParamsWithRootCfgBuilderAndRepoRetriever(ctx, s.Repo, s.Revision, s.GithubRetriever, s.RootConfigBuilder) + adhocExecutionParams, err := adhoc.ConstructAdhocExecParamsWithRootCfgBuilderAndRepoRetriever(ctx, s.Repo, s.PRNum, s.PullFetcher, s.PullConverter, s.InstallationRetriever, s.RootConfigBuilder) if err != nil { s.Logger.Error(err.Error()) return