summaryrefslogtreecommitdiffstats
path: root/models/pull.go
diff options
context:
space:
mode:
authorguillep2k <18600385+guillep2k@users.noreply.github.com>2019-10-02 19:28:30 -0300
committerAntoine GIRARD <sapk@users.noreply.github.com>2019-10-03 00:28:30 +0200
commitcd13f273d1663ef5f1f506bf3824f79bc73656ff (patch)
tree71fbbbe24ec017482fb9c81b68643ef0ceca4b37 /models/pull.go
parentc9f819eae0ac9cdce82e25abe25c067b1927e923 (diff)
downloadgitea-cd13f273d1663ef5f1f506bf3824f79bc73656ff.tar.gz
gitea-cd13f273d1663ef5f1f506bf3824f79bc73656ff.zip
Transaction-aware retry create issue to cope with duplicate keys (#8307)
* Revert #7898 * Transaction-aware retry create issue to cope with duplicate keys * Restore INSERT ... WHERE usage * Rearrange code for clarity * Fix error return in newIssue() * Fix error message
Diffstat (limited to 'models/pull.go')
-rw-r--r--models/pull.go20
1 files changed, 19 insertions, 1 deletions
diff --git a/models/pull.go b/models/pull.go
index b41fb004e1..ff1f7b773b 100644
--- a/models/pull.go
+++ b/models/pull.go
@@ -657,6 +657,24 @@ func (pr *PullRequest) testPatch(e Engine) (err error) {
// NewPullRequest creates new pull request with labels for repository.
func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte, assigneeIDs []int64) (err error) {
+ // Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
+ i := 0
+ for {
+ if err = newPullRequestAttempt(repo, pull, labelIDs, uuids, pr, patch, assigneeIDs); err == nil {
+ return nil
+ }
+ if !IsErrNewIssueInsert(err) {
+ return err
+ }
+ if i++; i == issueMaxDupIndexAttempts {
+ break
+ }
+ log.Error("NewPullRequest: error attempting to insert the new issue; will retry. Original error: %v", err)
+ }
+ return fmt.Errorf("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v", err)
+}
+
+func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte, assigneeIDs []int64) (err error) {
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
@@ -671,7 +689,7 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
IsPull: true,
AssigneeIDs: assigneeIDs,
}); err != nil {
- if IsErrUserDoesNotHaveAccessToRepo(err) {
+ if IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
return err
}
return fmt.Errorf("newIssue: %v", err)