1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

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

* Fix indention

Signed-off-by: kolaente <k@knt.li>

* Add option to merge a pr right now without waiting for the checks to succeed

Signed-off-by: kolaente <k@knt.li>

* Fix lint

Signed-off-by: kolaente <k@knt.li>

* Add scheduled pr merge to tables used for testing

Signed-off-by: kolaente <k@knt.li>

* Add status param to make GetPullRequestByHeadBranch reusable

Signed-off-by: kolaente <k@knt.li>

* Move "Merge now" to a seperate button to make the ui clearer

Signed-off-by: kolaente <k@knt.li>

* Update models/scheduled_pull_request_merge.go

Co-authored-by: 赵智超 <1012112796@qq.com>

* Update web_src/js/index.js

Co-authored-by: 赵智超 <1012112796@qq.com>

* Update web_src/js/index.js

Co-authored-by: 赵智超 <1012112796@qq.com>

* Re-add migration after merge

* Fix frontend lint

* Fix version compare

* Add vendored dependencies

* Add basic tets

* Make sure the api route is capable of scheduling PRs for merging

* Fix comparing version

* make vendor

* adopt refactor

* apply suggestion: User -> Doer

* init var once

* Fix Test

* Update templates/repo/issue/view_content/comments.tmpl

* adopt

* nits

* next

* code format

* lint

* use same name schema; rm CreateUnScheduledPRToAutoMergeComment

* API: can not create schedule twice

* Add TestGetBranchNamesForSha

* nits

* new go routine for each pull to merge

* Update models/pull.go

Co-authored-by: a1012112796 <1012112796@qq.com>

* Update models/scheduled_pull_request_merge.go

Co-authored-by: a1012112796 <1012112796@qq.com>

* fix & add renaming sugestions

* Update services/automerge/pull_auto_merge.go

Co-authored-by: a1012112796 <1012112796@qq.com>

* fix conflict relicts

* apply latest refactors

* fix: migration after merge

* Update models/error.go

Co-authored-by: delvh <dev.lh@web.de>

* Update options/locale/locale_en-US.ini

Co-authored-by: delvh <dev.lh@web.de>

* Update options/locale/locale_en-US.ini

Co-authored-by: delvh <dev.lh@web.de>

* adapt latest refactors

* fix test

* use more context

* skip potential edgecases

* document func usage

* GetBranchNamesForSha() -> GetRefsBySha()

* start refactoring

* ajust to new changes

* nit

* docu nit

* the great check move

* move checks for branchprotection into own package

* resolve todo now ...

* move & rename

* unexport if posible

* fix

* check if merge is allowed before merge on scheduled pull

* debugg

* wording

* improve SetDefaults & nits

* NotAllowedToMerge -> DisallowedToMerge

* fix test

* merge files

* use package "errors"

* merge files

* add string names

* other implementation for gogit

* adapt refactor

* more context for models/pull.go

* GetUserRepoPermission use context

* more ctx

* use context for loading pull head/base-repo

* more ctx

* more ctx

* models.LoadIssueCtx()

* models.LoadIssueCtx()

* Handle pull_service.Merge in one DB transaction

* add TODOs

* next

* next

* next

* more ctx

* more ctx

* Start refactoring structure of old pull code ...

* move code into new packages

* shorter names ... and finish **restructure**

* Update models/branches.go

Co-authored-by: zeripath <art27@cantab.net>

* finish UpdateProtectBranch

* more and fix

* update datum

* template: use "svg" helper

* rename prQueue 2 prPatchCheckerQueue

* handle automerge in queue

* lock pull on git&db actions ...

* lock pull on git&db actions ...

* add TODO notes

* the regex

* transaction in tests

* GetRepositoryByIDCtx

* shorter table name and lint fix

* close transaction bevore notify

* Update models/pull.go

* next

* CheckPullMergable check all branch protections!

* Update routers/web/repo/pull.go

* CheckPullMergable check all branch protections!

* Revert "PullService lock via pullID (#19520)" (for now...)

This reverts commit 6cde7c9159a5ea75a10356feb7b8c7ad4c434a9a.

* Update services/pull/check.go

* Use for a repo action one database transaction

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: delvh <dev.lh@web.de>

* Update services/issue/status.go

Co-authored-by: delvh <dev.lh@web.de>

* Update services/issue/status.go

Co-authored-by: delvh <dev.lh@web.de>

* use db.WithTx()

* gofmt

* make pr.GetDefaultMergeMessage() context aware

* make MergePullRequestForm.SetDefaults context aware

* use db.WithTx()

* pull.SetMerged only with context

* fix deadlock in `test-sqlite\#TestAPIBranchProtection`

* dont forget templates

