-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
181 lines (158 loc) · 4.45 KB
/
main.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
174
175
176
177
178
179
180
181
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v57/github"
"github.com/joho/godotenv"
"golang.org/x/oauth2"
)
func main() {
// Get values from environment variables
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
githubPersonalAccessToken := os.Getenv("GITHUB_TOKEN")
if githubPersonalAccessToken == "" {
fmt.Println("Error: GITHUB_TOKEN environment variable not set")
return
}
repoURL := os.Getenv("REPO_URL")
branchPrefix := os.Getenv("BRANCH_PREFIX")
authorName := os.Getenv("AUTHOR_NAME")
authorEmail := os.Getenv("AUTHOR_EMAIL")
baseBranch := os.Getenv("BASE_BRANCH")
commitMsg := os.Getenv("COMMIT_MESSAGE")
prTitle := os.Getenv("PR_TITLE")
prBody := os.Getenv("PR_BODY")
//Get code
branchName, repo, auth, err := GetCode(branchPrefix, repoURL, githubPersonalAccessToken)
if err != nil {
fmt.Printf("Error retrieving code: %s\n", err)
return
}
// Open repository if it already exists
if err == git.ErrRepositoryAlreadyExists {
repo, err = git.PlainOpen("./terragoat")
if err != nil {
fmt.Printf("Error opening repository: %s\n", err)
return
}
}
// Create and checkout new branch
w, err := repo.Worktree()
if err != nil {
fmt.Printf("Error getting worktree: %s\n", err)
return
}
fmt.Printf("Creating and checking out branch: %s\n", branchName)
b := plumbing.NewBranchReferenceName(branchName)
err = w.Checkout(&git.CheckoutOptions{
Create: true,
Branch: b,
Force: false,
})
if err != nil {
fmt.Printf("Error creating branch: %s\n", err)
return
}
// Create an example file
exampleFile := "./terragoat/example2.tf"
fmt.Printf("Creating file: %s\n", exampleFile)
content, err := os.ReadFile("new_resource.tf")
err = os.WriteFile(exampleFile, content, 0644)
if err != nil {
fmt.Printf("Error creating file: %s\n", err)
return
}
// Stage changes
fmt.Println("Staging changes...")
_, err = w.Add(".")
if err != nil {
fmt.Printf("Error staging files: %s\n", err)
return
}
// Create commit
fmt.Println("Creating commit...")
_, err = w.Commit(commitMsg, &git.CommitOptions{
Author: &object.Signature{
Name: authorName,
Email: authorEmail,
When: time.Now(),
},
})
if err != nil {
fmt.Printf("Error committing: %s\n", err)
return
}
// Push changes
fmt.Println("Pushing changes...")
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", branchName, branchName)),
},
Auth: auth,
})
if err != nil {
fmt.Printf("Error pushing: %s\n", err)
return
}
// Create PR
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubPersonalAccessToken})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
// Extract owner and repo from URL
parts := strings.Split(strings.TrimSuffix(repoURL, ".git"), "/")
owner := parts[len(parts)-2]
repoName := parts[len(parts)-1]
newPR := &github.NewPullRequest{
Title: github.String(prTitle),
Head: github.String(branchName),
Base: github.String(baseBranch),
Body: github.String(prBody),
}
pr, _, err := client.PullRequests.Create(ctx, owner, repoName, newPR)
if err != nil {
fmt.Printf("Error creating PR: %s\n", err)
return
}
fmt.Printf("Successfully created PR #%d\n", pr.GetNumber())
fmt.Printf("PR URL: %s\n", pr.GetHTMLURL())
dirRemovalErr := os.RemoveAll("./terragoat")
if dirRemovalErr != nil {
log.Fatal(err)
}
fmt.Printf("Successfully removed directory.\n")
}
func GetCode(branchPrefix string, repoURL string, githubPersonalAccessToken string) (string, *git.Repository, *http.BasicAuth, error) {
// Generate unique branch name
branchName := fmt.Sprintf("%s-%s", branchPrefix, time.Now().Format("20060102-150405"))
// Set up authentication
auth := &http.BasicAuth{
Username: "x-access-token",
Password: githubPersonalAccessToken,
}
// Clone repository
fmt.Println("Cloning repository...")
repo, err := git.PlainClone("./terragoat", false, &git.CloneOptions{
URL: repoURL,
Progress: os.Stdout,
Auth: auth,
})
if err != nil && err != git.ErrRepositoryAlreadyExists {
fmt.Printf("Error cloning: %s\n", err)
return "", nil, nil, err
}
return branchName, repo, auth, nil
}