From 788ec59749df076b98e208909b44fdef02779deb Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 13 Mar 2016 23:18:27 -0400 Subject: [PATCH] Add CRUD issues --- gogs.go | 2 +- issue.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++ issue_label.go | 10 ++++++ issue_milestone.go | 18 ++++++++++ repo.go | 4 +-- repo_hooks.go | 5 +++ utils.go | 8 +++++ 7 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 issue.go create mode 100644 issue_label.go create mode 100644 issue_milestone.go diff --git a/gogs.go b/gogs.go index 8ec21d1..8e4596b 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.7.3" + return "0.8.0" } // Client represents a Gogs API client. diff --git a/issue.go b/issue.go new file mode 100644 index 0000000..236f41f --- /dev/null +++ b/issue.go @@ -0,0 +1,83 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gogs + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" +) + +type PullRequestMeta struct { + HasMerged bool `json:"merged"` + Merged *time.Time `json:"merged_at"` +} + +type Issue struct { + ID int64 `json:"id"` + Index int64 `json:"number"` + State string `json:"state"` + Title string `json:"title"` + Body string `json:"body"` + User *User `json:"user"` + Labels []*Label `json:"labels"` + Assignee *User `json:"assignee"` + Milestone *Milestone `json:"milestone"` + Comments int `json:"comments"` + PullRequest *PullRequestMeta `json:"pull_request"` + Created time.Time `json:"created_at"` + Updated time.Time `json:"updated_at"` +} + +type ListIssueOption struct { + Page int +} + +func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) { + issues := make([]*Issue, 0, 10) + return issues, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues?page=%d", owner, repo, opt.Page), nil, nil, &issues) +} + +func (c *Client) GetIssue(owner, repo string, index int) (*Issue, error) { + issue := new(Issue) + return issue, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue) +} + +type CreateIssueOption struct { + Title string `json:"title" binding:"Required"` + Body string `json:"body"` + Assignee string `json:"assignee"` + Milestone int64 `json:"milestone"` + Labels []int64 `json:"labels"` +} + +func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + issue := new(Issue) + return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), issue) +} + +type EditIssueOption struct { + Title string `json:"title"` + Body *string `json:"body"` + Assignee *string `json:"assignee"` + Milestone *int64 `json:"milestone"` +} + +func (c *Client) EditIssue(owner, repo string, index int, opt EditIssueOption) (*Issue, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + issue := new(Issue) + return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), issue) +} diff --git a/issue_label.go b/issue_label.go new file mode 100644 index 0000000..9e00ef7 --- /dev/null +++ b/issue_label.go @@ -0,0 +1,10 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gogs + +type Label struct { + Name string `json:"name"` + Color string `json:"color"` +} diff --git a/issue_milestone.go b/issue_milestone.go new file mode 100644 index 0000000..c6c0704 --- /dev/null +++ b/issue_milestone.go @@ -0,0 +1,18 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gogs + +import "time" + +type Milestone struct { + ID int64 `json:"id"` + State string `json:"state"` + Title string `json:"title"` + Description string `json:"description"` + OpenIssues int `json:"open_issues"` + ClosedIssues int `json:"closed_issues"` + Closed *time.Time `json:"closed_at"` + Deadline *time.Time `json:"due_on"` +} diff --git a/repo.go b/repo.go index 7f703cb..14a5a0b 100644 --- a/repo.go +++ b/repo.go @@ -20,8 +20,8 @@ type Permission struct { // Repository represents a API repository. type Repository struct { - Id int64 `json:"id"` - Owner User `json:"owner"` + ID int64 `json:"id"` + Owner *User `json:"owner"` FullName string `json:"full_name"` Private bool `json:"private"` Fork bool `json:"fork"` diff --git a/repo_hooks.go b/repo_hooks.go index 06c9687..348458c 100644 --- a/repo_hooks.go +++ b/repo_hooks.go @@ -105,6 +105,11 @@ type PayloadRepo struct { DefaultBranch string `json:"default_branch"` } +var ( + _ Payloader = &CreatePayload{} + _ Payloader = &PushPayload{} +) + // _________ __ // \_ ___ \_______ ____ _____ _/ |_ ____ // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \ diff --git a/utils.go b/utils.go index 134fd8e..0083e05 100644 --- a/utils.go +++ b/utils.go @@ -7,3 +7,11 @@ package gogs func Bool(v bool) *bool { return &v } + +func String(v string) *string { + return &v +} + +func Int64(v int64) *int64 { + return &v +}