* db.WithTx allow to set the parentCtx

* handle db transaction in service packages but not router

* issue_service.ChangeStatus just had caused another deadlock :/
it has to do something with how notification package is handled

* if we merge a pull in one database transaktion, we get a lock, because merge infoce internal api that cant handle open db sessions to the same repo

* ajust to current master

* Apply suggestions from code review

Co-authored-by: delvh <dev.lh@web.de>

* dont open db transaction in router

* make generate-swagger

* one _success less

* wording nit

* rm

* adapt

* remove not needed test files

* rm less diff & use attr in JS

* ...

* Update services/repository/files/commit.go

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>

* ajust db schema for PullAutoMerge

* skip broken pull refs

* more context in error messages

* remove webUI part for another pull

* remove more WebUI only parts

* API: add CancleAutoMergePR

* Apply suggestions from code review

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>

* fix lint

* Apply suggestions from code review

* cancle -> cancel

Co-authored-by: delvh <dev.lh@web.de>

* change queue identifyer

* fix swagger

* prevent nil issue

* fix and dont drop error

* as per @zeripath

* Update integrations/git_test.go

Co-authored-by: delvh <dev.lh@web.de>

* Update integrations/git_test.go

Co-authored-by: delvh <dev.lh@web.de>

* more declarative integration tests (dedup code)

* use assert.False/True helper

Co-authored-by: 赵智超 <1012112796@qq.com>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
kolaente
2022-05-07 19:05:52 +02:00
committed by GitHub
parent 8adba93498
commit 59b30f060a
47 changed files with 869 additions and 26 deletions

View File

@@ -144,3 +144,19 @@ func (repo *Repository) WalkReferences(arg ObjectType, skip, limit int, walkfn f
})
return i, err
}
// GetRefsBySha returns all references filtered with prefix that belong to a sha commit hash
func (repo *Repository) GetRefsBySha(sha, prefix string) ([]string, error) {
var revList []string
iter, err := repo.gogitRepo.References()
if err != nil {
return nil, err
}
err = iter.ForEach(func(ref *plumbing.Reference) error {
if ref.Hash().String() == sha && strings.HasPrefix(string(ref.Name()), prefix) {
revList = append(revList, string(ref.Name()))
}
return nil
})
return revList, err
}

View File

@@ -190,3 +190,15 @@ func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, wal
}
return i, nil
}
// GetRefsBySha returns all references filtered with prefix that belong to a sha commit hash
func (repo *Repository) GetRefsBySha(sha, prefix string) ([]string, error) {
var revList []string
_, err := walkShowRef(repo.Ctx, repo.Path, "", 0, 0, func(walkSha, refname string) error {
if walkSha == sha && strings.HasPrefix(refname, prefix) {
revList = append(revList, refname)
}
return nil
})
return revList, err
}

View File

@@ -54,3 +54,44 @@ func BenchmarkRepository_GetBranches(b *testing.B) {
}
}
}
func TestGetRefsBySha(t *testing.T) {
bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls")
bareRepo5, err := OpenRepository(DefaultContext, bareRepo5Path)
if err != nil {
t.Fatal(err)
}
defer bareRepo5.Close()
// do not exist
branches, err := bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "")
assert.NoError(t, err)
assert.Len(t, branches, 0)
// refs/pull/1/head
branches, err = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", PullPrefix)
assert.NoError(t, err)
assert.EqualValues(t, []string{"refs/pull/1/head"}, branches)
branches, err = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", BranchPrefix)
assert.NoError(t, err)
assert.EqualValues(t, []string{"refs/heads/master", "refs/heads/master-clone"}, branches)
branches, err = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", BranchPrefix)
assert.NoError(t, err)
assert.EqualValues(t, []string{"refs/heads/test-patch-1"}, branches)
}
func BenchmarkGetRefsBySha(b *testing.B) {
bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls")
bareRepo5, err := OpenRepository(DefaultContext, bareRepo5Path)
if err != nil {
b.Fatal(err)
}
defer bareRepo5.Close()
_, _ = bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "")
_, _ = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", "")
_, _ = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", "")
_, _ = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", "")
}

View File

@@ -0,0 +1 @@
ref: refs/heads/master

View File

@@ -0,0 +1,6 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
[receive]
advertisePushOptions = true

View File

@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

View File

