-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
173 lines (140 loc) · 4.59 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"errors"
"os"
"strings"
"time"
console "github.com/fatih/color"
memfs "github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
git_http "github.com/go-git/go-git/v5/plumbing/transport/http"
memory "github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v33/github"
"golang.org/x/oauth2"
)
var gitRepoURL, gitUrlFound = os.LookupEnv("NHN_K8S_GIT_REPO_URL")
var gitRepoName, gitRepoNameFound = os.LookupEnv("NHN_K8S_GIT_REPO_NAME")
var gitUserName, gitUserNameFound = os.LookupEnv("NHN_GIT_USERNAME")
var gitAccessToken, gitTokenFound = os.LookupEnv("NHN_GIT_ACCESS_TOKEN")
var fileSystem = memfs.New()
var gitRepo *git.Repository
var auth *git_http.BasicAuth
func GitSetup() bool {
if !EnsureGitEnvFound() {
return false
}
CreateAuth()
CloneRepo()
return true
}
// EnsureGitEnvFound checks that git environment variables are found, Access token and repo URL
func EnsureGitEnvFound() bool {
if !gitUrlFound || !gitTokenFound || !gitRepoNameFound || !gitUserNameFound {
console.Red("Failed to init ENV vars for git.")
console.Red("Git repo name was: " + gitRepoName)
console.Red("Git username was: " + gitUserName)
console.Red("Git repo URL was: " + gitRepoURL)
console.Red("Access token was: " + gitAccessToken)
return false
}
return true
}
func CreateAuth() {
auth = &git_http.BasicAuth{
Username: "any string except empty",
Password: gitAccessToken,
}
}
func CloneRepo() {
console.Green("git clone " + gitRepoURL)
var err error
gitRepo, err = git.Clone(memory.NewStorage(), fileSystem, &git.CloneOptions{
URL: gitRepoURL,
Progress: os.Stdout,
Auth: auth,
})
PanicIfError(err)
}
func CreateFile(path string, fileContent []byte) string {
filePath := path
file, err := fileSystem.Create(filePath)
PanicIfError(err)
file.Write(fileContent)
file.Close()
return filePath
}
func CheckoutLatestMainBranch() *git.Worktree {
workTree, err := gitRepo.Worktree()
PanicIfError(err)
mainBranch := plumbing.ReferenceName("refs/heads/main")
console.Green("git checkout main")
err = workTree.Checkout(&git.CheckoutOptions{
Create: false,
Branch: mainBranch,
})
PanicIfError(err)
console.Green("git pull")
workTree.Pull(&git.PullOptions{
Auth: auth,
})
return workTree
}
func CreateBranch(branchName string, workTree *git.Worktree) error {
branchRef := plumbing.ReferenceName("refs/heads/" + branchName)
console.Green("git checkout -b " + branchName)
err := workTree.Checkout(&git.CheckoutOptions{
Create: true,
Branch: branchRef,
})
return err
}
// CreateNewServiceConfig creates a new pull request with config files for a new microservice
func CreateNewServiceConfig(service Service) error {
workTree := CheckoutLatestMainBranch()
appFolder, err := fileSystem.ReadDir("/app/" + service.GatewayEndpoint)
if appFolder != nil {
return errors.New("Config files already exists for gateway endpoint '" + service.GatewayEndpoint + "'")
}
PanicIfError(err)
err = CreateBranch(service.Name, workTree)
if err != nil {
if strings.Compare("a branch named \"refs/heads/"+service.Name+"\" already exists", err.Error()) == 0 {
return err
}
PanicIfError(err)
}
helmFileContent := CreateHelmValuesFile(service)
helmFilePath := CreateFile("app/"+service.GatewayEndpoint+"/"+service.Name+"-helm-values.yaml", helmFileContent)
workTree.Add(helmFilePath)
argoFileContent := CreateArgoAppFile(service)
argoFilePath := CreateFile("argocd/apps/"+service.GatewayEndpoint+"/values.yaml", argoFileContent)
workTree.Add(argoFilePath)
workTree.Commit("Add service config for "+service.Name, &git.CommitOptions{Author: &object.Signature{
Name: "Adminservice",
Email: "adminservice@api.nhn.no",
When: time.Now(),
}})
err = gitRepo.Push(&git.PushOptions{Auth: auth, Force: true})
PanicIfError(err)
// Create PR
context := context.Background()
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gitAccessToken},
)
oauthClient := oauth2.NewClient(context, tokenSource)
githubClient := github.NewClient(oauthClient)
newPR := &github.NewPullRequest{
Title: github.String("New kubernetes deployment: " + service.Name),
Head: github.String(service.Name),
Base: github.String("main"),
Body: github.String("Automated PR for application " + service.Name),
MaintainerCanModify: github.Bool(true),
}
_, _, err = githubClient.PullRequests.Create(context, gitUserName, gitRepoName, newPR)
PanicIfError(err)
console.Yellow("PR created")
return nil
}