-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.go
214 lines (200 loc) · 7.74 KB
/
github.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/http"
"path"
"strings"
"github.com/cli/go-gh/pkg/repository"
"github.com/google/go-github/v49/github"
"golang.org/x/crypto/nacl/box"
"ariga.io/gh-atlas/gen"
)
type (
// gitService handles communication with the git data related methods of the GitHub API.
gitService interface {
GetRef(ctx context.Context, owner string, repo string, ref string) (*github.Reference, *github.Response, error)
CreateRef(ctx context.Context, owner string, repo string, ref *github.Reference) (*github.Reference, *github.Response, error)
GetTree(ctx context.Context, owner string, repo string, sha string, recursive bool) (*github.Tree, *github.Response, error)
}
// repositoriesService handles communication with the repository related methods of the GitHub API.
repositoriesService interface {
Get(ctx context.Context, owner, repo string) (*github.Repository, *github.Response, error)
GetContents(ctx context.Context, owner, repo, path string, opts *github.RepositoryContentGetOptions) (fileContent *github.RepositoryContent, directoryContent []*github.RepositoryContent, resp *github.Response, err error)
CreateFile(ctx context.Context, owner, repo, path string, opts *github.RepositoryContentFileOptions) (*github.RepositoryContentResponse, *github.Response, error)
}
// actionsService handles communication with the actions related methods of the GitHub API.
actionsService interface {
GetRepoSecret(ctx context.Context, owner, repo, name string) (*github.Secret, *github.Response, error)
CreateOrUpdateRepoSecret(ctx context.Context, owner, repo string, eSecret *github.EncryptedSecret) (*github.Response, error)
GetRepoPublicKey(ctx context.Context, owner, repo string) (*github.PublicKey, *github.Response, error)
}
// pullRequestsService handles communication with the pull request related methods of the GitHub API.
pullRequestsService interface {
Create(ctx context.Context, owner, repo string, pr *github.NewPullRequest) (*github.PullRequest, *github.Response, error)
}
// githubClient is a wrapper around the GitHub API client.
githubClient struct {
Git gitService
Repositories repositoriesService
Actions actionsService
PullRequests pullRequestsService
}
// RepoExplorer defines methods for exploring and retrieving information from a repository.
RepoExplorer interface {
// MigrationDirectories returns a list of paths to directories containing migration files.
MigrationDirectories(ctx context.Context) ([]string, error)
// ConfigFiles returns a list of paths to configuration files (atlas.hcl) in the repository.
ConfigFiles(ctx context.Context) ([]string, error)
// ReadContent retrieves the content of a file at the specified path.
ReadContent(ctx context.Context, path string) (string, error)
}
)
type Repository struct {
owner string
name string
defaultBranch string
client *githubClient
}
var _ RepoExplorer = (*Repository)(nil)
// NewRepository creates a new repository object.
func NewRepository(client *githubClient, current repository.Repository, defaultBranch string) *Repository {
return &Repository{
owner: current.Owner(),
name: current.Name(),
defaultBranch: defaultBranch,
client: client,
}
}
// CheckoutNewBranch creates a new branch on top of the default branch.
func (r *Repository) CheckoutNewBranch(ctx context.Context, branchName string) error {
defaultBranch, _, err := r.client.Git.GetRef(ctx, r.owner, r.name, "refs/heads/"+r.defaultBranch)
if err != nil {
return err
}
newBranch := &github.Reference{
Ref: github.String("refs/heads/" + branchName),
Object: &github.GitObject{
SHA: defaultBranch.Object.SHA,
},
}
_, _, err = r.client.Git.CreateRef(ctx, r.owner, r.name, newBranch)
return err
}
// SetSecret sets Secret for the repository with the given name and value.
// if the secret already exists, it will not be updated.
func (r *Repository) SetSecret(ctx context.Context, name, value string) error {
_, res, err := r.client.Actions.GetRepoSecret(ctx, r.owner, r.name, name)
if err != nil && res.StatusCode != http.StatusNotFound {
return err
}
if res.StatusCode == http.StatusOK {
return fmt.Errorf("secret %q already exists", name)
}
key, _, err := r.client.Actions.GetRepoPublicKey(ctx, r.owner, r.name)
if err != nil {
return err
}
decodedPK, err := base64.StdEncoding.DecodeString(key.GetKey())
if err != nil {
return err
}
// Convert the decodedPK to a usable format of *[32]byte which is required by the box.SealAnonymous function.
var publicKey [32]byte
copy(publicKey[:], decodedPK)
encrypted, err := box.SealAnonymous(nil, []byte(value), &publicKey, nil)
if err != nil {
return errors.New("failed to encrypt secret value")
}
secret := &github.EncryptedSecret{
Name: name,
KeyID: key.GetKeyID(),
EncryptedValue: base64.StdEncoding.EncodeToString(encrypted),
}
res, err = r.client.Actions.CreateOrUpdateRepoSecret(ctx, r.owner, r.name, secret)
if res.StatusCode == http.StatusForbidden {
return errors.New("forbidden: make sure you have access to set secrets for this repository")
}
return err
}
// AddAtlasYAML create commit with atlas ci yaml file on the branch.
func (r *Repository) AddAtlasYAML(ctx context.Context, cfg *gen.Config, branchName, commitMsg string, replace bool) error {
var actionFilePath = ".github/workflows/ci-atlas.yaml"
content, err := gen.Generate(cfg)
if err != nil {
return err
}
newFile := &github.RepositoryContentFileOptions{
Message: github.String(commitMsg),
Content: content,
Branch: github.String(branchName),
}
current, _, _, err := r.client.Repositories.GetContents(ctx, r.owner, r.name, actionFilePath, nil)
switch e := err.(type) {
case nil:
if !replace {
return errors.New("atlas ci yaml file already exists, use --replace to replace it")
}
newFile.SHA = current.SHA
case *github.ErrorResponse:
if e.Message != "Not Found" {
return err
}
default:
return err
}
_, _, err = r.client.Repositories.CreateFile(ctx, r.owner, r.name, actionFilePath, newFile)
return err
}
// CreatePR creates a pull request for the branch and returns the link to the PR.
func (r *Repository) CreatePR(ctx context.Context, title string, body string, branchName string) (string, error) {
newPR := &github.NewPullRequest{
Title: &title,
Head: &branchName,
Body: &body,
Base: &r.defaultBranch,
}
pr, _, err := r.client.PullRequests.Create(ctx, r.owner, r.name, newPR)
if err != nil {
return "", err
}
return pr.GetHTMLURL(), nil
}
// MigrationDirectories returns a list of paths to directories containing migration files.
func (r *Repository) MigrationDirectories(ctx context.Context) ([]string, error) {
t, _, err := r.client.Git.GetTree(ctx, r.owner, r.name, r.defaultBranch, true)
if err != nil {
return nil, err
}
var paths []string
for _, e := range t.Entries {
if e.GetType() == "blob" && strings.HasSuffix(e.GetPath(), "atlas.sum") {
paths = append(paths, path.Dir(e.GetPath()))
}
}
return paths, nil
}
// ConfigFiles returns a list of paths to atlas.hcl files in the repository.
func (r *Repository) ConfigFiles(ctx context.Context) ([]string, error) {
t, _, err := r.client.Git.GetTree(ctx, r.owner, r.name, r.defaultBranch, true)
if err != nil {
return nil, err
}
var paths []string
for _, e := range t.Entries {
if e.GetType() == "blob" && strings.HasSuffix(e.GetPath(), "atlas.hcl") {
paths = append(paths, e.GetPath())
}
}
return paths, nil
}
// ReadContent retrieves the content of a file at the specified path.
func (r *Repository) ReadContent(ctx context.Context, path string) (string, error) {
fileContents, _, _, err := r.client.Repositories.GetContents(ctx, r.owner, r.name, path, nil)
if err != nil {
return "", err
}
return fileContents.GetContent()
}