@@ -9,7 +9,10 @@ import (
99 "fmt"
1010 "io"
1111 "net/http"
12+ "net/url"
13+ "strings"
1214 "testing"
15+ "time"
1316
1417 auth_model "code.gitea.io/gitea/models/auth"
1518 "code.gitea.io/gitea/models/db"
@@ -18,11 +21,15 @@ import (
1821 repo_model "code.gitea.io/gitea/models/repo"
1922 "code.gitea.io/gitea/models/unittest"
2023 user_model "code.gitea.io/gitea/models/user"
24+ "code.gitea.io/gitea/modules/git"
2125 "code.gitea.io/gitea/modules/setting"
2226 api "code.gitea.io/gitea/modules/structs"
27+ "code.gitea.io/gitea/services/convert"
2328 "code.gitea.io/gitea/services/forms"
2429 "code.gitea.io/gitea/services/gitdiff"
2530 issue_service "code.gitea.io/gitea/services/issue"
31+ pull_service "code.gitea.io/gitea/services/pull"
32+ files_service "code.gitea.io/gitea/services/repository/files"
2633 "code.gitea.io/gitea/tests"
2734
2835 "github.com/stretchr/testify/assert"
@@ -425,3 +432,94 @@ func TestAPICommitPullRequest(t *testing.T) {
425432 req = NewRequestf (t , "GET" , "/api/v1/repos/%s/%s/commits/%s/pull" , owner .Name , repo .Name , invalidCommitSHA ).AddTokenAuth (ctx .Token )
426433 ctx .Session .MakeRequest (t , req , http .StatusNotFound )
427434}
435+
436+ func TestAPIViewPullFilesWithHeadRepoDeleted (t * testing.T ) {
437+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
438+ baseRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
439+ user1 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 1 })
440+
441+ ctx := NewAPITestContext (t , "user1" , baseRepo .Name , auth_model .AccessTokenScopeAll )
442+
443+ doAPIForkRepository (ctx , "user2" )(t )
444+
445+ forkedRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ForkID : baseRepo .ID , OwnerName : "user1" })
446+
447+ // add a new file to the forked repo
448+ addFileToForkedResp , err := files_service .ChangeRepoFiles (git .DefaultContext , forkedRepo , user1 , & files_service.ChangeRepoFilesOptions {
449+ Files : []* files_service.ChangeRepoFile {
450+ {
451+ Operation : "create" ,
452+ TreePath : "file_1.txt" ,
453+ ContentReader : strings .NewReader ("file1" ),
454+ },
455+ },
456+ Message : "add file1" ,
457+ OldBranch : "master" ,
458+ NewBranch : "fork-branch-1" ,
459+ Author : & files_service.IdentityOptions {
460+ Name : user1 .Name ,
461+ Email : user1 .Email ,
462+ },
463+ Committer : & files_service.IdentityOptions {
464+ Name : user1 .Name ,
465+ Email : user1 .Email ,
466+ },
467+ Dates : & files_service.CommitDateOptions {
468+ Author : time .Now (),
469+ Committer : time .Now (),
470+ },
471+ })
472+ assert .NoError (t , err )
473+ assert .NotEmpty (t , addFileToForkedResp )
474+
475+ // create Pull
476+ pullIssue := & issues_model.Issue {
477+ RepoID : baseRepo .ID ,
478+ Title : "Test pull-request-target-event" ,
479+ PosterID : user1 .ID ,
480+ Poster : user1 ,
481+ IsPull : true ,
482+ }
483+ pullRequest := & issues_model.PullRequest {
484+ HeadRepoID : forkedRepo .ID ,
485+ BaseRepoID : baseRepo .ID ,
486+ HeadBranch : "fork-branch-1" ,
487+ BaseBranch : "master" ,
488+ HeadRepo : forkedRepo ,
489+ BaseRepo : baseRepo ,
490+ Type : issues_model .PullRequestGitea ,
491+ }
492+
493+ prOpts := & pull_service.NewPullRequestOptions {Repo : baseRepo , Issue : pullIssue , PullRequest : pullRequest }
494+ err = pull_service .NewPullRequest (git .DefaultContext , prOpts )
495+ assert .NoError (t , err )
496+ pr := convert .ToAPIPullRequest (context .Background (), pullRequest , user1 )
497+
498+ ctx = NewAPITestContext (t , "user2" , baseRepo .Name , auth_model .AccessTokenScopeAll )
499+ doAPIGetPullFiles (ctx , pr , func (t * testing.T , files []* api.ChangedFile ) {
500+ if assert .Len (t , files , 1 ) {
501+ assert .Equal (t , "file_1.txt" , files [0 ].Filename )
502+ assert .Empty (t , files [0 ].PreviousFilename )
503+ assert .Equal (t , 1 , files [0 ].Additions )
504+ assert .Equal (t , 1 , files [0 ].Changes )
505+ assert .Equal (t , 0 , files [0 ].Deletions )
506+ assert .Equal (t , "added" , files [0 ].Status )
507+ }
508+ })(t )
509+
510+ // delete the head repository of the pull request
511+ forkCtx := NewAPITestContext (t , "user1" , forkedRepo .Name , auth_model .AccessTokenScopeAll )
512+ doAPIDeleteRepository (forkCtx )(t )
513+
514+ doAPIGetPullFiles (ctx , pr , func (t * testing.T , files []* api.ChangedFile ) {
515+ if assert .Len (t , files , 1 ) {
516+ assert .Equal (t , "file_1.txt" , files [0 ].Filename )
517+ assert .Empty (t , files [0 ].PreviousFilename )
518+ assert .Equal (t , 1 , files [0 ].Additions )
519+ assert .Equal (t , 1 , files [0 ].Changes )
520+ assert .Equal (t , 0 , files [0 ].Deletions )
521+ assert .Equal (t , "added" , files [0 ].Status )
522+ }
523+ })(t )
524+ })
525+ }
0 commit comments