From b7ede03f527224b1ce6999dfc1c891518c9fbf54 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 5 Mar 2021 12:29:36 +0100 Subject: [PATCH 1/6] Intorduce workaround for missing pull head sha --- cmd/pulls.go | 4 ++++ modules/task/pull_checkout.go | 5 +++++ modules/task/pull_clean.go | 5 +++++ modules/workaround/pull.go | 25 +++++++++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 modules/workaround/pull.go diff --git a/cmd/pulls.go b/cmd/pulls.go index b89011e..dddd67e 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/tea/modules/interact" "code.gitea.io/tea/modules/print" "code.gitea.io/tea/modules/utils" + "code.gitea.io/tea/modules/workaround" "code.gitea.io/sdk/gitea" "github.com/urfave/cli/v2" @@ -65,6 +66,9 @@ func runPullDetail(cmd *cli.Context, index string) error { if err != nil { return err } + if err := workaround.FixPullHeadSha(client, pr, ctx.Owner, ctx.Repo); err != nil { + return err + } reviews, _, err := client.ListPullReviews(ctx.Owner, ctx.Repo, idx, gitea.ListPullReviewsOptions{}) if err != nil { diff --git a/modules/task/pull_checkout.go b/modules/task/pull_checkout.go index 573b11d..95eb2b1 100644 --- a/modules/task/pull_checkout.go +++ b/modules/task/pull_checkout.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/tea/modules/config" local_git "code.gitea.io/tea/modules/git" + "code.gitea.io/tea/modules/workaround" "github.com/go-git/go-git/v5" git_plumbing "github.com/go-git/go-git/v5/plumbing" @@ -28,6 +29,10 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, forceCreateBr if err != nil { return err } + if err := workaround.FixPullHeadSha(client, pr, repoOwner, repoName); err != nil { + return err + } + remoteDeleted := pr.Head.Ref == fmt.Sprintf("refs/pull/%d/head", pr.Index) if remoteDeleted { return fmt.Errorf("Can't checkout: remote head branch was already deleted") diff --git a/modules/task/pull_clean.go b/modules/task/pull_clean.go index bcefbbc..e1a212b 100644 --- a/modules/task/pull_clean.go +++ b/modules/task/pull_clean.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/tea/modules/config" local_git "code.gitea.io/tea/modules/git" + "code.gitea.io/tea/modules/workaround" "code.gitea.io/sdk/gitea" git_config "github.com/go-git/go-git/v5/config" @@ -33,6 +34,10 @@ func PullClean(login *config.Login, repoOwner, repoName string, index int64, ign if err != nil { return err } + if err := workaround.FixPullHeadSha(client, pr, repoOwner, repoName); err != nil { + return err + } + if pr.State == gitea.StateOpen { return fmt.Errorf("PR is still open, won't delete branches") } diff --git a/modules/workaround/pull.go b/modules/workaround/pull.go new file mode 100644 index 0000000..0acf839 --- /dev/null +++ b/modules/workaround/pull.go @@ -0,0 +1,25 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package workaround + +import ( + "net/url" + + "code.gitea.io/sdk/gitea" +) + +// FixPullHeadSha is a workaround for https://github.com/go-gitea/gitea/issues/12675 +func FixPullHeadSha(client *gitea.Client, pr *gitea.PullRequest, repoOwner, repoName string) error { + if pr.Head != nil && pr.Head.Sha == "" { + headCommit, resp, err := client.GetSingleCommit(repoOwner, repoName, url.PathEscape(pr.Head.Ref)) + if err != nil && resp == nil || err != nil && resp.StatusCode != 404 { + return err + } + if headCommit != nil { + pr.Head.Sha = headCommit.SHA + } + } + return nil +} -- 2.40.1 From 5e5a3ce76deecbee4857785a802279e257692d3b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 5 Mar 2021 12:31:15 +0100 Subject: [PATCH 2/6] fix date --- modules/workaround/pull.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/workaround/pull.go b/modules/workaround/pull.go index 0acf839..c60d366 100644 --- a/modules/workaround/pull.go +++ b/modules/workaround/pull.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. +// Copyright 2021 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -- 2.40.1 From e73e60c58ff74e7bddf3fc2011d7459294042a7b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 5 Mar 2021 12:41:28 +0100 Subject: [PATCH 3/6] debug --- modules/workaround/pull.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/workaround/pull.go b/modules/workaround/pull.go index c60d366..10199ca 100644 --- a/modules/workaround/pull.go +++ b/modules/workaround/pull.go @@ -5,6 +5,7 @@ package workaround import ( + "fmt" "net/url" "code.gitea.io/sdk/gitea" @@ -13,11 +14,16 @@ import ( // FixPullHeadSha is a workaround for https://github.com/go-gitea/gitea/issues/12675 func FixPullHeadSha(client *gitea.Client, pr *gitea.PullRequest, repoOwner, repoName string) error { if pr.Head != nil && pr.Head.Sha == "" { + fmt.Println("TRY workaround") headCommit, resp, err := client.GetSingleCommit(repoOwner, repoName, url.PathEscape(pr.Head.Ref)) - if err != nil && resp == nil || err != nil && resp.StatusCode != 404 { + if resp != nil && resp.StatusCode == 404 { + fmt.Println("Got 404") + return nil + } else if err != nil { return err } if headCommit != nil { + fmt.Printf("Got: '%s'\n", headCommit.SHA) pr.Head.Sha = headCommit.SHA } } -- 2.40.1 From e361b6b9314fed597989c5e0fc004e7ff6942d57 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 5 Mar 2021 12:47:23 +0100 Subject: [PATCH 4/6] more debug --- modules/workaround/pull.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/workaround/pull.go b/modules/workaround/pull.go index 10199ca..0ff7899 100644 --- a/modules/workaround/pull.go +++ b/modules/workaround/pull.go @@ -14,7 +14,7 @@ import ( // FixPullHeadSha is a workaround for https://github.com/go-gitea/gitea/issues/12675 func FixPullHeadSha(client *gitea.Client, pr *gitea.PullRequest, repoOwner, repoName string) error { if pr.Head != nil && pr.Head.Sha == "" { - fmt.Println("TRY workaround") + fmt.Printf("TRY workaround (resolve '%s')\n", pr.Head.Ref) headCommit, resp, err := client.GetSingleCommit(repoOwner, repoName, url.PathEscape(pr.Head.Ref)) if resp != nil && resp.StatusCode == 404 { fmt.Println("Got 404") -- 2.40.1 From 37e27b51bf925367a510816e3a7a2c44a6ce5c9a Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Sun, 7 Mar 2021 15:52:29 +0100 Subject: [PATCH 5/6] use GetRepoRefs to resolve ref + simplify call signature --- cmd/pulls.go | 2 +- modules/task/pull_checkout.go | 2 +- modules/task/pull_clean.go | 2 +- modules/workaround/pull.go | 23 ++++++++++++----------- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index dddd67e..db789db 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -66,7 +66,7 @@ func runPullDetail(cmd *cli.Context, index string) error { if err != nil { return err } - if err := workaround.FixPullHeadSha(client, pr, ctx.Owner, ctx.Repo); err != nil { + if err := workaround.FixPullHeadSha(client, pr); err != nil { return err } diff --git a/modules/task/pull_checkout.go b/modules/task/pull_checkout.go index 95eb2b1..6140ea0 100644 --- a/modules/task/pull_checkout.go +++ b/modules/task/pull_checkout.go @@ -29,7 +29,7 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, forceCreateBr if err != nil { return err } - if err := workaround.FixPullHeadSha(client, pr, repoOwner, repoName); err != nil { + if err := workaround.FixPullHeadSha(client, pr); err != nil { return err } diff --git a/modules/task/pull_clean.go b/modules/task/pull_clean.go index e1a212b..0528cb4 100644 --- a/modules/task/pull_clean.go +++ b/modules/task/pull_clean.go @@ -34,7 +34,7 @@ func PullClean(login *config.Login, repoOwner, repoName string, index int64, ign if err != nil { return err } - if err := workaround.FixPullHeadSha(client, pr, repoOwner, repoName); err != nil { + if err := workaround.FixPullHeadSha(client, pr); err != nil { return err } diff --git a/modules/workaround/pull.go b/modules/workaround/pull.go index 0ff7899..54d7019 100644 --- a/modules/workaround/pull.go +++ b/modules/workaround/pull.go @@ -6,26 +6,27 @@ package workaround import ( "fmt" - "net/url" "code.gitea.io/sdk/gitea" ) // FixPullHeadSha is a workaround for https://github.com/go-gitea/gitea/issues/12675 -func FixPullHeadSha(client *gitea.Client, pr *gitea.PullRequest, repoOwner, repoName string) error { +// When no head sha is available, this is because the branch got deleted in the base repo. +// pr.Head.Ref points in this case not to the head repo branch name, but the base repo ref, +// which stays available to resolve the commit sha. +func FixPullHeadSha(client *gitea.Client, pr *gitea.PullRequest) error { + owner := pr.Base.Repository.Owner.UserName + repo := pr.Base.Repository.Name if pr.Head != nil && pr.Head.Sha == "" { fmt.Printf("TRY workaround (resolve '%s')\n", pr.Head.Ref) - headCommit, resp, err := client.GetSingleCommit(repoOwner, repoName, url.PathEscape(pr.Head.Ref)) - if resp != nil && resp.StatusCode == 404 { - fmt.Println("Got 404") - return nil - } else if err != nil { + refs, _, err := client.GetRepoRefs(owner, repo, pr.Head.Ref) + if err != nil { return err + } else if len(refs) == 0 { + return fmt.Errorf("unable to resolve PR ref '%s'", pr.Head.Ref) } - if headCommit != nil { - fmt.Printf("Got: '%s'\n", headCommit.SHA) - pr.Head.Sha = headCommit.SHA - } + pr.Head.Sha = refs[0].Object.SHA } + fmt.Println("SHA", pr.Head.Sha) return nil } -- 2.40.1 From 45199b9c2a9b71202e6a6a7f57e5e8c6bbec968e Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Sun, 7 Mar 2021 15:52:47 +0100 Subject: [PATCH 6/6] remove debug statements --- modules/workaround/pull.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/workaround/pull.go b/modules/workaround/pull.go index 54d7019..79d5b31 100644 --- a/modules/workaround/pull.go +++ b/modules/workaround/pull.go @@ -18,7 +18,6 @@ func FixPullHeadSha(client *gitea.Client, pr *gitea.PullRequest) error { owner := pr.Base.Repository.Owner.UserName repo := pr.Base.Repository.Name if pr.Head != nil && pr.Head.Sha == "" { - fmt.Printf("TRY workaround (resolve '%s')\n", pr.Head.Ref) refs, _, err := client.GetRepoRefs(owner, repo, pr.Head.Ref) if err != nil { return err @@ -27,6 +26,5 @@ func FixPullHeadSha(client *gitea.Client, pr *gitea.PullRequest) error { } pr.Head.Sha = refs[0].Object.SHA } - fmt.Println("SHA", pr.Head.Sha) return nil } -- 2.40.1