|
| 1 | +// Copyright 2016 The Gogs Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package gogs |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "net/http" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +type PullRequestMeta struct { |
| 16 | + HasMerged bool `json:"merged"` |
| 17 | + Merged *time.Time `json:"merged_at"` |
| 18 | +} |
| 19 | + |
| 20 | +type Issue struct { |
| 21 | + ID int64 `json:"id"` |
| 22 | + Index int64 `json:"number"` |
| 23 | + State string `json:"state"` |
| 24 | + Title string `json:"title"` |
| 25 | + Body string `json:"body"` |
| 26 | + User *User `json:"user"` |
| 27 | + Labels []*Label `json:"labels"` |
| 28 | + Assignee *User `json:"assignee"` |
| 29 | + Milestone *Milestone `json:"milestone"` |
| 30 | + Comments int `json:"comments"` |
| 31 | + PullRequest *PullRequestMeta `json:"pull_request"` |
| 32 | + Created time.Time `json:"created_at"` |
| 33 | + Updated time.Time `json:"updated_at"` |
| 34 | +} |
| 35 | + |
| 36 | +type ListIssueOption struct { |
| 37 | + Page int |
| 38 | +} |
| 39 | + |
| 40 | +func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) { |
| 41 | + issues := make([]*Issue, 0, 10) |
| 42 | + return issues, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues?page=%d", owner, repo, opt.Page), nil, nil, &issues) |
| 43 | +} |
| 44 | + |
| 45 | +func (c *Client) GetIssue(owner, repo string, index int) (*Issue, error) { |
| 46 | + issue := new(Issue) |
| 47 | + return issue, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue) |
| 48 | +} |
| 49 | + |
| 50 | +type CreateIssueOption struct { |
| 51 | + Title string `json:"title" binding:"Required"` |
| 52 | + Body string `json:"body"` |
| 53 | + Assignee string `json:"assignee"` |
| 54 | + Milestone int64 `json:"milestone"` |
| 55 | + Labels []int64 `json:"labels"` |
| 56 | +} |
| 57 | + |
| 58 | +func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, error) { |
| 59 | + body, err := json.Marshal(&opt) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + issue := new(Issue) |
| 64 | + return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo), |
| 65 | + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), issue) |
| 66 | +} |
| 67 | + |
| 68 | +type EditIssueOption struct { |
| 69 | + Title string `json:"title"` |
| 70 | + Body *string `json:"body"` |
| 71 | + Assignee *string `json:"assignee"` |
| 72 | + Milestone *int64 `json:"milestone"` |
| 73 | +} |
| 74 | + |
| 75 | +func (c *Client) EditIssue(owner, repo string, index int, opt EditIssueOption) (*Issue, error) { |
| 76 | + body, err := json.Marshal(&opt) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + issue := new(Issue) |
| 81 | + return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), |
| 82 | + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), issue) |
| 83 | +} |
0 commit comments