-
Notifications
You must be signed in to change notification settings - Fork 4
/
yaml.go
187 lines (171 loc) · 5.92 KB
/
yaml.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
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"strings"
"io/ioutil"
"os"
"github.com/google/go-github/v33/github"
"gopkg.in/yaml.v2"
)
type YamlRepositoryMerge struct {
AllowMergeCommit *bool `yaml:"allowMergeCommit"`
AllowSquashMerge *bool `yaml:"allowSquashMerge"`
AllowRebaseMerge *bool `yaml:"allowRebaseMerge"`
}
type YamlRepositoryPages struct {
Issues *bool `yaml:"issues"`
Projects *bool `yaml:"projects"`
Wiki *bool `yaml:"wiki"`
}
type YamlOnCreate struct {
AutoInit *bool `yaml:"autoInit"`
Gitignore *string `yaml:"gitignore"`
License *string `yaml:"license"`
}
type YamlRepository struct {
Name *string `yaml:"name"`
Description *string `yaml:"description"`
Homepage *string `yaml:"homepage"`
Private *bool `yaml:"private"`
DefaultBranch *string `yaml:"defaultBranch"`
DeleteBranchOnMerge *bool `yaml:"deleteBranchOnMerge"`
OnCreate YamlOnCreate `yaml:"onCreate"`
Pages YamlRepositoryPages `yaml:"pages"`
Merge YamlRepositoryMerge `yaml:"merge"`
Teams map[string]string `yaml:"teams"`
Branches []YamlBranch `yaml:"branches"`
}
type YamlGithub struct {
Repository YamlRepository `yaml:"repo"`
}
type YamlTop struct {
Github YamlGithub `yaml:"github"`
}
type YamlBranchRequiredStatusChecks struct {
RequiredBranchesUpToDate bool `yaml:"requiredBranchesUpToDate"`
Contexts []string `yaml:"contexts"`
}
type YamlPush struct {
Users []string `yaml:"users"`
Teams []string `yaml:"teams"`
}
type YamlBranchOnCreate struct {
Commits []YamlBranchCommit `yaml:"commits"`
}
type YamlBranchCommit struct {
Message string `yaml:"message"`
FileName string `yaml:"fileName"`
Text string `yaml:"text"`
Destination string `yaml:"destination"`
}
type YamlBranch struct {
Name string `yaml:"name"`
MinApprove int `yaml:"minApprove"`
CodeOwners bool `yaml:"codeOwners"`
IncludeAdmins bool `yaml:"includeAdmins"`
RequiredStatusChecks YamlBranchRequiredStatusChecks `yaml:"requiredStatusChecks"`
Push YamlPush `yaml:"push"`
OnCreate YamlBranchOnCreate `yaml:"onCreate"`
}
func repoToYaml(obj *github.Repository) YamlRepository {
yamlRepo := YamlRepository{
Name: obj.Name,
Description: obj.Description,
Homepage: obj.Homepage,
Private: obj.Private,
DefaultBranch: obj.DefaultBranch,
DeleteBranchOnMerge: obj.DeleteBranchOnMerge,
OnCreate: YamlOnCreate{
AutoInit: obj.AutoInit,
Gitignore: obj.GitignoreTemplate,
License: obj.LicenseTemplate,
},
Pages: YamlRepositoryPages{
Wiki: obj.HasWiki,
Projects: obj.HasProjects,
Issues: obj.HasIssues,
},
Merge: YamlRepositoryMerge{
AllowMergeCommit: obj.AllowMergeCommit,
AllowRebaseMerge: obj.AllowRebaseMerge,
AllowSquashMerge: obj.AllowSquashMerge,
},
Teams: getRepoTeams(*obj.Owner.Login, *obj.Name),
Branches: getRepoProtections(*obj.Owner.Login, *obj.Name),
}
return yamlRepo
}
func applyYaml(org string, fileName string, format string) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Println(err)
return
}
var yamlTop YamlTop
if err := yaml.Unmarshal(bytes, &yamlTop); err != nil {
fmt.Println("failed to parse ", fileName, ", err: ", err)
return
}
fmt.Println("Parsed Yaml from file: ", yamlTop)
repo := yamlTop.Github.Repository
createOrUpdateRepo(org,
repo.Name, repo.Description, repo.Homepage, repo.Private,
not(repo.Pages.Issues), not(repo.Pages.Projects), not(repo.Pages.Wiki),
repo.OnCreate.AutoInit, repo.OnCreate.Gitignore, repo.OnCreate.License,
not(repo.Merge.AllowMergeCommit), not(repo.Merge.AllowRebaseMerge), not(repo.Merge.AllowSquashMerge),
repo.DefaultBranch, repo.DeleteBranchOnMerge,
format, true)
for teamName, teamPerm := range repo.Teams {
addTeamToRepo(org, *repo.Name, teamName, teamPerm)
}
for _, b := range repo.Branches {
if b.Name != "master" {
createBranch(org, *repo.Name, b.Name, format)
}
for _, c := range b.OnCreate.Commits {
e := getPrimaryEmail()
if c.FileName == "" && c.Text == "" {
CheckIfError(fmt.Errorf("eighter specify fileName or text"))
} else if c.Text != "" && c.Destination == "" {
CheckIfError(fmt.Errorf("destination is empty"))
} else if c.Text != "" && c.Destination != "" {
c.FileName, err = createFileWithContent(c.Text)
if err != nil {
CheckIfError(fmt.Errorf("can't write to temp file"))
}
}
addFile(org, *repo.Name, b.Name, c.FileName, c.Destination, c.Message, e, format)
}
createProtection(org, *repo.Name, b.Name, b.MinApprove,
false, b.CodeOwners, b.RequiredStatusChecks.RequiredBranchesUpToDate, b.IncludeAdmins,
"", "",
makeCommaSeparatedString(b.Push.Users), makeCommaSeparatedString(b.Push.Teams), makeCommaSeparatedString(b.RequiredStatusChecks.Contexts))
}
createOrUpdateRepo(org,
repo.Name, repo.Description, repo.Homepage, repo.Private,
not(repo.Pages.Issues), not(repo.Pages.Projects), not(repo.Pages.Wiki),
repo.OnCreate.AutoInit, repo.OnCreate.Gitignore, repo.OnCreate.License,
not(repo.Merge.AllowMergeCommit), not(repo.Merge.AllowRebaseMerge), not(repo.Merge.AllowSquashMerge),
repo.DefaultBranch, repo.DeleteBranchOnMerge,
format, false)
}
func makeCommaSeparatedString(arr []string) string {
var result string
for _, v := range arr {
result += strings.TrimSpace(v) + ","
}
return result
}
func createFileWithContent(content string) (fileName string, err error) {
fileName = os.TempDir() + TempFileName()
Info(fileName)
return fileName, ioutil.WriteFile(fileName, []byte(content), 0644)
}
// TempFileName generates a temporary filename for use in testing or whatever
func TempFileName() string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return hex.EncodeToString(randBytes)
}