Monorepo for Tangled tangled.org
859
fork

Configure Feed

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

knotserver: create a fork PR link #341

open opened by willdot.net targeting master from willdot.net/tangled-fork: fork-pr-command

This should allow the PR link when pushing, to cater for forks.

Full disclosure, I haven't tested this because I can't get my local knot setup to work properly 🫠

Signed-off-by: Will Andrews did:plc:dadhhalkfcq3gucaq25hjqon

Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:dadhhalkfcq3gucaq25hjqon/sh.tangled.repo.pull/3ml7kaijyha22
+108 -7
Diff #1
+18
knotserver/git/git.go
··· 281 281 return strings.TrimSpace(string(output)), nil 282 282 } 283 283 284 + func (g *GitRepo) Remote() (string, error) { 285 + remote, err := g.r.Remote("origin") 286 + if err != nil { 287 + return "", err 288 + } 289 + 290 + if remote == nil { 291 + return "", nil 292 + } 293 + 294 + urls := remote.Config().URLs 295 + if len(urls) == 0 { 296 + return "", nil 297 + } 298 + 299 + return urls[0], nil 300 + } 301 + 284 302 // WriteTar writes itself from a tree into a binary tar file format. 285 303 // prefix is root folder to be appended. 286 304 func (g *GitRepo) WriteTar(w io.Writer, prefix string) error {
+46 -7
knotserver/internal.go
··· 453 453 return err 454 454 } 455 455 456 + remote, err := gr.Remote() 457 + if err != nil { 458 + return fmt.Errorf("checking for upstream remote: %w", err) 459 + } 460 + 456 461 defaultBranch, err := gr.FindMainBranch() 457 462 if err != nil { 458 463 return err ··· 471 476 user = userIdent.Handle.String() 472 477 } 473 478 474 - query := url.Values{} 475 - query.Set("source", "branch") 476 - query.Set("sourceBranch", pushedBranch) 477 - query.Set("targetBranch", defaultBranch) 478 - 479 - basePath, err := url.JoinPath(h.c.AppViewEndpoint, user, repoName, "pulls", "new") 479 + pullURL, err := createPullURL(h.c.AppViewEndpoint, remote, user, repoName, pushedBranch, defaultBranch) 480 480 if err != nil { 481 481 return err 482 482 } 483 - pullURL := basePath + "?" + query.Encode() 484 483 485 484 ZWS := "\u200B" 486 485 *clientMsgs = append(*clientMsgs, ZWS) ··· 490 489 return nil 491 490 } 492 491 492 + func createPullURL(appviewURL, remote, user, repoName, pushedBranch, defaultBranch string) (string, error) { 493 + if remote != "" { 494 + return createForkPullURL(appviewURL, remote, user, repoName, pushedBranch, defaultBranch) 495 + } 496 + 497 + query := url.Values{} 498 + 499 + query.Set("source", "branch") 500 + query.Set("sourceBranch", pushedBranch) 501 + query.Set("targetBranch", defaultBranch) 502 + 503 + basePath, err := url.JoinPath(appviewURL, user, repoName, "pulls", "new") 504 + if err != nil { 505 + return "", err 506 + } 507 + pullURL := basePath + "?" + query.Encode() 508 + return pullURL, nil 509 + } 510 + 511 + func createForkPullURL(appviewURL, remote, user, repoName, pushedBranch, defaultBranch string) (string, error) { 512 + query := url.Values{} 513 + 514 + query.Set("fork", fmt.Sprintf("%s/%s", user, repoName)) 515 + query.Set("source", "fork") 516 + query.Set("sourceBranch", pushedBranch) 517 + query.Set("targetBranch", defaultBranch) 518 + 519 + u, err := url.Parse(remote) 520 + if err != nil { 521 + return "", fmt.Errorf("invalid remote: %w", err) 522 + } 523 + 524 + basePath, err := url.JoinPath(appviewURL, u.Path, "pulls", "new") 525 + if err != nil { 526 + return "", err 527 + } 528 + pullURL := basePath + "?" + query.Encode() 529 + return pullURL, nil 530 + } 531 + 493 532 func Internal(ctx context.Context, c *config.Config, db *db.DB, e *rbac.Enforcer, n *notifier.Notifier, res *idresolver.Resolver) http.Handler { 494 533 r := chi.NewRouter() 495 534 l := log.FromContext(ctx)
+44
knotserver/internal_test.go
··· 1 + package knotserver 2 + 3 + import ( 4 + "testing" 5 + 6 + "github.com/alecthomas/assert/v2" 7 + "github.com/stretchr/testify/require" 8 + ) 9 + 10 + const ( 11 + appviewURL = "https://tangled.org/" 12 + user = "did:plc:dadhhalkfcq3gucaq25hjqon" 13 + pushedBranch = "feature-abc" 14 + defaultBranch = "main" 15 + ) 16 + 17 + func TestCreatePullURL(t *testing.T) { 18 + 19 + tt := map[string]struct { 20 + repoName string 21 + remote string 22 + expectedURL string 23 + }{ 24 + "not a fork": { 25 + repoName: "knot-testing", 26 + remote: "", 27 + expectedURL: "https://tangled.org/did:plc:dadhhalkfcq3gucaq25hjqon/knot-testing/pulls/new?source=branch&sourceBranch=feature-abc&targetBranch=main", 28 + }, 29 + "is fork": { 30 + repoName: "knot-testing-fork", 31 + remote: "https://knot1.tangled.sh/did:plc:dadhhalkfcq3gucaq25hjqon/knot-testing", 32 + expectedURL: "https://tangled.org/did:plc:dadhhalkfcq3gucaq25hjqon/knot-testing/pulls/new?fork=did%3Aplc%3Adadhhalkfcq3gucaq25hjqon%2Fknot-testing-fork&source=fork&sourceBranch=feature-abc&targetBranch=main", 33 + }, 34 + } 35 + 36 + for name, tc := range tt { 37 + t.Run(name, func(t *testing.T) { 38 + res, err := createPullURL(appviewURL, tc.remote, user, tc.repoName, pushedBranch, defaultBranch) 39 + require.NoError(t, err) 40 + 41 + assert.Equal(t, tc.expectedURL, res) 42 + }) 43 + } 44 + }

History

5 rounds 0 comments
sign up or login to add to the discussion
1 commit
expand
knotserver: create a fork PR link
no conflicts, ready to merge
expand 0 comments
1 commit
expand
knotserver: create a fork PR link
expand 0 comments
1 commit
expand
knotserver: create a fork PR link
expand 0 comments
1 commit
expand
knotserver: create a fork PR link
expand 0 comments
1 commit
expand
knotserver: create a fork PR link
expand 0 comments