From 3a231b90e640bc9e680e196d74426b1028da4528 Mon Sep 17 00:00:00 2001 From: Shawna Monero <66325812+smonero@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:27:40 -0700 Subject: [PATCH] [DEVCON-6918] Helper funcs for getting the github repo (#743) --- cmd/adhoc.go | 8 ++- .../adhoc_execution_params.go | 25 ++++++++-- .../adhoc_github_helpers.go | 50 +++++++++++++++++++ server/neptune/adhoc/server.go | 16 ++++++ 4 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go diff --git a/cmd/adhoc.go b/cmd/adhoc.go index 4a90d8499..cd22bb6e7 100644 --- a/cmd/adhoc.go +++ b/cmd/adhoc.go @@ -66,11 +66,9 @@ func (a *Adhoc) NewServer(userConfig legacy.UserConfig, config legacy.Config) (S StatsNamespace: userConfig.StatsNamespace, Metrics: globalCfg.Metrics, AdhocExecutionParams: adhocHelpers.AdhocTerraformWorkflowExecutionParams{ - AtlantisRoot: globalCfg.AdhocMode.Root, - AtlantisRepo: globalCfg.AdhocMode.Repo, - Revision: "", - TerraformRoot: terraform.Root{}, - GithubRepo: github.Repo{}, + Revision: "", + TerraformRoots: []terraform.Root{}, + GithubRepo: github.Repo{}, }, GithubHostname: userConfig.GithubHostname, GithubAppID: userConfig.GithubAppID, diff --git a/server/neptune/adhoc/adhocexecutionhelpers/adhoc_execution_params.go b/server/neptune/adhoc/adhocexecutionhelpers/adhoc_execution_params.go index c21423ebf..bbdb19b1a 100644 --- a/server/neptune/adhoc/adhocexecutionhelpers/adhoc_execution_params.go +++ b/server/neptune/adhoc/adhocexecutionhelpers/adhoc_execution_params.go @@ -1,15 +1,30 @@ package adhoc import ( + "context" + + "github.com/pkg/errors" + "github.com/runatlantis/atlantis/server/neptune/adhoc/adhocgithubhelpers" "github.com/runatlantis/atlantis/server/neptune/workflows/activities/github" "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform" ) type AdhocTerraformWorkflowExecutionParams struct { - AtlantisRoot string - AtlantisRepo string - Revision string - TerraformRoot terraform.Root - GithubRepo github.Repo + Revision string + TerraformRoots []terraform.Root + GithubRepo github.Repo // 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) (AdhocTerraformWorkflowExecutionParams, error) { + // TODO: in the future, could potentially pass in the owner instead of hardcoding lyft + repo, err := githubRetriever.GetRepository(ctx, "lyft", repoName) + if err != nil { + return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "getting repo") + } + + return AdhocTerraformWorkflowExecutionParams{ + Revision: revision, + GithubRepo: repo, + }, nil +} diff --git a/server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go b/server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go new file mode 100644 index 000000000..fb0854c57 --- /dev/null +++ b/server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go @@ -0,0 +1,50 @@ +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/server.go b/server/neptune/adhoc/server.go index 7c78f31c5..ed3d21529 100644 --- a/server/neptune/adhoc/server.go +++ b/server/neptune/adhoc/server.go @@ -26,6 +26,7 @@ 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" @@ -37,6 +38,7 @@ 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/interceptor" @@ -57,6 +59,7 @@ type Server struct { AdhocExecutionParams adhoc.AdhocTerraformWorkflowExecutionParams TerraformTaskQueue string RootConfigBuilder *root_config.Builder + GithubRetriever *adhocGithubHelpers.AdhocGithubRetriever } func NewServer(config *adhocconfig.Config) (*Server, error) { @@ -196,6 +199,18 @@ func NewServer(config *adhocconfig.Config) (*Server, error) { Scope: scope.SubScope("event.filters.root"), } + repoConverter := github_converter.RepoConverter{} + repoRetriever := &github.RepoRetriever{ + ClientCreator: clientCreator, + RepoConverter: repoConverter, + } + + // This exists to convert a repo name to a repo object + githubRetriever := &adhocGithubHelpers.AdhocGithubRetriever{ + RepoRetriever: repoRetriever, + InstallationRetriever: installationFetcher, + } + server := Server{ Logger: config.CtxLogger, CronScheduler: cronScheduler, @@ -215,6 +230,7 @@ func NewServer(config *adhocconfig.Config) (*Server, error) { GithubActivities: githubActivities, AdhocExecutionParams: config.AdhocExecutionParams, RootConfigBuilder: rootConfigBuilder, + GithubRetriever: githubRetriever, } return &server, nil }