mirror of
https://github.com/go-gitea/gitea.git
synced 2026-07-13 05:13:29 +00:00
3dc2724d36
Backport: #23189 Close: #22784 1. On GH, we can reopen a PR which was closed before after pushing commits. After reopening PR, we can see the commits that were pushed after closing PR in the time line. So the case of [issue](https://github.com/go-gitea/gitea/issues/22784) is a bug which needs to be fixed. 2. After closing a PR and pushing commits, `headBranchSha` is not equal to `sha`(which is the last commit ID string of reference). If the judgement exists, the button of reopen will not display. So, skip the judgement if the status of PR is closed.  3. Even if PR is already close, we should still insert comment record into DB when we push commits. So we should still call function `CreatePushPullComment()`. https://github.com/go-gitea/gitea/blob/067b0c2664d127c552ccdfd264257caca4907a77/services/pull/pull.go#L260-L282 So, I add a switch(`includeClosed`) to the `GetUnmergedPullRequestsByHeadInfo` func to control whether the status of PR must be open. In this case, by setting `includeClosed` to `true`, we can query the closed PR.  4. In the loop of comments, I use the`latestCloseCommentID` variable to record the last occurrence of the close comment. In the go template, if the status of PR is closed, the comments whose type is `CommentTypePullRequestPush(29)` after `latestCloseCommentID` won't be rendered.  e.g. 1). The initial status of the PR is opened.  2). Then I click the button of `Close`. PR is closed now.  3). I try to push a commit to this PR, even though its current status is closed.  But in comments list, this commit do not display.This is as expected :)  4). Click the `Reopen` button, the commit which is pushed after closing PR display now. 
199 lines
5.9 KiB
Go
199 lines
5.9 KiB
Go
// Copyright 2019 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 issues
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
|
"code.gitea.io/gitea/models/unit"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/base"
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
// PullRequestsOptions holds the options for PRs
|
|
type PullRequestsOptions struct {
|
|
db.ListOptions
|
|
State string
|
|
SortType string
|
|
Labels []string
|
|
MilestoneID int64
|
|
}
|
|
|
|
func listPullRequestStatement(baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) {
|
|
sess := db.GetEngine(db.DefaultContext).Where("pull_request.base_repo_id=?", baseRepoID)
|
|
|
|
sess.Join("INNER", "issue", "pull_request.issue_id = issue.id")
|
|
switch opts.State {
|
|
case "closed", "open":
|
|
sess.And("issue.is_closed=?", opts.State == "closed")
|
|
}
|
|
|
|
if labelIDs, err := base.StringsToInt64s(opts.Labels); err != nil {
|
|
return nil, err
|
|
} else if len(labelIDs) > 0 {
|
|
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
|
|
In("issue_label.label_id", labelIDs)
|
|
}
|
|
|
|
if opts.MilestoneID > 0 {
|
|
sess.And("issue.milestone_id=?", opts.MilestoneID)
|
|
}
|
|
|
|
return sess, nil
|
|
}
|
|
|
|
// GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
|
|
// by given head information (repo and branch).
|
|
// arg `includeClosed` controls whether the SQL returns closed PRs
|
|
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string, includeClosed bool) ([]*PullRequest, error) {
|
|
prs := make([]*PullRequest, 0, 2)
|
|
sess := db.GetEngine(db.DefaultContext).
|
|
Join("INNER", "issue", "issue.id = pull_request.issue_id").
|
|
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND flow = ?", repoID, branch, false, PullRequestFlowGithub)
|
|
if !includeClosed {
|
|
sess.Where("issue.is_closed = ?", false)
|
|
}
|
|
return prs, sess.Find(&prs)
|
|
}
|
|
|
|
// CanMaintainerWriteToBranch check whether user is a maintainer and could write to the branch
|
|
func CanMaintainerWriteToBranch(p access_model.Permission, branch string, user *user_model.User) bool {
|
|
if p.CanWrite(unit.TypeCode) {
|
|
return true
|
|
}
|
|
|
|
if len(p.Units) < 1 {
|
|
return false
|
|
}
|
|
|
|
prs, err := GetUnmergedPullRequestsByHeadInfo(p.Units[0].RepoID, branch, false)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
for _, pr := range prs {
|
|
if pr.AllowMaintainerEdit {
|
|
err = pr.LoadBaseRepo()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
prPerm, err := access_model.GetUserRepoPermission(db.DefaultContext, pr.BaseRepo, user)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if prPerm.CanWrite(unit.TypeCode) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// HasUnmergedPullRequestsByHeadInfo checks if there are open and not merged pull request
|
|
// by given head information (repo and branch)
|
|
func HasUnmergedPullRequestsByHeadInfo(ctx context.Context, repoID int64, branch string) (bool, error) {
|
|
return db.GetEngine(ctx).
|
|
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?",
|
|
repoID, branch, false, false, PullRequestFlowGithub).
|
|
Join("INNER", "issue", "issue.id = pull_request.issue_id").
|
|
Exist(&PullRequest{})
|
|
}
|
|
|
|
// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
|
|
// by given base information (repo and branch).
|
|
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
|
|
prs := make([]*PullRequest, 0, 2)
|
|
return prs, db.GetEngine(db.DefaultContext).
|
|
Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
|
|
repoID, branch, false, false).
|
|
Join("INNER", "issue", "issue.id=pull_request.issue_id").
|
|
Find(&prs)
|
|
}
|
|
|
|
// GetPullRequestIDsByCheckStatus returns all pull requests according the special checking status.
|
|
func GetPullRequestIDsByCheckStatus(status PullRequestStatus) ([]int64, error) {
|
|
prs := make([]int64, 0, 10)
|
|
return prs, db.GetEngine(db.DefaultContext).Table("pull_request").
|
|
Where("status=?", status).
|
|
Cols("pull_request.id").
|
|
Find(&prs)
|
|
}
|
|
|
|
// PullRequests returns all pull requests for a base Repo by the given conditions
|
|
func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest, int64, error) {
|
|
if opts.Page <= 0 {
|
|
opts.Page = 1
|
|
}
|
|
|
|
countSession, err := listPullRequestStatement(baseRepoID, opts)
|
|
if err != nil {
|
|
log.Error("listPullRequestStatement: %v", err)
|
|
return nil, 0, err
|
|
}
|
|
maxResults, err := countSession.Count(new(PullRequest))
|
|
if err != nil {
|
|
log.Error("Count PRs: %v", err)
|
|
return nil, maxResults, err
|
|
}
|
|
|
|
findSession, err := listPullRequestStatement(baseRepoID, opts)
|
|
sortIssuesSession(findSession, opts.SortType, 0)
|
|
if err != nil {
|
|
log.Error("listPullRequestStatement: %v", err)
|
|
return nil, maxResults, err
|
|
}
|
|
findSession = db.SetSessionPagination(findSession, opts)
|
|
prs := make([]*PullRequest, 0, opts.PageSize)
|
|
return prs, maxResults, findSession.Find(&prs)
|
|
}
|
|
|
|
// PullRequestList defines a list of pull requests
|
|
type PullRequestList []*PullRequest
|
|
|
|
func (prs PullRequestList) loadAttributes(ctx context.Context) error {
|
|
if len(prs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// Load issues.
|
|
issueIDs := prs.GetIssueIDs()
|
|
issues := make([]*Issue, 0, len(issueIDs))
|
|
if err := db.GetEngine(ctx).
|
|
Where("id > 0").
|
|
In("id", issueIDs).
|
|
Find(&issues); err != nil {
|
|
return fmt.Errorf("find issues: %w", err)
|
|
}
|
|
|
|
set := make(map[int64]*Issue)
|
|
for i := range issues {
|
|
set[issues[i].ID] = issues[i]
|
|
}
|
|
for i := range prs {
|
|
prs[i].Issue = set[prs[i].IssueID]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetIssueIDs returns all issue ids
|
|
func (prs PullRequestList) GetIssueIDs() []int64 {
|
|
issueIDs := make([]int64, 0, len(prs))
|
|
for i := range prs {
|
|
issueIDs = append(issueIDs, prs[i].IssueID)
|
|
}
|
|
return issueIDs
|
|
}
|
|
|
|
// LoadAttributes load all the prs attributes
|
|
func (prs PullRequestList) LoadAttributes() error {
|
|
return prs.loadAttributes(db.DefaultContext)
|
|
}
|