-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathplugin.go
More file actions
259 lines (215 loc) · 5.03 KB
/
plugin.go
File metadata and controls
259 lines (215 loc) · 5.03 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"context"
"os"
"github.com/appleboy/drone-git-push/repo"
)
type (
// Netrc structure
Netrc struct {
Machine string
Login string
Password string
}
// Commit structure
Commit struct {
Author Author
}
// Author structure
Author struct {
Name string
Email string
}
// Config structure
Config struct {
Key string
Remote string
RemoteName string
Branch string
LocalBranch string
Path string
Force bool
FollowTags bool
SkipVerify bool
Commit bool
CommitMessage string
Tag string
EmptyCommit bool
NoVerify bool
Rebase bool
Mirror bool
}
// Plugin Structure
Plugin struct {
Netrc Netrc
Commit Commit
Config Config
}
)
// Exec starts the plugin execution.
func (p Plugin) Exec(ctx context.Context) error {
if err := p.HandlePath(); err != nil {
return err
}
if err := p.WriteConfig(ctx); err != nil {
return err
}
if err := p.WriteKey(); err != nil {
return err
}
if err := p.WriteNetrc(); err != nil {
return err
}
if err := p.WriteToken(); err != nil {
return err
}
if err := p.HandleCommit(ctx); err != nil {
return err
}
if err := p.HandleTag(ctx); err != nil {
return err
}
if err := p.HandleRemote(ctx); err != nil {
return err
}
if err := p.HandleRebase(ctx); err != nil {
return err
}
if err := p.HandlePush(ctx); err != nil {
return err
}
return p.HandleCleanup(ctx)
}
// WriteConfig writes all required configurations.
func (p Plugin) WriteConfig(ctx context.Context) error {
if err := execute(repo.GlobalName(ctx, p.Commit.Author.Name)); err != nil {
return err
}
if err := execute(repo.GlobalUser(ctx, p.Commit.Author.Email)); err != nil {
return err
}
if p.Config.SkipVerify {
if err := execute(repo.SkipVerify(ctx)); err != nil {
return err
}
}
return nil
}
// WriteKey writes the private SSH key.
func (p Plugin) WriteKey() error {
return repo.WriteKey(
p.Config.Key,
)
}
// WriteNetrc writes the netrc config.
func (p Plugin) WriteNetrc() error {
return repo.WriteNetrc(
p.Netrc.Machine,
p.Netrc.Login,
p.Netrc.Password,
)
}
// WriteToken writes token.
func (p Plugin) WriteToken() error {
var err error
p.Config.Remote, err = repo.WriteToken(
p.Config.Remote,
p.Netrc.Login,
p.Netrc.Password,
)
return err
}
// HandleRemote adds the git remote if required.
// If the remote already exists, it updates the URL instead.
func (p Plugin) HandleRemote(ctx context.Context) error {
if p.Config.Remote != "" {
if repo.RemoteExists(ctx, p.Config.RemoteName) {
cmd := repo.RemoteSetURL(ctx, p.Config.RemoteName, p.Config.Remote)
if err := execute(cmd); err != nil {
return err
}
} else {
cmd := repo.RemoteAdd(ctx, p.Config.RemoteName, p.Config.Remote)
if err := execute(cmd); err != nil {
return err
}
}
}
return nil
}
// HandlePath changes to a different directory if required
func (p Plugin) HandlePath() error {
if p.Config.Path != "" {
if err := os.Chdir(p.Config.Path); err != nil {
return err
}
}
return nil
}
// HandleCommit commits dirty changes if required.
func (p Plugin) HandleCommit(ctx context.Context) error {
if p.Config.Commit {
if err := execute(repo.Add(ctx)); err != nil {
return err
}
if err := execute(repo.TestCleanTree(ctx)); err != nil {
// changes to commit
if err := execute(repo.ForceCommit(ctx, p.Config.CommitMessage, p.Config.NoVerify, p.Commit.Author.Name, p.Commit.Author.Email)); err != nil {
return err
}
} else { // no changes
if p.Config.EmptyCommit {
// no changes but commit anyway
if err := execute(repo.EmptyCommit(ctx, p.Config.CommitMessage, p.Config.NoVerify, p.Commit.Author.Name, p.Commit.Author.Email)); err != nil {
return err
}
}
}
}
return nil
}
// HandleTag add tag if required.
func (p Plugin) HandleTag(ctx context.Context) error {
if p.Config.Tag != "" {
if err := execute(repo.Tag(ctx, p.Config.Tag)); err != nil {
return err
}
}
return nil
}
// HandlePush pushs the changes to the remote repo.
func (p Plugin) HandlePush(ctx context.Context) error {
if p.Config.Mirror {
return execute(repo.RemotePushMirror(ctx, p.Config.RemoteName))
}
var (
name = p.Config.RemoteName
local = p.Config.LocalBranch
branch = p.Config.Branch
force = p.Config.Force
followtags = p.Config.FollowTags
)
return execute(repo.RemotePushNamedBranch(ctx, name, local, branch, force, followtags))
}
// HanldeRebase pull rebases before pushing
func (p Plugin) HandleRebase(ctx context.Context) error {
if p.Config.Rebase {
var (
name = p.Config.RemoteName
branch = p.Config.Branch
)
if err := execute(repo.RemotePullRebaseNamedBranch(ctx, name, branch)); err != nil {
return err
}
}
return nil
}
// HandleCleanup does eventually do some cleanup.
func (p Plugin) HandleCleanup(ctx context.Context) error {
if p.Config.Remote != "" {
if err := execute(repo.RemoteRemove(ctx, p.Config.RemoteName)); err != nil {
return err
}
}
return nil
}