-
Notifications
You must be signed in to change notification settings - Fork 0
/
playbook.go
218 lines (192 loc) · 4.71 KB
/
playbook.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"errors"
"os"
"os/exec"
"github.com/google/shlex"
"github.com/jonaz/mgit/pkg/git"
"github.com/jonaz/mgit/pkg/models"
"github.com/jonaz/mgit/pkg/providers"
"github.com/jonaz/mgit/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
func generatePlaybook(c *cli.Context) error {
p := &models.Playbook{
Tasks: []models.Task{
models.Task{
Actions: []models.Action{
{
Command: "",
Regexp: "",
With: "",
FileRegexp: "",
},
},
},
},
}
err := utils.InEachRepo(c.String("dir"), func(path string) error {
r := git.NewRepo(path)
origin, err := r.RemoteURL()
if err != nil {
return err
}
p.Tasks[0].Repos = append(p.Tasks[0].Repos, origin)
return nil
})
if err != nil {
return err
}
return yaml.NewEncoder(os.Stdout).Encode(p)
}
func readFile(fn string) (*models.Playbook, error) {
file, err := os.Open(fn)
if err != nil {
return nil, err
}
defer file.Close()
p := &models.Playbook{}
err = yaml.NewDecoder(file).Decode(&p)
return p, err
}
func runPlaybook(c *cli.Context) error {
p, err := readFile(c.Args().First())
if err != nil {
return err
}
for _, task := range p.Tasks {
task := task
provider := &providers.DefaultProvider{Dir: c.String("dir")}
for _, repoURL := range task.Repos {
dir := utils.RepoDir(c.String("dir"), repoURL)
logrus.Infof("clone %s into %s", repoURL, dir)
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
_, err := git.Clone(repoURL, dir)
if err != nil {
logrus.Error(err)
continue
}
} else {
r := git.NewRepo(dir)
err := r.Pull()
if err != nil {
logrus.Error(err)
}
}
provider.RepoURLWhitelist = append(provider.RepoURLWhitelist, repoURL)
}
for _, a := range task.Actions {
err := runAction(provider, a)
if err != nil {
return err
}
}
// ignore unchanged repos
newWhitelist := []string{}
err = eachRepoInPlay(provider, func(repo git.Repo) error {
hasChanges, err := repo.HasLocalChanges()
if err != nil {
return err
}
if hasChanges {
u, _ := repo.RemoteURL()
newWhitelist = append(newWhitelist, u)
}
return nil
})
if err != nil {
return err
}
if len(newWhitelist) == 0 {
logrus.Info("found no changes in repos in current play. aborting")
return nil
}
provider.RepoURLWhitelist = newWhitelist
err = eachRepoInPlay(provider, func(repo git.Repo) error {
logrus.Infof("%s: create branch %s", repo.WorkDir(), task.TargetBranch)
return repo.Checkout(task.TargetBranch)
})
if err != nil {
return err
}
err = eachRepoInPlay(provider, func(repo git.Repo) error {
logrus.Infof("%s: commit %s", repo.WorkDir(), task.TargetBranch)
return repo.Commit(task.CommitMessage, "")
})
if err != nil {
return err
}
err = eachRepoInPlay(provider, func(repo git.Repo) error {
repoURL, err := repo.RemoteURL()
if err != nil {
return err
}
logrus.Infof("%s: push %s", repo.WorkDir(), repoURL)
return repo.Push(repoURL, c.Bool("force"))
})
if err != nil {
return err
}
}
return nil
}
func eachRepoInPlay(provider providers.Provider, cb func(repo git.Repo) error) error {
return utils.InEachRepo(provider.WorkDir(), func(path string) error {
ok, err := provider.ShouldProcessRepo(path)
if err != nil {
return err
}
if !ok {
return nil
}
r := git.NewRepo(path)
return cb(r)
})
}
func openPR(c *cli.Context) error {
p, err := readFile(c.Args().First())
if err != nil {
return err
}
provider, err := providers.GetProvider(c)
if err != nil {
return err
}
repos := []string{}
for _, v := range p.Tasks {
repos = append(repos, v.Repos...)
}
return provider.PR(repos, c.String("mode"))
}
func runAction(provider providers.Provider, action models.Action) error {
if action.Regexp != "" && action.Command == "" {
err := provider.Replace(action.Regexp, action.With, action.FileRegexp, action.PathRegexp, action.ContentRegexp)
if err != nil {
return err
}
}
if action.Regexp == "" && action.Command != "" {
if len(action.ContentRegexp) > 0 || action.FileRegexp != "" || action.PathRegexp != "" {
return provider.CommandEachMatchingFile(action.Command, action.FileRegexp, action.PathRegexp, action.ContentRegexp)
}
err := eachRepoInPlay(provider, func(repo git.Repo) error {
logrus.Infof("%s: running command: %s", repo.WorkDir(), action.Command)
args, err := shlex.Split(action.Command)
if err != nil {
return err
}
cmd := exec.Command(args[0], args[1:]...) // #nosec
cmd.Dir = repo.WorkDir()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
})
if err != nil {
return err
}
}
return nil
}