You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
4.9 KiB
172 lines
4.9 KiB
7 years ago
|
// Copyright 2018 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.
|
||
|
|
||
3 years ago
|
package issues
|
||
7 years ago
|
|
||
|
import (
|
||
3 years ago
|
"context"
|
||
7 years ago
|
"fmt"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/util"
|
||
7 years ago
|
)
|
||
|
|
||
|
// IssueAssignees saves all issue assignees
|
||
|
type IssueAssignees struct {
|
||
|
ID int64 `xorm:"pk autoincr"`
|
||
|
AssigneeID int64 `xorm:"INDEX"`
|
||
|
IssueID int64 `xorm:"INDEX"`
|
||
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(IssueAssignees))
|
||
|
}
|
||
|
|
||
5 years ago
|
// LoadAssignees load assignees of this issue.
|
||
3 years ago
|
func (issue *Issue) LoadAssignees(ctx context.Context) (err error) {
|
||
7 years ago
|
// Reset maybe preexisting assignees
|
||
3 years ago
|
issue.Assignees = []*user_model.User{}
|
||
3 years ago
|
issue.Assignee = nil
|
||
7 years ago
|
|
||
3 years ago
|
err = db.GetEngine(ctx).Table("`user`").
|
||
7 years ago
|
Join("INNER", "issue_assignees", "assignee_id = `user`.id").
|
||
|
Where("issue_assignees.issue_id = ?", issue.ID).
|
||
|
Find(&issue.Assignees)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Check if we have at least one assignee and if yes put it in as `Assignee`
|
||
|
if len(issue.Assignees) > 0 {
|
||
|
issue.Assignee = issue.Assignees[0]
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
5 years ago
|
// GetAssigneeIDsByIssue returns the IDs of users assigned to an issue
|
||
|
// but skips joining with `user` for performance reasons.
|
||
|
// User permissions must be verified elsewhere if required.
|
||
|
func GetAssigneeIDsByIssue(issueID int64) ([]int64, error) {
|
||
|
userIDs := make([]int64, 0, 5)
|
||
3 years ago
|
return userIDs, db.GetEngine(db.DefaultContext).Table("issue_assignees").
|
||
5 years ago
|
Cols("assignee_id").
|
||
|
Where("issue_id = ?", issueID).
|
||
|
Distinct("assignee_id").
|
||
|
Find(&userIDs)
|
||
|
}
|
||
|
|
||
7 years ago
|
// IsUserAssignedToIssue returns true when the user is assigned to the issue
|
||
3 years ago
|
func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.User) (isAssigned bool, err error) {
|
||
|
return db.GetByBean(ctx, &IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID})
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
// ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
|
||
|
func ToggleIssueAssignee(issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) {
|
||
3 years ago
|
ctx, committer, err := db.TxContext()
|
||
|
if err != nil {
|
||
5 years ago
|
return false, nil, err
|
||
7 years ago
|
}
|
||
3 years ago
|
defer committer.Close()
|
||
7 years ago
|
|
||
3 years ago
|
removed, comment, err = toggleIssueAssignee(ctx, issue, doer, assigneeID, false)
|
||
5 years ago
|
if err != nil {
|
||
|
return false, nil, err
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
if err := committer.Commit(); err != nil {
|
||
5 years ago
|
return false, nil, err
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
return removed, comment, nil
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
func toggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.User, assigneeID int64, isCreate bool) (removed bool, comment *Comment, err error) {
|
||
3 years ago
|
removed, err = toggleUserAssignee(ctx, issue, assigneeID)
|
||
7 years ago
|
if err != nil {
|
||
5 years ago
|
return false, nil, fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
|
||
7 years ago
|
}
|
||
|
|
||
|
// Repo infos
|
||
3 years ago
|
if err = issue.LoadRepo(ctx); err != nil {
|
||
5 years ago
|
return false, nil, fmt.Errorf("loadRepo: %v", err)
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
opts := &CreateCommentOptions{
|
||
5 years ago
|
Type: CommentTypeAssignees,
|
||
|
Doer: doer,
|
||
|
Repo: issue.Repo,
|
||
|
Issue: issue,
|
||
|
RemovedAssignee: removed,
|
||
|
AssigneeID: assigneeID,
|
||
5 years ago
|
}
|
||
|
// Comment
|
||
3 years ago
|
comment, err = CreateCommentCtx(ctx, opts)
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
return false, nil, fmt.Errorf("createComment: %v", err)
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// if pull request is in the middle of creation - don't call webhook
|
||
6 years ago
|
if isCreate {
|
||
5 years ago
|
return removed, comment, err
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
return removed, comment, nil
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
// toggles user assignee state in database
|
||
3 years ago
|
func toggleUserAssignee(ctx context.Context, issue *Issue, assigneeID int64) (removed bool, err error) {
|
||
5 years ago
|
// Check if the user exists
|
||
3 years ago
|
assignee, err := user_model.GetUserByIDCtx(ctx, assigneeID)
|
||
5 years ago
|
if err != nil {
|
||
|
return false, err
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
// Check if the submitted user is already assigned, if yes delete him otherwise add him
|
||
3 years ago
|
found := false
|
||
|
i := 0
|
||
|
for ; i < len(issue.Assignees); i++ {
|
||
5 years ago
|
if issue.Assignees[i].ID == assigneeID {
|
||
3 years ago
|
found = true
|
||
5 years ago
|
break
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
assigneeIn := IssueAssignees{AssigneeID: assigneeID, IssueID: issue.ID}
|
||
3 years ago
|
if found {
|
||
|
issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i+1:]...)
|
||
|
_, err = db.DeleteByBean(ctx, &assigneeIn)
|
||
7 years ago
|
if err != nil {
|
||
3 years ago
|
return found, err
|
||
5 years ago
|
}
|
||
|
} else {
|
||
|
issue.Assignees = append(issue.Assignees, assignee)
|
||
3 years ago
|
if err = db.Insert(ctx, &assigneeIn); err != nil {
|
||
|
return found, err
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
return found, nil
|
||
7 years ago
|
}
|
||
|
|
||
|
// MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
|
||
|
func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
|
||
5 years ago
|
var requestAssignees []string
|
||
|
|
||
7 years ago
|
// Keeping the old assigning method for compatibility reasons
|
||
5 years ago
|
if oneAssignee != "" && !util.IsStringInSlice(oneAssignee, multipleAssignees) {
|
||
|
requestAssignees = append(requestAssignees, oneAssignee)
|
||
|
}
|
||
7 years ago
|
|
||
4 years ago
|
// Prevent empty assignees
|
||
5 years ago
|
if len(multipleAssignees) > 0 && multipleAssignees[0] != "" {
|
||
|
requestAssignees = append(requestAssignees, multipleAssignees...)
|
||
7 years ago
|
}
|
||
|
|
||
|
// Get the IDs of all assignees
|
||
3 years ago
|
assigneeIDs, err = user_model.GetUserIDsByNames(requestAssignees, false)
|
||
7 years ago
|
|
||
|
return
|
||
|
}
|