2020-01-10 08:53:53 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-10 08:53:53 +01:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-01-20 00:26:57 +01:00
|
|
|
"context"
|
2020-01-31 22:13:51 +01:00
|
|
|
"fmt"
|
|
|
|
|
2024-12-10 22:15:06 +01:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 11:37:59 +02:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-28 12:58:28 +01:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 12:09:36 +02:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2024-12-10 22:15:06 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-05-31 14:10:11 +02:00
|
|
|
"code.gitea.io/gitea/modules/cache"
|
2020-01-10 08:53:53 +01:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 21:09:51 +01:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2020-01-10 08:53:53 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToAPIPullRequest assumes following fields have been assigned with valid values:
|
|
|
|
// Required - Issue
|
|
|
|
// Optional - Merger
|
2022-06-13 11:37:59 +02:00
|
|
|
func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) *api.PullRequest {
|
2020-01-10 08:53:53 +01:00
|
|
|
var (
|
|
|
|
baseBranch *git.Branch
|
|
|
|
headBranch *git.Branch
|
|
|
|
baseCommit *git.Commit
|
|
|
|
err error
|
|
|
|
)
|
2020-01-31 22:13:51 +01:00
|
|
|
|
2024-12-19 07:10:08 +01:00
|
|
|
if err = pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("pr.LoadIssue[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:11:15 +02:00
|
|
|
if err = pr.Issue.LoadRepo(ctx); err != nil {
|
2020-01-31 22:13:51 +01:00
|
|
|
log.Error("pr.Issue.LoadRepo[%d]: %v", pr.ID, err)
|
2020-01-10 08:53:53 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-31 22:13:51 +01:00
|
|
|
|
2024-04-08 23:26:41 +02:00
|
|
|
apiIssue := ToAPIIssue(ctx, doer, pr.Issue)
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadBaseRepo(ctx); err != nil {
|
2020-03-02 23:31:55 +01:00
|
|
|
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
2020-01-10 08:53:53 +01:00
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadHeadRepo(ctx); err != nil {
|
2020-03-02 23:31:55 +01:00
|
|
|
log.Error("GetRepositoryById[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
2020-01-10 08:53:53 +01:00
|
|
|
}
|
|
|
|
|
2024-05-31 14:10:11 +02:00
|
|
|
var doerID int64
|
|
|
|
if doer != nil {
|
|
|
|
doerID = doer.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
const repoDoerPermCacheKey = "repo_doer_perm_cache"
|
|
|
|
p, err := cache.GetWithContextCache(ctx, repoDoerPermCacheKey, fmt.Sprintf("%d_%d", pr.BaseRepoID, doerID),
|
|
|
|
func() (access_model.Permission, error) {
|
|
|
|
return access_model.GetUserRepoPermission(ctx, pr.BaseRepo, doer)
|
|
|
|
})
|
2021-10-07 02:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserRepoPermission[%d]: %v", pr.BaseRepoID, err)
|
2021-11-28 12:58:28 +01:00
|
|
|
p.AccessMode = perm.AccessModeNone
|
2021-10-07 02:03:37 +02:00
|
|
|
}
|
|
|
|
|
2020-01-10 08:53:53 +01:00
|
|
|
apiPullRequest := &api.PullRequest{
|
2024-05-26 06:08:13 +02:00
|
|
|
ID: pr.ID,
|
|
|
|
URL: pr.Issue.HTMLURL(),
|
|
|
|
Index: pr.Index,
|
|
|
|
Poster: apiIssue.Poster,
|
|
|
|
Title: apiIssue.Title,
|
|
|
|
Body: apiIssue.Body,
|
|
|
|
Labels: apiIssue.Labels,
|
|
|
|
Milestone: apiIssue.Milestone,
|
|
|
|
Assignee: apiIssue.Assignee,
|
|
|
|
Assignees: apiIssue.Assignees,
|
|
|
|
State: apiIssue.State,
|
|
|
|
Draft: pr.IsWorkInProgress(ctx),
|
|
|
|
IsLocked: apiIssue.IsLocked,
|
|
|
|
Comments: apiIssue.Comments,
|
|
|
|
ReviewComments: pr.GetReviewCommentsCount(ctx),
|
|
|
|
HTMLURL: pr.Issue.HTMLURL(),
|
|
|
|
DiffURL: pr.Issue.DiffURL(),
|
|
|
|
PatchURL: pr.Issue.PatchURL(),
|
|
|
|
HasMerged: pr.HasMerged,
|
|
|
|
MergeBase: pr.MergeBase,
|
|
|
|
Mergeable: pr.Mergeable(ctx),
|
|
|
|
Deadline: apiIssue.Deadline,
|
|
|
|
Created: pr.Issue.CreatedUnix.AsTimePtr(),
|
|
|
|
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
|
|
|
|
PinOrder: apiIssue.PinOrder,
|
2020-03-02 23:31:55 +01:00
|
|
|
|
2022-04-28 17:45:33 +02:00
|
|
|
AllowMaintainerEdit: pr.AllowMaintainerEdit,
|
|
|
|
|
2020-03-02 23:31:55 +01:00
|
|
|
Base: &api.PRBranchInfo{
|
2020-01-10 08:53:53 +01:00
|
|
|
Name: pr.BaseBranch,
|
|
|
|
Ref: pr.BaseBranch,
|
|
|
|
RepoID: pr.BaseRepoID,
|
2023-06-22 15:08:08 +02:00
|
|
|
Repository: ToRepo(ctx, pr.BaseRepo, p),
|
2020-03-02 23:31:55 +01:00
|
|
|
},
|
|
|
|
Head: &api.PRBranchInfo{
|
|
|
|
Name: pr.HeadBranch,
|
2021-12-02 20:36:50 +01:00
|
|
|
Ref: fmt.Sprintf("%s%d/head", git.PullPrefix, pr.Index),
|
2020-03-02 23:31:55 +01:00
|
|
|
RepoID: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:06:27 +02:00
|
|
|
if err = pr.LoadRequestedReviewers(ctx); err != nil {
|
|
|
|
log.Error("LoadRequestedReviewers[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-05 12:59:53 +02:00
|
|
|
if err = pr.LoadRequestedReviewersTeams(ctx); err != nil {
|
|
|
|
log.Error("LoadRequestedReviewersTeams[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:06:27 +02:00
|
|
|
for _, reviewer := range pr.RequestedReviewers {
|
|
|
|
apiPullRequest.RequestedReviewers = append(apiPullRequest.RequestedReviewers, ToUser(ctx, reviewer, nil))
|
|
|
|
}
|
|
|
|
|
2024-08-05 12:59:53 +02:00
|
|
|
for _, reviewerTeam := range pr.RequestedReviewersTeams {
|
|
|
|
convertedTeam, err := ToTeam(ctx, reviewerTeam, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("LoadRequestedReviewersTeams[%d]: %v", pr.ID, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
apiPullRequest.RequestedReviewersTeams = append(apiPullRequest.RequestedReviewersTeams, convertedTeam)
|
|
|
|
}
|
|
|
|
|
2023-01-17 12:42:32 +01:00
|
|
|
if pr.Issue.ClosedUnix != 0 {
|
|
|
|
apiPullRequest.Closed = pr.Issue.ClosedUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 21:09:51 +01:00
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
|
2021-11-24 08:56:24 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
baseBranch, err = gitRepo.GetBranch(pr.BaseBranch)
|
2020-03-02 23:31:55 +01:00
|
|
|
if err != nil && !git.IsErrBranchNotExist(err) {
|
|
|
|
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
2020-01-10 08:53:53 +01:00
|
|
|
baseCommit, err = baseBranch.GetCommit()
|
2020-03-02 23:31:55 +01:00
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
log.Error("GetCommit[%s]: %v", baseBranch.Name, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Base.Sha = baseCommit.ID.String()
|
2020-01-10 08:53:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
if pr.Flow == issues_model.PullRequestFlowAGit {
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 21:09:51 +01:00
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
|
2021-07-28 11:42:56 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("OpenRepository[%s]: %v", pr.GetGitRefName(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
apiPullRequest.Head.Sha, err = gitRepo.GetRefCommitID(pr.GetGitRefName())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
apiPullRequest.Head.RepoID = pr.BaseRepoID
|
|
|
|
apiPullRequest.Head.Repository = apiPullRequest.Base.Repository
|
|
|
|
apiPullRequest.Head.Name = ""
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
if pr.HeadRepo != nil && pr.Flow == issues_model.PullRequestFlowGithub {
|
2022-05-11 12:09:36 +02:00
|
|
|
p, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
|
2021-10-07 02:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserRepoPermission[%d]: %v", pr.HeadRepoID, err)
|
2021-11-28 12:58:28 +01:00
|
|
|
p.AccessMode = perm.AccessModeNone
|
2021-10-07 02:03:37 +02:00
|
|
|
}
|
|
|
|
|
2020-03-02 23:31:55 +01:00
|
|
|
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
|
2023-06-22 15:08:08 +02:00
|
|
|
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p)
|
2020-03-02 23:31:55 +01:00
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 21:09:51 +01:00
|
|
|
headGitRepo, err := gitrepo.OpenRepository(ctx, pr.HeadRepo)
|
2020-01-10 08:53:53 +01:00
|
|
|
if err != nil {
|
2020-03-02 23:31:55 +01:00
|
|
|
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer headGitRepo.Close()
|
|
|
|
|
|
|
|
headBranch, err = headGitRepo.GetBranch(pr.HeadBranch)
|
|
|
|
if err != nil && !git.IsErrBranchNotExist(err) {
|
|
|
|
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-26 06:08:13 +02:00
|
|
|
// Outer scope variables to be used in diff calculation
|
|
|
|
var (
|
|
|
|
startCommitID string
|
|
|
|
endCommitID string
|
|
|
|
)
|
|
|
|
|
2020-03-02 23:31:55 +01:00
|
|
|
if git.IsErrBranchNotExist(err) {
|
|
|
|
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
2020-03-09 08:06:38 +01:00
|
|
|
log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
|
2020-01-10 08:53:53 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-02 23:31:55 +01:00
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Head.Sha = headCommitID
|
2024-05-26 06:08:13 +02:00
|
|
|
endCommitID = headCommitID
|
2020-03-02 23:31:55 +01:00
|
|
|
}
|
2020-01-10 08:53:53 +01:00
|
|
|
} else {
|
2020-03-02 23:31:55 +01:00
|
|
|
commit, err := headBranch.GetCommit()
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
|
|
|
|
return nil
|
2020-01-31 22:13:51 +01:00
|
|
|
}
|
2020-03-02 23:31:55 +01:00
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Head.Ref = pr.HeadBranch
|
|
|
|
apiPullRequest.Head.Sha = commit.ID.String()
|
2024-05-26 06:08:13 +02:00
|
|
|
endCommitID = commit.ID.String()
|
2020-01-31 22:13:51 +01:00
|
|
|
}
|
2020-01-10 08:53:53 +01:00
|
|
|
}
|
2024-05-26 06:08:13 +02:00
|
|
|
|
|
|
|
// Calculate diff
|
|
|
|
startCommitID = pr.MergeBase
|
|
|
|
|
|
|
|
apiPullRequest.ChangedFiles, apiPullRequest.Additions, apiPullRequest.Deletions, err = gitRepo.GetDiffShortStat(startCommitID, endCommitID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetDiffShortStat: %v", err)
|
|
|
|
}
|
2020-01-10 08:53:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-08 21:48:31 +01:00
|
|
|
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 21:09:51 +01:00
|
|
|
baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
|
2021-03-08 21:48:31 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer baseGitRepo.Close()
|
|
|
|
refs, err := baseGitRepo.GetRefsFiltered(apiPullRequest.Head.Ref)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetRefsFiltered[%s]: %v", apiPullRequest.Head.Ref, err)
|
|
|
|
return nil
|
|
|
|
} else if len(refs) == 0 {
|
|
|
|
log.Error("unable to resolve PR head ref")
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Head.Sha = refs[0].Object.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 08:53:53 +01:00
|
|
|
if pr.HasMerged {
|
|
|
|
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
|
|
|
|
apiPullRequest.MergedCommitID = &pr.MergedCommitID
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 14:37:34 +01:00
|
|
|
apiPullRequest.MergedBy = ToUser(ctx, pr.Merger, nil)
|
2020-01-10 08:53:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiPullRequest
|
|
|
|
}
|
2024-12-10 22:15:06 +01:00
|
|
|
|
|
|
|
func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs issues_model.PullRequestList, doer *user_model.User) ([]*api.PullRequest, error) {
|
|
|
|
for _, pr := range prs {
|
|
|
|
pr.BaseRepo = baseRepo
|
|
|
|
if pr.BaseRepoID == pr.HeadRepoID {
|
|
|
|
pr.HeadRepo = baseRepo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: load head repositories
|
|
|
|
if err := prs.LoadRepositories(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issueList, err := prs.LoadIssues(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := issueList.LoadLabels(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := issueList.LoadPosters(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := issueList.LoadAttachments(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := issueList.LoadMilestones(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := issueList.LoadAssignees(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reviews, err := prs.LoadReviews(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = reviews.LoadReviewers(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reviewersMap := make(map[int64][]*user_model.User)
|
|
|
|
for _, review := range reviews {
|
|
|
|
if review.Reviewer != nil {
|
|
|
|
reviewersMap[review.IssueID] = append(reviewersMap[review.IssueID], review.Reviewer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reviewCounts, err := prs.LoadReviewCommentsCounts(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, baseRepo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, baseRepo, doer)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserRepoPermission[%d]: %v", baseRepo.ID, err)
|
|
|
|
baseRepoPerm.AccessMode = perm.AccessModeNone
|
|
|
|
}
|
|
|
|
|
|
|
|
apiRepo := ToRepo(ctx, baseRepo, baseRepoPerm)
|
|
|
|
baseBranchCache := make(map[string]*git_model.Branch)
|
|
|
|
apiPullRequests := make([]*api.PullRequest, 0, len(prs))
|
|
|
|
for _, pr := range prs {
|
|
|
|
apiIssue := ToAPIIssue(ctx, doer, pr.Issue)
|
|
|
|
|
|
|
|
apiPullRequest := &api.PullRequest{
|
|
|
|
ID: pr.ID,
|
|
|
|
URL: pr.Issue.HTMLURL(),
|
|
|
|
Index: pr.Index,
|
|
|
|
Poster: apiIssue.Poster,
|
|
|
|
Title: apiIssue.Title,
|
|
|
|
Body: apiIssue.Body,
|
|
|
|
Labels: apiIssue.Labels,
|
|
|
|
Milestone: apiIssue.Milestone,
|
|
|
|
Assignee: apiIssue.Assignee,
|
|
|
|
Assignees: apiIssue.Assignees,
|
|
|
|
State: apiIssue.State,
|
|
|
|
Draft: pr.IsWorkInProgress(ctx),
|
|
|
|
IsLocked: apiIssue.IsLocked,
|
|
|
|
Comments: apiIssue.Comments,
|
|
|
|
ReviewComments: reviewCounts[pr.IssueID],
|
|
|
|
HTMLURL: pr.Issue.HTMLURL(),
|
|
|
|
DiffURL: pr.Issue.DiffURL(),
|
|
|
|
PatchURL: pr.Issue.PatchURL(),
|
|
|
|
HasMerged: pr.HasMerged,
|
|
|
|
MergeBase: pr.MergeBase,
|
|
|
|
Mergeable: pr.Mergeable(ctx),
|
|
|
|
Deadline: apiIssue.Deadline,
|
|
|
|
Created: pr.Issue.CreatedUnix.AsTimePtr(),
|
|
|
|
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
|
|
|
|
PinOrder: apiIssue.PinOrder,
|
|
|
|
|
|
|
|
AllowMaintainerEdit: pr.AllowMaintainerEdit,
|
|
|
|
|
|
|
|
Base: &api.PRBranchInfo{
|
|
|
|
Name: pr.BaseBranch,
|
|
|
|
Ref: pr.BaseBranch,
|
|
|
|
RepoID: pr.BaseRepoID,
|
|
|
|
Repository: apiRepo,
|
|
|
|
},
|
|
|
|
Head: &api.PRBranchInfo{
|
|
|
|
Name: pr.HeadBranch,
|
|
|
|
Ref: fmt.Sprintf("%s%d/head", git.PullPrefix, pr.Index),
|
|
|
|
RepoID: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pr.RequestedReviewers = reviewersMap[pr.IssueID]
|
|
|
|
for _, reviewer := range pr.RequestedReviewers {
|
|
|
|
apiPullRequest.RequestedReviewers = append(apiPullRequest.RequestedReviewers, ToUser(ctx, reviewer, nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, reviewerTeam := range pr.RequestedReviewersTeams {
|
|
|
|
convertedTeam, err := ToTeam(ctx, reviewerTeam, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("LoadRequestedReviewersTeams[%d]: %v", pr.ID, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiPullRequest.RequestedReviewersTeams = append(apiPullRequest.RequestedReviewersTeams, convertedTeam)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pr.Issue.ClosedUnix != 0 {
|
|
|
|
apiPullRequest.Closed = pr.Issue.ClosedUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
|
|
|
|
baseBranch, ok := baseBranchCache[pr.BaseBranch]
|
|
|
|
if !ok {
|
|
|
|
baseBranch, err = git_model.GetBranch(ctx, baseRepo.ID, pr.BaseBranch)
|
|
|
|
if err == nil {
|
|
|
|
baseBranchCache[pr.BaseBranch] = baseBranch
|
|
|
|
} else if !git_model.IsErrBranchNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if baseBranch != nil {
|
|
|
|
apiPullRequest.Base.Sha = baseBranch.CommitID
|
|
|
|
}
|
|
|
|
|
|
|
|
if pr.Flow == issues_model.PullRequestFlowAGit {
|
|
|
|
apiPullRequest.Head.Sha, err = gitRepo.GetRefCommitID(pr.GetGitRefName())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
apiPullRequest.Head.RepoID = pr.BaseRepoID
|
|
|
|
apiPullRequest.Head.Repository = apiPullRequest.Base.Repository
|
|
|
|
apiPullRequest.Head.Name = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var headGitRepo *git.Repository
|
|
|
|
if pr.HeadRepo != nil && pr.Flow == issues_model.PullRequestFlowGithub {
|
|
|
|
if pr.HeadRepoID == pr.BaseRepoID {
|
|
|
|
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
|
|
|
|
apiPullRequest.Head.Repository = apiRepo
|
|
|
|
headGitRepo = gitRepo
|
|
|
|
} else {
|
|
|
|
p, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserRepoPermission[%d]: %v", pr.HeadRepoID, err)
|
|
|
|
p.AccessMode = perm.AccessModeNone
|
|
|
|
}
|
|
|
|
|
|
|
|
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
|
|
|
|
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p)
|
|
|
|
|
|
|
|
headGitRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer headGitRepo.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
headBranch, err := headGitRepo.GetBranch(pr.HeadBranch)
|
|
|
|
if err != nil && !git.IsErrBranchNotExist(err) {
|
|
|
|
log.Error("GetBranch[%s]: %v", pr.HeadBranch, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outer scope variables to be used in diff calculation
|
|
|
|
var (
|
|
|
|
startCommitID string
|
|
|
|
endCommitID string
|
|
|
|
)
|
|
|
|
|
|
|
|
if git.IsErrBranchNotExist(err) {
|
|
|
|
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
log.Error("GetCommit[%s]: %v", pr.HeadBranch, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Head.Sha = headCommitID
|
|
|
|
endCommitID = headCommitID
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
commit, err := headBranch.GetCommit()
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
log.Error("GetCommit[%s]: %v", headBranch.Name, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
apiPullRequest.Head.Ref = pr.HeadBranch
|
|
|
|
apiPullRequest.Head.Sha = commit.ID.String()
|
|
|
|
endCommitID = commit.ID.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate diff
|
|
|
|
startCommitID = pr.MergeBase
|
|
|
|
|
|
|
|
apiPullRequest.ChangedFiles, apiPullRequest.Additions, apiPullRequest.Deletions, err = gitRepo.GetDiffShortStat(startCommitID, endCommitID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetDiffShortStat: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
|
|
|
|
refs, err := gitRepo.GetRefsFiltered(apiPullRequest.Head.Ref)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetRefsFiltered[%s]: %v", apiPullRequest.Head.Ref, err)
|
|
|
|
return nil, err
|
|
|
|
} else if len(refs) == 0 {
|
|
|
|
log.Error("unable to resolve PR head ref")
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Head.Sha = refs[0].Object.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pr.HasMerged {
|
|
|
|
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr()
|
|
|
|
apiPullRequest.MergedCommitID = &pr.MergedCommitID
|
|
|
|
apiPullRequest.MergedBy = ToUser(ctx, pr.Merger, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
apiPullRequests = append(apiPullRequests, apiPullRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiPullRequests, nil
|
|
|
|
}
|