-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
97 lines (70 loc) · 2.62 KB
/
main.go
File metadata and controls
97 lines (70 loc) · 2.62 KB
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
package main
import (
"fmt"
"os"
"strings"
"github.com/google/go-github/github"
log "github.com/sirupsen/logrus"
"github.com/hostwithquantum/github-org-sync-action/repo"
)
func init() {
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}
func main() {
org := os.Getenv("GITHUB_ORG")
repositories := strings.Split(os.Getenv("GITHUB_REPOS"), " ")
// this is the main copy which contains the files we want to sync
skeletonRepository := os.Getenv("GITHUB_SKELETON")
log.Info(fmt.Sprintf("Running for '%s' and syncing to: %s", org, repositories))
log.Info(fmt.Sprintf("From: %s", skeletonRepository))
// init CurrentUser (for auth)
currentUser := repo.CurrentUser{
Email: os.Getenv("GITHUB_EMAIL"),
Name: os.Getenv("GITHUB_USER"),
Token: os.Getenv("GITHUB_ACCESS_TOKEN"),
}
githubClient := repo.NewGithub(currentUser, org)
log.Info(fmt.Sprintf("Using: %s (of %s)", currentUser.Email, currentUser.Name))
tmpDirectory := fmt.Sprintf("./tmp/%s", org)
log.Info(fmt.Sprintf("Temp files will be created in: %s", tmpDirectory))
// clone skeleton repository
skeleton := repo.NewRepo(org, skeletonRepository, currentUser, tmpDirectory)
skeletonGithub := repo.GithubLink(org, skeletonRepository)
log.Info("Cloned skeleton")
handler := repo.NewHandler(skeletonRepository, tmpDirectory)
for _, repository := range repositories {
target := repo.NewRepo(org, repository, currentUser, tmpDirectory)
githubLink := repo.GithubLink(org, repository)
log.Infof("Cloned: '%s'", githubLink)
defaultBranch := target.GetDefaultBranch()
log.Debugf("Default branch for '%s' is '%s'", githubLink, defaultBranch)
target.CreateBranch("chore/update-workflows")
handler.Sync(fmt.Sprintf("%s/%s", tmpDirectory, repository))
if target.NeedsCommit() != true {
log.Info(fmt.Sprintf("Repo '%s' is clean\n", githubLink))
continue
}
commitMsg := fmt.Sprintf(
"Chore: updates to .github/workflows\n\nSee %s@%s",
skeletonGithub,
skeleton.GetLastCommit(),
)
target.CommitAndPush(commitMsg, "chore/update-workflows")
description := fmt.Sprintf(
"Updating from %s: %s@%s",
skeletonGithub,
skeletonGithub,
target.GetLastCommit())
newPR := &github.NewPullRequest{
Title: github.String(fmt.Sprintf("[github-org-sync] Updating files via %s", skeletonGithub)),
Head: github.String("chore/update-workflows"),
Base: github.String(defaultBranch),
Body: github.String(description),
MaintainerCanModify: github.Bool(true),
}
githubClient.CreatePullRequest(repository, newPR)
}
err := os.RemoveAll(tmpDirectory)
repo.CheckIfError(err)
}