@@ -9,7 +9,10 @@ import (
9
9
"fmt"
10
10
"io"
11
11
"net/http"
12
+ "net/url"
13
+ "strings"
12
14
"testing"
15
+ "time"
13
16
14
17
auth_model "code.gitea.io/gitea/models/auth"
15
18
"code.gitea.io/gitea/models/db"
@@ -18,11 +21,15 @@ import (
18
21
repo_model "code.gitea.io/gitea/models/repo"
19
22
"code.gitea.io/gitea/models/unittest"
20
23
user_model "code.gitea.io/gitea/models/user"
24
+ "code.gitea.io/gitea/modules/git"
21
25
"code.gitea.io/gitea/modules/setting"
22
26
api "code.gitea.io/gitea/modules/structs"
27
+ "code.gitea.io/gitea/services/convert"
23
28
"code.gitea.io/gitea/services/forms"
24
29
"code.gitea.io/gitea/services/gitdiff"
25
30
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"
26
33
"code.gitea.io/gitea/tests"
27
34
28
35
"github.com/stretchr/testify/assert"
@@ -425,3 +432,94 @@ func TestAPICommitPullRequest(t *testing.T) {
425
432
req = NewRequestf (t , "GET" , "/api/v1/repos/%s/%s/commits/%s/pull" , owner .Name , repo .Name , invalidCommitSHA ).AddTokenAuth (ctx .Token )
426
433
ctx .Session .MakeRequest (t , req , http .StatusNotFound )
427
434
}
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
+ GitUserName : user1 .Name ,
461
+ GitUserEmail : user1 .Email ,
462
+ },
463
+ Committer : & files_service.IdentityOptions {
464
+ GitUserName : user1 .Name ,
465
+ GitUserEmail : 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 (t .Context (), 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