@@ -810,6 +810,34 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
810810 return
811811 }
812812
813+ lastCommitStatus , err := pr .GetLastCommitStatus ()
814+ if err != nil {
815+ return
816+ }
817+ if form .MergeWhenChecksSucceed && ! lastCommitStatus .State .IsSuccess () {
818+ err = models .ScheduleAutoMerge (& models.ScheduledPullRequestMerge {
819+ PullID : pr .ID ,
820+ UserID : ctx .User .ID ,
821+ MergeStyle : models .MergeStyle (form .Do ),
822+ Message : message ,
823+ })
824+ if err != nil {
825+ ctx .Flash .Error (ctx .Tr ("repo.issues.dependency.pr_close_blocked" ))
826+ ctx .Redirect (ctx .Repo .RepoLink + "/pulls/" + com .ToStr (pr .Index ))
827+ return
828+ }
829+ ctx .Flash .Success (ctx .Tr ("repo.pulls.merge_on_status_success_success" ))
830+ ctx .Redirect (ctx .Repo .RepoLink + "/pulls/" + com .ToStr (pr .Index ))
831+ return
832+ }
833+ // Removing an auto merge pull request is something we can execute whether or not a pull request auto merge was
834+ // scheduled before, hece we can remove it without checking for its existence.
835+ err = models .RemoveScheduledMergeRequest (& models.ScheduledPullRequestMerge {PullID : pr .ID })
836+ if err != nil {
837+ ctx .ServerError ("RemoveScheduledMergeRequest" , err )
838+ return
839+ }
840+
813841 if err = pull_service .Merge (pr , ctx .User , ctx .Repo .GitRepo , models .MergeStyle (form .Do ), message ); err != nil {
814842 if models .IsErrInvalidMergeStyle (err ) {
815843 ctx .Flash .Error (ctx .Tr ("repo.pulls.invalid_merge_option" ))
@@ -860,6 +888,33 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
860888 ctx .Redirect (ctx .Repo .RepoLink + "/pulls/" + com .ToStr (pr .Index ))
861889}
862890
891+ // CancelAutoMergePullRequest cancels a scheduled pr
892+ func CancelAutoMergePullRequest (ctx * context.Context ) {
893+ issue := checkPullInfo (ctx )
894+ if ctx .Written () {
895+ return
896+ }
897+ pr := issue .PullRequest
898+ exists , scheduledInfo , err := models .GetScheduledMergeRequestByPullID (pr .ID )
899+ if err != nil {
900+ ctx .ServerError ("GetScheduledMergeRequestByPullID" , err )
901+ return
902+ }
903+ if ! exists {
904+ ctx .Flash .Error (ctx .Tr ("repo.pulls.pull_request_not_scheduled" ))
905+ ctx .Redirect (ctx .Repo .RepoLink + "/pulls/" + com .ToStr (pr .Index ))
906+ return
907+ }
908+ err = models .RemoveScheduledMergeRequest (scheduledInfo )
909+ if err != nil {
910+ ctx .ServerError ("RemoveScheduledMergeRequest" , err )
911+ return
912+ }
913+
914+ ctx .Flash .Success (ctx .Tr ("repo.pulls.pull_request_schedule_canceled" ))
915+ ctx .Redirect (ctx .Repo .RepoLink + "/pulls/" + com .ToStr (issue .Index ))
916+ }
917+
863918func stopTimerIfAvailable (user * models.User , issue * models.Issue ) error {
864919
865920 if models .StopwatchExists (user .ID , issue .ID ) {
0 commit comments