-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
492 lines (461 loc) · 13.5 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package main
import (
"bufio"
"context"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"time"
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/bluekeyes/patch2pr"
"github.com/google/go-github/v58/github"
"github.com/joho/godotenv"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
type FileContentQuery struct {
Repository struct {
Object struct {
Blob struct {
Text githubv4.String
} `graphql:"... on Blob"`
} `graphql:"object(expression: $expression)"`
} `graphql:"repository(name: $name, owner: $owner)"`
}
type FileContentResponse struct {
Data struct {
Repository struct {
Object struct {
Blob struct {
Text string `json:"text"`
} `json:"blob"`
} `json:"object"`
} `json:"repository"`
} `json:"data"`
}
func fetchFileContent(client *githubv4.Client, owner, name, expression string) (string, error) {
var query FileContentQuery
variables := map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(name),
"expression": githubv4.String(expression),
}
err := client.Query(context.Background(), &query, variables)
if err != nil {
return "", err
}
return string(query.Repository.Object.Blob.Text), nil
}
type OidQuery struct {
Repository struct {
DefaultBranchRef struct {
Target struct {
OID githubv4.String
} `graphql:"target"`
} `graphql:"defaultBranchRef"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
type OidResponse struct {
Data struct {
Repository struct {
DefaultBranchRef struct {
Target struct {
OID string `json:"oid"`
} `json:"target"`
} `json:"defaultBranchRef"`
} `json:"repository"`
} `json:"data"`
}
func fetchOid(client *githubv4.Client, owner, name string) (string, error) {
var query OidQuery
variables := map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(name),
}
err := client.Query(context.Background(), &query, variables)
if err != nil {
return "", err
}
return string(query.Repository.DefaultBranchRef.Target.OID), nil
}
type customError struct {
errType string
message error
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
log.Fatalf("Unauthorized, token empty")
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
clientv3 := github.NewClient(nil).WithAuthToken(token)
// Open the file containing the list of repositories
file, err := os.Open("repos.txt")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
var wg sync.WaitGroup
scanner := bufio.NewScanner(file)
scannedLines := []string{}
for scanner.Scan() {
scannedLines = append(scannedLines, scanner.Text())
}
errChan := make(chan error, len(scannedLines))
for _, scannedLine := range scannedLines {
wg.Add(1)
go func(line string) {
defer wg.Done()
parts := strings.Split(line, "/")
repoOwner := parts[0]
repoName := parts[1]
fork, _, _ := clientv3.Repositories.Get(context.Background(), "arunsathiya", repoName)
if fork == nil {
_, _, err = clientv3.Repositories.CreateFork(context.Background(), repoOwner, repoName, &github.RepositoryCreateForkOptions{
DefaultBranchOnly: true,
})
if err != nil {
errChan <- err
return
}
}
time.Sleep(2 * time.Second)
// Create directories
dir := filepath.Join(repoName, ".github", "workflows")
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, os.ModePerm)
}
}(scannedLine)
}
go func() {
wg.Wait()
close(errChan)
}()
for err := range errChan {
if err != nil {
log.Printf("error: %v", err)
}
}
repoOwner := strings.Split(scannedLines[0], "/")[0]
scannedDirs := []fs.DirEntry{}
dirs, err := os.ReadDir(".")
if err != nil {
log.Fatalf("error reading the root directory: %v", err)
}
scannedDirs = append(scannedDirs, dirs...)
gitErrChan := make(chan error, len(scannedDirs))
for _, scannedDir := range scannedDirs {
if scannedDir.IsDir() && scannedDir.Name() != ".git" {
wg.Add(1)
go func(scannedDir fs.DirEntry) {
defer wg.Done()
repoName := scannedDir.Name()
if _, err := os.Stat(filepath.Join(repoName, ".git")); os.IsNotExist(err) {
fCmd := fmt.Sprintf("git init && git remote add origin [email protected]:%s/%s.git", repoOwner, repoName)
cmd := exec.Command("sh", "-c", fCmd)
cmd.Dir = repoName
_, err := cmd.CombinedOutput()
if err != nil {
gitErrChan <- err
return
}
}
}(scannedDir)
}
}
go func() {
wg.Wait()
close(gitErrChan)
}()
for err := range gitErrChan {
if err != nil {
log.Printf("git init error: %v", err)
}
}
workflowFileErrChan := make(chan customError, len(scannedLines))
for _, scannedLine := range scannedLines {
wg.Add(1)
go func(scannedLine string) {
defer wg.Done()
parts := strings.Split(scannedLine, "/")
repoOwner := parts[0]
repoName := parts[1]
filePath := strings.Join(parts[2:], "/")
expression := fmt.Sprintf("HEAD:%s", filePath)
// Create file
fullPath := filepath.Join(repoName, filePath)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
file, err := os.Create(fullPath)
if err != nil {
workflowFileErrChan <- customError{"create", err}
return
}
file.Close()
}
fileContent, err := fetchFileContent(client, repoOwner, repoName, expression)
if err != nil {
workflowFileErrChan <- customError{"read", err}
return
}
// Write the content to a file
err = os.WriteFile(path.Join(repoName, filePath), []byte(fileContent), 0644)
if err != nil {
workflowFileErrChan <- customError{"write", err}
}
}(scannedLine)
}
go func() {
wg.Wait()
close(workflowFileErrChan)
}()
for err := range workflowFileErrChan {
if err.message != nil {
switch err.errType {
case "create":
log.Printf("file creation error: %v", err.message)
case "fetch":
log.Printf("file contents fetch error: %v", err.message)
case "write":
log.Printf("write contents error: %v", err.message)
}
}
}
commitErrChan := make(chan error, len(scannedDirs))
for _, scannedDir := range scannedDirs {
if scannedDir.IsDir() && scannedDir.Name() != ".git" {
wg.Add(1)
go func(scannedDir fs.DirEntry) {
defer wg.Done()
repoName := scannedDir.Name()
fCmd := "git add . && git commit -m \"taken from source\""
cmd := exec.Command("sh", "-c", fCmd)
cmd.Dir = repoName
if _, err := cmd.CombinedOutput(); err != nil {
gitErrChan <- err
return
}
}(scannedDir)
}
}
go func() {
wg.Wait()
close(commitErrChan)
}()
for err := range commitErrChan {
if err != nil {
log.Printf("error creating initial commit: %v", err)
}
}
commitPatchErrChan := make(chan customError, len(scannedDirs))
for _, scannedDir := range scannedDirs {
if scannedDir.IsDir() && scannedDir.Name() != ".git" {
wg.Add(1)
go func(scannedDir fs.DirEntry) {
defer wg.Done()
repoName := scannedDir.Name()
if err := processReplacements(repoName); err != nil {
commitPatchErrChan <- customError{"replacements", err}
return
}
if err := genPatch(repoName); err != nil {
commitPatchErrChan <- customError{"generating patch", err}
return
}
patch, err := os.Open(filepath.Join(repoName, "changes.patch"))
if err != nil {
commitPatchErrChan <- customError{"open patch", err}
return
}
patchFiles, _, err := gitdiff.Parse(patch)
if err != nil {
commitPatchErrChan <- customError{"parse patch", err}
return
}
if len(patchFiles) == 0 {
commitPatchErrChan <- customError{"patch files length", fmt.Errorf("patch is empty for %s", repoName)}
return
}
fork, _, _ := clientv3.Repositories.Get(context.Background(), "arunsathiya", repoName)
oid, err := fetchOid(client, *fork.Owner.Login, *fork.Name)
if err != nil {
commitPatchErrChan <- customError{"oid", err}
return
}
graphqlApplier := patch2pr.NewGraphQLApplier(
client,
patch2pr.Repository{
Owner: *fork.Owner.Login,
Name: *fork.Name,
},
oid,
)
for _, patchFile := range patchFiles {
err := graphqlApplier.Apply(context.Background(), patchFile)
if err != nil {
if patch2pr.IsUnsupported(err) {
commitPatchErrChan <- customError{"unsupported apply operation", err}
return
} else {
commitPatchErrChan <- customError{"error applying", err}
return
}
}
}
prTitle := "ci: Use GITHUB_OUTPUT envvar instead of set-output command"
prBody := "`save-state` and `set-output` commands used in GitHub Actions are deprecated and [GitHub recommends using environment files](https://github.blog/changelog/2023-07-24-github-actions-update-on-save-state-and-set-output-commands/).\n\nThis PR updates the usage of `::set-output` to `\"$GITHUB_OUTPUT\"`\n\nInstructions for envvar usage from GitHub docs:\n\nhttps://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter"
sha, err := graphqlApplier.Commit(
context.Background(),
"refs/heads/"+*fork.DefaultBranch,
&gitdiff.PatchHeader{
Author: &gitdiff.PatchIdentity{
Name: "Arun",
Email: "[email protected]",
},
AuthorDate: time.Now(),
Committer: &gitdiff.PatchIdentity{
Name: "Arun",
Email: "[email protected]",
},
CommitterDate: time.Now(),
Title: prTitle,
Body: prBody,
},
)
fmt.Printf("Commit SHA for %s: %s\n", repoName, sha)
if err != nil {
commitPatchErrChan <- customError{"error preparing commit", err}
return
}
time.Sleep(2 * time.Second)
var maintainerCanModify bool = true
var draft bool = false
var base string = *fork.Source.DefaultBranch
var head string = *fork.Owner.Login + ":" + *fork.DefaultBranch
var headRepo string = *fork.Name
_, _, err = clientv3.PullRequests.Create(context.Background(), repoOwner, repoName, &github.NewPullRequest{
Title: &prTitle,
Body: &prBody,
MaintainerCanModify: &maintainerCanModify,
Draft: &draft,
Base: &base,
Head: &head,
HeadRepo: &headRepo,
})
if err != nil {
commitPatchErrChan <- customError{"error preparing PR", err}
return
}
}(scannedDir)
}
}
go func() {
wg.Wait()
close(commitPatchErrChan)
}()
for err := range commitPatchErrChan {
if err.message != nil {
switch err.errType {
case "replacements":
log.Printf("error processing replacements: %v", err.message)
case "generating patch":
log.Printf("error generating patch: %v", err.message)
case "open patch":
log.Printf("error opening patch: %v", err.message)
case "parse patch":
log.Printf("error parsing patch: %v", err.message)
case "patch files length":
log.Printf("error with patch files length: %v", err.message)
case "unsupported apply operation":
log.Printf("error in applying operation: %v", err.message)
case "error applying":
log.Printf("error applying: %v", err.message)
case "error preparing commit":
log.Printf("error preparing commit: %v", err.message)
case "error preparing PR":
log.Printf("error preparing PR: %v", err.message)
}
}
}
replaceFilesErrChan := make(chan customError, len(scannedLines))
for _, scannedLine := range scannedLines {
wg.Add(1)
go func(scannedLine string) {
defer wg.Done()
parts := strings.Split(scannedLine, "/")
repoName := parts[1]
filePath := strings.Join(parts[2:], "/")
expression := fmt.Sprintf("HEAD:%s", filePath)
fileContent, err := fetchFileContent(client, "arunsathiya", repoName, expression)
if err != nil {
replaceFilesErrChan <- customError{"read", err}
return
}
fullPath := filepath.Join(repoName, filePath)
err = os.WriteFile(fullPath, []byte(fileContent), os.ModePerm)
if err != nil {
replaceFilesErrChan <- customError{"write", err}
}
}(scannedLine)
}
go func() {
wg.Wait()
close(replaceFilesErrChan)
}()
for err := range replaceFilesErrChan {
if err.message != nil {
switch err.errType {
case "read":
log.Printf("read from origin error: %v", err.message)
case "write":
log.Printf("write origin content error: %v", err.message)
}
}
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading from file:", err)
}
}
func processReplacements(repoDir string) error {
// Check for ::set-output in cloned directory
grepCmd := "grep -rnw '.' -e 'set-output'"
grep := exec.Command("bash", "-c", grepCmd)
grep.Dir = repoDir
if err := grep.Run(); err != nil {
return fmt.Errorf("set-output not found or error in grep: %s", err)
}
types := []string{".yml", ".yaml"}
for _, ext := range types {
// Replace ::set-output command
fCmd := fmt.Sprintf(`LC_ALL=C find . -type f -name '*%s' -exec sed -i '' -E 's/::set-output name=([^:]*)::(.*)/\1=\2 >> "$GITHUB_OUTPUT"/g' {} +`, ext)
cmd := exec.Command("bash", "-c", fCmd)
cmd.Dir = repoDir
if err := cmd.Run(); err != nil {
return fmt.Errorf("error replacing: %v", err)
}
}
return nil
}
func genPatch(repoName string) error {
fCmd := "git diff > changes.patch && git reset --hard"
cmd := exec.Command("sh", "-c", fCmd)
cmd.Dir = repoName
if err := cmd.Run(); err != nil {
return fmt.Errorf("patch creation failed: %s", err)
}
return nil
}