Skip to content

Commit 363bcaa

Browse files
committed
Adding code
1 parent c294e3f commit 363bcaa

File tree

5 files changed

+90
-60
lines changed

5 files changed

+90
-60
lines changed

.github/workflows/test.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,40 @@ jobs:
139139
- run: |
140140
make build-service
141141
./scripts/e2e.sh
142+
e2e-gitlab:
143+
runs-on: ubuntu-latest
144+
# dont run e2e tests on forked PRs
145+
if: github.event.pull_request.head.repo.fork == false
146+
env:
147+
TERRAFORM_VERSION: 1.9.2
148+
ATLANTIS_GITLAB_USER: ${{ secrets.ATLANTISBOT_GITLAB_USERNAME }}
149+
ATLANTIS_GITLAB_TOKEN: ${{ secrets.ATLANTISBOT_GITLAB_TOKEN }}
150+
NGROK_AUTH_TOKEN: ${{ secrets.ATLANTISBOT_NGROK_AUTH_TOKEN }}
151+
steps:
152+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
153+
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5
154+
with:
155+
go-version-file: go.mod
156+
157+
# This version of TF will be downloaded before Atlantis is started.
158+
# We do this instead of setting --default-tf-version because setting
159+
# that flag starts the download asynchronously so we'd have a race
160+
# condition.
161+
- uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8 # v3
162+
with:
163+
terraform_version: ${{ env.TERRAFORM_VERSION }}
164+
165+
- name: Setup ngrok
166+
run: |
167+
wget -q -O ngrok.tar.gz https://bin.equinox.io/a/4no1PS1PoRF/ngrok-v3-3.13.0-linux-amd64.tar.gz
168+
tar -xzf ngrok.tar.gz
169+
chmod +x ngrok
170+
./ngrok version
171+
- name: Setup gitconfig
172+
run: |
173+
git config --global user.email "[email protected]"
174+
git config --global user.name "atlantisbot"
175+
176+
- run: |
177+
make build-service
178+
./scripts/e2e.sh

e2e/e2e.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,28 @@ func (t *E2ETester) Start(ctx context.Context) (*E2EResult, error) {
147147
}
148148

