main.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "github.com/ghodss/yaml"
  10. "github.com/mohae/deepcopy"
  11. "k8s.io/apimachinery/pkg/util/sets"
  12. "k8s.io/test-infra/prow/config"
  13. )
  14. // DefaultTriggerFor returns the default regexp string used to match comments
  15. // that should trigger the job with this name.
  16. func DefaultTriggerFor(name string) string {
  17. return fmt.Sprintf(`(?m)^\+test( | .* )%s,?($|\s.*)`, name)
  18. }
  19. // DefaultRerunCommandFor returns the default rerun command for the job with
  20. // this name.
  21. func DefaultRerunCommandFor(name string) string {
  22. return fmt.Sprintf("+test %s", name)
  23. }
  24. type Image struct {
  25. Image []struct {
  26. Name string `yaml:"name"`
  27. Image string `yaml:"image"`
  28. } `yaml:"image"`
  29. }
  30. type Global struct {
  31. Template *config.Config
  32. AppendTask *config.Config
  33. AlwaysRun *config.Config
  34. Result *config.Config
  35. Image map[string]string
  36. Labels sets.String
  37. DefaultLabels sets.String
  38. TemplateLabels Configuration
  39. }
  40. var GlobalStatue Global
  41. type Owner struct {
  42. Approvers []string `yaml:"approvers"`
  43. Reviewers []string `yaml:"reviewers"`
  44. Labels []string `yaml:"labels"`
  45. }
  46. // LabelTarget specifies the intent of the label (PR or issue)
  47. type LabelTarget string
  48. const (
  49. bothTarget = "both"
  50. )
  51. type Label struct {
  52. // Name is the current name of the label
  53. Name string `json:"name"`
  54. // Color is rrggbb or color
  55. Color string `json:"color"`
  56. // Description is brief text explaining its meaning, who can apply it
  57. Description string `json:"description"` // What does this label mean, who can apply it
  58. // Target specifies whether it targets PRs, issues or both
  59. Target LabelTarget `json:"target"`
  60. // ProwPlugin specifies which prow plugin add/removes this label
  61. ProwPlugin string `json:"prowPlugin,omitempty"`
  62. // AddedBy specifies whether human/munger/bot adds the label
  63. AddedBy string `json:"addedBy"`
  64. // Previously lists deprecated names for this label
  65. Previously []Label `json:"previously,omitempty"`
  66. // DeleteAfter specifies the label is retired and a safe date for deletion
  67. DeleteAfter *time.Time `json:"deleteAfter,omitempty"`
  68. }
  69. // RepoConfig contains only labels for the moment
  70. type RepoConfig struct {
  71. Labels []Label `json:"labels"`
  72. }
  73. // Configuration is a list of Required Labels to sync in all kubernetes repos
  74. type Configuration struct {
  75. Repos map[string]RepoConfig `json:"repos,omitempty"`
  76. Default RepoConfig `json:"default"`
  77. }
  78. func generate() {
  79. filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
  80. if strings.Contains(path, "app") && len(strings.Split(path, "/")) > 5 {
  81. return filepath.SkipDir
  82. }
  83. if strings.HasPrefix(path, "vendor") || strings.HasPrefix(path, "build") || strings.HasPrefix(path, ".rider") || strings.HasPrefix(path, ".git") {
  84. return nil
  85. }
  86. if info.Name() == "OWNERS" && !info.IsDir() {
  87. var owner Owner
  88. yamlFile, err := ioutil.ReadFile(path)
  89. if err != nil {
  90. return err
  91. }
  92. err = yaml.Unmarshal(yamlFile, &owner)
  93. if err != nil {
  94. return err
  95. }
  96. if len(owner.Labels) == 0 {
  97. return nil
  98. }
  99. GlobalStatue.Labels.Insert(owner.Labels...)
  100. ts, ok := GlobalStatue.Template.JobConfig.Presubmits["platform/go-common"]
  101. if !ok {
  102. fmt.Println("wrong project name")
  103. return nil
  104. }
  105. labels := sets.NewString(owner.Labels...)
  106. isLib := labels.Has("library")
  107. labels.Delete("library", "admin", "interface", "infra", "common", "service", "job", "vendor", "tool")
  108. labels.Delete("bbq", "ep", "ops", "video", "openplatform", "main", "live")
  109. labels.Delete("new-project", "new-main-service-project", "new-main-job-project", "new-main-interface-project", "new-main-admin-project")
  110. owner.Labels = labels.List()
  111. if len(owner.Labels) == 0 {
  112. return nil
  113. }
  114. for _, t := range ts {
  115. if isLib {
  116. if t.Name == "__bazel_build_job_name__" || t.Name == "__bazel_test_job_name__" {
  117. continue
  118. }
  119. }
  120. v := (deepcopy.Copy(t)).(config.Presubmit)
  121. v.Name = JobName(v.Name, owner.Labels[0])
  122. v.Context = Trigger(v.Name, owner.Labels[0])
  123. //v.Spec.Containers[0].Image = JobImage(v.Spec.Containers[0].Image)
  124. v.Spec.Containers[0].Name = v.Name
  125. for index, arg := range v.Spec.Containers[0].Args {
  126. if strings.Contains(arg, "<<bazel_dir_param>>") {
  127. v.Spec.Containers[0].Args[index] = JobBazelPath(v.Spec.Containers[0].Args[index], owner.Labels[0])
  128. }
  129. }
  130. v.UntrustedLabels = []string{}
  131. v.Trigger = DefaultTriggerFor(v.Name)
  132. v.RerunCommand = DefaultRerunCommandFor(v.Name)
  133. v.RunPRPushed = true
  134. v.TrustedLabels = append(v.TrustedLabels, owner.Labels[0])
  135. v.UntrustedLabels = append(v.UntrustedLabels, t.UntrustedLabels...)
  136. GlobalStatue.Result.Presubmits["platform/go-common"] = append(GlobalStatue.Result.Presubmits["platform/go-common"], v)
  137. }
  138. return nil
  139. }
  140. return nil
  141. })
  142. //GlobalStatue.AlwaysRun.Presubmits["platform/go-common"][0].RunAfterSuccess = append(GlobalStatue.AlwaysRun.Presubmits["platform/go-common"][0].RunAfterSuccess, GlobalStatue.Result.Presubmits["platform/go-common"]...)
  143. //GlobalStatue.AlwaysRun.Presubmits["platform/go-common"][0].RunAfterSuccess = append(GlobalStatue.AlwaysRun.Presubmits["platform/go-common"][0].RunAfterSuccess, GlobalStatue.AppendTask.Presubmits["platform/go-common"]...)
  144. for _, v := range GlobalStatue.AppendTask.Presubmits["platform/go-common"] {
  145. v.Trigger = DefaultTriggerFor(v.Name)
  146. v.RerunCommand = DefaultRerunCommandFor(v.Name)
  147. GlobalStatue.Result.Presubmits["platform/go-common"] = append(GlobalStatue.Result.Presubmits["platform/go-common"], v)
  148. }
  149. d, err := yaml.Marshal(GlobalStatue.Result)
  150. if err != nil {
  151. fmt.Println("fail to Marshal")
  152. }
  153. ioutil.WriteFile("./build/root/go_common_job.yaml", d, 0644)
  154. generateLabel()
  155. }
  156. func replaceimage() {
  157. ts, ok := GlobalStatue.Template.JobConfig.Presubmits["platform/go-common"]
  158. if !ok {
  159. fmt.Println("wrong project name")
  160. return
  161. }
  162. for _, t := range ts {
  163. t.Spec.Containers[0].Image = JobImage(t.Spec.Containers[0].Image)
  164. }
  165. at, ok := GlobalStatue.AppendTask.JobConfig.Presubmits["platform/go-common"]
  166. if !ok {
  167. fmt.Println("wrong project name")
  168. return
  169. }
  170. for _, t := range at {
  171. t.Spec.Containers[0].Image = JobImage(t.Spec.Containers[0].Image)
  172. }
  173. ar, ok := GlobalStatue.AlwaysRun.JobConfig.Presubmits["platform/go-common"]
  174. if !ok {
  175. fmt.Println("wrong project name")
  176. return
  177. }
  178. for _, a := range ar {
  179. a.Spec.Containers[0].Image = JobImage(a.Spec.Containers[0].Image)
  180. }
  181. }
  182. func generateLabel() (err error) {
  183. var repo RepoConfig
  184. repo.Labels = []Label{}
  185. for _, label := range GlobalStatue.Labels.List() {
  186. if GlobalStatue.DefaultLabels.Has(label) {
  187. continue
  188. }
  189. repo.Labels = append(repo.Labels, Label{
  190. Name: label,
  191. Color: "0052cc",
  192. Description: "Categorizes an issue or PR as relevant to " + label,
  193. Target: bothTarget,
  194. AddedBy: "anyone",
  195. ProwPlugin: "label",
  196. })
  197. }
  198. GlobalStatue.TemplateLabels.Repos = make(map[string]RepoConfig)
  199. GlobalStatue.TemplateLabels.Repos["platform/go-common"] = repo
  200. d, err := yaml.Marshal(GlobalStatue.TemplateLabels)
  201. if err != nil {
  202. fmt.Println("fail to Marshal")
  203. }
  204. ioutil.WriteFile("./build/root/labels.yaml", d, 0644)
  205. return nil
  206. }
  207. func ReadTemplate() (err error) {
  208. GlobalStatue.Template, err = config.Load("./build/config.yaml", "./build/template/task")
  209. if err != nil {
  210. fmt.Println(err)
  211. return err
  212. }
  213. GlobalStatue.AlwaysRun, err = config.Load("./build/config.yaml", "./build/template/always_run.yaml")
  214. if err != nil {
  215. fmt.Println(err)
  216. return err
  217. }
  218. GlobalStatue.AppendTask, err = config.Load("./build/config.yaml", "./build/template/append_task")
  219. if err != nil {
  220. fmt.Println(err)
  221. return err
  222. }
  223. yamlFile, err := ioutil.ReadFile("./build/template/image.yaml")
  224. if err != nil {
  225. fmt.Println("yamlFile.Get err ", err)
  226. }
  227. var i Image
  228. err = yaml.Unmarshal(yamlFile, &i)
  229. if err != nil {
  230. fmt.Println("Unmarshal: ", err)
  231. return err
  232. }
  233. for _, im := range i.Image {
  234. GlobalStatue.Image[im.Name] = im.Image
  235. }
  236. labelTemp, err := ioutil.ReadFile("./build/labels-temp.yaml")
  237. if err != nil {
  238. fmt.Println("labels-temp err ", err)
  239. }
  240. var labels Configuration
  241. err = yaml.Unmarshal(labelTemp, &labels)
  242. if err != nil {
  243. fmt.Println("Unmarshal: ", err)
  244. return err
  245. }
  246. for _, n := range labels.Default.Labels {
  247. GlobalStatue.DefaultLabels.Insert(n.Name)
  248. }
  249. GlobalStatue.TemplateLabels = labels
  250. replaceimage()
  251. return nil
  252. }
  253. func init() {
  254. GlobalStatue.TemplateLabels = Configuration{}
  255. GlobalStatue.Labels = sets.NewString()
  256. GlobalStatue.DefaultLabels = sets.NewString()
  257. GlobalStatue.Result = &config.Config{}
  258. GlobalStatue.Result.Presubmits = make(map[string][]config.Presubmit)
  259. GlobalStatue.Image = make(map[string]string)
  260. }
  261. func main() {
  262. ReadTemplate()
  263. generate()
  264. }