Skip to content

Commit

Permalink
[DEVCON-6918] Helper funcs for getting the github repo (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
smonero authored Apr 5, 2024
1 parent db9a213 commit 3a231b9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
8 changes: 3 additions & 5 deletions cmd/adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
50 changes: 50 additions & 0 deletions server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions server/neptune/adhoc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -215,6 +230,7 @@ func NewServer(config *adhocconfig.Config) (*Server, error) {
GithubActivities: githubActivities,
AdhocExecutionParams: config.AdhocExecutionParams,
RootConfigBuilder: rootConfigBuilder,
GithubRetriever: githubRetriever,
}
return &server, nil
}
Expand Down

0 comments on commit 3a231b9

Please sign in to comment.