7
7
"errors"
8
8
gohttp "net/http"
9
9
"path"
10
+ "path/filepath"
10
11
"regexp"
11
12
"strings"
12
13
"sync"
@@ -95,6 +96,10 @@ func GitLSRemote(repo *gaia.GitRepo) error {
95
96
// UpdateRepository takes a git type repository and updates
96
97
// it by pulling in new code if it's available.
97
98
func (s * GaiaPipelineService ) UpdateRepository (pipe * gaia.Pipeline ) error {
99
+ gaia .Cfg .Logger .Debug ("updating repository for pipeline type" , "type" , pipe .Type )
100
+ if pipe .Type == gaia .PTypeNodeJS {
101
+ pipe .Repo .LocalDest = filepath .Join (pipe .Repo .LocalDest , nodeJSInternalCloneFolder )
102
+ }
98
103
r , err := git .PlainOpen (pipe .Repo .LocalDest )
99
104
if err != nil {
100
105
// We don't stop gaia working because of an automated update failed.
@@ -110,7 +115,9 @@ func (s *GaiaPipelineService) UpdateRepository(pipe *gaia.Pipeline) error {
110
115
gaia .Cfg .Logger .Error ("error getting auth info while doing a pull request: " , "error" , err .Error ())
111
116
return err
112
117
}
118
+
113
119
tree , _ := r .Worktree ()
120
+
114
121
o := & git.PullOptions {
115
122
ReferenceName : plumbing .ReferenceName (pipe .Repo .SelectedBranch ),
116
123
SingleBranch : true ,
@@ -126,12 +133,20 @@ func (s *GaiaPipelineService) UpdateRepository(pipe *gaia.Pipeline) error {
126
133
return err
127
134
}
128
135
o .Auth = auth
129
- err = tree .Pull (o )
130
- if err != nil {
136
+ if err := tree .Pull (o ); err != nil {
131
137
return err
132
138
}
133
139
} else if strings .Contains (err .Error (), "worktree contains unstaged changes" ) {
134
- // ignore this error, the pull overwrote everything anyways.
140
+ gaia .Cfg .Logger .Error ("worktree contains unstaged changes, resetting" , "error" , err .Error ())
141
+ // Clean the worktree. Because of various builds, it can happen that the local folder if polluted.
142
+ // For example go build tends to edit the go.mod file.
143
+ if err := tree .Reset (& git.ResetOptions {
144
+ Mode : git .HardReset ,
145
+ }); err != nil {
146
+ gaia .Cfg .Logger .Error ("failed to reset worktree" , "error" , err .Error ())
147
+ return err
148
+ }
149
+ // Success, move on.
135
150
err = nil
136
151
} else {
137
152
// It's also an error if the repo is already up to date so we just move on.
@@ -142,14 +157,19 @@ func (s *GaiaPipelineService) UpdateRepository(pipe *gaia.Pipeline) error {
142
157
143
158
gaia .Cfg .Logger .Debug ("updating pipeline: " , "message" , pipe .Name )
144
159
b := newBuildPipeline (pipe .Type )
145
- createPipeline := & gaia.CreatePipeline {
146
- Pipeline : gaia.Pipeline {
147
- Repo : & gaia.GitRepo {},
148
- },
160
+ createPipeline := & gaia.CreatePipeline {Pipeline : * pipe }
161
+ if err := b .ExecuteBuild (createPipeline ); err != nil {
162
+ gaia .Cfg .Logger .Error ("error while executing the build" , "error" , err .Error ())
163
+ return err
164
+ }
165
+ if err := b .SavePipeline (& createPipeline .Pipeline ); err != nil {
166
+ gaia .Cfg .Logger .Error ("failed to save pipeline" , "error" , err .Error ())
167
+ return err
168
+ }
169
+ if err := b .CopyBinary (createPipeline ); err != nil {
170
+ gaia .Cfg .Logger .Error ("error while copying binary to plugin folder" , "error" , err .Error ())
171
+ return err
149
172
}
150
- createPipeline .Pipeline = * pipe
151
- _ = b .ExecuteBuild (createPipeline )
152
- _ = b .CopyBinary (createPipeline )
153
173
gaia .Cfg .Logger .Debug ("successfully updated: " , "message" , pipe .Name )
154
174
return nil
155
175
}
@@ -240,7 +260,8 @@ func NewGithubClient(httpClient *gohttp.Client, repoMock GithubRepoService) Gith
240
260
}
241
261
}
242
262
243
- func createGithubWebhook (token string , repo * gaia.GitRepo , gitRepo GithubRepoService ) error {
263
+ func createGithubWebhook (token string , repo * gaia.GitRepo , id string , gitRepo GithubRepoService ) error {
264
+ name := gaia .SecretNamePrefix + id
244
265
vault , err := services .DefaultVaultService ()
245
266
if err != nil {
246
267
gaia .Cfg .Logger .Error ("unable to initialize vault: " , "error" , err .Error ())
@@ -260,10 +281,10 @@ func createGithubWebhook(token string, repo *gaia.GitRepo, gitRepo GithubRepoSer
260
281
tc := oauth2 .NewClient (ctx , ts )
261
282
config := make (map [string ]interface {})
262
283
config ["url" ] = gaia .Cfg .Hostname + "/api/" + gaia .APIVersion + "/pipeline/githook"
263
- secret , err := vault .Get ("GITHUB_WEBHOOK_SECRET" )
284
+ secret , err := vault .Get (name )
264
285
if err != nil {
265
286
secret = []byte (generateWebhookSecret ())
266
- vault .Add ("GITHUB_WEBHOOK_SECRET" , secret )
287
+ vault .Add (name , secret )
267
288
err = vault .SaveSecrets ()
268
289
if err != nil {
269
290
return err
0 commit comments