2018-08-06 10:13:22 +05:30
// Copyright 2018 The Gitea Authors. All rights reserved.
2022-11-27 23:50:29 +05:30
// SPDX-License-Identifier: MIT
2018-08-06 10:13:22 +05:30
2022-06-13 15:07:59 +05:30
package issues
2018-08-06 10:13:22 +05:30
import (
2021-12-10 06:57:50 +05:30
"context"
2020-04-18 19:20:25 +05:30
"fmt"
2023-12-28 15:08:59 +05:30
"slices"
2019-11-14 08:27:36 +05:30
"strings"
2018-08-06 10:13:22 +05:30
2021-09-19 17:19:59 +05:30
"code.gitea.io/gitea/models/db"
2022-06-12 21:21:54 +05:30
git_model "code.gitea.io/gitea/models/git"
2022-03-29 11:59:02 +05:30
"code.gitea.io/gitea/models/organization"
2021-11-28 17:28:28 +05:30
"code.gitea.io/gitea/models/perm"
2022-05-11 15:39:36 +05:30
access_model "code.gitea.io/gitea/models/perm/access"
2021-11-10 01:27:58 +05:30
"code.gitea.io/gitea/models/unit"
2021-11-24 15:19:20 +05:30
user_model "code.gitea.io/gitea/models/user"
2022-06-13 15:07:59 +05:30
"code.gitea.io/gitea/modules/structs"
2019-08-15 20:16:21 +05:30
"code.gitea.io/gitea/modules/timeutil"
2022-07-19 18:50:28 +05:30
"code.gitea.io/gitea/modules/util"
2018-08-06 10:13:22 +05:30
2019-06-23 20:52:43 +05:30
"xorm.io/builder"
2018-08-06 10:13:22 +05:30
)
2022-06-13 15:07:59 +05:30
// ErrReviewNotExist represents a "ReviewNotExist" kind of error.
type ErrReviewNotExist struct {
ID int64
}
// IsErrReviewNotExist checks if an error is a ErrReviewNotExist.
func IsErrReviewNotExist ( err error ) bool {
_ , ok := err . ( ErrReviewNotExist )
return ok
}
func ( err ErrReviewNotExist ) Error ( ) string {
return fmt . Sprintf ( "review does not exist [id: %d]" , err . ID )
}
2022-10-18 11:20:37 +05:30
func ( err ErrReviewNotExist ) Unwrap ( ) error {
return util . ErrNotExist
}
2022-06-13 15:07:59 +05:30
// ErrNotValidReviewRequest an not allowed review request modify
type ErrNotValidReviewRequest struct {
Reason string
UserID int64
RepoID int64
}
// IsErrNotValidReviewRequest checks if an error is a ErrNotValidReviewRequest.
func IsErrNotValidReviewRequest ( err error ) bool {
_ , ok := err . ( ErrNotValidReviewRequest )
return ok
}
func ( err ErrNotValidReviewRequest ) Error ( ) string {
return fmt . Sprintf ( "%s [user_id: %d, repo_id: %d]" ,
err . Reason ,
err . UserID ,
err . RepoID )
}
2022-10-18 11:20:37 +05:30
func ( err ErrNotValidReviewRequest ) Unwrap ( ) error {
return util . ErrInvalidArgument
}
2018-08-06 10:13:22 +05:30
// ReviewType defines the sort of feedback a review gives
type ReviewType int
// ReviewTypeUnknown unknown review type
const ReviewTypeUnknown ReviewType = - 1
const (
// ReviewTypePending is a review which is not published yet
ReviewTypePending ReviewType = iota
// ReviewTypeApprove approves changes
ReviewTypeApprove
// ReviewTypeComment gives general feedback
ReviewTypeComment
// ReviewTypeReject gives feedback blocking merge
ReviewTypeReject
2020-04-06 22:03:34 +05:30
// ReviewTypeRequest request review from others
ReviewTypeRequest
2018-08-06 10:13:22 +05:30
)
// Icon returns the corresponding icon for the review type
func ( rt ReviewType ) Icon ( ) string {
switch rt {
case ReviewTypeApprove :
2020-04-03 10:42:42 +05:30
return "check"
2018-08-06 10:13:22 +05:30
case ReviewTypeReject :
2020-07-17 20:45:12 +05:30
return "diff"
2020-04-03 10:42:42 +05:30
case ReviewTypeComment :
2018-10-19 19:06:41 +05:30
return "comment"
2020-04-06 22:03:34 +05:30
case ReviewTypeRequest :
2020-07-17 20:45:12 +05:30
return "dot-fill"
2018-08-06 10:13:22 +05:30
default :
return "comment"
}
}
// Review represents collection of code comments giving feedback for a PR
type Review struct {
2020-01-23 22:58:15 +05:30
ID int64 ` xorm:"pk autoincr" `
Type ReviewType
2022-03-29 11:59:02 +05:30
Reviewer * user_model . User ` xorm:"-" `
ReviewerID int64 ` xorm:"index" `
ReviewerTeamID int64 ` xorm:"NOT NULL DEFAULT 0" `
ReviewerTeam * organization . Team ` xorm:"-" `
2020-01-23 22:58:15 +05:30
OriginalAuthor string
OriginalAuthorID int64
Issue * Issue ` xorm:"-" `
IssueID int64 ` xorm:"index" `
Content string ` xorm:"TEXT" `
2019-12-04 06:38:56 +05:30
// Official is a review made by an assigned approver (counts towards approval)
2021-02-11 23:02:25 +05:30
Official bool ` xorm:"NOT NULL DEFAULT false" `
2024-01-19 21:35:02 +05:30
CommitID string ` xorm:"VARCHAR(64)" `
2021-02-11 23:02:25 +05:30
Stale bool ` xorm:"NOT NULL DEFAULT false" `
Dismissed bool ` xorm:"NOT NULL DEFAULT false" `
2018-08-06 10:13:22 +05:30
2019-08-15 20:16:21 +05:30
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX created" `
UpdatedUnix timeutil . TimeStamp ` xorm:"INDEX updated" `
2018-08-06 10:13:22 +05:30
// CodeComments are the initial code comments of the review
CodeComments CodeComments ` xorm:"-" `
2020-01-23 22:58:15 +05:30
Comments [ ] * Comment ` xorm:"-" `
2018-08-06 10:13:22 +05:30
}
2021-09-19 17:19:59 +05:30
func init ( ) {
db . RegisterModel ( new ( Review ) )
}
2022-01-20 04:56:57 +05:30
// LoadCodeComments loads CodeComments
func ( r * Review ) LoadCodeComments ( ctx context . Context ) ( err error ) {
2020-05-02 05:50:51 +05:30
if r . CodeComments != nil {
2023-07-09 17:28:06 +05:30
return err
2020-05-02 05:50:51 +05:30
}
2022-05-20 19:38:52 +05:30
if err = r . loadIssue ( ctx ) ; err != nil {
2023-07-09 17:28:06 +05:30
return err
2019-11-15 18:29:21 +05:30
}
2023-06-21 21:38:12 +05:30
r . CodeComments , err = fetchCodeCommentsByReview ( ctx , r . Issue , nil , r , false )
2022-06-20 15:32:49 +05:30
return err
2018-08-06 10:13:22 +05:30
}
2022-05-20 19:38:52 +05:30
func ( r * Review ) loadIssue ( ctx context . Context ) ( err error ) {
2020-05-02 05:50:51 +05:30
if r . Issue != nil {
2023-07-09 17:28:06 +05:30
return err
2020-05-02 05:50:51 +05:30
}
2022-06-13 15:07:59 +05:30
r . Issue , err = GetIssueByID ( ctx , r . IssueID )
2022-06-20 15:32:49 +05:30
return err
2018-08-06 10:13:22 +05:30
}
2022-11-19 13:42:33 +05:30
// LoadReviewer loads reviewer
func ( r * Review ) LoadReviewer ( ctx context . Context ) ( err error ) {
2020-10-13 01:25:13 +05:30
if r . ReviewerID == 0 || r . Reviewer != nil {
2023-07-09 17:28:06 +05:30
return err
2018-08-06 10:13:22 +05:30
}
Implement actions (#21937)
Close #13539.
Co-authored by: @lunny @appleboy @fuxiaohei and others.
Related projects:
- https://gitea.com/gitea/actions-proto-def
- https://gitea.com/gitea/actions-proto-go
- https://gitea.com/gitea/act
- https://gitea.com/gitea/act_runner
### Summary
The target of this PR is to bring a basic implementation of "Actions",
an internal CI/CD system of Gitea. That means even though it has been
merged, the state of the feature is **EXPERIMENTAL**, and please note
that:
- It is disabled by default;
- It shouldn't be used in a production environment currently;
- It shouldn't be used in a public Gitea instance currently;
- Breaking changes may be made before it's stable.
**Please comment on #13539 if you have any different product design
ideas**, all decisions reached there will be adopted here. But in this
PR, we don't talk about **naming, feature-creep or alternatives**.
### ⚠️ Breaking
`gitea-actions` will become a reserved user name. If a user with the
name already exists in the database, it is recommended to rename it.
### Some important reviews
- What is `DEFAULT_ACTIONS_URL` in `app.ini` for?
- https://github.com/go-gitea/gitea/pull/21937#discussion_r1055954954
- Why the api for runners is not under the normal `/api/v1` prefix?
- https://github.com/go-gitea/gitea/pull/21937#discussion_r1061173592
- Why DBFS?
- https://github.com/go-gitea/gitea/pull/21937#discussion_r1061301178
- Why ignore events triggered by `gitea-actions` bot?
- https://github.com/go-gitea/gitea/pull/21937#discussion_r1063254103
- Why there's no permission control for actions?
- https://github.com/go-gitea/gitea/pull/21937#discussion_r1090229868
### What it looks like
<details>
#### Manage runners
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205870657-c72f590e-2e08-4cd4-be7f-2e0abb299bbf.png">
#### List runs
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205872794-50fde990-2b45-48c1-a178-908e4ec5b627.png">
#### View logs
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205872501-9b7b9000-9542-4991-8f55-18ccdada77c3.png">
</details>
### How to try it
<details>
#### 1. Start Gitea
Clone this branch and [install from
source](https://docs.gitea.io/en-us/install-from-source).
Add additional configurations in `app.ini` to enable Actions:
```ini
[actions]
ENABLED = true
```
Start it.
If all is well, you'll see the management page of runners:
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205877365-8e30a780-9b10-4154-b3e8-ee6c3cb35a59.png">
#### 2. Start runner
Clone the [act_runner](https://gitea.com/gitea/act_runner), and follow
the
[README](https://gitea.com/gitea/act_runner/src/branch/main/README.md)
to start it.
If all is well, you'll see a new runner has been added:
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205878000-216f5937-e696-470d-b66c-8473987d91c3.png">
#### 3. Enable actions for a repo
Create a new repo or open an existing one, check the `Actions` checkbox
in settings and submit.
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205879705-53e09208-73c0-4b3e-a123-2dcf9aba4b9c.png">
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205879383-23f3d08f-1a85-41dd-a8b3-54e2ee6453e8.png">
If all is well, you'll see a new tab "Actions":
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205881648-a8072d8c-5803-4d76-b8a8-9b2fb49516c1.png">
#### 4. Upload workflow files
Upload some workflow files to `.gitea/workflows/xxx.yaml`, you can
follow the [quickstart](https://docs.github.com/en/actions/quickstart)
of GitHub Actions. Yes, Gitea Actions is compatible with GitHub Actions
in most cases, you can use the same demo:
```yaml
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
```
If all is well, you'll see a new run in `Actions` tab:
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205884473-79a874bc-171b-4aaf-acd5-0241a45c3b53.png">
#### 5. Check the logs of jobs
Click a run and you'll see the logs:
<img width="1792" alt="image"
src="https://user-images.githubusercontent.com/9418365/205884800-994b0374-67f7-48ff-be9a-4c53f3141547.png">
#### 6. Go on
You can try more examples in [the
documents](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions)
of GitHub Actions, then you might find a lot of bugs.
Come on, PRs are welcome.
</details>
See also: [Feature Preview: Gitea
Actions](https://blog.gitea.io/2022/12/feature-preview-gitea-actions/)
---------
Co-authored-by: a1012112796 <1012112796@qq.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: ChristopherHX <christopher.homberger@web.de>
Co-authored-by: John Olheiser <john.olheiser@gmail.com>
2023-01-31 07:15:19 +05:30
r . Reviewer , err = user_model . GetPossibleUserByID ( ctx , r . ReviewerID )
2024-02-14 22:01:51 +05:30
if err != nil {
if ! user_model . IsErrUserNotExist ( err ) {
return fmt . Errorf ( "GetPossibleUserByID [%d]: %w" , r . ReviewerID , err )
}
r . ReviewerID = user_model . GhostUserID
r . Reviewer = user_model . NewGhostUser ( )
return nil
}
2022-06-20 15:32:49 +05:30
return err
2018-08-06 10:13:22 +05:30
}
2022-11-19 13:42:33 +05:30
// LoadReviewerTeam loads reviewer team
func ( r * Review ) LoadReviewerTeam ( ctx context . Context ) ( err error ) {
2020-10-13 01:25:13 +05:30
if r . ReviewerTeamID == 0 || r . ReviewerTeam != nil {
2023-07-09 17:28:06 +05:30
return nil
2020-10-13 01:25:13 +05:30
}
2022-05-20 19:38:52 +05:30
r . ReviewerTeam , err = organization . GetTeamByID ( ctx , r . ReviewerTeamID )
2022-06-20 15:32:49 +05:30
return err
2020-10-13 01:25:13 +05:30
}
2022-01-20 04:56:57 +05:30
// LoadAttributes loads all attributes except CodeComments
func ( r * Review ) LoadAttributes ( ctx context . Context ) ( err error ) {
2022-05-20 19:38:52 +05:30
if err = r . loadIssue ( ctx ) ; err != nil {
2023-07-09 17:28:06 +05:30
return err
2018-08-06 10:13:22 +05:30
}
2022-01-20 04:56:57 +05:30
if err = r . LoadCodeComments ( ctx ) ; err != nil {
2023-07-09 17:28:06 +05:30
return err
2020-05-02 05:50:51 +05:30
}
2022-11-19 13:42:33 +05:30
if err = r . LoadReviewer ( ctx ) ; err != nil {
2023-07-09 17:28:06 +05:30
return err
2018-08-06 10:13:22 +05:30
}
2022-11-19 13:42:33 +05:30
if err = r . LoadReviewerTeam ( ctx ) ; err != nil {
2023-07-09 17:28:06 +05:30
return err
2020-10-13 01:25:13 +05:30
}
2022-06-20 15:32:49 +05:30
return err
2018-08-06 10:13:22 +05:30
}
2023-05-10 14:46:44 +05:30
func ( r * Review ) HTMLTypeColorName ( ) string {
switch r . Type {
case ReviewTypeApprove :
2023-08-04 02:58:21 +05:30
if r . Stale {
return "yellow"
}
2023-05-10 14:46:44 +05:30
return "green"
case ReviewTypeComment :
return "grey"
case ReviewTypeReject :
return "red"
case ReviewTypeRequest :
return "yellow"
}
return "grey"
}
2022-05-20 19:38:52 +05:30
// GetReviewByID returns the review by the given ID
func GetReviewByID ( ctx context . Context , id int64 ) ( * Review , error ) {
2018-08-06 10:13:22 +05:30
review := new ( Review )
2022-05-20 19:38:52 +05:30
if has , err := db . GetEngine ( ctx ) . ID ( id ) . Get ( review ) ; err != nil {
2018-08-06 10:13:22 +05:30
return nil , err
} else if ! has {
return nil , ErrReviewNotExist { ID : id }
}
2023-10-24 08:24:59 +05:30
return review , nil
2018-08-06 10:13:22 +05:30
}
// CreateReviewOptions represent the options to create a review. Type, Issue and Reviewer are required.
type CreateReviewOptions struct {
2020-10-13 01:25:13 +05:30
Content string
Type ReviewType
Issue * Issue
2021-11-24 15:19:20 +05:30
Reviewer * user_model . User
2022-03-29 11:59:02 +05:30
ReviewerTeam * organization . Team
2020-10-13 01:25:13 +05:30
Official bool
CommitID string
Stale bool
2019-12-04 06:38:56 +05:30
}
2020-10-13 01:25:13 +05:30
// IsOfficialReviewer check if at least one of the provided reviewers can make official reviews in issue (counts towards required approvals)
2023-09-29 04:54:36 +05:30
func IsOfficialReviewer ( ctx context . Context , issue * Issue , reviewer * user_model . User ) ( bool , error ) {
2024-03-21 18:43:08 +05:30
if err := issue . LoadPullRequest ( ctx ) ; err != nil {
2019-12-04 06:38:56 +05:30
return false , err
}
2023-01-16 13:30:22 +05:30
2024-03-21 18:43:08 +05:30
pr := issue . PullRequest
2023-01-16 13:30:22 +05:30
rule , err := git_model . GetFirstMatchProtectedBranchRule ( ctx , pr . BaseRepoID , pr . BaseBranch )
if err != nil {
2019-12-04 06:38:56 +05:30
return false , err
}
2023-01-16 13:30:22 +05:30
if rule == nil {
2023-09-29 04:54:36 +05:30
// if no rule is found, then user with write access can make official reviews
err := pr . LoadBaseRepo ( ctx )
if err != nil {
return false , err
}
writeAccess , err := access_model . HasAccessUnit ( ctx , reviewer , pr . BaseRepo , unit . TypeCode , perm . AccessModeWrite )
if err != nil {
return false , err
}
return writeAccess , nil
2019-12-04 06:38:56 +05:30
}
2023-09-29 04:54:36 +05:30
official , err := git_model . IsUserOfficialReviewer ( ctx , rule , reviewer )
if official || err != nil {
return official , err
2020-10-13 01:25:13 +05:30
}
return false , nil
}
// IsOfficialReviewerTeam check if reviewer in this team can make official reviews in issue (counts towards required approvals)
2022-05-20 19:38:52 +05:30
func IsOfficialReviewerTeam ( ctx context . Context , issue * Issue , team * organization . Team ) ( bool , error ) {
2024-03-21 18:43:08 +05:30
if err := issue . LoadPullRequest ( ctx ) ; err != nil {
2020-10-13 01:25:13 +05:30
return false , err
}
2024-03-21 18:43:08 +05:30
pb , err := git_model . GetFirstMatchProtectedBranchRule ( ctx , issue . PullRequest . BaseRepoID , issue . PullRequest . BaseBranch )
2023-01-16 13:30:22 +05:30
if err != nil {
2020-10-13 01:25:13 +05:30
return false , err
}
2023-01-16 13:30:22 +05:30
if pb == nil {
2020-10-13 01:25:13 +05:30
return false , nil
}
2023-01-16 13:30:22 +05:30
if ! pb . EnableApprovalsWhitelist {
2022-12-10 08:16:31 +05:30
return team . UnitAccessMode ( ctx , unit . TypeCode ) >= perm . AccessModeWrite , nil
2020-10-13 01:25:13 +05:30
}
2023-12-28 15:08:59 +05:30
return slices . Contains ( pb . ApprovalsWhitelistTeamIDs , team . ID ) , nil
2018-08-06 10:13:22 +05:30
}
2022-05-20 19:38:52 +05:30
// CreateReview creates a new review based on opts
func CreateReview ( ctx context . Context , opts CreateReviewOptions ) ( * Review , error ) {
2024-02-19 19:12:18 +05:30
ctx , committer , err := db . TxContext ( ctx )
if err != nil {
return nil , err
}
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2018-08-06 10:13:22 +05:30
review := & Review {
2020-10-13 01:25:13 +05:30
Issue : opts . Issue ,
IssueID : opts . Issue . ID ,
Reviewer : opts . Reviewer ,
ReviewerTeam : opts . ReviewerTeam ,
Content : opts . Content ,
Official : opts . Official ,
CommitID : opts . CommitID ,
Stale : opts . Stale ,
}
2024-02-19 19:12:18 +05:30
2020-10-13 01:25:13 +05:30
if opts . Reviewer != nil {
2024-02-19 19:12:18 +05:30
review . Type = opts . Type
2020-10-13 01:25:13 +05:30
review . ReviewerID = opts . Reviewer . ID
2024-02-19 19:12:18 +05:30
reviewCond := builder . Eq { "reviewer_id" : opts . Reviewer . ID , "issue_id" : opts . Issue . ID }
// make sure user review requests are cleared
if opts . Type != ReviewTypePending {
if _ , err := sess . Where ( reviewCond . And ( builder . Eq { "type" : ReviewTypeRequest } ) ) . Delete ( new ( Review ) ) ; err != nil {
return nil , err
}
2020-10-13 01:25:13 +05:30
}
2024-02-19 19:12:18 +05:30
// make sure if the created review gets dismissed no old review surface
// other types can be ignored, as they don't affect branch protection
if opts . Type == ReviewTypeApprove || opts . Type == ReviewTypeReject {
if _ , err := sess . Where ( reviewCond . And ( builder . In ( "type" , ReviewTypeApprove , ReviewTypeReject ) ) ) .
Cols ( "dismissed" ) . Update ( & Review { Dismissed : true } ) ; err != nil {
return nil , err
}
}
} else if opts . ReviewerTeam != nil {
review . Type = ReviewTypeRequest
2020-10-13 01:25:13 +05:30
review . ReviewerTeamID = opts . ReviewerTeam . ID
2024-02-19 19:12:18 +05:30
} else {
return nil , fmt . Errorf ( "provide either reviewer or reviewer team" )
}
if _ , err := sess . Insert ( review ) ; err != nil {
return nil , err
2018-08-06 10:13:22 +05:30
}
2024-02-19 19:12:18 +05:30
return review , committer . Commit ( )
2018-08-06 10:13:22 +05:30
}
2022-05-20 19:38:52 +05:30
// GetCurrentReview returns the current pending review of reviewer for given issue
func GetCurrentReview ( ctx context . Context , reviewer * user_model . User , issue * Issue ) ( * Review , error ) {
2018-08-06 10:13:22 +05:30
if reviewer == nil {
return nil , nil
}
2022-05-20 19:38:52 +05:30
reviews , err := FindReviews ( ctx , FindReviewOptions {
2018-08-06 10:13:22 +05:30
Type : ReviewTypePending ,
IssueID : issue . ID ,
ReviewerID : reviewer . ID ,
} )
if err != nil {
return nil , err
}
if len ( reviews ) == 0 {
return nil , ErrReviewNotExist { }
}
2018-10-18 16:53:05 +05:30
reviews [ 0 ] . Reviewer = reviewer
reviews [ 0 ] . Issue = issue
2018-08-06 10:13:22 +05:30
return reviews [ 0 ] , nil
}
2019-11-24 11:16:16 +05:30
// ReviewExists returns whether a review exists for a particular line of code in the PR
2023-09-29 17:42:54 +05:30
func ReviewExists ( ctx context . Context , issue * Issue , treePath string , line int64 ) ( bool , error ) {
return db . GetEngine ( ctx ) . Cols ( "id" ) . Exist ( & Comment { IssueID : issue . ID , TreePath : treePath , Line : line , Type : CommentTypeCode } )
2019-11-24 11:16:16 +05:30
}
2019-11-14 08:27:36 +05:30
// ContentEmptyErr represents an content empty error
2021-03-15 00:22:12 +05:30
type ContentEmptyErr struct { }
2019-11-14 08:27:36 +05:30
func ( ContentEmptyErr ) Error ( ) string {
return "Review content is empty"
}
// IsContentEmptyErr returns true if err is a ContentEmptyErr
func IsContentEmptyErr ( err error ) bool {
_ , ok := err . ( ContentEmptyErr )
return ok
}
// SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
2023-09-29 17:42:54 +05:30
func SubmitReview ( ctx context . Context , doer * user_model . User , issue * Issue , reviewType ReviewType , content , commitID string , stale bool , attachmentUUIDs [ ] string ) ( * Review , * Comment , error ) {
ctx , committer , err := db . TxContext ( ctx )
2021-11-19 19:09:57 +05:30
if err != nil {
2019-11-14 08:27:36 +05:30
return nil , nil , err
}
2021-11-19 19:09:57 +05:30
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2019-11-14 08:27:36 +05:30
2021-03-15 00:22:12 +05:30
official := false
2019-12-04 06:38:56 +05:30
2022-05-20 19:38:52 +05:30
review , err := GetCurrentReview ( ctx , doer , issue )
2019-11-14 08:27:36 +05:30
if err != nil {
if ! IsErrReviewNotExist ( err ) {
return nil , nil , err
}
2019-11-15 02:28:01 +05:30
if reviewType != ReviewTypeApprove && len ( strings . TrimSpace ( content ) ) == 0 {
2019-11-14 08:27:36 +05:30
return nil , nil , ContentEmptyErr { }
}
2019-12-04 06:38:56 +05:30
if reviewType == ReviewTypeApprove || reviewType == ReviewTypeReject {
// Only reviewers latest review of type approve and reject shall count as "official", so existing reviews needs to be cleared
2022-05-20 19:38:52 +05:30
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?" , false , issue . ID , doer . ID ) ; err != nil {
2019-12-04 06:38:56 +05:30
return nil , nil , err
}
2022-05-20 19:38:52 +05:30
if official , err = IsOfficialReviewer ( ctx , issue , doer ) ; err != nil {
2019-12-04 06:38:56 +05:30
return nil , nil , err
}
}
2019-11-14 08:27:36 +05:30
// No current review. Create a new one!
2022-05-20 19:38:52 +05:30
if review , err = CreateReview ( ctx , CreateReviewOptions {
2019-11-14 08:27:36 +05:30
Type : reviewType ,
Issue : issue ,
Reviewer : doer ,
Content : content ,
2019-12-04 06:38:56 +05:30
Official : official ,
2020-01-09 07:17:45 +05:30
CommitID : commitID ,
Stale : stale ,
2020-10-13 01:25:13 +05:30
} ) ; err != nil {
2019-11-14 08:27:36 +05:30
return nil , nil , err
}
} else {
2022-01-20 04:56:57 +05:30
if err := review . LoadCodeComments ( ctx ) ; err != nil {
2019-11-14 08:27:36 +05:30
return nil , nil , err
}
2019-11-15 02:28:01 +05:30
if reviewType != ReviewTypeApprove && len ( review . CodeComments ) == 0 && len ( strings . TrimSpace ( content ) ) == 0 {
2019-11-14 08:27:36 +05:30
return nil , nil , ContentEmptyErr { }
}
2019-12-04 06:38:56 +05:30
if reviewType == ReviewTypeApprove || reviewType == ReviewTypeReject {
// Only reviewers latest review of type approve and reject shall count as "official", so existing reviews needs to be cleared
2022-05-20 19:38:52 +05:30
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?" , false , issue . ID , doer . ID ) ; err != nil {
2019-12-04 06:38:56 +05:30
return nil , nil , err
}
2022-05-20 19:38:52 +05:30
if official , err = IsOfficialReviewer ( ctx , issue , doer ) ; err != nil {
2019-12-04 06:38:56 +05:30
return nil , nil , err
}
}
review . Official = official
2019-11-14 08:27:36 +05:30
review . Issue = issue
review . Content = content
review . Type = reviewType
2020-01-09 07:17:45 +05:30
review . CommitID = commitID
review . Stale = stale
2019-12-04 06:38:56 +05:30
2020-01-09 07:17:45 +05:30
if _ , err := sess . ID ( review . ID ) . Cols ( "content, type, official, commit_id, stale" ) . Update ( review ) ; err != nil {
2019-11-14 08:27:36 +05:30
return nil , nil , err
}
2018-08-06 10:13:22 +05:30
}
2019-11-14 08:27:36 +05:30
2022-12-10 08:16:31 +05:30
comm , err := CreateComment ( ctx , & CreateCommentOptions {
2021-06-15 06:42:33 +05:30
Type : CommentTypeReview ,
Doer : doer ,
Content : review . Content ,
Issue : issue ,
Repo : issue . Repo ,
ReviewID : review . ID ,
Attachments : attachmentUUIDs ,
2019-11-14 08:27:36 +05:30
} )
if err != nil || comm == nil {
return nil , nil , err
}
2020-10-13 01:25:13 +05:30
// try to remove team review request if need
if issue . Repo . Owner . IsOrganization ( ) && ( reviewType == ReviewTypeApprove || reviewType == ReviewTypeReject ) {
teamReviewRequests := make ( [ ] * Review , 0 , 10 )
2021-10-08 02:09:59 +05:30
if err := sess . SQL ( "SELECT * FROM review WHERE issue_id = ? AND reviewer_team_id > 0 AND type = ?" , issue . ID , ReviewTypeRequest ) . Find ( & teamReviewRequests ) ; err != nil {
2020-10-13 01:25:13 +05:30
return nil , nil , err
}
for _ , teamReviewRequest := range teamReviewRequests {
2022-03-29 11:59:02 +05:30
ok , err := organization . IsTeamMember ( ctx , issue . Repo . OwnerID , teamReviewRequest . ReviewerTeamID , doer . ID )
2020-10-13 01:25:13 +05:30
if err != nil {
return nil , nil , err
} else if ! ok {
continue
}
2023-12-26 01:55:29 +05:30
if _ , err := db . DeleteByID [ Review ] ( ctx , teamReviewRequest . ID ) ; err != nil {
2020-10-13 01:25:13 +05:30
return nil , nil , err
}
}
}
2019-11-14 08:27:36 +05:30
comm . Review = review
2021-11-19 19:09:57 +05:30
return review , comm , committer . Commit ( )
2018-08-06 10:13:22 +05:30
}
2018-11-22 18:47:36 +05:30
2020-10-13 01:25:13 +05:30
// GetReviewByIssueIDAndUserID get the latest review of reviewer for a pull request
2022-05-20 19:38:52 +05:30
func GetReviewByIssueIDAndUserID ( ctx context . Context , issueID , userID int64 ) ( * Review , error ) {
2020-10-13 01:25:13 +05:30
review := new ( Review )
2020-04-06 22:03:34 +05:30
2023-12-20 20:49:58 +05:30
has , err := db . GetEngine ( ctx ) . Where (
builder . In ( "type" , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest ) .
And ( builder . Eq { "issue_id" : issueID , "reviewer_id" : userID , "original_author_id" : 0 } ) ) .
Desc ( "id" ) .
2020-10-13 01:25:13 +05:30
Get ( review )
if err != nil {
return nil , err
}
if ! has {
return nil , ErrReviewNotExist { }
}
return review , nil
}
2022-01-10 15:02:37 +05:30
// GetTeamReviewerByIssueIDAndTeamID get the latest review request of reviewer team for a pull request
2023-12-20 20:49:58 +05:30
func GetTeamReviewerByIssueIDAndTeamID ( ctx context . Context , issueID , teamID int64 ) ( * Review , error ) {
review := new ( Review )
2020-10-13 01:25:13 +05:30
2023-12-20 20:49:58 +05:30
has , err := db . GetEngine ( ctx ) . Where ( builder . Eq { "issue_id" : issueID , "reviewer_team_id" : teamID } ) .
Desc ( "id" ) .
Get ( review )
if err != nil {
2020-04-06 22:03:34 +05:30
return nil , err
}
2020-10-13 01:25:13 +05:30
if ! has {
return nil , ErrReviewNotExist { 0 }
}
2022-06-20 15:32:49 +05:30
return review , err
2020-04-06 22:03:34 +05:30
}
2020-01-09 07:17:45 +05:30
// MarkReviewsAsStale marks existing reviews as stale
2023-09-29 17:42:54 +05:30
func MarkReviewsAsStale ( ctx context . Context , issueID int64 ) ( err error ) {
_ , err = db . GetEngine ( ctx ) . Exec ( "UPDATE `review` SET stale=? WHERE issue_id=?" , true , issueID )
2020-01-09 07:17:45 +05:30
2022-06-20 15:32:49 +05:30
return err
2020-01-09 07:17:45 +05:30
}
// MarkReviewsAsNotStale marks existing reviews as not stale for a giving commit SHA
2023-09-29 17:42:54 +05:30
func MarkReviewsAsNotStale ( ctx context . Context , issueID int64 , commitID string ) ( err error ) {
_ , err = db . GetEngine ( ctx ) . Exec ( "UPDATE `review` SET stale=? WHERE issue_id=? AND commit_id=?" , false , issueID , commitID )
2020-01-09 07:17:45 +05:30
2022-06-20 15:32:49 +05:30
return err
2020-01-09 07:17:45 +05:30
}
2020-01-23 22:58:15 +05:30
2021-02-11 23:02:25 +05:30
// DismissReview change the dismiss status of a review
2023-07-20 12:48:52 +05:30
func DismissReview ( ctx context . Context , review * Review , isDismiss bool ) ( err error ) {
2021-02-11 23:02:25 +05:30
if review . Dismissed == isDismiss || ( review . Type != ReviewTypeApprove && review . Type != ReviewTypeReject ) {
return nil
}
review . Dismissed = isDismiss
2021-04-15 15:33:11 +05:30
if review . ID == 0 {
return ErrReviewNotExist { }
}
2023-07-20 12:48:52 +05:30
_ , err = db . GetEngine ( ctx ) . ID ( review . ID ) . Cols ( "dismissed" ) . Update ( review )
2021-02-11 23:02:25 +05:30
2022-06-20 15:32:49 +05:30
return err
2021-02-11 23:02:25 +05:30
}
2020-01-23 22:58:15 +05:30
// InsertReviews inserts review and review comments
2023-09-29 17:42:54 +05:30
func InsertReviews ( ctx context . Context , reviews [ ] * Review ) error {
ctx , committer , err := db . TxContext ( ctx )
2021-11-21 21:11:00 +05:30
if err != nil {
2020-01-23 22:58:15 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2020-01-23 22:58:15 +05:30
for _ , review := range reviews {
if _ , err := sess . NoAutoTime ( ) . Insert ( review ) ; err != nil {
return err
}
if _ , err := sess . NoAutoTime ( ) . Insert ( & Comment {
Type : CommentTypeReview ,
Content : review . Content ,
PosterID : review . ReviewerID ,
OriginalAuthor : review . OriginalAuthor ,
OriginalAuthorID : review . OriginalAuthorID ,
IssueID : review . IssueID ,
ReviewID : review . ID ,
CreatedUnix : review . CreatedUnix ,
UpdatedUnix : review . UpdatedUnix ,
} ) ; err != nil {
return err
}
for _ , c := range review . Comments {
c . ReviewID = review . ID
}
2020-04-20 08:34:08 +05:30
if len ( review . Comments ) > 0 {
if _ , err := sess . NoAutoTime ( ) . Insert ( review . Comments ) ; err != nil {
return err
}
2020-01-23 22:58:15 +05:30
}
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2020-01-23 22:58:15 +05:30
}
2020-04-06 22:03:34 +05:30
2020-05-01 01:54:08 +05:30
// AddReviewRequest add a review request from one reviewer
2023-08-10 08:09:21 +05:30
func AddReviewRequest ( ctx context . Context , issue * Issue , reviewer , doer * user_model . User ) ( * Comment , error ) {
ctx , committer , err := db . TxContext ( ctx )
2021-11-19 19:09:57 +05:30
if err != nil {
2020-04-06 22:03:34 +05:30
return nil , err
}
2021-11-19 19:09:57 +05:30
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2020-04-06 22:03:34 +05:30
2022-05-20 19:38:52 +05:30
review , err := GetReviewByIssueIDAndUserID ( ctx , issue . ID , reviewer . ID )
2020-10-13 01:25:13 +05:30
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 22:03:34 +05:30
return nil , err
}
2020-10-13 01:25:13 +05:30
// skip it when reviewer hase been request to review
if review != nil && review . Type == ReviewTypeRequest {
2024-03-25 12:30:16 +05:30
return nil , committer . Commit ( ) // still commit the transaction, or committer.Close() will rollback it, even if it's a reused transaction.
2020-04-06 22:03:34 +05:30
}
2023-09-29 04:54:36 +05:30
// if the reviewer is an official reviewer,
// remove the official flag in the all previous reviews
official , err := IsOfficialReviewer ( ctx , issue , reviewer )
2020-10-13 01:25:13 +05:30
if err != nil {
return nil , err
} else if official {
2020-04-06 22:03:34 +05:30
if _ , err := sess . Exec ( "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?" , false , issue . ID , reviewer . ID ) ; err != nil {
return nil , err
}
}
2022-05-20 19:38:52 +05:30
review , err = CreateReview ( ctx , CreateReviewOptions {
2020-04-06 22:03:34 +05:30
Type : ReviewTypeRequest ,
Issue : issue ,
Reviewer : reviewer ,
Official : official ,
Stale : false ,
2020-10-20 23:48:25 +05:30
} )
if err != nil {
2020-10-13 01:25:13 +05:30
return nil , err
2020-04-06 22:03:34 +05:30
}
2022-12-10 08:16:31 +05:30
comment , err := CreateComment ( ctx , & CreateCommentOptions {
2020-04-06 22:03:34 +05:30
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : false , // Use RemovedAssignee as !isRequest
AssigneeID : reviewer . ID , // Use AssigneeID as reviewer ID
2020-10-20 23:48:25 +05:30
ReviewID : review . ID ,
2020-04-06 22:03:34 +05:30
} )
if err != nil {
return nil , err
}
2024-02-13 14:15:31 +05:30
// func caller use the created comment to retrieve created review too.
comment . Review = review
2021-11-19 19:09:57 +05:30
return comment , committer . Commit ( )
2020-04-06 22:03:34 +05:30
}
2021-03-15 00:22:12 +05:30
// RemoveReviewRequest remove a review request from one reviewer
2023-08-10 08:09:21 +05:30
func RemoveReviewRequest ( ctx context . Context , issue * Issue , reviewer , doer * user_model . User ) ( * Comment , error ) {
ctx , committer , err := db . TxContext ( ctx )
2021-11-19 19:09:57 +05:30
if err != nil {
2020-10-13 01:25:13 +05:30
return nil , err
}
2021-11-19 19:09:57 +05:30
defer committer . Close ( )
2020-10-13 01:25:13 +05:30
2022-05-20 19:38:52 +05:30
review , err := GetReviewByIssueIDAndUserID ( ctx , issue . ID , reviewer . ID )
2020-10-13 01:25:13 +05:30
if err != nil && ! IsErrReviewNotExist ( err ) {
return nil , err
2020-04-06 22:03:34 +05:30
}
2020-10-13 01:25:13 +05:30
if review == nil || review . Type != ReviewTypeRequest {
2020-04-06 22:03:34 +05:30
return nil , nil
}
2022-05-20 19:38:52 +05:30
if _ , err = db . DeleteByBean ( ctx , review ) ; err != nil {
2020-10-13 01:25:13 +05:30
return nil , err
}
2022-05-20 19:38:52 +05:30
official , err := IsOfficialReviewer ( ctx , issue , reviewer )
2020-10-13 01:25:13 +05:30
if err != nil {
return nil , err
} else if official {
2023-01-15 10:30:09 +05:30
if err := restoreLatestOfficialReview ( ctx , issue . ID , reviewer . ID ) ; err != nil {
2020-10-13 01:25:13 +05:30
return nil , err
}
}
2022-12-10 08:16:31 +05:30
comment , err := CreateComment ( ctx , & CreateCommentOptions {
2020-10-13 01:25:13 +05:30
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : true , // Use RemovedAssignee as !isRequest
AssigneeID : reviewer . ID , // Use AssigneeID as reviewer ID
} )
if err != nil {
return nil , err
}
2021-11-19 19:09:57 +05:30
return comment , committer . Commit ( )
2020-10-13 01:25:13 +05:30
}
2023-01-15 10:30:09 +05:30
// Recalculate the latest official review for reviewer
func restoreLatestOfficialReview ( ctx context . Context , issueID , reviewerID int64 ) error {
review , err := GetReviewByIssueIDAndUserID ( ctx , issueID , reviewerID )
if err != nil && ! IsErrReviewNotExist ( err ) {
return err
}
if review != nil {
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE id=?" , true , review . ID ) ; err != nil {
return err
}
}
return nil
}
2020-10-13 01:25:13 +05:30
// AddTeamReviewRequest add a review request from one team
2023-08-10 08:09:21 +05:30
func AddTeamReviewRequest ( ctx context . Context , issue * Issue , reviewer * organization . Team , doer * user_model . User ) ( * Comment , error ) {
ctx , committer , err := db . TxContext ( ctx )
2021-11-19 19:09:57 +05:30
if err != nil {
2020-04-06 22:03:34 +05:30
return nil , err
}
2021-11-19 19:09:57 +05:30
defer committer . Close ( )
2020-04-06 22:03:34 +05:30
2022-05-20 19:38:52 +05:30
review , err := GetTeamReviewerByIssueIDAndTeamID ( ctx , issue . ID , reviewer . ID )
2020-10-13 01:25:13 +05:30
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 22:03:34 +05:30
return nil , err
}
2020-10-13 01:25:13 +05:30
// This team already has been requested to review - therefore skip this.
if review != nil {
return nil , nil
}
2022-05-20 19:38:52 +05:30
official , err := IsOfficialReviewerTeam ( ctx , issue , reviewer )
2020-04-06 22:03:34 +05:30
if err != nil {
2022-10-25 00:59:17 +05:30
return nil , fmt . Errorf ( "isOfficialReviewerTeam(): %w" , err )
2020-10-13 01:25:13 +05:30
} else if ! official {
2022-05-20 19:38:52 +05:30
if official , err = IsOfficialReviewer ( ctx , issue , doer ) ; err != nil {
2022-10-25 00:59:17 +05:30
return nil , fmt . Errorf ( "isOfficialReviewer(): %w" , err )
2020-10-13 01:25:13 +05:30
}
}
2022-05-20 19:38:52 +05:30
if review , err = CreateReview ( ctx , CreateReviewOptions {
2020-10-13 01:25:13 +05:30
Type : ReviewTypeRequest ,
Issue : issue ,
ReviewerTeam : reviewer ,
Official : official ,
Stale : false ,
} ) ; err != nil {
return nil , err
2020-04-06 22:03:34 +05:30
}
if official {
2022-05-20 19:38:52 +05:30
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_team_id=?" , false , issue . ID , reviewer . ID ) ; err != nil {
2020-10-13 01:25:13 +05:30
return nil , err
}
}
2022-12-10 08:16:31 +05:30
comment , err := CreateComment ( ctx , & CreateCommentOptions {
2020-10-13 01:25:13 +05:30
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : false , // Use RemovedAssignee as !isRequest
AssigneeTeamID : reviewer . ID , // Use AssigneeTeamID as reviewer team ID
2020-10-20 23:48:25 +05:30
ReviewID : review . ID ,
2020-10-13 01:25:13 +05:30
} )
if err != nil {
2022-12-10 08:16:31 +05:30
return nil , fmt . Errorf ( "CreateComment(): %w" , err )
2020-10-13 01:25:13 +05:30
}
2021-11-19 19:09:57 +05:30
return comment , committer . Commit ( )
2020-10-13 01:25:13 +05:30
}
2021-03-15 00:22:12 +05:30
// RemoveTeamReviewRequest remove a review request from one team
2023-08-10 08:09:21 +05:30
func RemoveTeamReviewRequest ( ctx context . Context , issue * Issue , reviewer * organization . Team , doer * user_model . User ) ( * Comment , error ) {
ctx , committer , err := db . TxContext ( ctx )
2021-11-19 19:09:57 +05:30
if err != nil {
2020-10-13 01:25:13 +05:30
return nil , err
}
2021-11-19 19:09:57 +05:30
defer committer . Close ( )
2020-10-13 01:25:13 +05:30
2022-05-20 19:38:52 +05:30
review , err := GetTeamReviewerByIssueIDAndTeamID ( ctx , issue . ID , reviewer . ID )
2020-10-13 01:25:13 +05:30
if err != nil && ! IsErrReviewNotExist ( err ) {
return nil , err
}
if review == nil {
return nil , nil
}
2020-04-06 22:03:34 +05:30
2022-05-20 19:38:52 +05:30
if _ , err = db . DeleteByBean ( ctx , review ) ; err != nil {
2020-10-13 01:25:13 +05:30
return nil , err
}
2022-05-20 19:38:52 +05:30
official , err := IsOfficialReviewerTeam ( ctx , issue , reviewer )
2020-10-13 01:25:13 +05:30
if err != nil {
2022-10-25 00:59:17 +05:30
return nil , fmt . Errorf ( "isOfficialReviewerTeam(): %w" , err )
2020-10-13 01:25:13 +05:30
}
if official {
// recalculate which is the latest official review from that team
2022-05-20 19:38:52 +05:30
review , err := GetReviewByIssueIDAndUserID ( ctx , issue . ID , - reviewer . ID )
2020-10-13 01:25:13 +05:30
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 22:03:34 +05:30
return nil , err
}
if review != nil {
2022-05-20 19:38:52 +05:30
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE id=?" , true , review . ID ) ; err != nil {
2020-04-06 22:03:34 +05:30
return nil , err
}
}
}
2020-10-13 01:25:13 +05:30
if doer == nil {
2021-11-19 19:09:57 +05:30
return nil , committer . Commit ( )
2020-04-06 22:03:34 +05:30
}
2022-12-10 08:16:31 +05:30
comment , err := CreateComment ( ctx , & CreateCommentOptions {
2020-04-06 22:03:34 +05:30
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : true , // Use RemovedAssignee as !isRequest
2020-10-13 01:25:13 +05:30
AssigneeTeamID : reviewer . ID , // Use AssigneeTeamID as reviewer team ID
2020-04-06 22:03:34 +05:30
} )
if err != nil {
2022-12-10 08:16:31 +05:30
return nil , fmt . Errorf ( "CreateComment(): %w" , err )
2020-04-06 22:03:34 +05:30
}
2021-11-19 19:09:57 +05:30
return comment , committer . Commit ( )
2020-04-06 22:03:34 +05:30
}
2020-04-18 19:20:25 +05:30
// MarkConversation Add or remove Conversation mark for a code comment
2023-09-29 17:42:54 +05:30
func MarkConversation ( ctx context . Context , comment * Comment , doer * user_model . User , isResolve bool ) ( err error ) {
2020-04-18 19:20:25 +05:30
if comment . Type != CommentTypeCode {
return nil
}
if isResolve {
if comment . ResolveDoerID != 0 {
return nil
}
2023-09-29 17:42:54 +05:30
if _ , err = db . GetEngine ( ctx ) . Exec ( "UPDATE `comment` SET resolve_doer_id=? WHERE id=?" , doer . ID , comment . ID ) ; err != nil {
2020-04-18 19:20:25 +05:30
return err
}
} else {
if comment . ResolveDoerID == 0 {
return nil
}
2023-09-29 17:42:54 +05:30
if _ , err = db . GetEngine ( ctx ) . Exec ( "UPDATE `comment` SET resolve_doer_id=? WHERE id=?" , 0 , comment . ID ) ; err != nil {
2020-04-18 19:20:25 +05:30
return err
}
}
return nil
}
// CanMarkConversation Add or remove Conversation mark for a code comment permission check
// the PR writer , offfcial reviewer and poster can do it
2023-09-29 17:42:54 +05:30
func CanMarkConversation ( ctx context . Context , issue * Issue , doer * user_model . User ) ( permResult bool , err error ) {
2020-04-18 19:20:25 +05:30
if doer == nil || issue == nil {
return false , fmt . Errorf ( "issue or doer is nil" )
}
if doer . ID != issue . PosterID {
2023-09-29 17:42:54 +05:30
if err = issue . LoadRepo ( ctx ) ; err != nil {
2020-04-18 19:20:25 +05:30
return false , err
}
2023-09-29 17:42:54 +05:30
p , err := access_model . GetUserRepoPermission ( ctx , issue . Repo , doer )
2020-04-18 19:20:25 +05:30
if err != nil {
return false , err
}
2021-11-28 17:28:28 +05:30
permResult = p . CanAccess ( perm . AccessModeWrite , unit . TypePullRequests )
2020-04-18 19:20:25 +05:30
if ! permResult {
2023-09-29 17:42:54 +05:30
if permResult , err = IsOfficialReviewer ( ctx , issue , doer ) ; err != nil {
2020-04-18 19:20:25 +05:30
return false , err
}
}
if ! permResult {
return false , nil
}
}
return true , nil
}
2020-05-02 05:50:51 +05:30
// DeleteReview delete a review and it's code comments
2023-09-29 17:42:54 +05:30
func DeleteReview ( ctx context . Context , r * Review ) error {
ctx , committer , err := db . TxContext ( ctx )
2021-11-21 21:11:00 +05:30
if err != nil {
2020-05-02 05:50:51 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2020-05-02 05:50:51 +05:30
if r . ID == 0 {
return fmt . Errorf ( "review is not allowed to be 0" )
}
2020-10-20 23:48:25 +05:30
if r . Type == ReviewTypeRequest {
return fmt . Errorf ( "review request can not be deleted using this method" )
}
2020-05-02 05:50:51 +05:30
opts := FindCommentsOptions {
Type : CommentTypeCode ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
2023-12-26 01:55:29 +05:30
if _ , err := db . Delete [ Comment ] ( ctx , opts ) ; err != nil {
2020-05-02 05:50:51 +05:30
return err
}
opts = FindCommentsOptions {
Type : CommentTypeReview ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
2023-12-26 01:55:29 +05:30
if _ , err := db . Delete [ Comment ] ( ctx , opts ) ; err != nil {
2020-05-02 05:50:51 +05:30
return err
}
2023-11-05 18:55:40 +05:30
opts = FindCommentsOptions {
Type : CommentTypeDismissReview ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
2023-12-26 01:55:29 +05:30
if _ , err := db . Delete [ Comment ] ( ctx , opts ) ; err != nil {
2023-11-05 18:55:40 +05:30
return err
}
2023-12-26 01:55:29 +05:30
if _ , err := db . DeleteByID [ Review ] ( ctx , r . ID ) ; err != nil {
2020-05-02 05:50:51 +05:30
return err
}
2023-01-15 10:30:09 +05:30
if r . Official {
if err := restoreLatestOfficialReview ( ctx , r . IssueID , r . ReviewerID ) ; err != nil {
return err
}
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2020-05-02 05:50:51 +05:30
}
// GetCodeCommentsCount return count of CodeComments a Review has
2023-09-29 17:42:54 +05:30
func ( r * Review ) GetCodeCommentsCount ( ctx context . Context ) int {
2020-05-02 05:50:51 +05:30
opts := FindCommentsOptions {
Type : CommentTypeCode ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
2023-01-18 02:33:44 +05:30
conds := opts . ToConds ( )
2020-05-02 05:50:51 +05:30
if r . ID == 0 {
conds = conds . And ( builder . Eq { "invalidated" : false } )
}
2023-09-29 17:42:54 +05:30
count , err := db . GetEngine ( ctx ) . Where ( conds ) . Count ( new ( Comment ) )
2020-05-02 05:50:51 +05:30
if err != nil {
return 0
}
return int ( count )
}
// HTMLURL formats a URL-string to the related review issue-comment
2023-09-29 17:42:54 +05:30
func ( r * Review ) HTMLURL ( ctx context . Context ) string {
2020-05-02 05:50:51 +05:30
opts := FindCommentsOptions {
Type : CommentTypeReview ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
comment := new ( Comment )
2023-09-29 17:42:54 +05:30
has , err := db . GetEngine ( ctx ) . Where ( opts . ToConds ( ) ) . Get ( comment )
2020-05-02 05:50:51 +05:30
if err != nil || ! has {
return ""
}
2023-09-29 17:42:54 +05:30
return comment . HTMLURL ( ctx )
2020-05-02 05:50:51 +05:30
}
2022-02-01 23:50:28 +05:30
// RemapExternalUser ExternalUserRemappable interface
func ( r * Review ) RemapExternalUser ( externalName string , externalID , userID int64 ) error {
r . OriginalAuthor = externalName
r . OriginalAuthorID = externalID
r . ReviewerID = userID
return nil
}
// GetUserID ExternalUserRemappable interface
func ( r * Review ) GetUserID ( ) int64 { return r . ReviewerID }
// GetExternalName ExternalUserRemappable interface
func ( r * Review ) GetExternalName ( ) string { return r . OriginalAuthor }
// GetExternalID ExternalUserRemappable interface
func ( r * Review ) GetExternalID ( ) int64 { return r . OriginalAuthorID }
2022-06-13 15:07:59 +05:30
// UpdateReviewsMigrationsByType updates reviews' migrations information via given git service type and original id and poster id
2023-09-29 17:42:54 +05:30
func UpdateReviewsMigrationsByType ( ctx context . Context , tp structs . GitServiceType , originalAuthorID string , posterID int64 ) error {
_ , err := db . GetEngine ( ctx ) . Table ( "review" ) .
2022-06-13 15:07:59 +05:30
Where ( "original_author_id = ?" , originalAuthorID ) .
And ( migratedIssueCond ( tp ) ) .
2023-07-05 00:06:08 +05:30
Update ( map [ string ] any {
2022-06-13 15:07:59 +05:30
"reviewer_id" : posterID ,
"original_author" : "" ,
"original_author_id" : 0 ,
} )
return err
}