loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request 'feat(webhook): sourcehut: submit SSH URL for private repository (or when pre-filled)' (#6445) from oliverpool/forgejo:sourcehut-refactor into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6445
Reviewed-by: Gusted <gusted@noreply.codeberg.org>

Gusted cfb18e2b 520a1ab5

+151 -8
+1
release-notes/6445.md
··· 1 + feat: webhook: sourcehut: submit SSH URL for private repository or when pre-filled
+18 -8
services/webhook/sourcehut/builds.go
··· 8 8 "context" 9 9 "fmt" 10 10 "html/template" 11 + "io" 11 12 "io/fs" 12 13 "net/http" 13 14 "strings" ··· 189 190 return graphqlPayload[buildsVariables]{}, shared.ErrPayloadTypeNotSupported 190 191 } 191 192 192 - // mustBuildManifest adjusts the manifest to submit to the builds service 193 + // newPayload opens and adjusts the manifest to submit to the builds service 193 194 // 194 195 // in case of an error the Error field will be set, to be visible by the end-user under recent deliveries 195 196 func (pc sourcehutConvertor) newPayload(repo *api.Repository, commitID, ref, note string, trusted bool) (graphqlPayload[buildsVariables], error) { 196 - manifest, err := pc.buildManifest(repo, commitID, ref) 197 + manifest, err := pc.constructManifest(repo, commitID, ref) 197 198 if err != nil { 198 199 if len(manifest) == 0 { 199 200 return graphqlPayload[buildsVariables]{}, err ··· 238 239 }, nil 239 240 } 240 241 241 - // buildManifest adjusts the manifest to submit to the builds service 242 + // constructManifest opens and adjusts the manifest to submit to the builds service 242 243 // in case of an error the []byte might contain an error that can be displayed to the user 243 - func (pc sourcehutConvertor) buildManifest(repo *api.Repository, commitID, gitRef string) ([]byte, error) { 244 + func (pc sourcehutConvertor) constructManifest(repo *api.Repository, commitID, gitRef string) ([]byte, error) { 244 245 gitRepo, err := gitrepo.OpenRepository(pc.ctx, repo) 245 246 if err != nil { 246 247 msg := "could not open repository" ··· 265 266 } 266 267 defer r.Close() 267 268 269 + return adjustManifest(repo, commitID, gitRef, r, pc.meta.ManifestPath) 270 + } 271 + 272 + func adjustManifest(repo *api.Repository, commitID, gitRef string, r io.Reader, path string) ([]byte, error) { 268 273 // reference: https://man.sr.ht/builds.sr.ht/manifest.md 269 274 var manifest struct { 270 275 Sources []string `yaml:"sources"` ··· 273 278 Rest map[string]yaml.Node `yaml:",inline"` 274 279 } 275 280 if err := yaml.NewDecoder(r).Decode(&manifest); err != nil { 276 - msg := fmt.Sprintf("could not decode manifest %q", pc.meta.ManifestPath) 281 + msg := fmt.Sprintf("could not decode manifest %q", path) 277 282 return []byte(msg), fmt.Errorf(msg+": %w", err) 278 283 } 279 284 ··· 284 289 manifest.Environment["BUILD_SUBMITTER_URL"] = setting.AppURL 285 290 manifest.Environment["GIT_REF"] = gitRef 286 291 287 - source := repo.CloneURL + "#" + commitID 288 292 found := false 289 293 for i, s := range manifest.Sources { 290 - if s == repo.CloneURL { 291 - manifest.Sources[i] = source 294 + if s == repo.CloneURL || s == repo.SSHURL { 295 + manifest.Sources[i] = s + "#" + commitID 292 296 found = true 293 297 break 294 298 } 295 299 } 296 300 if !found { 301 + source := repo.CloneURL 302 + if repo.Private || setting.Repository.DisableHTTPGit { 303 + // default to ssh for private repos or when git clone is disabled over http 304 + source = repo.SSHURL 305 + } 306 + source += "#" + commitID 297 307 manifest.Sources = append(manifest.Sources, source) 298 308 } 299 309
+132
services/webhook/sourcehut/builds_test.go
··· 5 5 6 6 import ( 7 7 "context" 8 + "strings" 8 9 "testing" 9 10 10 11 webhook_model "code.gitea.io/gitea/models/webhook" ··· 384 385 require.NoError(t, err) 385 386 assert.Equal(t, "json test", body.Variables.Note) 386 387 } 388 + 389 + func TestSourcehutAdjustManifest(t *testing.T) { 390 + defer test.MockVariableValue(&setting.AppURL, "https://example.forgejo.org/")() 391 + t.Run("without sources", func(t *testing.T) { 392 + repo := &api.Repository{ 393 + CloneURL: "http://localhost:3000/testdata/repo.git", 394 + } 395 + 396 + manifest, err := adjustManifest(repo, "58771003157b81abc6bf41df0c5db4147a3e3c83", "refs/heads/main", strings.NewReader(`image: alpine/edge 397 + tasks: 398 + - say-hello: | 399 + echo hello 400 + - say-world: echo world`), ".build.yml") 401 + 402 + require.NoError(t, err) 403 + assert.Equal(t, `sources: 404 + - http://localhost:3000/testdata/repo.git#58771003157b81abc6bf41df0c5db4147a3e3c83 405 + environment: 406 + BUILD_SUBMITTER: forgejo 407 + BUILD_SUBMITTER_URL: https://example.forgejo.org/ 408 + GIT_REF: refs/heads/main 409 + image: alpine/edge 410 + tasks: 411 + - say-hello: | 412 + echo hello 413 + - say-world: echo world 414 + `, string(manifest)) 415 + }) 416 + 417 + t.Run("with other sources", func(t *testing.T) { 418 + repo := &api.Repository{ 419 + CloneURL: "http://localhost:3000/testdata/repo.git", 420 + } 421 + 422 + manifest, err := adjustManifest(repo, "58771003157b81abc6bf41df0c5db4147a3e3c83", "refs/heads/main", strings.NewReader(`image: alpine/edge 423 + sources: 424 + - http://other.example.conm/repo.git 425 + tasks: 426 + - hello: echo world`), ".build.yml") 427 + 428 + require.NoError(t, err) 429 + assert.Equal(t, `sources: 430 + - http://other.example.conm/repo.git 431 + - http://localhost:3000/testdata/repo.git#58771003157b81abc6bf41df0c5db4147a3e3c83 432 + environment: 433 + BUILD_SUBMITTER: forgejo 434 + BUILD_SUBMITTER_URL: https://example.forgejo.org/ 435 + GIT_REF: refs/heads/main 436 + image: alpine/edge 437 + tasks: 438 + - hello: echo world 439 + `, string(manifest)) 440 + }) 441 + 442 + t.Run("with same source", func(t *testing.T) { 443 + repo := &api.Repository{ 444 + CloneURL: "http://localhost:3000/testdata/repo.git", 445 + } 446 + 447 + manifest, err := adjustManifest(repo, "58771003157b81abc6bf41df0c5db4147a3e3c83", "refs/heads/main", strings.NewReader(`image: alpine/edge 448 + sources: 449 + - http://localhost:3000/testdata/repo.git 450 + - http://other.example.conm/repo.git 451 + tasks: 452 + - hello: echo world`), ".build.yml") 453 + 454 + require.NoError(t, err) 455 + assert.Equal(t, `sources: 456 + - http://localhost:3000/testdata/repo.git#58771003157b81abc6bf41df0c5db4147a3e3c83 457 + - http://other.example.conm/repo.git 458 + environment: 459 + BUILD_SUBMITTER: forgejo 460 + BUILD_SUBMITTER_URL: https://example.forgejo.org/ 461 + GIT_REF: refs/heads/main 462 + image: alpine/edge 463 + tasks: 464 + - hello: echo world 465 + `, string(manifest)) 466 + }) 467 + 468 + t.Run("with ssh source", func(t *testing.T) { 469 + repo := &api.Repository{ 470 + CloneURL: "http://localhost:3000/testdata/repo.git", 471 + SSHURL: "git@localhost:testdata/repo.git", 472 + } 473 + 474 + manifest, err := adjustManifest(repo, "58771003157b81abc6bf41df0c5db4147a3e3c83", "refs/heads/main", strings.NewReader(`image: alpine/edge 475 + sources: 476 + - git@localhost:testdata/repo.git 477 + - http://other.example.conm/repo.git 478 + tasks: 479 + - hello: echo world`), ".build.yml") 480 + 481 + require.NoError(t, err) 482 + assert.Equal(t, `sources: 483 + - git@localhost:testdata/repo.git#58771003157b81abc6bf41df0c5db4147a3e3c83 484 + - http://other.example.conm/repo.git 485 + environment: 486 + BUILD_SUBMITTER: forgejo 487 + BUILD_SUBMITTER_URL: https://example.forgejo.org/ 488 + GIT_REF: refs/heads/main 489 + image: alpine/edge 490 + tasks: 491 + - hello: echo world 492 + `, string(manifest)) 493 + }) 494 + 495 + t.Run("private without source", func(t *testing.T) { 496 + repo := &api.Repository{ 497 + CloneURL: "http://localhost:3000/testdata/repo.git", 498 + SSHURL: "git@localhost:testdata/repo.git", 499 + Private: true, 500 + } 501 + 502 + manifest, err := adjustManifest(repo, "58771003157b81abc6bf41df0c5db4147a3e3c83", "refs/heads/main", strings.NewReader(`image: alpine/edge 503 + tasks: 504 + - hello: echo world`), ".build.yml") 505 + 506 + require.NoError(t, err) 507 + assert.Equal(t, `sources: 508 + - git@localhost:testdata/repo.git#58771003157b81abc6bf41df0c5db4147a3e3c83 509 + environment: 510 + BUILD_SUBMITTER: forgejo 511 + BUILD_SUBMITTER_URL: https://example.forgejo.org/ 512 + GIT_REF: refs/heads/main 513 + image: alpine/edge 514 + tasks: 515 + - hello: echo world 516 + `, string(manifest)) 517 + }) 518 + }