@@ -0,0 +1 @@
x%<25><>n<EFBFBD>0 <0C>;<3B>)<29>0H<30><48>1 P<>]<5D><>(<28>F2<46>T<><54>k<EFBFBD>7|<7C>wu]<5D>{O<>қ<19>H<><48>p<EFBFBD><70>8<EFBFBD>$A<>1<EFBFBD>"\<5C><>a<EFBFBD>Rf<52><66>f<EFBFBD>4<> <0B><>#ZL:J<>\-<2D><>#fO2s<32><73>N<EFBFBD><4E><EFBFBD>6<EFBFBD><36>ӯ<EFBFBD><D3AF>N<>;<3B>v<><02>#<23><> 3p<><70>׺5<D7BA><35><EFBFBD>p<EFBFBD>y^<5E><><EFBFBD>y<1F><>L)x<>ۼs_<0F>n<EFBFBD>1]<5D>ާa<DEA7>_<EFBFBD>)@X

View File

@@ -0,0 +1,2 @@
x<01><>MN<4D>0 <0C>Y<EFBFBD><14>l<06><06>'<27><><1C> <20><>i%<25><><16>
<EFBFBD><=<3D>}~<7E><>2Mcc<>M<EFBFBD>"<22><><14>h֬z<D6AC><7A><EFBFBD>)q(<28><>CRI<52>O<EFBFBD><4F>tk<74>27Ƚ1=<3D>GrL&]<5D>Y<EFBFBD>BFt<46>'&o<><6F>?^<5E>/<2F>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE>*<2A>L<EFBFBD><4C><EFBFBD>ݛ<EFBFBD>ů6,<15><>O<EFBFBD><4F><EFBFBD><EFBFBD>

View File

@@ -0,0 +1,2 @@
x<01><>AJAE]<5D>)<29><>"<22>VwW<77>t E<>čz<C48D>NU5<06>$<24>T<EFBFBD>9<EFBFBD><39><EFBFBD>&<26>$'1<>+<2B>y|<7C><><EFBFBD><EFBFBD><EFBFBD>f<EFBFBD>6=^XS<58>NpE̅"<22>R<EFBFBD>1v>W<>(<28><17><>gD<67><44><EFBFBD>J<EFBFBD><4A>@%W<>PKZ
<EFBFBD>c<EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD>D2)<29>r<EFBFBD><72><EFBFBD><EFBFBD>m<EFBFBD>`<60><><EFBFBD>Yy<59>f<EFBFBD><66><1D><>h<EFBFBD>:j<>\<5C><>)<29>۩<EFBFBD>=<3D><><EFBFBD><05>."<22>><3E>W<1D>~6<><36>5w<|>><3E>/<2F><><EFBFBD><EFBFBD><EFBFBD>|

View File

@@ -0,0 +1,3 @@
x<01><>AJAE]<5D>)<29><>!V<><56>tM<74><4D>"Y<08>F=@uw5<06>$<24><><EFBFBD>D\yo<><6F>h<EFBFBD>
n<EFBFBD><EFBFBD>?<3F><><EFBFBD><EFBFBD>l<EFBFBD><06>xbMd<>,<2C>T<EFBFBD><54><EFBFBD>C7f%<25><><75>P<EFBFBD><50>3Jr;i:ԎJ,<2C>`<60><>5<EFBFBD>P)<29>a<EFBFBD>̔<EFBFBD><CC94>
9y<EFBFBD><EFBFBD>m<EFBFBD>9<EFBFBD><EFBFBD><EFBFBD><EFBFBD>U<EFBFBD><EFBFBD>.n<>Ig<49><67>Y<EFBFBD><59><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD>l<EFBFBD>G,<2C><>:<3A>=<3D>q<EFBFBD>s$D<><EFBFBD>M<EFBFBD><4D><EFBFBD><1D><1F>w<EFBFBD><77><EFBFBD><EFBFBD><EFBFBD>a<EFBFBD>_<>S<EFBFBD>6<EFBFBD>o9X<39>

View File

@@ -0,0 +1,2 @@
P pack-81423f591973f5d9dab89cc45afa1c544448133e.pack

View File

@@ -0,0 +1,5 @@
# pack-refs with: peeled fully-peeled sorted
c83380d7056593c51a699d12b9c00627bd5743e9 refs/heads/test-patch-1
c83380d7056593c51a699d12b9c00627bd5743e9 refs/pull/1/head
111cac04bd7d20301964e27a93698aabb5781b80 refs/pull/1/merge
72866af952e98d02a73003501836074b286a78f6 refs/tags/v0.9.99

View File

@@ -0,0 +1 @@
d8e0bbb45f200e67d9a784ce55bd90821af45ebd

View File

@@ -0,0 +1 @@
d8e0bbb45f200e67d9a784ce55bd90821af45ebd

View File

@@ -0,0 +1 @@
58a4bcc53ac13e7ff76127e0fb518b5262bf09af

View File

@@ -0,0 +1 @@
58a4bcc53ac13e7ff76127e0fb518b5262bf09af