-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflows.go
124 lines (108 loc) · 3.11 KB
/
workflows.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
package templater
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/google/go-github/v62/github"
"github.com/sirupsen/logrus"
)
type wfParameters struct {
Image string
BuildPre, BuildPost string
}
func (t *Templater) RenderWorkflows(ctx context.Context) error {
for owner, ownerRepos := range t.config.Repositories {
for repo, repoConfig := range ownerRepos {
if err := t.renderWorkflows(ctx, owner, repo, repoConfig); err != nil {
return err
}
}
}
return nil
}
func (t *Templater) loadWorkflowTemplates() {
t.loadTemplates.Do(func() {
const workflowsDir = "workflows"
files, err := os.ReadDir(workflowsDir)
if err != nil {
panic(err)
}
t.templates = make(map[string]*template.Template)
for _, f := range files {
fp := filepath.Join(workflowsDir, f.Name())
b, err := os.ReadFile(fp)
if err != nil {
panic(err)
}
fn := f.Name()
t.templates[fn] = template.Must(template.New(fn).Delims("{{{", "}}}").Funcs(sprig.TxtFuncMap()).Parse(string(b)))
}
})
}
func (t *Templater) renderWorkflows(ctx context.Context, owner, repo string, cfg *RepositoryConfiguration) error {
ref, _, err := t.client.Git.GetRef(ctx, owner, repo, "heads/main")
if err != nil {
return fmt.Errorf("getting base ref: %w", err)
}
refSHA := ref.GetObject().GetSHA()
logrus.WithFields(logrus.Fields{
"ref": refSHA,
}).Info("loaded base ref")
t.loadWorkflowTemplates()
params := wfParameters{
Image: fmt.Sprintf("%s/%s", t.config.Registry, repo),
}
if cfg != nil {
params.BuildPre = cfg.PreBuild
params.BuildPost = cfg.PostBuild
}
var entries []*github.TreeEntry
for name, tmpl := range t.templates {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, params); err != nil {
return err
}
rendered := buf.String()
// Skip if the content matches the current file:
wfPath := fmt.Sprintf(".github/workflows/%s", name)
if existing, _, _, err := t.client.Repositories.GetContents(ctx, owner, repo, wfPath, &github.RepositoryContentGetOptions{Ref: refSHA}); err == nil {
if ec, err := existing.GetContent(); err == nil && ec == rendered {
continue
}
}
entries = append(entries, &github.TreeEntry{
Path: github.String(wfPath),
Type: github.String("blob"),
Content: github.String(rendered),
Mode: github.String("100644"),
})
}
if len(entries) == 0 {
return nil
}
tree, _, err := t.client.Git.CreateTree(ctx, owner, repo, refSHA, entries)
if err != nil {
return fmt.Errorf("creating tree: %w", err)
}
commit, _, err := t.client.Git.CreateCommit(ctx, owner, repo, &github.Commit{
Message: github.String("Update workflows"),
Author: t.config.Committer,
Tree: tree,
Parents: []*github.Commit{
{SHA: ref.GetObject().SHA},
},
}, &github.CreateCommitOptions{})
if err != nil {
return fmt.Errorf("creating commit: %w", err)
}
logrus.WithField("commit", commit.GetSHA()).Info("created commit")
ref.Object.SHA = commit.SHA
if _, _, err = t.client.Git.UpdateRef(ctx, owner, repo, ref, false); err != nil {
return fmt.Errorf("updating ref: %w", err)
}
return nil
}