149149
func checkStatus(state string) bool {
150-
for _, s := range []string{"success", "error", "failure"} {
150+
for _, s := range []string{"success", "error", "failure", "failed"} {
151151
if state == s {
152152
return false
153153
}
154154
}
155155
return true
156156
}
157157

158-
func cleanUp(ctx context.Context, t *E2ETester, pullRequestNumber int, branchName string) error {
158+
func cleanUp(ctx context.Context, t *E2ETester, pullRequestNumber int, branchName string) {
159159
// clean up
160160
err := t.vcsClient.ClosePullRequest(ctx, pullRequestNumber)
161161
if err != nil {
162-
return err
162+
log.Printf("Failed to close PR %d: %v", pullRequestNumber, err)
163+
return
163164
}
164165
log.Printf("closed pull request %d", pullRequestNumber)
165166

166-
deleteBranchName := fmt.Sprintf("%s/%s", "heads", branchName)
167-
err = t.vcsClient.DeleteBranch(ctx, deleteBranchName)
167+
err = t.vcsClient.DeleteBranch(ctx, branchName)
168168
if err != nil {
169-
return fmt.Errorf("error while deleting branch %s: %v", deleteBranchName, err)
169+
log.Printf("Failed to delete branch %s: %v", branchName, err)
170+
return
170171
}
171-
log.Printf("deleted branch %s", deleteBranchName)
172+
log.Printf("deleted branch %s", branchName)
172173

173-
return nil
174174
}

e2e/github.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ func (g GithubClient) ClosePullRequest(ctx context.Context, pullRequestNumber in
153153
}
154154
func (g GithubClient) DeleteBranch(ctx context.Context, branchName string) error {
155155

156-
_, err := g.client.Git.DeleteRef(ctx, g.ownerName, g.repoName, branchName)
156+
deleteBranchName := fmt.Sprintf("%s/%s", "heads", branchName)
157+
158+
_, err := g.client.Git.DeleteRef(ctx, g.ownerName, g.repoName, deleteBranchName)
157159
if err != nil {
158160
return fmt.Errorf("error while deleting branch %s: %v", branchName, err)
159161
}

e2e/gitlab.go

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import (
2424
)
2525

2626
type GitlabClient struct {
27-
client *gitlab.Client
28-
username string
29-
ownerName string
30-
repoName string
31-
token string
32-
projectId int
27+
client *gitlab.Client
28+
username string
29+
ownerName string
30+
repoName string
31+
token string
32+
projectId int
33+
branchToMR map[string]int
3334
}
3435

3536
func NewGitlabClient() *GitlabClient {
@@ -61,12 +62,13 @@ func NewGitlabClient() *GitlabClient {
6162
}
6263

6364
return &GitlabClient{
64-
client: gitlabClient,
65-
username: gitlabUsername,
66-
ownerName: ownerName,
67-
repoName: repoName,
68-
token: gitlabToken,
69-
projectId: project.ID,
65+
client: gitlabClient,
66+
username: gitlabUsername,
67+
ownerName: ownerName,
68+
repoName: repoName,
69+
token: gitlabToken,
70+
projectId: project.ID,
71+
branchToMR: make(map[string]int),
7072
}
7173

7274
}
@@ -107,53 +109,44 @@ func (g GitlabClient) DeleteAtlantisHook(ctx context.Context, hookID int64) erro
107109
return nil
108110
}
109111

110-
func (g GitlabClient) CreatePullRequest(ctx context.Context, title, branchName string) (string, PullRequest, error) {
112+
func (g GitlabClient) CreatePullRequest(ctx context.Context, title, branchName string) (string, int, error) {
113+
// log.Printf("Sleeping 10 seconds to prevent race conditions creating MR")
114+
// time.Sleep(time.Second * 10)
111115
mr, _, err := g.client.MergeRequests.CreateMergeRequest(g.projectId, &gitlab.CreateMergeRequestOptions{
112116
Title: gitlab.Ptr(title),
113117
SourceBranch: gitlab.Ptr(branchName),
114118
TargetBranch: gitlab.Ptr("main"),
115119
})
116120
if err != nil {
117-
return "", PullRequest{}, fmt.Errorf("error while creating new pull request: %v", err)
121+
return "", 0, fmt.Errorf("error while creating new pull request: %v", err)
118122
}
119-
return mr.WebURL, PullRequest{
120-
id: mr.IID,
121-
branch: branchName,
122-
}, nil
123+
g.branchToMR[branchName] = mr.IID
124+
return mr.WebURL, mr.IID, nil
123125

124126
}
125127

126-
func (g GitlabClient) GetAtlantisStatus(ctx context.Context, pullRequest PullRequest) (string, error) {
128+
func (g GitlabClient) GetAtlantisStatus(ctx context.Context, branchName string) (string, error) {
127129

128-
fmt.Println(pullRequest)
129-
res, _, err := g.client.MergeRequests.ListMergeRequestPipelines(g.projectId, pullRequest.id)
130+
pipelineInfos, _, err := g.client.MergeRequests.ListMergeRequestPipelines(g.projectId, g.branchToMR[branchName])
131+
if err != nil {
132+
return "", err
133+
}
134+
// Possible todo: determine which status in the pipeline we care about?
135+
if len(pipelineInfos) != 1 {
136+
return "", fmt.Errorf("unexpected pipelines: %d", len(pipelineInfos))
137+
}
138+
pipelineInfo := pipelineInfos[0]
139+
pipeline, _, err := g.client.Pipelines.GetPipeline(g.projectId, pipelineInfo.ID)
130140
if err != nil {
131-
fmt.Println("I have error", err)
132141
return "", err
133142
}
134-
fmt.Println(res)
135-
return "", nil
136-
/*
137-
138-
// check repo status
139-
combinedStatus, _, err := g.client.Repositories.GetCombinedStatus(ctx, g.ownerName, g.repoName, branchName, nil)
140-
if err != nil {
141-
return "", err
142-
}
143-
144-
for _, status := range combinedStatus.Statuses {
145-
if status.GetContext() == "atlantis/plan" {
146-
return status.GetState(), nil
147-
}
148-
}
149143

150-
return "", nil
151-
*/
144+
return pipeline.Status, nil
152145
}
153146

154-
func (g GitlabClient) ClosePullRequest(ctx context.Context, pullRequest PullRequest) error {
147+
func (g GitlabClient) ClosePullRequest(ctx context.Context, pullRequestNumber int) error {
155148
// clean up
156-
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.projectId, pullRequest.id, &gitlab.UpdateMergeRequestOptions{
149+
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.projectId, pullRequestNumber, &gitlab.UpdateMergeRequestOptions{
157150
StateEvent: gitlab.Ptr("close"),
158151
})
159152
if err != nil {
@@ -163,13 +156,11 @@ func (g GitlabClient) ClosePullRequest(ctx context.Context, pullRequest PullRequ
163156

164157
}
165158
func (g GitlabClient) DeleteBranch(ctx context.Context, branchName string) error {
166-
panic("I'mdeleting branch")
167-
/*
168-
169-
_, err := g.client.Git.DeleteRef(ctx, g.ownerName, g.repoName, branchName)
170-
if err != nil {
171-
return fmt.Errorf("error while deleting branch %s: %v", branchName, err)
172-
}
173-
return nil
174-
*/
159+
_, err := g.client.Branches.DeleteBranch(g.projectId, branchName)
160+
161+
if err != nil {
162+
return fmt.Errorf("error while deleting branch %s: %v", branchName, err)
163+
}
164+
return nil
165+
175166
}

scripts/e2e.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ IFS=$'\n\t'
1111
--gitlab-token="$ATLANTISBOT_GITLAB_TOKEN" \
1212
--data-dir="/tmp" \
1313
--log-level="debug" \
14-
--repo-allowlist="github.com/runatlantis/atlantis-tests" \
14+
--repo-allowlist="github.com/runatlantis/atlantis-tests,gitlab.com/run-atlantis/atlantis-tests" \
1515
--repo-config-json='{"repos":[{"id":"/.*/", "allowed_overrides":["apply_requirements","workflow"], "allow_custom_workflows":true}]}' \
1616
&> /tmp/atlantis-server.log &
1717
sleep 2

0 commit comments

Comments
 (0)