44package integration
55
66import (
7+ "encoding/base64"
78 "fmt"
9+ "net/http"
810 "net/url"
911 "strings"
1012 "testing"
@@ -24,7 +26,9 @@ import (
2426 "code.gitea.io/gitea/modules/git"
2527 "code.gitea.io/gitea/modules/gitrepo"
2628 "code.gitea.io/gitea/modules/setting"
29+ api "code.gitea.io/gitea/modules/structs"
2730 "code.gitea.io/gitea/modules/test"
31+ "code.gitea.io/gitea/modules/util"
2832 pull_service "code.gitea.io/gitea/services/pull"
2933 release_service "code.gitea.io/gitea/services/release"
3034 repo_service "code.gitea.io/gitea/services/repository"
@@ -451,3 +455,109 @@ func TestCreateDeleteRefEvent(t *testing.T) {
451455 assert .NotNil (t , run )
452456 })
453457}
458+
459+ func TestClosePullRequestWithPath (t * testing.T ) {
460+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
461+ // user2 is the owner of the base repo
462+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
463+ user2Token := getTokenForLoggedInUser (t , loginUser (t , user2 .Name ), auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
464+ // user4 is the owner of the fork repo
465+ user4 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 4 })
466+ user4Token := getTokenForLoggedInUser (t , loginUser (t , user4 .Name ), auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
467+
468+ // create the base repo
469+ req := NewRequestWithJSON (t , "POST" , "/api/v1/user/repos" , & api.CreateRepoOption {
470+ Name : "close-pull-request-with-path" ,
471+ Private : false ,
472+ Readme : "Default" ,
473+ AutoInit : true ,
474+ DefaultBranch : "main" ,
475+ }).AddTokenAuth (user2Token )
476+ resp := MakeRequest (t , req , http .StatusCreated )
477+ var apiBaseRepo api.Repository
478+ DecodeJSON (t , resp , & apiBaseRepo )
479+ baseRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : apiBaseRepo .ID })
480+ user2APICtx := NewAPITestContext (t , baseRepo .OwnerName , baseRepo .Name , auth_model .AccessTokenScopeWriteRepository )
481+
482+ // init the workflow
483+ wfTreePath := ".gitea/workflows/pull.yml"
484+ wfFileContent := `name: Pull Request
485+ on:
486+ pull_request:
487+ types:
488+ - closed
489+ paths:
490+ - 'app/**'
491+ jobs:
492+ echo:
493+ runs-on: ubuntu-latest
494+ steps:
495+ - run: echo 'Hello World'
496+ `
497+
498+ req = NewRequestWithJSON (t , "POST" , fmt .Sprintf ("/api/v1/repos/%s/%s/contents/%s" , baseRepo .OwnerName , baseRepo .Name , wfTreePath ), & api.CreateFileOptions {
499+ FileOptions : api.FileOptions {
500+ BranchName : baseRepo .DefaultBranch ,
501+ Message : "create " + wfTreePath ,
502+ Author : api.Identity {
503+ Name : user2 .Name ,
504+ Email : user2 .Email ,
505+ },
506+ Committer : api.Identity {
507+ Name : user2 .Name ,
508+ Email : user2 .Email ,
509+ },
510+ Dates : api.CommitDateOptions {
511+ Author : time .Now (),
512+ Committer : time .Now (),
513+ },
514+ },
515+ ContentBase64 : base64 .StdEncoding .EncodeToString ([]byte (wfFileContent )),
516+ }).AddTokenAuth (user2Token )
517+ MakeRequest (t , req , http .StatusCreated )
518+
519+ // user4 forks the repo
520+ req = NewRequestWithJSON (t , "POST" , fmt .Sprintf ("/api/v1/repos/%s/%s/forks" , baseRepo .OwnerName , baseRepo .Name ),
521+ & api.CreateForkOption {
522+ Name : util .ToPointer ("close-pull-request-with-path-fork" ),
523+ }).AddTokenAuth (user4Token )
524+ resp = MakeRequest (t , req , http .StatusAccepted )
525+ var apiForkRepo api.Repository
526+ DecodeJSON (t , resp , & apiForkRepo )
527+ forkRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : apiForkRepo .ID })
528+ user4APICtx := NewAPITestContext (t , user4 .Name , forkRepo .Name , auth_model .AccessTokenScopeWriteRepository )
529+
530+ // user4 creates a pull request to add file "app/main.go"
531+ doAPICreateFile (user4APICtx , "app/main.go" , & api.CreateFileOptions {
532+ FileOptions : api.FileOptions {
533+ NewBranchName : "user4/add-main" ,
534+ Message : "create main.go" ,
535+ Author : api.Identity {
536+ Name : user4 .Name ,
537+ Email : user4 .Email ,
538+ },
539+ Committer : api.Identity {
540+ Name : user4 .Name ,
541+ Email : user4 .Email ,
542+ },
543+ Dates : api.CommitDateOptions {
544+ Author : time .Now (),
545+ Committer : time .Now (),
546+ },
547+ },
548+ ContentBase64 : base64 .StdEncoding .EncodeToString ([]byte ("// main.go" )),
549+ })(t )
550+ apiPull , err := doAPICreatePullRequest (user4APICtx , baseRepo .OwnerName , baseRepo .Name , baseRepo .DefaultBranch , user4 .Name + ":user4/add-main" )(t )
551+ assert .NoError (t , err )
552+
553+ doAPIMergePullRequest (user2APICtx , baseRepo .OwnerName , baseRepo .Name , apiPull .Index )(t )
554+
555+ pullRequest := unittest .AssertExistsAndLoadBean (t , & issues_model.PullRequest {ID : apiPull .ID })
556+
557+ // load and compare ActionRun
558+ assert .Equal (t , 1 , unittest .GetCount (t , & actions_model.ActionRun {RepoID : baseRepo .ID }))
559+ actionRun := unittest .AssertExistsAndLoadBean (t , & actions_model.ActionRun {RepoID : baseRepo .ID })
560+ assert .Equal (t , actions_module .GithubEventPullRequest , actionRun .TriggerEvent )
561+ assert .Equal (t , pullRequest .MergedCommitID , actionRun .CommitSHA )
562+ })
563+ }
0 commit comments