Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto merge pull requests when all checks succeeded via API #9307

Merged
merged 307 commits into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
307 commits
Select commit Hold shift + click to select a range
341aef2
Fix indention
kolaente Jul 19, 2020
10d2dd6
Add option to merge a pr right now without waiting for the checks to …
kolaente Jul 19, 2020
6044bc6
Fix lint
kolaente Jul 19, 2020
452310d
Add scheduled pr merge to tables used for testing
kolaente Jul 19, 2020
72ae3de
Merge branch 'master' into feature/auto-merge
kolaente Jul 22, 2020
7b14e16
Add status param to make GetPullRequestByHeadBranch reusable
kolaente Jul 22, 2020
9b99253
Move "Merge now" to a seperate button to make the ui clearer
kolaente Jul 22, 2020
df39bcc
Update models/scheduled_pull_request_merge.go
kolaente Jul 22, 2020
5f5c84c
Merge branch 'master' into feature/auto-merge
kolaente Jul 22, 2020
2c3e2a6
Update web_src/js/index.js
kolaente Jul 23, 2020
3321ad0
Merge branch 'master' into feature/auto-merge
kolaente Jul 23, 2020
eb63b61
Update web_src/js/index.js
kolaente Jul 23, 2020
e86a3ab
Merge branch 'master' into feature/auto-merge
kolaente Jul 27, 2020
38278bb
Merge branch 'master' into feature/auto-merge
kolaente Dec 15, 2020
2dbd642
Merge remote-tracking branch 'origin/feature/auto-merge' into feature…
kolaente Dec 15, 2020
308d423
Re-add migration after merge
kolaente Dec 15, 2020
81efbaf
Fix frontend lint
kolaente Dec 15, 2020
1d54f4c
Fix version compare
kolaente Dec 15, 2020
d4337b2
Add vendored dependencies
kolaente Dec 15, 2020
0c9a741
Add basic tets
kolaente Dec 15, 2020
407bdab
Make sure the api route is capable of scheduling PRs for merging
kolaente Dec 15, 2020
2ac0136
Merge branch 'master' into feature/auto-merge
kolaente Dec 15, 2020
5f2d076
Fix comparing version
kolaente Dec 15, 2020
1109de2
Merge branch 'feature/auto-merge' of github.com:kolaente/gitea into f…
kolaente Dec 15, 2020
4cd7ba0
make vendor
kolaente Dec 15, 2020
11cc7a0
Merge branch 'master' into feature/auto-merge
6543 Feb 12, 2021
f2c2547
adopt refactor
6543 Feb 12, 2021
7316455
apply suggestion: User -> Doer
6543 Feb 12, 2021
01cc62f
init var once
6543 Feb 12, 2021
5fe32f1
Fix Test
6543 Feb 12, 2021
c55d67a
Merge branch 'master' into feature/auto-merge
kolaente Feb 13, 2021
6cfe63b
Merge branch 'master' into feature/auto-merge
6543 Feb 19, 2021
79667e9
Merge branch 'master' into feature/auto-merge
kolaente Feb 22, 2021
9808a8b
Merge branch 'master' into feature/auto-merge
kolaente Feb 27, 2021
8ffc978
Merge branch 'master' into feature/auto-merge
6543 Mar 1, 2021
5c99f8b
Merge branch 'master' into feature/auto-merge
6543 Apr 8, 2021
bbe51fb
Update templates/repo/issue/view_content/comments.tmpl
6543 Apr 8, 2021
4d1b77b
Merge branch 'master' into feature/auto-merge
6543 Apr 8, 2021
a21bc92
adopt
6543 Apr 8, 2021
fd290d1
Merge branch 'master' into feature/auto-merge
6543 Apr 8, 2021
673b657
Merge branch 'master' into feature/auto-merge
6543 Apr 10, 2021
f30ab00
Merge branch 'master' into feature/auto-merge
6543 Apr 10, 2021
a1dab29
nits
6543 Apr 10, 2021
f92385f
next
6543 Apr 10, 2021
738f66a
code format
6543 Apr 10, 2021
fe86285
lint
6543 Apr 10, 2021
b3f90f2
Merge branch 'master' into feature/auto-merge
6543 Apr 11, 2021
ac6c002
use same name schema; rm CreateUnScheduledPRToAutoMergeComment
6543 Apr 11, 2021
7745e74
API: can not create schedule twice
6543 Apr 11, 2021
b271c15
Add TestGetBranchNamesForSha
6543 Apr 11, 2021
b1a7f4d
Merge branch 'master' into feature/auto-merge
6543 Apr 11, 2021
0927f33
nits
6543 Apr 11, 2021
7029905
new go routine for each pull to merge
6543 Apr 11, 2021
003a0cd
Merge branch 'master' into feature/auto-merge
6543 Apr 13, 2021
d75691d
Merge branch 'master' into feature/auto-merge
6543 Apr 15, 2021
d1c1f31
Merge branch 'master' into feature/auto-merge
kolaente Apr 26, 2021
6f00624
Merge branch 'main' into feature/auto-merge
kolaente May 8, 2021
c317e30
Merge branch 'main' into feature/auto-merge
6543 May 12, 2021
68475a3
Update models/pull.go
kolaente May 13, 2021
1f0231f
Update models/scheduled_pull_request_merge.go
kolaente May 13, 2021
f0a4720
Merge branch 'main' into feature/auto-merge
6543 May 16, 2021
8432030
fix & add renaming sugestions
6543 May 16, 2021
55ac505
Update services/automerge/pull_auto_merge.go
6543 May 16, 2021
745b784
Merge branch 'main' into feature/auto-merge
kolaente May 17, 2021
efa97ce
Merge branch 'master' into feature/auto-merge
6543 Jul 3, 2021
38c9538
Merge branch 'main' into feature/auto-merge
6543 Jul 7, 2021
2a8d230
Merge branch 'master' into feature/auto-merge
6543 Jul 18, 2021
848990c
fix conflict relicts
6543 Jul 18, 2021
ab20836
Merge branch 'master' into feature/auto-merge
6543 Sep 27, 2021
4e52813
apply latest refactors
6543 Sep 27, 2021
6f1bee0
Merge branch 'main' into feature/auto-merge
kolaente Oct 15, 2021
cf0aea9
fix: migration after merge
kolaente Oct 15, 2021
f6c0cad
Merge branch 'main' into feature/auto-merge
6543 Oct 15, 2021
0b89b6c
Update models/error.go
kolaente Oct 16, 2021
0b6724b
Update options/locale/locale_en-US.ini
kolaente Oct 16, 2021
6d2bb4e
Update options/locale/locale_en-US.ini
kolaente Oct 16, 2021
5abb112
Merge branch 'master' into feature/auto-merge
6543 Oct 16, 2021
8522e4c
Merge branch 'main' into feature/auto-merge
6543 Oct 17, 2021
4100e19
Merge branch 'master' into feature/auto-merge
6543 Mar 29, 2022
635abdd
adapt latest refactors
6543 Mar 29, 2022
af22476
fix test
6543 Mar 29, 2022
68c0bd7
Merge branch 'master' into feature/auto-merge
6543 Mar 29, 2022
dfc8a7a
use more context
6543 Mar 29, 2022
397bb67
skip potential edgecases
6543 Mar 29, 2022
c0fbcaa
document func usage
6543 Mar 29, 2022
529cf03
Merge branch 'master' into feature/auto-merge
6543 Mar 29, 2022
f63742e
GetBranchNamesForSha() -> GetRefsBySha()
6543 Mar 29, 2022
28295f7
start refactoring
6543 Mar 29, 2022
af9a58b
Merge branch 'master' into feature/auto-merge
6543 Mar 29, 2022
b2930ec
ajust to new changes
6543 Mar 29, 2022
47c815d
nit
6543 Mar 29, 2022
dcbd06d
docu nit
6543 Mar 29, 2022
9b9bd7b
Merge branch 'master' into feature/auto-merge
6543 Mar 30, 2022
03eb945
the great check move
6543 Mar 30, 2022
6a9dae2
Merge branch 'master' into feature/auto-merge
6543 Mar 30, 2022
d8e0686
move checks for branchprotection into own package
6543 Mar 30, 2022
6f15ec8
resolve todo now ...
6543 Mar 30, 2022
f17abd1
move & rename
6543 Mar 30, 2022
2a66e96
unexport if posible
6543 Mar 30, 2022
d2023d8
fix
6543 Mar 31, 2022
8292182
check if merge is allowed before merge on scheduled pull
6543 Mar 31, 2022
c8496aa
debugg
6543 Mar 31, 2022
80b63ee
Merge branch 'master' into feature/auto-merge
6543 Mar 31, 2022
a1073bc
Merge branch 'main' into single-place-to-enforce-protected-branch-rules
6543 Mar 31, 2022
52cb7c4
wording
6543 Mar 31, 2022
f4b4a0b
Merge branch 'main' into single-place-to-enforce-protected-branch-rules
6543 Mar 31, 2022
c0e932a
improve SetDefaults & nits
6543 Mar 31, 2022
6959e27
NotAllowedToMerge -> DisallowedToMerge
6543 Mar 31, 2022
4775c3a
fix test
6543 Mar 31, 2022
8c6a54f
Merge branch 'main' into single-place-to-enforce-protected-branch-rules
6543 Mar 31, 2022
943d1f2
merge files
6543 Mar 31, 2022
f7b89a5
Merge branch 'master' into single-place-to-enforce-protected-branch-r…
6543 Mar 31, 2022
19200ac
Merge branch 'master' into feature/auto-merge
6543 Mar 31, 2022
5d3f346
Merge branch 'single-place-to-enforce-protected-branch-rules' into fe…
6543 Mar 31, 2022
120d84d
use package "errors"
6543 Mar 31, 2022
d0a2312
merge files
6543 Mar 31, 2022
ce534e0
Merge branch 'single-place-to-enforce-protected-branch-rules' into fe…
6543 Mar 31, 2022
4c2460f
Merge branch 'master' into feature/auto-merge
6543 Mar 31, 2022
f187990
add string names
6543 Mar 31, 2022
e99481c
other implementation for gogit
6543 Mar 31, 2022
6a10944
Merge branch 'master' into feature/auto-merge
6543 Apr 1, 2022
ff93d83
Merge branch 'master' into feature/auto-merge
6543 Apr 2, 2022
3baf1d0
Merge branch 'main' into feature/auto-merge
6543 Apr 21, 2022
4f1344c
Merge branch 'master' into feature/auto-merge
6543 Apr 26, 2022
03cbafd
adapt refactor
6543 Apr 26, 2022
261b407
more context for models/pull.go
6543 Apr 26, 2022
90f96cf
GetUserRepoPermission use context
6543 Apr 26, 2022
505c2cd
Merge branch 'master' into feature/auto-merge
6543 Apr 26, 2022
1583951
more ctx
6543 Apr 26, 2022
ec11287
Merge branch 'master' into refactor-from-9307
6543 Apr 26, 2022
832d7d6
use context for loading pull head/base-repo
6543 Apr 26, 2022
abf5067
more ctx
6543 Apr 26, 2022
fea87e6
more ctx
6543 Apr 26, 2022
e3e3741
models.LoadIssueCtx()
6543 Apr 26, 2022
1013108
models.LoadIssueCtx()
6543 Apr 26, 2022
9088331
Handle pull_service.Merge in one DB transaction
6543 Apr 26, 2022
673a81a
add TODOs
6543 Apr 26, 2022
6765298
next
6543 Apr 26, 2022
82259d5
next
6543 Apr 26, 2022
0b2a290
next
6543 Apr 26, 2022
e283421
Merge branch 'master' into refactor-from-9307
6543 Apr 26, 2022
811e15a
more ctx
6543 Apr 26, 2022
bf432d8
more ctx
6543 Apr 26, 2022
fbb9f50
Merge branch 'main' into feature/auto-merge
6543 Apr 26, 2022
461bea5
Start refactoring structure of old pull code ...
6543 Apr 26, 2022
b226064
move code into new packages
6543 Apr 26, 2022
981cdde
shorter names ... and finish **restructure**
6543 Apr 26, 2022
0e56288
Update models/branches.go
6543 Apr 26, 2022
2d079ee
finish UpdateProtectBranch
6543 Apr 26, 2022
bc474bb
more and fix
6543 Apr 26, 2022
594517f
Merge branch 'refactor-from-9307' into feature/auto-merge
6543 Apr 26, 2022
7c411f4
update datum
6543 Apr 26, 2022
6d793b4
template: use "svg" helper
6543 Apr 26, 2022
fd58a14
rename prQueue 2 prPatchCheckerQueue
6543 Apr 26, 2022
cad41d2
handle automerge in queue
6543 Apr 26, 2022
fe4b06f
lock pull on git&db actions ...
6543 Apr 26, 2022
e6b7918
lock pull on git&db actions ...
6543 Apr 26, 2022
29319c4
add TODO notes
6543 Apr 26, 2022
524ece7
Merge branch 'pull_service-lock-per-pull' into feature/auto-merge
6543 Apr 27, 2022
a4d5f48
Merge branch 'main' into refactor-from-9307
6543 Apr 27, 2022
72ad384
Merge branch 'master' into refactor-from-9307
6543 Apr 27, 2022
4e74172
the regex
6543 Apr 27, 2022
61786f8
transaction in tests
6543 Apr 27, 2022
266f4b3
GetRepositoryByIDCtx
6543 Apr 27, 2022
20d00b8
Merge branch 'master' into feature/auto-merge
6543 Apr 27, 2022
76f0501
Merge branch 'refactor-from-9307' into feature/auto-merge
6543 Apr 27, 2022
b422328
shorter table name and lint fix
6543 Apr 27, 2022
cdc8706
Merge branch 'main' into refactor-from-9307
6543 Apr 28, 2022
1eb7812
Merge branch 'master' into refactor-from-9307
6543 Apr 28, 2022
ef4fcbe
close transaction bevore notify
6543 Apr 28, 2022
c856a2e
Merge branch 'refactor-from-9307' into feature/auto-merge
6543 Apr 28, 2022
6c9ff86
Merge branch 'master' into feature/auto-merge
6543 Apr 28, 2022
545d33e
Merge branch 'master' into feature/auto-merge
6543 Apr 28, 2022
ddf1157
Update models/pull.go
6543 Apr 28, 2022
35b3071
Merge branch 'master' into feature/auto-merge
6543 Apr 29, 2022
b5b8ca3
Merge branch 'main' into feature/auto-merge
6543 May 1, 2022
02abd0c
next
6543 May 1, 2022
10b277b
CheckPullMergable check all branch protections!
6543 May 1, 2022
9330ec4
Merge branch 'main' into feature/auto-merge
6543 May 1, 2022
d9c07c9
Update routers/web/repo/pull.go
6543 May 1, 2022
22f7a4b
CheckPullMergable check all branch protections!
6543 May 1, 2022
2003602
Revert "PullService lock via pullID (#19520)" (for now...)
6543 May 1, 2022
8411cc9
Merge branch 'CheckPullMergable_check_all_branch_protections' into fe…
6543 May 1, 2022
ceda060
Merge branch 'master' into feature/auto-merge
6543 May 2, 2022
faa06f1
Update services/pull/check.go
6543 May 2, 2022
b09b46b
Use for a repo action one database transaction
6543 May 2, 2022
04207b9
Apply suggestions from code review
6543 May 2, 2022
a6a335a
Merge branch 'main' into more-context-again
6543 May 2, 2022
cd48d31
Apply suggestions from code review
6543 May 3, 2022
54e849a
Update services/issue/status.go
6543 May 3, 2022
48b76f9
Merge branch 'master' into more-context-again
6543 May 3, 2022
443168a
Update services/issue/status.go
6543 May 3, 2022
419f064
use db.WithTx()
6543 May 3, 2022
0312d2f
gofmt
6543 May 3, 2022
427ba1e
Merge branch 'master' into more-context-again
6543 May 3, 2022
cdb4683
Merge branch 'master' into feature/auto-merge
6543 May 3, 2022
9cc0d28
Merge branch 'more-context-again' into feature/auto-merge
6543 May 3, 2022
7538993
make pr.GetDefaultMergeMessage() context aware
6543 May 3, 2022
ca8c1f1
make MergePullRequestForm.SetDefaults context aware
6543 May 3, 2022
e188963
use db.WithTx()
6543 May 3, 2022
3d3b847
pull.SetMerged only with context
6543 May 3, 2022
5d0a243
fix deadlock in `test-sqlite\#TestAPIBranchProtection`
6543 May 3, 2022
7741935
dont forget templates
6543 May 3, 2022
244f3f2
db.WithTx allow to set the parentCtx
6543 May 3, 2022
95df0f2
handle db transaction in service packages but not router
6543 May 3, 2022
58a7322
issue_service.ChangeStatus just had caused another deadlock :/
6543 May 3, 2022
4e099bd
if we merge a pull in one database transaktion, we get a lock, becaus…
6543 May 3, 2022
9813c63
Merge branch 'more-context-again' into feature/auto-merge
6543 May 3, 2022
d127b81
Merge branch 'master' into feature/auto-merge
6543 May 3, 2022
6d60e05
ajust to current master
6543 May 3, 2022
d3cc762
Apply suggestions from code review
6543 May 3, 2022
6e30456
dont open db transaction in router
6543 May 3, 2022
b0b8ed6
make generate-swagger
6543 May 3, 2022
713796e
one _success less
6543 May 3, 2022
1db379f
wording nit
6543 May 3, 2022
8bb0bd6
rm
6543 May 3, 2022
27ce2b0
adapt
6543 May 3, 2022
a47cd88
Merge branch 'main' into feature/auto-merge
6543 May 3, 2022
7ce6963
remove not needed test files
6543 May 4, 2022
faef76e
rm less diff & use attr in JS
6543 May 4, 2022
7456862
...
6543 May 4, 2022
01cebae
Merge branch 'main' into feature/auto-merge
6543 May 4, 2022
3911dcf
Update services/repository/files/commit.go
6543 May 4, 2022
4c0a287
Merge branch 'main' into feature/auto-merge
6543 May 4, 2022
16c7bbb
Merge branch 'main' into feature/auto-merge
6543 May 4, 2022
6633101
Merge branch 'master' into feature/auto-merge
6543 May 5, 2022
07ebb0d
ajust db schema for PullAutoMerge
6543 May 5, 2022
6eec790
skip broken pull refs
6543 May 5, 2022
5cf709d
more context in error messages
6543 May 5, 2022
9f43fc9
remove webUI part for another pull
6543 May 5, 2022
6566141
remove more WebUI only parts
6543 May 5, 2022
93702a4
API: add CancleAutoMergePR
6543 May 5, 2022
89670eb
Apply suggestions from code review
6543 May 5, 2022
23e2b84
Merge branch 'main' into feature/auto-merge
6543 May 5, 2022
f5458d2
fix lint
6543 May 5, 2022
34d0600
Merge branch 'main' into feature/auto-merge
6543 May 5, 2022
e4922f7
Apply suggestions from code review
zeripath May 5, 2022
3fa4840
cancle -> cancel
6543 May 6, 2022
1e17e83
Merge branch 'main' into feature/auto-merge
6543 May 6, 2022
cd522b3
change queue identifyer
6543 May 6, 2022
a57444e
fix swagger
6543 May 6, 2022
4f97aa1
prevent nil issue
6543 May 6, 2022
e374f9f
Merge branch 'master' into feature/auto-merge
6543 May 7, 2022
ea5e940
fix and dont drop error
6543 May 7, 2022
8df24af
as per @zeripath
6543 May 7, 2022
bbbcf40
Update integrations/git_test.go
6543 May 7, 2022
2364692
Update integrations/git_test.go
6543 May 7, 2022
749df65
more declarative integration tests (dedup code)
6543 May 7, 2022
95052c9
use assert.False/True helper
6543 May 7, 2022
5fd0d6f
Merge branch 'main' into feature/auto-merge
6543 May 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ var migrations = []Migration{
NewMigration("Add Branch Protection Block Outdated Branch", addBlockOnOutdatedBranch),
// v138 -> v139
NewMigration("Add ResolveDoerID to Comment table", addResolveDoerIDCommentColumn),
// v139 -> v140
NewMigration("Add auto merge table", addAutoMergeTable),
}

