mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-18 23:20:51 +00:00
## Checklist - [x] go to the last cherry-pick PR (forgejo/forgejo#7965) to figure out how far it went: [gitea@9d4ebc1f2c](9d4ebc1f2c
) - [x] cherry-pick and open PR (forgejo/forgejo#8040) - [ ] have the PR pass the CI - end-to-end (specially important if there are actions related changes) - [ ] add `run-end-to-end` label - [ ] check the result - [ ] write release notes - [ ] assign reviewers - [ ] 48h later, last call - merge 1 hour after the last call ## Legend - ❓ - No decision about the commit has been made. - 🍒 - The commit has been cherry picked. - ⏩ - The commit has been skipped. - 💡 - The commit has been skipped, but should be ported to Forgejo. - ✍️ - The commit has been skipped, and a port to Forgejo already exists. ## Commits - 🍒 [`gitea`](50d9565088
) -> [`forgejo`](c3e6eab732
) Add sort option recentclose for issues and pulls ([gitea#34525](https://github.com/go-gitea/gitea/pull/34525)) ## TODO - 💡 [`gitea`](d5bbaee64e
) Retain issue sort type when a keyword search is introduced ([gitea#34559](https://github.com/go-gitea/gitea/pull/34559)) UI: Small bat might be nice. Test needed? Do we've frontend tests covering the search? ------ - 💡 [`gitea`](82ea2387e4
) Always use an empty line to separate the commit message and trailer ([gitea#34512](https://github.com/go-gitea/gitea/pull/34512)) Needs merge ------ - 💡 [`gitea`](74858dc5ae
) Fix line-button issue after file selection in file tree ([gitea#34574](https://github.com/go-gitea/gitea/pull/34574)) Frontend: Makes it sense to pick/port ui logic in *.ts files? ------ - 💡 [`gitea`](7149c9c55d
) Fix doctor deleting orphaned issues attachments ([gitea#34142](https://github.com/go-gitea/gitea/pull/34142)) Doctor: seems useful. ------ - 💡 [`gitea`](0cec4b84e2
) Fix actions skipped commit status indicator ([gitea#34507](https://github.com/go-gitea/gitea/pull/34507)) Actions: Might benefit from additional tests. ------ - 💡 [`gitea`](4cb0c641ce
) Add "View workflow file" to Actions list page ([gitea#34538](https://github.com/go-gitea/gitea/pull/34538)) Actions: Needs tests ------ - 💡 [`gitea`](b0936f4f41
) Do not mutate incoming options to RenderUserSearch and SearchUsers ([gitea#34544](https://github.com/go-gitea/gitea/pull/34544)) Nice refactoring but needs manual merge. ------ - 💡 [`gitea`](498088c053
) Add webhook assigning test and fix possible bug ([gitea#34420](https://github.com/go-gitea/gitea/pull/34420)) Integrationtest has conflicts needs merge. ------ - 💡 [`gitea`](24a51059d7
) Fix possible nil description of pull request when migrating from CodeCommit ([gitea#34541](https://github.com/go-gitea/gitea/pull/34541)) Is this relevant to forgejo? Did not find the place to apply this small change. ------ - 💡 [`gitea`](688da55f54
) Split GetLatestCommitStatus as two functions ([gitea#34535](https://github.com/go-gitea/gitea/pull/34535)) Merge required. ------ - 💡 [`gitea`](ab9691291d
) Don't display error log when .git-blame-ignore-revs doesn't exist ([gitea#34457](https://github.com/go-gitea/gitea/pull/34457)) Unsure wheter this affects forgejo. Tests missing. ------ - 💡 [`gitea`](11ee7ff3bf
) fix: return 201 Created for CreateVariable API responses ([gitea#34517](https://github.com/go-gitea/gitea/pull/34517)) Actions: This is marked as breaking the api. Pls think about whether this breaking change iss needed & how this impact api-version-increase. The corresponding clinet change can be found here: https://gitea.com/gitea/go-sdk/pulls/713/files ------ - 💡 [`gitea`](9b295e984a
) Actions list ([gitea#34530](https://github.com/go-gitea/gitea/pull/34530)) Actions: Regression from https://github.com/go-gitea/gitea/pull/34337 Part of https://codeberg.org/forgejo/forgejo/pulls/7909 ------ ## Skipped - ⏩ [`gitea`](bb6377d080
) [skip ci] Updated translations via Crowdin ------ - ⏩ [`gitea`](07d802a815
) [skip ci] Updated translations via Crowdin ------ - ⏩ [`gitea`](c6e2093f42
) Clean up "file-view" related styles ([gitea#34558](https://github.com/go-gitea/gitea/pull/34558)) - gitea ui specific specific ------ - ⏩ [`gitea`](9f10885b21
) Refactor commit reader ([gitea#34542](https://github.com/go-gitea/gitea/pull/34542)) - gitea refactor specific ------ <details> <summary><h2>Stats</h2></summary> <br> Between [`gitea@9d4ebc1f2c`](9d4ebc1f2c
) and [`gitea@d5bbaee64e`](d5bbaee64e
), **18** commits have been reviewed. We picked **1**, skipped **4**, and decided to port **13**. </details> Co-authored-by: Markus Amshove <scm@amshove.org> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8040 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de> Co-committed-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
270 lines
8.5 KiB
Go
270 lines
8.5 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package issues
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"forgejo.org/models/db"
|
|
access_model "forgejo.org/models/perm/access"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/models/unit"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/container"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/util"
|
|
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
// PullRequestsOptions holds the options for PRs
|
|
type PullRequestsOptions struct {
|
|
db.ListOptions
|
|
State string
|
|
SortType string
|
|
Labels []int64
|
|
MilestoneID int64
|
|
PosterID int64
|
|
}
|
|
|
|
func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullRequestsOptions) *xorm.Session {
|
|
sess := db.GetEngine(ctx).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 len(opts.Labels) > 0 {
|
|
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
|
|
In("issue_label.label_id", opts.Labels)
|
|
}
|
|
|
|
if opts.MilestoneID > 0 {
|
|
sess.And("issue.milestone_id=?", opts.MilestoneID)
|
|
}
|
|
|
|
if opts.PosterID > 0 {
|
|
sess.And("issue.poster_id=?", opts.PosterID)
|
|
}
|
|
|
|
return sess
|
|
}
|
|
|
|
func GetUnmergedPullRequestsByHeadInfoMax(ctx context.Context, repoID, olderThan int64, branch string) ([]*PullRequest, error) {
|
|
prs := make([]*PullRequest, 0, 2)
|
|
sess := db.GetEngine(ctx).
|
|
Join("INNER", "issue", "issue.id = `pull_request`.issue_id").
|
|
Where("`pull_request`.head_repo_id = ? AND `pull_request`.head_branch = ? AND `pull_request`.has_merged = ? AND `issue`.is_closed = ? AND `pull_request`.flow = ? AND (`issue`.`created` IS NULL OR `issue`.`created` <= ?)", repoID, branch, false, false, PullRequestFlowGithub, olderThan)
|
|
return prs, sess.Find(&prs)
|
|
}
|
|
|
|
// GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged
|
|
func GetUnmergedPullRequestsByHeadInfo(ctx context.Context, repoID int64, branch string) ([]*PullRequest, error) {
|
|
prs := make([]*PullRequest, 0, 2)
|
|
sess := db.GetEngine(ctx).
|
|
Join("INNER", "issue", "issue.id = pull_request.issue_id").
|
|
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?", repoID, branch, false, false, PullRequestFlowGithub)
|
|
return prs, sess.Find(&prs)
|
|
}
|
|
|
|
// CanMaintainerWriteToBranch check whether user is a maintainer and could write to the branch
|
|
func CanMaintainerWriteToBranch(ctx context.Context, 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(ctx, p.Units[0].RepoID, branch)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
for _, pr := range prs {
|
|
if pr.AllowMaintainerEdit {
|
|
err = pr.LoadBaseRepo(ctx)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
prPerm, err := access_model.GetUserRepoPermission(ctx, 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(ctx context.Context, repoID int64, branch string) ([]*PullRequest, error) {
|
|
prs := make([]*PullRequest, 0, 2)
|
|
return prs, db.GetEngine(ctx).
|
|
Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
|
|
repoID, branch, false, false).
|
|
OrderBy("issue.updated_unix DESC").
|
|
Join("INNER", "issue", "issue.id=pull_request.issue_id").
|
|
Find(&prs)
|
|
}
|
|
|
|
// GetPullRequestIDsByCheckStatus returns all pull requests according the special checking status.
|
|
func GetPullRequestIDsByCheckStatus(ctx context.Context, status PullRequestStatus) ([]int64, error) {
|
|
prs := make([]int64, 0, 10)
|
|
return prs, db.GetEngine(ctx).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(ctx context.Context, baseRepoID int64, opts *PullRequestsOptions) (PullRequestList, int64, error) {
|
|
if opts.Page <= 0 {
|
|
opts.Page = 1
|
|
}
|
|
|
|
countSession := listPullRequestStatement(ctx, baseRepoID, opts)
|
|
maxResults, err := countSession.Count(new(PullRequest))
|
|
if err != nil {
|
|
log.Error("Count PRs: %v", err)
|
|
return nil, maxResults, err
|
|
}
|
|
|
|
findSession := listPullRequestStatement(ctx, baseRepoID, opts)
|
|
applySorts(findSession, opts.SortType, 0)
|
|
findSession = db.SetSessionPagination(findSession, opts)
|
|
prs := make([]*PullRequest, 0, opts.PageSize)
|
|
found := findSession.Find(&prs)
|
|
return prs, maxResults, found
|
|
}
|
|
|
|
// PullRequestList defines a list of pull requests
|
|
type PullRequestList []*PullRequest
|
|
|
|
func (prs PullRequestList) getRepositoryIDs() []int64 {
|
|
repoIDs := make(container.Set[int64])
|
|
for _, pr := range prs {
|
|
if pr.BaseRepo == nil && pr.BaseRepoID > 0 {
|
|
repoIDs.Add(pr.BaseRepoID)
|
|
}
|
|
if pr.HeadRepo == nil && pr.HeadRepoID > 0 {
|
|
repoIDs.Add(pr.HeadRepoID)
|
|
}
|
|
}
|
|
return repoIDs.Values()
|
|
}
|
|
|
|
func (prs PullRequestList) LoadRepositories(ctx context.Context) error {
|
|
repoIDs := prs.getRepositoryIDs()
|
|
reposMap := make(map[int64]*repo_model.Repository, len(repoIDs))
|
|
if err := db.GetEngine(ctx).
|
|
In("id", repoIDs).
|
|
Find(&reposMap); err != nil {
|
|
return fmt.Errorf("find repos: %w", err)
|
|
}
|
|
for _, pr := range prs {
|
|
if pr.BaseRepo == nil {
|
|
pr.BaseRepo = reposMap[pr.BaseRepoID]
|
|
}
|
|
if pr.HeadRepo == nil {
|
|
pr.HeadRepo = reposMap[pr.HeadRepoID]
|
|
pr.isHeadRepoLoaded = true
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (prs PullRequestList) LoadAttributes(ctx context.Context) error {
|
|
if _, err := prs.LoadIssues(ctx); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (prs PullRequestList) LoadIssues(ctx context.Context) (IssueList, error) {
|
|
if len(prs) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
// Load issues which are not loaded
|
|
issueIDs := container.FilterSlice(prs, func(pr *PullRequest) (int64, bool) {
|
|
return pr.IssueID, pr.Issue == nil && pr.IssueID > 0
|
|
})
|
|
issues := make(map[int64]*Issue, len(issueIDs))
|
|
if err := db.GetEngine(ctx).
|
|
In("id", issueIDs).
|
|
Find(&issues); err != nil {
|
|
return nil, fmt.Errorf("find issues: %w", err)
|
|
}
|
|
|
|
issueList := make(IssueList, 0, len(prs))
|
|
for _, pr := range prs {
|
|
if pr.Issue == nil {
|
|
pr.Issue = issues[pr.IssueID]
|
|
/*
|
|
Old code:
|
|
pr.Issue.PullRequest = pr // panic here means issueIDs and prs are not in sync
|
|
|
|
It's worth panic because it's almost impossible to happen under normal use.
|
|
But in integration testing, an asynchronous task could read a database that has been reset.
|
|
So returning an error would make more sense, let the caller has a choice to ignore it.
|
|
*/
|
|
if pr.Issue == nil {
|
|
return nil, fmt.Errorf("issues and prs may be not in sync: cannot find issue %v for pr %v: %w", pr.IssueID, pr.ID, util.ErrNotExist)
|
|
}
|
|
}
|
|
pr.Issue.PullRequest = pr
|
|
if pr.Issue.Repo == nil {
|
|
pr.Issue.Repo = pr.BaseRepo
|
|
}
|
|
issueList = append(issueList, pr.Issue)
|
|
}
|
|
return issueList, nil
|
|
}
|
|
|
|
// GetIssueIDs returns all issue ids
|
|
func (prs PullRequestList) GetIssueIDs() []int64 {
|
|
return container.FilterSlice(prs, func(pr *PullRequest) (int64, bool) {
|
|
return pr.IssueID, pr.IssueID > 0
|
|
})
|
|
}
|
|
|
|
// HasMergedPullRequestInRepo returns whether the user(poster) has merged pull-request in the repo
|
|
func HasMergedPullRequestInRepo(ctx context.Context, repoID, posterID int64) (bool, error) {
|
|
return db.GetEngine(ctx).
|
|
Join("INNER", "pull_request", "pull_request.issue_id = issue.id").
|
|
Where("repo_id=?", repoID).
|
|
And("poster_id=?", posterID).
|
|
And("is_pull=?", true).
|
|
And("pull_request.has_merged=?", true).
|
|
Select("issue.id").
|
|
Limit(1).
|
|
Get(new(Issue))
|
|
}
|
|
|
|
// GetPullRequestByIssueIDs returns all pull requests by issue ids
|
|
func GetPullRequestByIssueIDs(ctx context.Context, issueIDs []int64) (PullRequestList, error) {
|
|
prs := make([]*PullRequest, 0, len(issueIDs))
|
|
return prs, db.GetEngine(ctx).
|
|
Where("issue_id > 0").
|
|
In("issue_id", issueIDs).
|
|
Find(&prs)
|
|
}
|