Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Samra Belachew committed Jul 14, 2023
1 parent 75739b7 commit 5a43e4b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 80 deletions.
1 change: 1 addition & 0 deletions server/neptune/temporalworker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func NewServer(config *config.Config) (*Server, error) {

githubActivities, err := activities.NewGithub(
config.App,
0, // TODO, pass config here
scope.SubScope("app"),
config.DataDir,
featureAllocator,
Expand Down
45 changes: 20 additions & 25 deletions server/neptune/workflows/activities/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import (
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
)

type ClientContext struct {
InstallationToken int64
context.Context
}

var HashiGetter = func(ctx context.Context, dst, src string) error {
return getter.Get(dst, src, getter.WithContext(ctx))
}
Expand All @@ -34,16 +29,16 @@ var HashiGetter = func(ctx context.Context, dst, src string) error {
type gogetter func(ctx context.Context, dst, src string) error

type githubClient interface { //nolint:interfacebloat
CreateCheckRun(ctx internal.Context, owner, repo string, opts github.CreateCheckRunOptions) (*github.CheckRun, *github.Response, error)
UpdateCheckRun(ctx internal.Context, owner, repo string, checkRunID int64, opts github.UpdateCheckRunOptions) (*github.CheckRun, *github.Response, error)
GetArchiveLink(ctx internal.Context, owner, repo string, archiveformat github.ArchiveFormat, opts *github.RepositoryContentGetOptions, followRedirects bool) (*url.URL, *github.Response, error)
CompareCommits(ctx internal.Context, owner, repo string, base, head string, opts *github.ListOptions) (*github.CommitsComparison, *github.Response, error)
ListReviews(ctx internal.Context, owner string, repo string, number int) ([]*github.PullRequestReview, error)
GetPullRequest(ctx internal.Context, owner, repo string, number int) (*github.PullRequest, *github.Response, error)
ListCommits(ctx internal.Context, owner string, repo string, number int) ([]*github.RepositoryCommit, error)
DismissReview(ctx internal.Context, owner, repo string, number int, reviewID int64, review *github.PullRequestReviewDismissalRequest) (*github.PullRequestReview, *github.Response, error)
ListTeamMembers(ctx internal.Context, org string, teamSlug string) ([]*github.User, error)
CreateComment(ctx internal.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
CreateCheckRun(ctx context.Context, owner, repo string, opts github.CreateCheckRunOptions) (*github.CheckRun, *github.Response, error)
UpdateCheckRun(ctx context.Context, owner, repo string, checkRunID int64, opts github.UpdateCheckRunOptions) (*github.CheckRun, *github.Response, error)
GetArchiveLink(ctx context.Context, owner, repo string, archiveformat github.ArchiveFormat, opts *github.RepositoryContentGetOptions, followRedirects bool) (*url.URL, *github.Response, error)
CompareCommits(ctx context.Context, owner, repo string, base, head string, opts *github.ListOptions) (*github.CommitsComparison, *github.Response, error)
ListReviews(ctx context.Context, owner string, repo string, number int) ([]*github.PullRequestReview, error)
GetPullRequest(ctx context.Context, owner, repo string, number int) (*github.PullRequest, *github.Response, error)
ListCommits(ctx context.Context, owner string, repo string, number int) ([]*github.RepositoryCommit, error)
DismissReview(ctx context.Context, owner, repo string, number int, reviewID int64, review *github.PullRequestReviewDismissalRequest) (*github.PullRequestReview, *github.Response, error)
ListTeamMembers(ctx context.Context, org string, teamSlug string) ([]*github.User, error)
CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
}

type DiffDirection string
Expand Down Expand Up @@ -142,7 +137,7 @@ func (a *githubActivities) GithubUpdateCheckRun(ctx context.Context, request Upd
}

run, _, err := a.Client.UpdateCheckRun(
internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken),
ctx,
request.Repo.Owner, request.Repo.Name, request.ID, opts,
)

Expand Down Expand Up @@ -200,7 +195,7 @@ func (a *githubActivities) GithubCreateCheckRun(ctx context.Context, request Cre
}

run, _, err := a.Client.CreateCheckRun(
internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken),
ctx,
request.Repo.Owner, request.Repo.Name, opts,
)

Expand Down Expand Up @@ -275,7 +270,7 @@ func (a *githubActivities) GithubFetchRoot(ctx context.Context, request FetchRoo
Ref: request.Revision,
}
// note: this link exists for 5 minutes when fetching a private repository archive
archiveLink, resp, err := a.Client.GetArchiveLink(internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken), request.Repo.Owner, request.Repo.Name, github.Zipball, opts, true)
archiveLink, resp, err := a.Client.GetArchiveLink(ctx, request.Repo.Owner, request.Repo.Name, github.Zipball, opts, true)
if err != nil {
return FetchRootResponse{}, errors.Wrap(err, "getting repo archive link")
}
Expand Down Expand Up @@ -315,7 +310,7 @@ type CompareCommitResponse struct {
}

func (a *githubActivities) GithubCompareCommit(ctx context.Context, request CompareCommitRequest) (CompareCommitResponse, error) {
comparison, resp, err := a.Client.CompareCommits(internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken), request.Repo.Owner, request.Repo.Name, request.LatestDeployedRevision, request.DeployRequestRevision, &github.ListOptions{})
comparison, resp, err := a.Client.CompareCommits(ctx, request.Repo.Owner, request.Repo.Name, request.LatestDeployedRevision, request.DeployRequestRevision, &github.ListOptions{})

if err != nil {
return CompareCommitResponse{}, errors.Wrap(err, "comparing commits")
Expand All @@ -341,7 +336,7 @@ type GetPullRequestStateResponse struct {

func (a *githubActivities) GithubGetPullRequestState(ctx context.Context, request GetPullRequestStateRequest) (GetPullRequestStateResponse, error) {
resp, _, err := a.Client.GetPullRequest(
internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken),
ctx,
request.Repo.Owner,
request.Repo.Name,
request.PRNumber,
Expand All @@ -365,7 +360,7 @@ type ListPRReviewsResponse struct {

func (a *githubActivities) GithubListPRReviews(ctx context.Context, request ListPRReviewsRequest) (ListPRReviewsResponse, error) {
reviews, err := a.Client.ListReviews(
internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken),
ctx,
request.Repo.Owner,
request.Repo.Name,
request.PRNumber,
Expand All @@ -389,7 +384,7 @@ type ListPRCommitsResponse struct {

func (a *githubActivities) GithubListPRCommits(ctx context.Context, request ListPRCommitsRequest) (ListPRCommitsResponse, error) {
commits, err := a.Client.ListCommits(
internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken),
ctx,
request.Repo.Owner,
request.Repo.Name,
request.PRNumber,
Expand Down Expand Up @@ -426,7 +421,7 @@ func (a *githubActivities) GithubDismiss(ctx context.Context, request DismissReq
Message: github.String(request.DismissReason),
}
_, _, err = a.Client.DismissReview(
internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken),
ctx,
request.Repo.Owner,
request.Repo.Name,
request.PRNumber,
Expand All @@ -451,7 +446,7 @@ type ListTeamMembersResponse struct {

func (a *githubActivities) GithubListTeamMembers(ctx context.Context, request ListTeamMembersRequest) (ListTeamMembersResponse, error) {
users, err := a.Client.ListTeamMembers(
internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken),
ctx,
request.Org,
request.TeamSlug,
)
Expand Down Expand Up @@ -480,7 +475,7 @@ func (a *githubActivities) GithubCreateComment(ctx context.Context, request Crea
Body: github.String(request.CommentBody),
}
_, _, err := a.Client.CreateComment(
internal.ContextWithInstallationToken(ctx, request.Repo.Credentials.InstallationToken),
ctx,
request.Repo.Owner,
request.Repo.Name,
request.PRNumber,
Expand Down
64 changes: 22 additions & 42 deletions server/neptune/workflows/activities/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,30 @@ import (
)

type Client struct {
ClientCreator githubapp.ClientCreator
ClientCreator githubapp.ClientCreator
InstallationID int64
}

type Context interface {
GetInstallationToken() int64
context.Context
}

type contextWithToken struct {
InstallationToken int64
context.Context
}

func (c *contextWithToken) GetInstallationToken() int64 {
return c.InstallationToken
}

func ContextWithInstallationToken(ctx context.Context, installationToken int64) Context {
return &contextWithToken{
InstallationToken: installationToken,
Context: ctx,
}
}

func (c *Client) CreateCheckRun(ctx Context, owner, repo string, opts github.CreateCheckRunOptions) (*github.CheckRun, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) CreateCheckRun(ctx context.Context, owner, repo string, opts github.CreateCheckRunOptions) (*github.CheckRun, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)

if err != nil {
return nil, nil, errors.Wrap(err, "creating client from installation")
}

return client.Checks.CreateCheckRun(ctx, owner, repo, opts)
}
func (c *Client) UpdateCheckRun(ctx Context, owner, repo string, checkRunID int64, opts github.UpdateCheckRunOptions) (*github.CheckRun, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) UpdateCheckRun(ctx context.Context, owner, repo string, checkRunID int64, opts github.UpdateCheckRunOptions) (*github.CheckRun, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)

if err != nil {
return nil, nil, errors.Wrap(err, "creating client from installation")
}

return client.Checks.UpdateCheckRun(ctx, owner, repo, checkRunID, opts)
}
func (c *Client) GetArchiveLink(ctx Context, owner, repo string, archiveformat github.ArchiveFormat, opts *github.RepositoryContentGetOptions, followRedirects bool) (*url.URL, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) GetArchiveLink(ctx context.Context, owner, repo string, archiveformat github.ArchiveFormat, opts *github.RepositoryContentGetOptions, followRedirects bool) (*url.URL, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)

if err != nil {
return nil, nil, errors.Wrap(err, "creating client from installation")
Expand All @@ -63,8 +43,8 @@ func (c *Client) GetArchiveLink(ctx Context, owner, repo string, archiveformat g
return client.Repositories.GetArchiveLink(ctx, owner, repo, archiveformat, opts, followRedirects)
}

func (c *Client) CompareCommits(ctx Context, owner, repo string, base, head string, opts *github.ListOptions) (*github.CommitsComparison, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) CompareCommits(ctx context.Context, owner, repo string, base, head string, opts *github.ListOptions) (*github.CommitsComparison, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)

if err != nil {
return nil, nil, errors.Wrap(err, "creating client from installation")
Expand All @@ -73,8 +53,8 @@ func (c *Client) CompareCommits(ctx Context, owner, repo string, base, head stri
return client.Repositories.CompareCommits(ctx, owner, repo, base, head, opts)
}

func (c *Client) ListReviews(ctx Context, owner string, repo string, number int) ([]*github.PullRequestReview, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) ListReviews(ctx context.Context, owner string, repo string, number int) ([]*github.PullRequestReview, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)
if err != nil {
return nil, errors.Wrap(err, "creating client from installation")
}
Expand All @@ -89,16 +69,16 @@ func (c *Client) ListReviews(ctx Context, owner string, repo string, number int)
return gh_helper.Iterate(ctx, run)
}

func (c *Client) GetPullRequest(ctx Context, owner, repo string, number int) (*github.PullRequest, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) GetPullRequest(ctx context.Context, owner, repo string, number int) (*github.PullRequest, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)
if err != nil {
return nil, nil, errors.Wrap(err, "creating client from installation")
}
return client.PullRequests.Get(ctx, owner, repo, number)
}

func (c *Client) ListCommits(ctx Context, owner string, repo string, number int) ([]*github.RepositoryCommit, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) ListCommits(ctx context.Context, owner string, repo string, number int) ([]*github.RepositoryCommit, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)
if err != nil {
return nil, errors.Wrap(err, "creating client from installation")
}
Expand All @@ -113,16 +93,16 @@ func (c *Client) ListCommits(ctx Context, owner string, repo string, number int)
return gh_helper.Iterate(ctx, run)
}

func (c *Client) DismissReview(ctx Context, owner string, repo string, number int, reviewID int64, review *github.PullRequestReviewDismissalRequest) (*github.PullRequestReview, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) DismissReview(ctx context.Context, owner string, repo string, number int, reviewID int64, review *github.PullRequestReviewDismissalRequest) (*github.PullRequestReview, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)
if err != nil {
return nil, nil, errors.Wrap(err, "creating client from installation")
}
return client.PullRequests.DismissReview(ctx, owner, repo, number, reviewID, review)
}

func (c *Client) ListTeamMembers(ctx Context, org string, teamSlug string) ([]*github.User, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) ListTeamMembers(ctx context.Context, org string, teamSlug string) ([]*github.User, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)
if err != nil {
return nil, errors.Wrap(err, "creating client from installation")
}
Expand All @@ -139,8 +119,8 @@ func (c *Client) ListTeamMembers(ctx Context, org string, teamSlug string) ([]*g
return gh_helper.Iterate(ctx, run)
}

func (c *Client) CreateComment(ctx Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(ctx.GetInstallationToken())
func (c *Client) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
client, err := c.ClientCreator.NewInstallationClient(c.InstallationID)
if err != nil {
return nil, nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions server/neptune/workflows/activities/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func NewGithubWithClient(client githubClient, dataDir string, getter gogetter, a
}, nil
}

func NewGithub(appConfig githubapp.Config, scope tally.Scope, dataDir string, allocator feature.Allocator) (*Github, error) {
func NewGithub(appConfig githubapp.Config, installationID int64, scope tally.Scope, dataDir string, allocator feature.Allocator) (*Github, error) {
clientCreator, err := githubapp.NewDefaultCachingClientCreator(
appConfig,
githubapp.WithClientMiddleware(
Expand All @@ -233,7 +233,8 @@ func NewGithub(appConfig githubapp.Config, scope tally.Scope, dataDir string, al
}

client := &internal.Client{
ClientCreator: clientCreator,
ClientCreator: clientCreator,
InstallationID: installationID,
}

return NewGithubWithClient(client, dataDir, HashiGetter, allocator)
Expand Down
Loading

0 comments on commit 5a43e4b

Please sign in to comment.