// GetCurrentDBVersion returns the current db version
Expand Down
20 changes: 20 additions & 0 deletions models/migrations/v139.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import "xorm.io/xorm"

func addAutoMergeTable(x *xorm.Engine) error {
type MergeStyle string
type ScheduledPullRequestMerge struct {
ID int64 `xorm:"pk autoincr"`
PullID int64 `xorm:"BIGINT"`
UserID int64 `xorm:"BIGINT"`
MergeStyle MergeStyle `xorm:"varchar(50)"`
Message string `xorm:"TEXT"`
kolaente marked this conversation as resolved.
Show resolved Hide resolved
}

return x.Sync2(&ScheduledPullRequestMerge{})
}
13 changes: 13 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,16 @@ func (pr *PullRequest) updateCommitDivergence(e Engine, ahead, behind int) error
func (pr *PullRequest) IsSameRepo() bool {
return pr.BaseRepoID == pr.HeadRepoID
}

// GetPullRequestByHeadBranch returns a pr by head branch
func GetPullRequestByHeadBranch(headBranch string, headRepo *Repository) (pr *PullRequest, err error) {
kolaente marked this conversation as resolved.
Show resolved Hide resolved
pr = &PullRequest{}
kolaente marked this conversation as resolved.
Show resolved Hide resolved
exists, err := x.Where("head_branch = ? AND head_repo_id = ?", headBranch, headRepo.ID).Get(pr)
if !exists {
return nil, ErrPullRequestNotExist{
HeadBranch: headBranch,
HeadRepoID: headRepo.ID,
}
}
return
}
55 changes: 55 additions & 0 deletions models/scheduled_pull_request_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2019 Gitea. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import "code.gitea.io/gitea/modules/timeutil"

