Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemassa committed Aug 12, 2024
1 parent 2d1ee67 commit c294e3f
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 3 deletions.
175 changes: 175 additions & 0 deletions e2e/gitlab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2017 HootSuite Media Inc.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Modified hereafter by contributors to runatlantis/atlantis.

package main

import (
"context"
"fmt"
"log"
"os"
"os/exec"

"github.com/xanzy/go-gitlab"
)

type GitlabClient struct {
client *gitlab.Client
username string
ownerName string
repoName string
token string
projectId int
}

func NewGitlabClient() *GitlabClient {

gitlabUsername := os.Getenv("ATLANTISBOT_GITLAB_USERNAME")
if gitlabUsername == "" {
log.Fatalf("ATLANTISBOT_GITHUB_USERNAME cannot be empty")
}
gitlabToken := os.Getenv("ATLANTISBOT_GITLAB_TOKEN")
if gitlabToken == "" {
log.Fatalf("ATLANTISBOT_GITLAB_TOKEN cannot be empty")
}
ownerName := os.Getenv("GITHUB_REPO_OWNER_NAME")
if ownerName == "" {
ownerName = "run-atlantis"
}
repoName := os.Getenv("GITHUB_REPO_NAME")
if repoName == "" {
repoName = "atlantis-tests"
}

gitlabClient, err := gitlab.NewClient(gitlabToken)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
project, _, err := gitlabClient.Projects.GetProject(fmt.Sprintf("%s/%s", ownerName, repoName), &gitlab.GetProjectOptions{})
if err != nil {
log.Fatalf("Failed to find project: %v", err)
}

return &GitlabClient{
client: gitlabClient,
username: gitlabUsername,
ownerName: ownerName,
repoName: repoName,
token: gitlabToken,
projectId: project.ID,
}

}

func (g GitlabClient) 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 GitlabClient) CreateAtlantisWebhook(ctx context.Context, hookURL string) (int64, error) {
hook, _, err := g.client.Projects.AddProjectHook(g.projectId, &gitlab.AddProjectHookOptions{
URL: &hookURL,
IssuesEvents: gitlab.Ptr(true),
MergeRequestsEvents: gitlab.Ptr(true),
PushEvents: gitlab.Ptr(true),
})
if err != nil {
return 0, err
}
log.Printf("created webhook for %s", hook.URL)
return int64(hook.ID), err
}

func (g GitlabClient) DeleteAtlantisHook(ctx context.Context, hookID int64) error {
_, err := g.client.Projects.DeleteProjectHook(g.projectId, int(hookID))
if err != nil {
return err
}
log.Printf("deleted webhook id %d", hookID)
return nil
}

func (g GitlabClient) CreatePullRequest(ctx context.Context, title, branchName string) (string, PullRequest, error) {
mr, _, err := g.client.MergeRequests.CreateMergeRequest(g.projectId, &gitlab.CreateMergeRequestOptions{
Title: gitlab.Ptr(title),
SourceBranch: gitlab.Ptr(branchName),
TargetBranch: gitlab.Ptr("main"),
})
if err != nil {
return "", PullRequest{}, fmt.Errorf("error while creating new pull request: %v", err)
}
return mr.WebURL, PullRequest{
id: mr.IID,
branch: branchName,
}, nil

}

func (g GitlabClient) GetAtlantisStatus(ctx context.Context, pullRequest PullRequest) (string, error) {

fmt.Println(pullRequest)
res, _, err := g.client.MergeRequests.ListMergeRequestPipelines(g.projectId, pullRequest.id)
if err != nil {
fmt.Println("I have error", err)
return "", err
}
fmt.Println(res)
return "", nil
/*
// 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 GitlabClient) ClosePullRequest(ctx context.Context, pullRequest PullRequest) error {
// clean up
_, _, err := g.client.MergeRequests.UpdateMergeRequest(g.projectId, pullRequest.id, &gitlab.UpdateMergeRequestOptions{
StateEvent: gitlab.Ptr("close"),
})
if err != nil {
return fmt.Errorf("error while closing new pull request: %v", err)
}
return nil

}
func (g GitlabClient) DeleteBranch(ctx context.Context, branchName string) error {
panic("I'mdeleting branch")
/*
_, 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
*/
}
9 changes: 9 additions & 0 deletions e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ require (
)

require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/xanzy/go-gitlab v0.107.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.29.1 // indirect
)
29 changes: 29 additions & 0 deletions e2e/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v59 v59.0.0 h1:7h6bgpF5as0YQLLkEiVqpgtJqjimMYhBkD4jT5aN3VA=
Expand All @@ -8,6 +13,30 @@ github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/xanzy/go-gitlab v0.107.0 h1:P2CT9Uy9yN9lJo3FLxpMZ4xj6uWcpnigXsjvqJ6nd2Y=
github.com/xanzy/go-gitlab v0.107.0/go.mod h1:wKNKh3GkYDMOsGmnfuX+ITCmDuSDWFO0G+C4AygL9RY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62UoM=
google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
22 changes: 19 additions & 3 deletions e2e/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package main

import (
"context"
"errors"
"log"
"os"

Expand All @@ -34,6 +35,18 @@ type Project struct {
ApplyCommand string
}

func getVCSClient() (VCSClient, error) {

if os.Getenv("ATLANTISBOT_GITHUB_USERNAME") != "" {
return NewGithubClient(), nil
}
if os.Getenv("ATLANTISBOT_GITLAB_USERNAME") != "" {
return NewGitlabClient(), nil
}

return nil, errors.New("could not determine which vcs client")
}

func main() {

atlantisURL := os.Getenv("ATLANTIS_URL")
Expand All @@ -55,18 +68,21 @@ func main() {
log.Fatalf("failed to clean dir %q before cloning, attempting to continue: %v", cloneDirRoot, err)
}

githubClient := NewGithubClient()
vcsClient, err := getVCSClient()
if err != nil {
log.Fatalf("failed to get vcs client: %v", err)
}
ctx := context.Background()
// we create atlantis hook once for the repo, since the atlantis server can handle multiple requests
log.Printf("creating atlantis webhook with %s url", atlantisURL)
hookID, err := githubClient.CreateAtlantisWebhook(ctx, atlantisURL)
hookID, err := vcsClient.CreateAtlantisWebhook(ctx, atlantisURL)
if err != nil {
log.Fatalf("error creating atlantis webhook: %v", err)
}

// create e2e test
e2e := E2ETester{
vcsClient: githubClient,
vcsClient: vcsClient,
hookID: hookID,
cloneDirRoot: cloneDirRoot,
}
Expand Down

0 comments on commit c294e3f

Please sign in to comment.