@@ -82,6 +82,7 @@ func testGit(t *testing.T, u *url.URL) {
82
82
83
83
t .Run ("CreateAgitFlowPull" , doCreateAgitFlowPull (dstPath , & httpContext , "master" , "test/head" ))
84
84
t .Run ("BranchProtectMerge" , doBranchProtectPRMerge (& httpContext , dstPath ))
85
+ t .Run ("AutoMerge" , doAutoPRMerge (& httpContext , dstPath ))
85
86
t .Run ("CreatePRAndSetManuallyMerged" , doCreatePRAndSetManuallyMerged (httpContext , httpContext , dstPath , "master" , "test-manually-merge" ))
86
87
t .Run ("MergeFork" , func (t * testing.T ) {
87
88
defer PrintCurrentTest (t )()
@@ -615,6 +616,88 @@ func doBranchDelete(ctx APITestContext, owner, repo, branch string) func(*testin
615
616
}
616
617
}
617
618
619
+ func doAutoPRMerge (baseCtx * APITestContext , dstPath string ) func (t * testing.T ) {
620
+ return func (t * testing.T ) {
621
+ defer PrintCurrentTest (t )()
622
+
623
+ ctx := NewAPITestContext (t , baseCtx .Username , baseCtx .Reponame )
624
+
625
+ t .Run ("CheckoutProtected" , doGitCheckoutBranch (dstPath , "protected" ))
626
+ t .Run ("PullProtected" , doGitPull (dstPath , "origin" , "protected" ))
627
+ t .Run ("GenerateCommit" , func (t * testing.T ) {
628
+ _ ,
err := generateCommitWithNewData (
littleSize ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
629
+ assert .NoError (t , err )
630
+ })
631
+ t .Run ("PushToUnprotectedBranch" , doGitPushTestRepository (dstPath , "origin" , "protected:unprotected3" ))
632
+ var pr api.PullRequest
633
+ var err error
634
+ t .Run ("CreatePullRequest" , func (t * testing.T ) {
635
+ pr , err = doAPICreatePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , "protected" , "unprotected3" )(t )
636
+ assert .NoError (t , err )
637
+ })
638
+
639
+ // Request repository commits page
640
+ req := NewRequest (t , "GET" , fmt .Sprintf ("/%s/%s/pulls/%d/commits" , baseCtx .Username , baseCtx .Reponame , pr .Index ))
641
+ resp := ctx .Session .MakeRequest (t , req , http .StatusOK )
642
+ doc := NewHTMLParser (t , resp .Body )
643
+
644
+ // Get first commit URL
645
+ commitURL , exists := doc .doc .Find ("#commits-table tbody tr td.sha a" ).Last ().Attr ("href" )
646
+ assert .True (t , exists )
647
+ assert .NotEmpty (t , commitURL )
648
+
649
+ commitID := path .Base (commitURL )
650
+
651
+ // Call API to add Pending status for commit
652
+ t .Run ("CreateStatus" , doAPICreateCommitStatus (ctx , commitID , api .CommitStatusPending ))
653
+
654
+ // Cancel not existing auto merge
655
+ ctx .ExpectedCode = http .StatusNotFound
656
+ t .Run ("CancelAutoMergePR" , doAPICancelAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
657
+
658
+ // Add auto merge request
659
+ ctx .ExpectedCode = http .StatusCreated
660
+ t .Run ("AutoMergePR" , doAPIAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
661
+
662
+ // Can not create schedule twice
663
+ ctx .ExpectedCode = http .StatusConflict
664
+ t .Run ("AutoMergePRTwice" , doAPIAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
665
+
666
+ // Cancel auto merge request
667
+ ctx .ExpectedCode = http .StatusNoContent
668
+ t .Run ("CancelAutoMergePR" , doAPICancelAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
669
+
670
+ // Add auto merge request
671
+ ctx .ExpectedCode = http .StatusCreated
672
+ t .Run ("AutoMergePR" , doAPIAutoMergePullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index ))
673
+
674
+ // Check pr status
675
+ ctx .ExpectedCode = 0
676
+ pr , err = doAPIGetPullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index )(t )
677
+ assert .NoError (t , err )
678
+ assert .False (t , pr .HasMerged )
679
+
680
+ // Call API to add Failure status for commit
681
+ t .Run ("CreateStatus" , doAPICreateCommitStatus (ctx , commitID , api .CommitStatusFailure ))
682
+
683
+ // Check pr status
684
+ pr , err = doAPIGetPullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index )(t )
685
+ assert .NoError (t , err )
686
+ assert .False (t , pr .HasMerged )
687
+
688
+ // Call API to add Success status for commit
689
+ t .Run ("CreateStatus" , doAPICreateCommitStatus (ctx , commitID , api .CommitStatusSuccess ))
690
+
691
+ // wait to let gitea merge stuff
692
+ time .Sleep (time .Second )
693
+
694
+ // test pr status
695
+ pr , err = doAPIGetPullRequest (ctx , baseCtx .Username , baseCtx .Reponame , pr .Index )(t )
696
+ assert .NoError (t , err )
697
+ assert .True (t , pr .HasMerged )
698
+ }
699
+ }
700
+
618
701
func doCreateAgitFlowPull (dstPath string , ctx * APITestContext , baseBranch , headBranch string ) func (t * testing.T ) {
619
702
return func (t * testing.T ) {
620
703
defer PrintCurrentTest (t )()
0 commit comments