forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: make e2e code generic to vcs (runatlantis#4732)
- Loading branch information
Showing
4 changed files
with
190 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,148 @@ | |
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/google/go-github/v59/github" | ||
) | ||
|
||
type GithubClient struct { | ||
client *github.Client | ||
username string | ||
client *github.Client | ||
username string | ||
ownerName string | ||
repoName string | ||
token string | ||
} | ||
|
||
func NewGithubClient() *GithubClient { | ||
|
||
githubUsername := os.Getenv("ATLANTISBOT_GITHUB_USERNAME") | ||
if githubUsername == "" { | ||
log.Fatalf("ATLANTISBOT_GITHUB_USERNAME cannot be empty") | ||
} | ||
githubToken := os.Getenv("ATLANTISBOT_GITHUB_TOKEN") | ||
if githubToken == "" { | ||
log.Fatalf("ATLANTISBOT_GITHUB_TOKEN cannot be empty") | ||
} | ||
ownerName := os.Getenv("GITHUB_REPO_OWNER_NAME") | ||
if ownerName == "" { | ||
ownerName = "runatlantis" | ||
} | ||
repoName := os.Getenv("GITHUB_REPO_NAME") | ||
if repoName == "" { | ||
repoName = "atlantis-tests" | ||
} | ||
|
||
// create github client | ||
tp := github.BasicAuthTransport{ | ||
Username: strings.TrimSpace(githubUsername), | ||
Password: strings.TrimSpace(githubToken), | ||
} | ||
ghClient := github.NewClient(tp.Client()) | ||
|
||
return &GithubClient{ | ||
client: ghClient, | ||
username: githubUsername, | ||
ownerName: ownerName, | ||
repoName: repoName, | ||
token: githubToken, | ||
} | ||
|
||
} | ||
|
||
func (g GithubClient) Clone(cloneDir string) error { | ||
|
||
repoURL := fmt.Sprintf("https://%s:%[email protected]/%s/%s.git", g.username, g.token, g.ownerName, g.repoName) | ||
cloneCmd := exec.Command("git", "clone", repoURL, cloneDir) | ||
// git clone the repo | ||
log.Printf("git cloning into %q", cloneDir) | ||
if output, err := cloneCmd.CombinedOutput(); err != nil { | ||
return fmt.Errorf("failed to clone repository: %v: %s", err, string(output)) | ||
} | ||
return nil | ||
} | ||
|
||
func (g GithubClient) CreateAtlantisWebhook(ctx context.Context, hookURL string) (int64, error) { | ||
// create atlantis hook | ||
atlantisHook := &github.Hook{ | ||
Events: []string{"issue_comment", "pull_request", "push"}, | ||
Config: map[string]interface{}{ | ||
"url": hookURL, | ||
"content_type": "json", | ||
}, | ||
Active: github.Bool(true), | ||
} | ||
|
||
hook, _, err := g.client.Repositories.CreateHook(ctx, g.ownerName, g.repoName, atlantisHook) | ||
if err != nil { | ||
return 0, err | ||
} | ||
log.Println(hook.GetURL()) | ||
|
||
return hook.GetID(), nil | ||
} | ||
|
||
func (g GithubClient) DeleteAtlantisHook(ctx context.Context, hookID int64) error { | ||
_, err := g.client.Repositories.DeleteHook(ctx, g.ownerName, g.repoName, hookID) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("deleted webhook id %d", hookID) | ||
|
||
return nil | ||
} | ||
|
||
func (g GithubClient) CreatePullRequest(ctx context.Context, title, branchName string) (string, int, error) { | ||
head := fmt.Sprintf("%s:%s", g.ownerName, branchName) | ||
body := "" | ||
base := "main" | ||
newPullRequest := &github.NewPullRequest{Title: &title, Head: &head, Body: &body, Base: &base} | ||
|
||
pull, _, err := g.client.PullRequests.Create(ctx, g.ownerName, g.repoName, newPullRequest) | ||
if err != nil { | ||
return "", 0, fmt.Errorf("error while creating new pull request: %v", err) | ||
} | ||
|
||
// set pull request url | ||
return pull.GetHTMLURL(), pull.GetNumber(), nil | ||
|
||
} | ||
|
||
func (g GithubClient) GetAtlantisStatus(ctx context.Context, branchName string) (string, error) { | ||
// check repo status | ||
combinedStatus, _, err := g.client.Repositories.GetCombinedStatus(ctx, g.ownerName, g.repoName, branchName, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, status := range combinedStatus.Statuses { | ||
if status.GetContext() == "atlantis/plan" { | ||
return status.GetState(), nil | ||
} | ||
} | ||
|
||
return "", nil | ||
} | ||
|
||
func (g GithubClient) ClosePullRequest(ctx context.Context, pullRequestNumber int) error { | ||
// clean up | ||
_, _, err := g.client.PullRequests.Edit(ctx, g.ownerName, g.repoName, pullRequestNumber, &github.PullRequest{State: github.String("closed")}) | ||
if err != nil { | ||
return fmt.Errorf("error while closing new pull request: %v", err) | ||
} | ||
return nil | ||
|
||
} | ||
func (g GithubClient) DeleteBranch(ctx context.Context, branchName string) error { | ||
|
||
_, err := g.client.Git.DeleteRef(ctx, g.ownerName, g.repoName, branchName) | ||
if err != nil { | ||
return fmt.Errorf("error while deleting branch %s: %v", branchName, err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.