// ScheduledPullRequestMerge represents a pull request scheduled for merging when checks succeed
type ScheduledPullRequestMerge struct {
ID int64 `xorm:"pk autoincr"`
PullID int64 `xorm:"BIGINT"`
UserID int64 `xorm:"BIGINT"`
User *User `xorm:"-"`
kolaente marked this conversation as resolved.
Show resolved Hide resolved
MergeStyle MergeStyle `xorm:"varchar(50)"`
Message string `xorm:"TEXT"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
}

// ScheduleAutoMerge schedules a pull request to be merged when all checks succeed
func ScheduleAutoMerge(opts *ScheduledPullRequestMerge) (err error) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
// Check if we already have a merge scheduled for that pull request
exists, err := x.Exist(&ScheduledPullRequestMerge{PullID: opts.PullID})
if err != nil {
return
}
if exists {
// Maybe FIXME: Should we return a custom error here?
return nil
kolaente marked this conversation as resolved.
Show resolved Hide resolved
}

_, err = x.Insert(opts)
return err
}

// GetScheduledMergeRequestByPullID gets a scheduled pull request merge by pull request id
func GetScheduledMergeRequestByPullID(pullID int64) (exists bool, scheduledPRM *ScheduledPullRequestMerge, err error) {
scheduledPRM = &ScheduledPullRequestMerge{}
6543 marked this conversation as resolved.
Show resolved Hide resolved
exists, err = x.Where("pull_id = ?", pullID).Get(scheduledPRM)
if err != nil || !exists {
return
}
scheduledPRM.User, err = getUserByID(x, scheduledPRM.UserID)
return
}

// RemoveScheduledMergeRequest cancels a previously scheduled pull request
func RemoveScheduledMergeRequest(scheduledPR *ScheduledPullRequestMerge) (err error) {
if scheduledPR.ID == 0 && scheduledPR.PullID != 0 {
kolaente marked this conversation as resolved.
Show resolved Hide resolved
_, err = x.Where("pull_id = ?", scheduledPR.PullID).Delete(&ScheduledPullRequestMerge{})
return
}
_, err = x.Where("id = ? AND pull_id = ?", scheduledPR.ID, scheduledPR.PullID).Delete(&ScheduledPullRequestMerge{})
return
}
9 changes: 5 additions & 4 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,11 @@ func (f *InitializeLabelsForm) Validate(ctx *macaron.Context, errs binding.Error
type MergePullRequestForm struct {
// required: true
// enum: merge,rebase,rebase-merge,squash
Do string `binding:"Required;In(merge,rebase,rebase-merge,squash)"`
MergeTitleField string
MergeMessageField string
ForceMerge *bool `json:"force_merge,omitempty"`
Do string `binding:"Required;In(merge,rebase,rebase-merge,squash)"`
MergeTitleField string
MergeMessageField string
ForceMerge *bool `json:"force_merge,omitempty"`
MergeWhenChecksSucceed bool
kolaente marked this conversation as resolved.
Show resolved Hide resolved
}

// Validate validates the fields
Expand Down
20 changes: 20 additions & 0 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
}

// GetBranchName gets the closes branch name (as returned by 'git name-rev')
// FIXME: This get only one branch, but one commit can be part of multiple branches
kolaente marked this conversation as resolved.
Show resolved Hide resolved
func (c *Commit) GetBranchName() (string, error) {
data, err := NewCommand("name-rev", c.ID.String()).RunInDirBytes(c.repo.Path)
if err != nil {
Expand All @@ -477,6 +478,25 @@ func (c *Commit) GetBranchName() (string, error) {
return strings.Split(strings.Split(string(data), " ")[1], "~")[0], nil
}

// GetBranchNames returns all branches a commit is part of
func (c *Commit) GetBranchNames() (branchNames []string, err error) {
data, err := NewCommand("name-rev", c.ID.String()).RunInDirBytes(c.repo.Path)
if err != nil {
return
}

namesRaw := strings.Split(string(data), "\n")
for _, s := range namesRaw {
s = strings.TrimSpace(s)
if s == "" {
continue
}
branchNames = append(branchNames, strings.Split(strings.Split(s, " ")[1], "~")[0])
}

return
}

// CommitFileStatus represents status of files in a commit.
type CommitFileStatus struct {
Added []string
Expand Down
8 changes: 8 additions & 0 deletions modules/repofiles/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/services/automerge"
)

// CreateCommitStatus creates a new CommitStatus given a bunch of parameters
Expand Down Expand Up @@ -37,5 +38,12 @@ func CreateCommitStatus(repo *models.Repository, creator *models.User, sha strin
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", repo.ID, creator.ID, sha, err)
}

if status.State.IsSuccess() {
err = automerge.MergeScheduledPullRequest(sha, repo)
if err != nil {
return fmt.Errorf("MergeScheduledPullRequest[repo_id: %d, user_id: %d, sha: %s]: %v", repo.ID, creator.ID, sha, err)
}
}

return nil
}
13 changes: 13 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,14 @@ pulls.rebase_merge_pull_request = Rebase and Merge
pulls.rebase_merge_commit_pull_request = Rebase and Merge (--no-ff)
pulls.squash_merge_pull_request = Squash and Merge
pulls.require_signed_wont_sign = The branch requires signed commits but this merge will not be signed
pulls.merge_pull_request_now = Merge Pull Request Now
pulls.rebase_merge_pull_request_now = Rebase and Merge Now
pulls.rebase_merge_commit_pull_request_now = Rebase and Merge Now (--no-ff)
pulls.squash_merge_pull_request_now = Squash and Merge Now
pulls.merge_pull_request_on_status_success = Merge Pull Request When All Checks Succeed
pulls.rebase_merge_pull_request_on_status_success = Rebase and Merge When All Checks Succeed
pulls.rebase_merge_commit_pull_request_on_status_success = Rebase and Merge (--no-ff) When All Checks Succeed
pulls.squash_merge_pull_request_on_status_success = Squash and Merge When All Checks Succeed
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
pulls.invalid_merge_option = You cannot use this merge option for this pull request.
pulls.merge_conflict = Merge Failed: There was a conflict whilst merging: %[1]s<br>%[2]s<br>Hint: Try a different strategy
pulls.rebase_conflict = Merge Failed: There was a conflict whilst rebasing commit: %[1]s<br>%[2]s<br>%[3]s<br>Hint:Try a different strategy
Expand All @@ -1154,6 +1162,11 @@ pulls.update_not_allowed = You are not allowed to update branch
pulls.outdated_with_base_branch = This branch is out-of-date with the base branch
pulls.closed_at = `closed this pull request <a id="%[1]s" href="#%[1]s">%[2]s</a>`
pulls.reopened_at = `reopened this pull request <a id="%[1]s" href="#%[1]s">%[2]s</a>`
pulls.merge_on_status_success_success = The pull request was successfully scheduled to merge when all checks succeed.
kolaente marked this conversation as resolved.
Show resolved Hide resolved
pulls.pr_has_pending_merge_on_success = This pull request has been set to auto merge when all checks succeed by %[1]s %[2]s.
pulls.merge_pull_on_success_cancel = Cancel automatic merge
6543 marked this conversation as resolved.
Show resolved Hide resolved
pulls.pull_request_not_scheduled = This pull request is not scheduled to auto merge.
pulls.pull_request_schedule_canceled = This pull request was successfully canceled for auto merge.

milestones.new = New Milestone
milestones.open_tab = %d Open
Expand Down
7 changes: 7 additions & 0 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,13 @@ func ViewIssue(ctx *context.Context) {
ctx.ServerError("GetReviewersByIssueID", err)
return
}

// Check if there is a pending pr merge
ctx.Data["HasPendingPullRequestMerge"], ctx.Data["PendingPullRequestMerge"], err = models.GetScheduledMergeRequestByPullID(pull.ID)
if err != nil {
ctx.ServerError("GetReviewersByIssueID", err)
kolaente marked this conversation as resolved.
Show resolved Hide resolved
return
}
}

// Get Dependencies
Expand Down
54 changes: 54 additions & 0 deletions routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,33 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
return
}

lastCommitStatus, err := pull_service.GetPullRequestCommitStatusState(pr)
kolaente marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return
}
if form.MergeWhenChecksSucceed && !lastCommitStatus.IsSuccess() {
err = models.ScheduleAutoMerge(&models.ScheduledPullRequestMerge{
PullID: pr.ID,
UserID: ctx.User.ID,
MergeStyle: models.MergeStyle(form.Do),
Message: message,
})
if err != nil {
ctx.ServerError("ScheduleAutoMerge", err)
return
}
ctx.Flash.Success(ctx.Tr("repo.pulls.merge_on_status_success_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
return
}
6543 marked this conversation as resolved.
Show resolved Hide resolved
// Removing an auto merge pull request is something we can execute whether or not a pull request auto merge was
// scheduled before, hece we can remove it without checking for its existence.
err = models.RemoveScheduledMergeRequest(&models.ScheduledPullRequestMerge{PullID: pr.ID})
if err != nil {
ctx.ServerError("RemoveScheduledMergeRequest", err)
return
}

if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
if models.IsErrInvalidMergeStyle(err) {
ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option"))
Expand Down Expand Up @@ -860,6 +887,33 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
}

// CancelAutoMergePullRequest cancels a scheduled pr
func CancelAutoMergePullRequest(ctx *context.Context) {
issue := checkPullInfo(ctx)
kolaente marked this conversation as resolved.
Show resolved Hide resolved
if ctx.Written() {
return
}
pr := issue.PullRequest
exists, scheduledInfo, err := models.GetScheduledMergeRequestByPullID(pr.ID)
if err != nil {
ctx.ServerError("GetScheduledMergeRequestByPullID", err)
return
}
if !exists {
ctx.Flash.Error(ctx.Tr("repo.pulls.pull_request_not_scheduled"))
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
return
}
err = models.RemoveScheduledMergeRequest(scheduledInfo)
if err != nil {
ctx.ServerError("RemoveScheduledMergeRequest", err)
return
}

ctx.Flash.Success(ctx.Tr("repo.pulls.pull_request_schedule_canceled"))
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
}

func stopTimerIfAvailable(user *models.User, issue *models.Issue) error {

if models.StopwatchExists(user.ID, issue.ID) {
Expand Down
1 change: 1 addition & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get(".patch", repo.DownloadPullPatch)
m.Get("/commits", context.RepoRef(), repo.ViewPullCommits)
m.Post("/merge", context.RepoMustNotBeArchived(), bindIgnErr(auth.MergePullRequestForm{}), repo.MergePullRequest)
m.Post("/cancel_auto_merge", context.RepoMustNotBeArchived(), repo.CancelAutoMergePullRequest)
m.Post("/update", repo.UpdatePullRequest)
m.Post("/cleanup", context.RepoMustNotBeArchived(), context.RepoRef(), repo.CleanUpPullRequest)
m.Group("/files", func() {
Expand Down
Loading