diff options
author | Unknwon <u@gogs.io> | 2015-09-02 05:09:12 -0400 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-09-02 05:09:12 -0400 |
commit | 1abfe4e05f2c936ab43c8188003a33213b245a56 (patch) | |
tree | 38cec881f7ce3e1a60d47ced5d68be6563dd853f /models | |
parent | 37e0cee8770fc4f14857e16eabe83ab7e93a0646 (diff) | |
download | gitea-1abfe4e05f2c936ab43c8188003a33213b245a56.tar.gz gitea-1abfe4e05f2c936ab43c8188003a33213b245a56.zip |
PR: nothing to commit and has pull request check
Diffstat (limited to 'models')
-rw-r--r-- | models/error.go | 19 | ||||
-rw-r--r-- | models/issue.go | 102 | ||||
-rw-r--r-- | models/models.go | 2 |
3 files changed, 77 insertions, 46 deletions
diff --git a/models/error.go b/models/error.go index 92cc187b43..1986c59026 100644 --- a/models/error.go +++ b/models/error.go @@ -308,18 +308,23 @@ func (err ErrIssueNotExist) Error() string { // |____| |____/|____/____/____|_ /\___ >__ |____/ \___ >____ > |__| // \/ \/ |__| \/ \/ -type ErrPullRepoNotExist struct { - ID int64 - PullID int64 +type ErrPullRequestNotExist struct { + ID int64 + PullID int64 + HeadRepoID int64 + BaseRepoID int64 + HeadBarcnh string + BaseBranch string } -func IsErrPullRepoNotExist(err error) bool { - _, ok := err.(ErrPullRepoNotExist) +func IsErrPullRequestNotExist(err error) bool { + _, ok := err.(ErrPullRequestNotExist) return ok } -func (err ErrPullRepoNotExist) Error() string { - return fmt.Sprintf("pull repo does not exist [id: %d, pull_id: %d]", err.ID, err.PullID) +func (err ErrPullRequestNotExist) Error() string { + return fmt.Sprintf("pull request does not exist [id: %d, pull_id: %d, head_repo_id: %d, base_repo_id: %d, head_branch: %s, base_branch: %s]", + err.ID, err.PullID, err.HeadRepoID, err.BaseRepoID, err.HeadBarcnh, err.BaseBranch) } // _________ __ diff --git a/models/issue.go b/models/issue.go index f48d3206ae..9f0cb3c9e9 100644 --- a/models/issue.go +++ b/models/issue.go @@ -46,10 +46,10 @@ type Issue struct { MilestoneID int64 Milestone *Milestone `xorm:"-"` AssigneeID int64 - Assignee *User `xorm:"-"` - IsRead bool `xorm:"-"` - IsPull bool // Indicates whether is a pull request or not. - PullRepo *PullRepo `xorm:"-"` + Assignee *User `xorm:"-"` + IsRead bool `xorm:"-"` + IsPull bool // Indicates whether is a pull request or not. + *PullRequest `xorm:"-"` IsClosed bool Content string `xorm:"TEXT"` RenderedContent string `xorm:"-"` @@ -96,9 +96,13 @@ func (i *Issue) AfterSet(colName string, _ xorm.Cell) { log.Error(3, "GetUserByID[%d]: %v", i.ID, err) } case "is_pull": - i.PullRepo, err = GetPullRepoByPullID(i.ID) + if !i.IsPull { + return + } + + i.PullRequest, err = GetPullRequestByPullID(i.ID) if err != nil { - log.Error(3, "GetPullRepoByPullID[%d]: %v", i.ID, err) + log.Error(3, "GetPullRequestByPullID[%d]: %v", i.ID, err) } case "created": i.Created = regulateTimeZone(i.Created) @@ -844,23 +848,25 @@ const ( PLLL_ERQUEST_GIT ) -// PullRepo represents relation between pull request and repositories. -type PullRepo struct { - ID int64 `xorm:"pk autoincr"` - PullID int64 `xorm:"INDEX"` - HeadRepoID int64 `xorm:"UNIQUE(s)"` - HeadRepo *Repository `xorm:"-"` - BaseRepoID int64 `xorm:"UNIQUE(s)"` - HeadUserName string - HeadBarcnh string `xorm:"UNIQUE(s)"` - BaseBranch string `xorm:"UNIQUE(s)"` - MergeBase string `xorm:"VARCHAR(40)"` - Type PullRequestType - CanAutoMerge bool - HasMerged bool -} - -func (pr *PullRepo) AfterSet(colName string, _ xorm.Cell) { +// PullRequest represents relation between pull request and repositories. +type PullRequest struct { + ID int64 `xorm:"pk autoincr"` + PullID int64 `xorm:"INDEX"` + PullIndex int64 + HeadRepoID int64 `xorm:"UNIQUE(s)"` + HeadRepo *Repository `xorm:"-"` + BaseRepoID int64 `xorm:"UNIQUE(s)"` + HeadUserName string + HeadBarcnh string `xorm:"UNIQUE(s)"` + BaseBranch string `xorm:"UNIQUE(s)"` + MergeBase string `xorm:"VARCHAR(40)"` + MergedCommitID string `xorm:"VARCHAR(40)"` + Type PullRequestType + CanAutoMerge bool + HasMerged bool +} + +func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) { var err error switch colName { case "head_repo_id": @@ -872,24 +878,24 @@ func (pr *PullRepo) AfterSet(colName string, _ xorm.Cell) { } // NewPullRequest creates new pull request with labels for repository. -func NewPullRequest(repo *Repository, pr *Issue, labelIDs []int64, uuids []string, pullRepo *PullRepo, patch []byte) (err error) { +func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) { sess := x.NewSession() defer sessionRelease(sess) if err = sess.Begin(); err != nil { return err } - if err = newIssue(sess, repo, pr, labelIDs, uuids); err != nil { + if err = newIssue(sess, repo, pull, labelIDs, uuids); err != nil { return fmt.Errorf("newIssue: %v", err) } // Notify watchers. act := &Action{ - ActUserID: pr.Poster.Id, - ActUserName: pr.Poster.Name, - ActEmail: pr.Poster.Email, + ActUserID: pull.Poster.Id, + ActUserName: pull.Poster.Name, + ActEmail: pull.Poster.Email, OpType: PULL_REQUEST, - Content: fmt.Sprintf("%d|%s", pr.Index, pr.Name), + Content: fmt.Sprintf("%d|%s", pull.Index, pull.Name), RepoID: repo.ID, RepoUserName: repo.Owner.Name, RepoName: repo.Name, @@ -920,26 +926,46 @@ func NewPullRequest(repo *Repository, pr *Issue, labelIDs []int64, uuids []strin return fmt.Errorf("git apply --check: %v - %s", err, stderr) } } - pullRepo.CanAutoMerge = !strings.Contains(stdout, "error: patch failed:") + pr.CanAutoMerge = !strings.Contains(stdout, "error: patch failed:") - pullRepo.PullID = pr.ID - if _, err = sess.Insert(pullRepo); err != nil { + pr.PullID = pull.ID + pr.PullIndex = pull.Index + if _, err = sess.Insert(pr); err != nil { return fmt.Errorf("insert pull repo: %v", err) } return sess.Commit() } -// GetPullRepoByPullID returns pull repo by given pull ID. -func GetPullRepoByPullID(pullID int64) (*PullRepo, error) { - pullRepo := new(PullRepo) - has, err := x.Where("pull_id=?", pullID).Get(pullRepo) +// GetPullRequest returnss a pull request by given info. +func GetPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) { + pr := &PullRequest{ + HeadRepoID: headRepoID, + BaseRepoID: baseRepoID, + HeadBarcnh: headBranch, + BaseBranch: baseBranch, + } + + has, err := x.Get(pr) + if err != nil { + return nil, err + } else if !has { + return nil, ErrPullRequestNotExist{0, 0, headRepoID, baseRepoID, headBranch, baseBranch} + } + + return pr, nil +} + +// GetPullRequestByPullID returns pull repo by given pull ID. +func GetPullRequestByPullID(pullID int64) (*PullRequest, error) { + pr := new(PullRequest) + has, err := x.Where("pull_id=?", pullID).Get(pr) if err != nil { return nil, err } else if !has { - return nil, ErrPullRepoNotExist{0, pullID} + return nil, ErrPullRequestNotExist{0, pullID, 0, 0, "", ""} } - return pullRepo, nil + return pr, nil } // .____ ___. .__ diff --git a/models/models.go b/models/models.go index 791018f8e5..9412c18dee 100644 --- a/models/models.go +++ b/models/models.go @@ -79,7 +79,7 @@ func init() { new(User), new(PublicKey), new(Oauth2), new(AccessToken), new(Repository), new(DeployKey), new(Collaboration), new(Access), new(Watch), new(Star), new(Follow), new(Action), - new(Issue), new(PullRepo), new(Comment), new(Attachment), new(IssueUser), + new(Issue), new(PullRequest), new(Comment), new(Attachment), new(IssueUser), new(Label), new(IssueLabel), new(Milestone), new(Mirror), new(Release), new(LoginSource), new(Webhook), new(UpdateTask), new(HookTask), |