99 "context"
1010 "fmt"
1111 "io"
12+ "net/url"
1213 "os"
1314 "path/filepath"
1415 "strings"
@@ -656,10 +657,25 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
656657 if pr .IsForkPullRequest () && pr .State != "closed" {
657658 if pr .Head .OwnerName != "" {
658659 remote := pr .Head .OwnerName
659- _ , ok := g .prHeadCache [remote ]
660+ ref := pr .Head .Ref
661+ cloneURL := pr .Head .CloneURL
662+ ok := false
663+
664+ ref = strings .ReplaceAll (strings .ReplaceAll (ref , " " , "" ), "'" , "" )
665+ _ , err := url .Parse (remote )
666+ if err != nil {
667+ remote = ""
668+ }
669+ _ , err = url .Parse (cloneURL )
670+ if err != nil {
671+ cloneURL = ""
672+ }
673+ // TODO: lint remote & ref
674+
675+ _ , ok = g .prHeadCache [remote ]
660676 if ! ok {
661677 // git remote add
662- err := g .gitRepo .AddRemote (remote , pr . Head . CloneURL , true )
678+ err := g .gitRepo .AddRemote (remote , cloneURL , true )
663679 if err != nil {
664680 log .Error ("AddRemote failed: %s" , err )
665681 } else {
@@ -669,11 +685,11 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
669685 }
670686
671687 if ok {
672- _ , err = git .NewCommand ( "fetch" , remote , pr . Head . Ref ).RunInDir (g .repo .RepoPath ())
688+ _ , err = git .NewCommandContext ( g . ctx , "fetch" , remote , ref ).RunInDir (g .repo .RepoPath ())
673689 if err != nil {
674- log .Error ("Fetch branch from %s failed: %v" , pr . Head . CloneURL , err )
690+ log .Error ("Fetch branch from %s failed: %v" , cloneURL , err )
675691 } else {
676- headBranch := filepath .Join (g .repo .RepoPath (), "refs" , "heads" , pr . Head . OwnerName , pr . Head . Ref )
692+ headBranch := filepath .Join (g .repo .RepoPath (), "refs" , "heads" , remote , ref )
677693 if err := os .MkdirAll (filepath .Dir (headBranch ), os .ModePerm ); err != nil {
678694 return nil , err
679695 }
@@ -686,7 +702,7 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
686702 if err != nil {
687703 return nil , err
688704 }
689- head = pr . Head . OwnerName + "/" + pr . Head . Ref
705+ head = remote + "/" + ref
690706 }
691707 }
692708 }
0 commit comments