2024-02-23 00:26:49 +00:00
|
|
|
// Copyright 2024 The Forgjo Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-03-14 03:18:27 +00:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2019-05-11 16:38:52 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-03-14 03:18:27 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2024-02-23 00:26:49 +00:00
|
|
|
package forgejo
|
2016-03-14 03:18:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-01-26 02:46:38 +00:00
|
|
|
"net/url"
|
2020-02-04 15:15:35 +00:00
|
|
|
"strings"
|
2019-10-13 02:34:01 +01:00
|
|
|
"time"
|
2016-08-14 11:32:53 +01:00
|
|
|
)
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// PullRequestMeta PR info if an issue is a PR
|
|
|
|
type PullRequestMeta struct {
|
|
|
|
HasMerged bool `json:"merged"`
|
|
|
|
Merged *time.Time `json:"merged_at"`
|
|
|
|
}
|
2016-03-14 03:18:27 +00:00
|
|
|
|
2020-02-04 15:15:35 +00:00
|
|
|
// RepositoryMeta basic repository information
|
|
|
|
type RepositoryMeta struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Owner string `json:"owner"`
|
|
|
|
FullName string `json:"full_name"`
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// Issue represents an issue in a repository
|
|
|
|
type Issue struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
URL string `json:"url"`
|
2020-11-29 15:07:36 +00:00
|
|
|
HTMLURL string `json:"html_url"`
|
2019-10-13 02:34:01 +01:00
|
|
|
Index int64 `json:"number"`
|
|
|
|
Poster *User `json:"user"`
|
|
|
|
OriginalAuthor string `json:"original_author"`
|
|
|
|
OriginalAuthorID int64 `json:"original_author_id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
2020-12-15 22:16:31 +00:00
|
|
|
Ref string `json:"ref"`
|
2019-10-13 02:34:01 +01:00
|
|
|
Labels []*Label `json:"labels"`
|
|
|
|
Milestone *Milestone `json:"milestone"`
|
2021-03-30 17:36:33 +01:00
|
|
|
Assignees []*User `json:"assignees"`
|
2019-10-13 02:34:01 +01:00
|
|
|
// Whether the issue is open or closed
|
|
|
|
State StateType `json:"state"`
|
2020-06-04 02:01:13 +01:00
|
|
|
IsLocked bool `json:"is_locked"`
|
2019-10-13 02:34:01 +01:00
|
|
|
Comments int `json:"comments"`
|
|
|
|
Created time.Time `json:"created_at"`
|
|
|
|
Updated time.Time `json:"updated_at"`
|
|
|
|
Closed *time.Time `json:"closed_at"`
|
|
|
|
Deadline *time.Time `json:"due_date"`
|
|
|
|
PullRequest *PullRequestMeta `json:"pull_request"`
|
2020-02-04 15:15:35 +00:00
|
|
|
Repository *RepositoryMeta `json:"repository"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListIssueOption list issue options
|
|
|
|
type ListIssueOption struct {
|
2020-02-05 07:59:55 +00:00
|
|
|
ListOptions
|
2020-05-20 07:48:06 +01:00
|
|
|
State StateType
|
|
|
|
Type IssueType
|
|
|
|
Labels []string
|
|
|
|
Milestones []string
|
|
|
|
KeyWord string
|
2021-07-01 12:39:50 +01:00
|
|
|
Since time.Time
|
|
|
|
Before time.Time
|
|
|
|
// filter by created by username
|
|
|
|
CreatedBy string
|
|
|
|
// filter by assigned to username
|
|
|
|
AssignedBy string
|
|
|
|
// filter by username mentioned
|
|
|
|
MentionedBy string
|
2022-04-28 01:43:52 +01:00
|
|
|
// filter by owner (only works on ListIssues on User)
|
|
|
|
Owner string
|
|
|
|
// filter by team (requires organization owner parameter to be provided and only works on ListIssues on User)
|
|
|
|
Team string
|
2020-01-26 02:46:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 07:58:46 +00:00
|
|
|
// StateType issue state type
|
|
|
|
type StateType string
|
|
|
|
|
|
|
|
const (
|
2020-03-31 12:13:06 +01:00
|
|
|
// StateOpen pr/issue is opend
|
2020-02-04 07:58:46 +00:00
|
|
|
StateOpen StateType = "open"
|
2020-03-31 12:13:06 +01:00
|
|
|
// StateClosed pr/issue is closed
|
2020-02-04 07:58:46 +00:00
|
|
|
StateClosed StateType = "closed"
|
|
|
|
// StateAll is all
|
|
|
|
StateAll StateType = "all"
|
|
|
|
)
|
|
|
|
|
2020-03-31 12:13:06 +01:00
|
|
|
// IssueType is issue a pull or only an issue
|
|
|
|
type IssueType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IssueTypeAll pr and issue
|
|
|
|
IssueTypeAll IssueType = ""
|
|
|
|
// IssueTypeIssue only issues
|
|
|
|
IssueTypeIssue IssueType = "issues"
|
|
|
|
// IssueTypePull only pulls
|
|
|
|
IssueTypePull IssueType = "pulls"
|
|
|
|
)
|
|
|
|
|
2020-01-26 19:08:47 +00:00
|
|
|
// QueryEncode turns options into querystring argument
|
2020-01-26 02:46:38 +00:00
|
|
|
func (opt *ListIssueOption) QueryEncode() string {
|
2020-02-05 07:59:55 +00:00
|
|
|
query := opt.getURLQuery()
|
2020-05-20 07:48:06 +01:00
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
if len(opt.State) > 0 {
|
2020-02-04 07:58:46 +00:00
|
|
|
query.Add("state", string(opt.State))
|
2020-01-26 02:46:38 +00:00
|
|
|
}
|
2020-05-20 07:48:06 +01:00
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
if len(opt.Labels) > 0 {
|
2020-05-20 07:48:06 +01:00
|
|
|
query.Add("labels", strings.Join(opt.Labels, ","))
|
2020-01-26 02:46:38 +00:00
|
|
|
}
|
2020-05-20 07:48:06 +01:00
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
if len(opt.KeyWord) > 0 {
|
|
|
|
query.Add("q", opt.KeyWord)
|
|
|
|
}
|
2020-05-20 07:48:06 +01:00
|
|
|
|
2020-03-31 12:13:06 +01:00
|
|
|
query.Add("type", string(opt.Type))
|
2020-01-26 02:46:38 +00:00
|
|
|
|
2020-05-20 07:48:06 +01:00
|
|
|
if len(opt.Milestones) > 0 {
|
|
|
|
query.Add("milestones", strings.Join(opt.Milestones, ","))
|
|
|
|
}
|
|
|
|
|
2021-07-01 12:39:50 +01:00
|
|
|
if !opt.Since.IsZero() {
|
|
|
|
query.Add("since", opt.Since.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
if !opt.Before.IsZero() {
|
|
|
|
query.Add("before", opt.Before.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(opt.CreatedBy) > 0 {
|
|
|
|
query.Add("created_by", opt.CreatedBy)
|
|
|
|
}
|
|
|
|
if len(opt.AssignedBy) > 0 {
|
|
|
|
query.Add("assigned_by", opt.AssignedBy)
|
|
|
|
}
|
|
|
|
if len(opt.MentionedBy) > 0 {
|
|
|
|
query.Add("mentioned_by", opt.MentionedBy)
|
|
|
|
}
|
2022-04-28 01:43:52 +01:00
|
|
|
if len(opt.Owner) > 0 {
|
|
|
|
query.Add("owner", opt.Owner)
|
|
|
|
}
|
|
|
|
if len(opt.Team) > 0 {
|
|
|
|
query.Add("team", opt.MentionedBy)
|
|
|
|
}
|
2021-07-01 12:39:50 +01:00
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
return query.Encode()
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
2016-03-14 03:18:27 +00:00
|
|
|
|
2016-11-09 16:03:04 +00:00
|
|
|
// ListIssues returns all issues assigned the authenticated user
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, *Response, error) {
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
issues := make([]*Issue, 0, opt.PageSize)
|
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
link, _ := url.Parse("/repos/issues/search")
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
|
2020-11-12 20:59:04 +00:00
|
|
|
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil {
|
2020-02-04 15:15:35 +00:00
|
|
|
for i := 0; i < len(issues); i++ {
|
|
|
|
if issues[i].Repository != nil {
|
|
|
|
issues[i].Repository.Owner = strings.Split(issues[i].Repository.FullName, "/")[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 15:07:36 +00:00
|
|
|
for i := range issues {
|
|
|
|
c.issueBackwardsCompatibility(issues[i])
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
return issues, resp, err
|
2016-11-09 16:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoIssues returns all issues for a given repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
issues := make([]*Issue, 0, opt.PageSize)
|
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo))
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
|
2020-11-12 20:59:04 +00:00
|
|
|
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil {
|
2020-02-04 15:15:35 +00:00
|
|
|
for i := 0; i < len(issues); i++ {
|
|
|
|
if issues[i].Repository != nil {
|
|
|
|
issues[i].Repository.Owner = strings.Split(issues[i].Repository.FullName, "/")[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 15:07:36 +00:00
|
|
|
for i := range issues {
|
|
|
|
c.issueBackwardsCompatibility(issues[i])
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
return issues, resp, err
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:03:04 +00:00
|
|
|
// GetIssue returns a single issue for a given repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2016-03-14 03:18:27 +00:00
|
|
|
issue := new(Issue)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
|
2020-11-12 20:59:04 +00:00
|
|
|
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil && issue.Repository != nil {
|
2020-02-04 15:15:35 +00:00
|
|
|
issue.Repository.Owner = strings.Split(issue.Repository.FullName, "/")[0]
|
|
|
|
}
|
2020-11-29 15:07:36 +00:00
|
|
|
c.issueBackwardsCompatibility(issue)
|
2020-09-14 03:37:09 +01:00
|
|
|
return issue, resp, err
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// CreateIssueOption options to create one issue
|
|
|
|
type CreateIssueOption struct {
|
2021-03-30 17:36:33 +01:00
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
Ref string `json:"ref"`
|
2019-10-13 02:34:01 +01:00
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Deadline *time.Time `json:"due_date"`
|
|
|
|
// milestone id
|
|
|
|
Milestone int64 `json:"milestone"`
|
|
|
|
// list of label ids
|
|
|
|
Labels []int64 `json:"labels"`
|
|
|
|
Closed bool `json:"closed"`
|
|
|
|
}
|
|
|
|
|
2020-06-09 11:47:55 +01:00
|
|
|
// Validate the CreateIssueOption struct
|
|
|
|
func (opt CreateIssueOption) Validate() error {
|
|
|
|
if len(strings.TrimSpace(opt.Title)) == 0 {
|
|
|
|
return fmt.Errorf("title is empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:03:04 +00:00
|
|
|
// CreateIssue create a new issue for a given repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-06-09 11:47:55 +01:00
|
|
|
if err := opt.Validate(); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2020-06-09 11:47:55 +01:00
|
|
|
}
|
2016-03-14 03:18:27 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
|
|
|
issue := new(Issue)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
|
2016-07-17 01:46:54 +01:00
|
|
|
jsonHeader, bytes.NewReader(body), issue)
|
2020-11-29 15:07:36 +00:00
|
|
|
c.issueBackwardsCompatibility(issue)
|
2020-09-14 03:37:09 +01:00
|
|
|
return issue, resp, err
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// EditIssueOption options for editing an issue
|
|
|
|
type EditIssueOption struct {
|
2021-03-30 17:36:33 +01:00
|
|
|
Title string `json:"title"`
|
|
|
|
Body *string `json:"body"`
|
|
|
|
Ref *string `json:"ref"`
|
2021-02-13 19:24:00 +00:00
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Milestone *int64 `json:"milestone"`
|
|
|
|
State *StateType `json:"state"`
|
|
|
|
Deadline *time.Time `json:"due_date"`
|
|
|
|
RemoveDeadline *bool `json:"unset_due_date"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 11:47:55 +01:00
|
|
|
// Validate the EditIssueOption struct
|
|
|
|
func (opt EditIssueOption) Validate() error {
|
|
|
|
if len(opt.Title) != 0 && len(strings.TrimSpace(opt.Title)) == 0 {
|
|
|
|
return fmt.Errorf("title is empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:03:04 +00:00
|
|
|
// EditIssue modify an existing issue for a given repository
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-06-09 11:47:55 +01:00
|
|
|
if err := opt.Validate(); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2020-06-09 11:47:55 +01:00
|
|
|
}
|
2016-03-14 03:18:27 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
|
|
|
issue := new(Issue)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("PATCH",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
|
2016-07-17 01:46:54 +01:00
|
|
|
jsonHeader, bytes.NewReader(body), issue)
|
2020-11-29 15:07:36 +00:00
|
|
|
c.issueBackwardsCompatibility(issue)
|
2020-09-14 03:37:09 +01:00
|
|
|
return issue, resp, err
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
2020-11-29 15:07:36 +00:00
|
|
|
|
2022-09-15 22:45:01 +01:00
|
|
|
// DeleteIssue delete a issue from a repository
|
|
|
|
func (c *Client) DeleteIssue(user, repo string, id int64) (*Response, error) {
|
|
|
|
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, resp, err := c.getResponse("DELETE",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/issues/%d", user, repo, id),
|
|
|
|
nil, nil)
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:07:36 +00:00
|
|
|
func (c *Client) issueBackwardsCompatibility(issue *Issue) {
|
|
|
|
if c.checkServerVersionGreaterThanOrEqual(version1_12_0) != nil {
|
2021-03-04 20:19:55 +00:00
|
|
|
c.mutex.RLock()
|
2020-11-29 15:07:36 +00:00
|
|
|
issue.HTMLURL = fmt.Sprintf("%s/%s/issues/%d", c.url, issue.Repository.FullName, issue.Index)
|
2021-03-04 20:19:55 +00:00
|
|
|
c.mutex.RUnlock()
|
2020-11-29 15:07:36 +00:00
|
|
|
}
|
|
|
|
}
|