-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.go
138 lines (124 loc) · 3.97 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
package main
// Copyright 2021 The Knative Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"gopkg.in/yaml.v1"
)
func main() {
// For each directory in actions:
// If there is an `auto-apply.yaml`:
// Read the yaml
// Use text.template to substitute values into `actions_template.yaml` from this dir
// Write the output to `.github/workflows/auto-$(dirname).yaml`
// This should be run from the repo root.
entries, err := os.ReadDir("actions")
if err != nil {
log.Print("Failed to open 'actions':", err)
os.Exit(1)
}
templFuncs := map[string]interface{}{
"split": strings.Split,
"join": strings.Join,
"github": GitHub,
}
templ := template.New("actions_template.yaml").Funcs(templFuncs)
templ = template.Must(templ.ParseFiles(filepath.Join("cmd", "gen-actions", "actions_template.yaml")))
for _, entry := range entries {
if !entry.IsDir() {
log.Printf("Skipping 'actions/%s', not a directory", entry.Name())
continue
}
err := handleDir(entry.Name(), templ)
if err != nil {
log.Printf("Unable to process 'actions/%s': %s", entry.Name(), err)
os.Exit(1)
}
}
}
func handleDir(path string, templ *template.Template) error {
metafile := filepath.Join("actions", path, "auto-apply.yaml")
fi, err := os.Stat(metafile)
if os.IsNotExist(err) {
log.Printf("Skipping %s, no auto-apply.yaml", path)
return nil
}
if !fi.Mode().IsRegular() {
return fmt.Errorf("%q is not a regular file", metafile)
}
bytes, err := os.ReadFile(metafile)
if err != nil {
return fmt.Errorf("Unable to read %q: %w", metafile, err)
}
c := conf{}
c.Repos = map[string]RepoRef{}
// Set defaults before parsing c
c.PRBody = `${{ github.event.inputs.reason || "Cron" }} -${{ github.actor }}
/cc ${{ matrix.assignees }}
/assign ${{ matrix.assignees }}
Produced by: ${{ github.repository }}/actions/${{}}
Details:
` + "```\n" + `${{ steps.updatedeps.outputs.deplog }}` + "\n```"
c.Repos["config"] = RepoRef{} // Empty Name means "this repo"
c.Repos["main"] = RepoRef{Name: "${{ matrix.name }}"}
if err := yaml.Unmarshal(bytes, &c); err != nil {
return fmt.Errorf("Unable to parse %q: %w", metafile, err)
}
c.Action = path
// Deliberately using unix path convertion here.
c.ActionRef = "./config/actions/" + path
outfileName := filepath.Join(".github", "workflows", "auto-"+path+".yaml")
outfile, err := os.OpenFile(outfileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("Unable to open %q: %w", outfileName, err)
}
if err := templ.Execute(outfile, &c); err != nil {
os.Remove(outfileName)
return fmt.Errorf("Failed to write %q: %w", outfileName, err)
}
log.Printf("Generated %q for %s", outfileName, path)
return nil
}
type conf struct {
Action string `yaml:"-"`
ActionRef string `yaml:"-"`
ShortName string `yaml:"shortName"`
Title string
PRTitle string `yaml:"prTitle"`
PRBody string `yaml:"prBody"`
CommitMessage string `yaml:"commitMessage"`
Repos map[string]RepoRef
Inputs []Input
// With allows passing _extra_ inputs to the action; all inputs will automatically be passed
With map[string]string
}
type Input struct {
Name string
Description string
Required bool
Default string
}
type RepoRef struct {
Name string
Ref string
}
// GitHub returns a github variable substitution
func GitHub(args ...string) string {
return "${{ " + strings.Join(args, " ") + " }}"
}