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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package sourcehut
import (
"context"
"testing"
webhook_model "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/webhook/shared"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func gitInit(t testing.TB) {
if setting.Git.HomePath != "" {
return
}
t.Cleanup(test.MockVariableValue(&setting.Git.HomePath, t.TempDir()))
require.NoError(t, git.InitSimple(context.Background()))
}
func TestSourcehutBuildsPayload(t *testing.T) {
gitInit(t)
defer test.MockVariableValue(&setting.RepoRootPath, ".")()
defer test.MockVariableValue(&setting.AppURL, "https://example.forgejo.org/")()
repo := &api.Repository{
HTMLURL: "http://localhost:3000/testdata/repo",
Name: "repo",
FullName: "testdata/repo",
Owner: &api.User{
UserName: "testdata",
},
CloneURL: "http://localhost:3000/testdata/repo.git",
}
pc := sourcehutConvertor{
ctx: git.DefaultContext,
meta: BuildsMeta{
ManifestPath: "adjust me in each test",
Visibility: "UNLISTED",
Secrets: true,
},
}
t.Run("Create/branch", func(t *testing.T) {
p := &api.CreatePayload{
Sha: "58771003157b81abc6bf41df0c5db4147a3e3c83",
Ref: "refs/heads/test",
RefType: "branch",
Repo: repo,
}
pc.meta.ManifestPath = "simple.yml"
pl, err := pc.Create(p)
require.NoError(t, err)
assert.Equal(t, `sources:
- http://localhost:3000/testdata/repo.git#58771003157b81abc6bf41df0c5db4147a3e3c83
environment:
BUILD_SUBMITTER: forgejo
BUILD_SUBMITTER_URL: https://example.forgejo.org/
GIT_REF: refs/heads/test
image: alpine/edge
tasks:
- say-hello: |
echo hello
- say-world: echo world
`, pl.Variables.Manifest)
assert.Equal(t, buildsVariables{
Manifest: pl.Variables.Manifest, // the manifest correctness is checked above, for nicer diff on error
Note: "branch test created",
Tags: []string{"testdata/repo", "branch/test", "simple.yml"},
Secrets: true,
Execute: true,
Visibility: "UNLISTED",
}, pl.Variables)
})
t.Run("Create/tag", func(t *testing.T) {
p := &api.CreatePayload{
Sha: "58771003157b81abc6bf41df0c5db4147a3e3c83",
Ref: "refs/tags/v1.0.0",
RefType: "tag",
Repo: repo,
}
pc.meta.ManifestPath = "simple.yml"
pl, err := pc.Create(p)
require.NoError(t, err)
assert.Equal(t, `sources:
- http://localhost:3000/testdata/repo.git#58771003157b81abc6bf41df0c5db4147a3e3c83
environment:
BUILD_SUBMITTER: forgejo
BUILD_SUBMITTER_URL: https://example.forgejo.org/
GIT_REF: refs/tags/v1.0.0
image: alpine/edge
tasks:
- say-hello: |
echo hello
- say-world: echo world
`, pl.Variables.Manifest)
assert.Equal(t, buildsVariables{
Manifest: pl.Variables.Manifest, // the manifest correctness is checked above, for nicer diff on error
Note: "tag v1.0.0 created",
Tags: []string{"testdata/repo", "tag/v1.0.0", "simple.yml"},
Secrets: true,
Execute: true,
Visibility: "UNLISTED",
}, pl.Variables)
})
t.Run("Delete", func(t *testing.T) {
p := &api.DeletePayload{}
pl, err := pc.Delete(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("Fork", func(t *testing.T) {
p := &api.ForkPayload{}
pl, err := pc.Fork(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("Push/simple", func(t *testing.T) {
p := &api.PushPayload{
Ref: "refs/heads/main",
HeadCommit: &api.PayloadCommit{
ID: "58771003157b81abc6bf41df0c5db4147a3e3c83",
Message: "add simple",
},
Repo: repo,
}
pc.meta.ManifestPath = "simple.yml"
pl, err := pc.Push(p)
require.NoError(t, err)
assert.Equal(t, `sources:
- http://localhost:3000/testdata/repo.git#58771003157b81abc6bf41df0c5db4147a3e3c83
environment:
BUILD_SUBMITTER: forgejo
BUILD_SUBMITTER_URL: https://example.forgejo.org/
GIT_REF: refs/heads/main
image: alpine/edge
tasks:
- say-hello: |
echo hello
- say-world: echo world
`, pl.Variables.Manifest)
assert.Equal(t, buildsVariables{
Manifest: pl.Variables.Manifest, // the manifest correctness is checked above, for nicer diff on error
Note: "add simple",
Tags: []string{"testdata/repo", "branch/main", "simple.yml"},
Secrets: true,
Execute: true,
Visibility: "UNLISTED",
}, pl.Variables)
})
t.Run("Push/complex", func(t *testing.T) {
p := &api.PushPayload{
Ref: "refs/heads/main",
HeadCommit: &api.PayloadCommit{
ID: "b0404943256a1f5a50c3726f4378756b4c1e5704",
Message: "replace simple with complex",
},
Repo: repo,
}
pc.meta.ManifestPath = "complex.yaml"
pc.meta.Visibility = "PRIVATE"
pc.meta.Secrets = false
pl, err := pc.Push(p)
require.NoError(t, err)
assert.Equal(t, `sources:
- http://localhost:3000/testdata/repo.git#b0404943256a1f5a50c3726f4378756b4c1e5704
environment:
BUILD_SUBMITTER: forgejo
BUILD_SUBMITTER_URL: https://example.forgejo.org/
GIT_REF: refs/heads/main
deploy: synapse@synapse-bt.org
image: archlinux
packages:
- nodejs
- npm
- rsync
secrets:
- 7ebab768-e5e4-4c9d-ba57-ec41a72c5665
tasks: []
triggers:
- condition: failure
action: email
to: Jim Jimson <jim@example.org>
# report back the status
- condition: always
action: webhook
url: https://hook.example.org
`, pl.Variables.Manifest)
assert.Equal(t, buildsVariables{
Manifest: pl.Variables.Manifest, // the manifest correctness is checked above, for nicer diff on error
Note: "replace simple with complex",
Tags: []string{"testdata/repo", "branch/main", "complex.yaml"},
Secrets: false,
Execute: true,
Visibility: "PRIVATE",
}, pl.Variables)
})
t.Run("Push/error", func(t *testing.T) {
p := &api.PushPayload{
Ref: "refs/heads/main",
HeadCommit: &api.PayloadCommit{
ID: "58771003157b81abc6bf41df0c5db4147a3e3c83",
Message: "add simple",
},
Repo: repo,
}
pc.meta.ManifestPath = "non-existing.yml"
pl, err := pc.Push(p)
require.NoError(t, err)
assert.Equal(t, graphqlPayload[buildsVariables]{
Error: "testdata/repo:refs/heads/main could not open manifest \"non-existing.yml\"",
}, pl)
})
t.Run("Issue", func(t *testing.T) {
p := &api.IssuePayload{}
p.Action = api.HookIssueOpened
pl, err := pc.Issue(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
p.Action = api.HookIssueClosed
pl, err = pc.Issue(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("IssueComment", func(t *testing.T) {
p := &api.IssueCommentPayload{}
pl, err := pc.IssueComment(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("PullRequest", func(t *testing.T) {
p := &api.PullRequestPayload{}
pl, err := pc.PullRequest(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("PullRequestComment", func(t *testing.T) {
p := &api.IssueCommentPayload{
IsPull: true,
}
pl, err := pc.IssueComment(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("Review", func(t *testing.T) {
p := &api.PullRequestPayload{}
p.Action = api.HookIssueReviewed
pl, err := pc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("Repository", func(t *testing.T) {
p := &api.RepositoryPayload{}
pl, err := pc.Repository(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("Package", func(t *testing.T) {
p := &api.PackagePayload{}
pl, err := pc.Package(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("Wiki", func(t *testing.T) {
p := &api.WikiPayload{}
p.Action = api.HookWikiCreated
pl, err := pc.Wiki(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
p.Action = api.HookWikiEdited
pl, err = pc.Wiki(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
p.Action = api.HookWikiDeleted
pl, err = pc.Wiki(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
t.Run("Release", func(t *testing.T) {
p := &api.ReleasePayload{}
pl, err := pc.Release(p)
require.Equal(t, shared.ErrPayloadTypeNotSupported, err)
require.Equal(t, graphqlPayload[buildsVariables]{}, pl)
})
}
func TestSourcehutJSONPayload(t *testing.T) {
gitInit(t)
defer test.MockVariableValue(&setting.RepoRootPath, ".")()
defer test.MockVariableValue(&setting.AppURL, "https://example.forgejo.org/")()
repo := &api.Repository{
HTMLURL: "http://localhost:3000/testdata/repo",
Name: "repo",
FullName: "testdata/repo",
Owner: &api.User{
UserName: "testdata",
},
CloneURL: "http://localhost:3000/testdata/repo.git",
}
p := &api.PushPayload{
Ref: "refs/heads/main",
HeadCommit: &api.PayloadCommit{
ID: "58771003157b81abc6bf41df0c5db4147a3e3c83",
Message: "json test",
},
Repo: repo,
}
data, err := p.JSONPayload()
require.NoError(t, err)
hook := &webhook_model.Webhook{
RepoID: 3,
IsActive: true,
Type: webhook_module.MATRIX,
URL: "https://sourcehut.example.com/api/jobs",
Meta: `{"manifest_path":"simple.yml"}`,
}
task := &webhook_model.HookTask{
HookID: hook.ID,
EventType: webhook_module.HookEventPush,
PayloadContent: string(data),
PayloadVersion: 2,
}
req, reqBody, err := BuildsHandler{}.NewRequest(context.Background(), hook, task)
require.NoError(t, err)
require.NotNil(t, req)
require.NotNil(t, reqBody)
assert.Equal(t, "POST", req.Method)
assert.Equal(t, "/api/jobs", req.URL.Path)
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
var body graphqlPayload[buildsVariables]
err = json.NewDecoder(req.Body).Decode(&body)
require.NoError(t, err)
assert.Equal(t, "json test", body.Variables.Note)
}
|