From f3ae95fdd78efe283653881e961142bde0cfbb13 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 26 Aug 2015 21:45:28 +0800 Subject: [PATCH 01/60] rename field --- repo_hooks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo_hooks.go b/repo_hooks.go index 85a0256..4bd4006 100644 --- a/repo_hooks.go +++ b/repo_hooks.go @@ -18,7 +18,7 @@ var ( ) type Hook struct { - Id int64 `json:"id"` + ID int64 `json:"id"` Type string `json:"type"` Events []string `json:"events"` Active bool `json:"active"` From 837d3db7914deef1af82624d91f7cd0dd39231bc Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 28 Aug 2015 19:06:00 +0800 Subject: [PATCH 02/60] new create repo options --- repo.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repo.go b/repo.go index f17e58d..6bcf162 100644 --- a/repo.go +++ b/repo.go @@ -43,8 +43,9 @@ type CreateRepoOption struct { Description string `json:"description" binding:"MaxSize(255)"` Private bool `json:"private"` AutoInit bool `json:"auto_init"` - Gitignore string `json:"gitignore"` + Gitignores string `json:"gitignores"` License string `json:"license"` + Readme string `json:"readme"` } // CreateRepo creates a repository for authenticated user. From c190da77d79df24dbf2180d2518744dfaf0cf3fd Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 28 Aug 2015 23:39:30 +0800 Subject: [PATCH 03/60] rename field --- repo_hooks.go | 117 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 96 insertions(+), 21 deletions(-) diff --git a/repo_hooks.go b/repo_hooks.go index 4bd4006..3f48570 100644 --- a/repo_hooks.go +++ b/repo_hooks.go @@ -61,23 +61,34 @@ func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) e return err } +type Payloader interface { + SetSecret(string) + JSONPayload() ([]byte, error) +} + type PayloadAuthor struct { Name string `json:"name"` Email string `json:"email"` UserName string `json:"username"` } +type PayloadUser struct { + UserName string `json:"login"` + ID int64 `json:"id"` + AvatarUrl string `json:"avatar_url"` +} + type PayloadCommit struct { - Id string `json:"id"` + ID string `json:"id"` Message string `json:"message"` - Url string `json:"url"` + URL string `json:"url"` Author *PayloadAuthor `json:"author"` } type PayloadRepo struct { - Id int64 `json:"id"` + ID int64 `json:"id"` Name string `json:"name"` - Url string `json:"url"` + URL string `json:"url"` Description string `json:"description"` Website string `json:"website"` Watchers int `json:"watchers"` @@ -85,26 +96,42 @@ type PayloadRepo struct { Private bool `json:"private"` } -// Payload represents a payload information of hook. -type Payload struct { - Secret string `json:"secret"` - Ref string `json:"ref"` - Commits []*PayloadCommit `json:"commits"` - Repo *PayloadRepo `json:"repository"` - Pusher *PayloadAuthor `json:"pusher"` - Before string `json:"before"` - After string `json:"after"` - CompareUrl string `json:"compare_url"` +// _________ __ +// \_ ___ \_______ ____ _____ _/ |_ ____ +// / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \ +// \ \____| | \/\ ___/ / __ \| | \ ___/ +// \______ /|__| \___ >____ /__| \___ > +// \/ \/ \/ \/ + +type CreatePayload struct { + Secret string `json:"secret"` + Ref string `json:"ref"` + RefType string `json:"ref_type"` + Repo *PayloadRepo `json:"repository"` + Sender *PayloadUser `json:"sender"` } -// ParseHook parses Gogs webhook content. -func ParseHook(raw []byte) (*Payload, error) { - hook := new(Payload) +func (p *CreatePayload) SetSecret(secret string) { + p.Secret = secret +} + +func (p *CreatePayload) JSONPayload() ([]byte, error) { + data, err := json.MarshalIndent(p, "", " ") + if err != nil { + return []byte{}, err + } + return data, nil +} + +// ParseCreateHook parses create event hook content. +func ParseCreateHook(raw []byte) (*CreatePayload, error) { + hook := new(CreatePayload) if err := json.Unmarshal(raw, hook); err != nil { return nil, err } + // it is possible the JSON was parsed, however, - // was not from Github (maybe was from Bitbucket) + // was not from Gogs (maybe was from Bitbucket) // So we'll check to be sure certain key fields // were populated switch { @@ -116,7 +143,55 @@ func ParseHook(raw []byte) (*Payload, error) { return hook, nil } -// Branch returns branch name from a payload -func (h *Payload) Branch() string { - return strings.Replace(h.Ref, "refs/heads/", "", -1) +// __________ .__ +// \______ \__ __ _____| |__ +// | ___/ | \/ ___/ | \ +// | | | | /\___ \| Y \ +// |____| |____//____ >___| / +// \/ \/ + +// PushPayload represents a payload information of push event. +type PushPayload struct { + Secret string `json:"secret"` + Ref string `json:"ref"` + Before string `json:"before"` + After string `json:"after"` + CompareUrl string `json:"compare_url"` + Commits []*PayloadCommit `json:"commits"` + Repo *PayloadRepo `json:"repository"` + Pusher *PayloadAuthor `json:"pusher"` + Sender *PayloadUser `json:"sender"` +} + +func (p *PushPayload) SetSecret(secret string) { + p.Secret = secret +} + +func (p *PushPayload) JSONPayload() ([]byte, error) { + data, err := json.MarshalIndent(p, "", " ") + if err != nil { + return []byte{}, err + } + return data, nil +} + +// ParsePushHook parses push event hook content. +func ParsePushHook(raw []byte) (*PushPayload, error) { + hook := new(PushPayload) + if err := json.Unmarshal(raw, hook); err != nil { + return nil, err + } + + switch { + case hook.Repo == nil: + return nil, ErrInvalidReceiveHook + case len(hook.Ref) == 0: + return nil, ErrInvalidReceiveHook + } + return hook, nil +} + +// Branch returns branch name from a payload +func (p *PushPayload) Branch() string { + return strings.Replace(p.Ref, "refs/heads/", "", -1) } From 519eee0af000c19dfe599a50749b0e111d31dafc Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 29 Aug 2015 11:49:06 +0800 Subject: [PATCH 04/60] more hook fields --- repo_hooks.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/repo_hooks.go b/repo_hooks.go index 3f48570..89d5ef9 100644 --- a/repo_hooks.go +++ b/repo_hooks.go @@ -11,6 +11,7 @@ import ( "fmt" "net/http" "strings" + "time" ) var ( @@ -18,11 +19,14 @@ var ( ) type Hook struct { - ID int64 `json:"id"` - Type string `json:"type"` - Events []string `json:"events"` - Active bool `json:"active"` - Config map[string]string `json:"config"` + ID int64 `json:"id"` + Type string `json:"type"` + URL string `json:"-"` + Config map[string]string `json:"config"` + Events []string `json:"events"` + Active bool `json:"active"` + Updated time.Time `json:"updated_at"` + Created time.Time `json:"created_at"` } func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) { @@ -33,6 +37,7 @@ func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) { type CreateHookOption struct { Type string `json:"type" binding:"Required"` Config map[string]string `json:"config" binding:"Required"` + Events []string `json:"events"` Active bool `json:"active"` } @@ -48,6 +53,7 @@ func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, type EditHookOption struct { Config map[string]string `json:"config"` + Events []string `json:"events"` Active *bool `json:"active"` } From e42d1bd6fe4c02e8b8f4ecf7a6fa23e12093b920 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 8 Oct 2015 21:00:46 -0400 Subject: [PATCH 05/60] add delete repo --- repo.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/repo.go b/repo.go index 6bcf162..b81da84 100644 --- a/repo.go +++ b/repo.go @@ -69,3 +69,8 @@ func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, e return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo) } + +// DeleteRepo deletes a repository of user or organization. +func (c *Client) DeleteRepo(owner, repo string) error { + return c.getParsedResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil) +} From 717e04de1ef67094e294418fcb380ad9528e5115 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 8 Oct 2015 22:34:54 -0400 Subject: [PATCH 06/60] fix error --- repo.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repo.go b/repo.go index b81da84..5f51944 100644 --- a/repo.go +++ b/repo.go @@ -72,5 +72,6 @@ func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, e // DeleteRepo deletes a repository of user or organization. func (c *Client) DeleteRepo(owner, repo string) error { - return c.getParsedResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil) + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil) + return err } From 8068507b500ea3ededecacc9d53c10da5f6ddd01 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 22 Oct 2015 17:42:42 -0400 Subject: [PATCH 07/60] work on #7 and #8 --- gogs.go | 7 ++++++- repo.go | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gogs.go b/gogs.go index f921090..98bc529 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.0.2" + return "0.0.4" } // Client represents a Gogs API client. @@ -33,6 +33,11 @@ func NewClient(url, token string) *Client { } } +// SetHTTPClient replaces default http.Client with user given one. +func (c *Client) SetHTTPClient(client *http.Client) { + c.client = client +} + func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) { req, err := http.NewRequest(method, c.url+"/api/v1"+path, body) if err != nil { diff --git a/repo.go b/repo.go index 5f51944..83de2de 100644 --- a/repo.go +++ b/repo.go @@ -70,6 +70,13 @@ func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, e http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo) } +// GetRepo returns information of a repository of given owner. +func (c *Client) GetRepo(owner, reponame string) (*Repository, error) { + repo := new(Repository) + return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), + http.Header{"content-type": []string{"application/json"}}, nil, repo) +} + // DeleteRepo deletes a repository of user or organization. func (c *Client) DeleteRepo(owner, repo string) error { _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil) From ee6b9a279b2fe47ff984c9d8ae09904864965ff2 Mon Sep 17 00:00:00 2001 From: Ruben Vermeersch Date: Wed, 28 Oct 2015 17:04:48 +0100 Subject: [PATCH 08/60] Add binding for POST /repos/migrate --- repo.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/repo.go b/repo.go index 83de2de..315c6aa 100644 --- a/repo.go +++ b/repo.go @@ -82,3 +82,29 @@ func (c *Client) DeleteRepo(owner, repo string) error { _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil) return err } + +type MigrateRepoOption struct { + CloneAddr string `json:"clone_addr" binding:"Required"` + AuthUsername string `json:"auth_username"` + AuthPassword string `json:"auth_password"` + Uid int `json:"uid" binding:"Required"` + RepoName string `json:"repo_name" binding:"Required"` + Mirror bool `json:"mirror"` + Private bool `json:"private"` + Description string `json:"description"` +} + +// MigrateRepo migrates a repository from other Git hosting sources for the +// authenticated user. +// +// To migrate a repository for a organization, the authenticated user must be a +// owner of the specified organization. +func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + repo := new(Repository) + return repo, c.getParsedResponse("POST", "/repos/migrate", + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo) +} From a022a5cc719783c094c1d6364a5f0c59c5ad5f44 Mon Sep 17 00:00:00 2001 From: Ruben Vermeersch Date: Thu, 29 Oct 2015 08:32:08 +0100 Subject: [PATCH 09/60] Rename Uid -> UID --- repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo.go b/repo.go index 315c6aa..b79ea22 100644 --- a/repo.go +++ b/repo.go @@ -87,7 +87,7 @@ type MigrateRepoOption struct { CloneAddr string `json:"clone_addr" binding:"Required"` AuthUsername string `json:"auth_username"` AuthPassword string `json:"auth_password"` - Uid int `json:"uid" binding:"Required"` + UID int `json:"uid" binding:"Required"` RepoName string `json:"repo_name" binding:"Required"` Mirror bool `json:"mirror"` Private bool `json:"private"` From 1547d4e91303516fab9551ba0026fa8706550aac Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 18 Nov 2015 21:09:58 -0500 Subject: [PATCH 10/60] REST endpoint for keys --- gogs.go | 2 +- repo_keys.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 repo_keys.go diff --git a/gogs.go b/gogs.go index 98bc529..f7c0f68 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.0.4" + return "0.1.0" } // Client represents a Gogs API client. diff --git a/repo_keys.go b/repo_keys.go new file mode 100644 index 0000000..0246476 --- /dev/null +++ b/repo_keys.go @@ -0,0 +1,47 @@ +// Copyright 2015 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 DeployKey struct { + ID int64 `json:"id"` + Key string `json:"key"` + URL string `json:"url"` + Title string `json:"title"` + Created time.Time `json:"created_at"` + ReadOnly bool `json:"read_only"` +} + +func (c *Client) ListDeployKeys(user, repo string) ([]*DeployKey, error) { + keys := make([]*DeployKey, 0, 10) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys", user, repo), nil, nil, &keys) +} + +type CreateDeployKeyOption struct { + Title string `json:"title" binding:"Required"` + Key string `json:"key" binding:"Required"` +} + +func (c *Client) CreateDeployKey(user, repo string, opt CreateDeployKeyOption) (*DeployKey, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + key := new(DeployKey) + return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key) +} + +func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/keys/%d", owner, repo, keyID), nil, nil) + return err +} From 4663f3ae455f7a6d2c31b360d82831bd53b40b0f Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 3 Dec 2015 00:16:21 -0500 Subject: [PATCH 11/60] public key --- repo_keys.go | 9 +++++++-- user_keys.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 user_keys.go diff --git a/repo_keys.go b/repo_keys.go index 0246476..dfd15c9 100644 --- a/repo_keys.go +++ b/repo_keys.go @@ -26,12 +26,17 @@ func (c *Client) ListDeployKeys(user, repo string) ([]*DeployKey, error) { return keys, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys", user, repo), nil, nil, &keys) } -type CreateDeployKeyOption struct { +func (c *Client) GetDeployKey(user, repo string, keyID int64) (*DeployKey, error) { + key := new(DeployKey) + return key, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/keys/%d", user, repo, keyID), nil, nil, &key) +} + +type CreateKeyOption struct { Title string `json:"title" binding:"Required"` Key string `json:"key" binding:"Required"` } -func (c *Client) CreateDeployKey(user, repo string, opt CreateDeployKeyOption) (*DeployKey, error) { +func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*DeployKey, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err diff --git a/user_keys.go b/user_keys.go new file mode 100644 index 0000000..cb21757 --- /dev/null +++ b/user_keys.go @@ -0,0 +1,51 @@ +// Copyright 2015 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 PublicKey struct { + ID int64 `json:"id"` + Key string `json:"key"` + URL string `json:"url,omitempty"` + Title string `json:"title,omitempty"` + Created time.Time `json:"created_at,omitempty"` +} + +func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) { + keys := make([]*PublicKey, 0, 10) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys) +} + +func (c *Client) ListMyPublicKeys(user string) ([]*PublicKey, error) { + keys := make([]*PublicKey, 0, 10) + return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys", user), nil, nil, &keys) +} + +func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) { + key := new(PublicKey) + return key, c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key) +} + +func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + key := new(PublicKey) + return key, c.getParsedResponse("POST", "/user/keys", + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key) +} + +func (c *Client) DeletePublicKey(keyID int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil) + return err +} From 41ded8a3f9f444a0546a6a136d15c64efb9f234c Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 4 Dec 2015 17:10:37 -0500 Subject: [PATCH 12/60] minor tag fix --- gogs.go | 2 +- miscellaneous.go | 11 +++++++++++ user_app.go | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 miscellaneous.go diff --git a/gogs.go b/gogs.go index f7c0f68..70c272d 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.1.0" + return "0.3.0" } // Client represents a Gogs API client. diff --git a/miscellaneous.go b/miscellaneous.go new file mode 100644 index 0000000..fcf362c --- /dev/null +++ b/miscellaneous.go @@ -0,0 +1,11 @@ +// Copyright 2015 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 MarkdownOption struct { + Text string + Mode string + Context string +} diff --git a/user_app.go b/user_app.go index 152492b..965ed6e 100644 --- a/user_app.go +++ b/user_app.go @@ -29,7 +29,7 @@ func (c *Client) ListAccessTokens(user, pass string) ([]*AccessToken, error) { } type CreateAccessTokenOption struct { - Name string `json:"name"` + Name string `json:"name" binding:"Required"` } func (c *Client) CreateAccessToken(user, pass string, opt CreateAccessTokenOption) (*AccessToken, error) { From a27e272ebf9568b02cf82217f60f80dcebb80064 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 5 Dec 2015 13:24:40 -0500 Subject: [PATCH 13/60] add more field to repo payload --- repo_hooks.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/repo_hooks.go b/repo_hooks.go index 89d5ef9..fa3a1e6 100644 --- a/repo_hooks.go +++ b/repo_hooks.go @@ -95,6 +95,8 @@ type PayloadRepo struct { ID int64 `json:"id"` Name string `json:"name"` URL string `json:"url"` + SSHURL string `json:"ssh_url"` + CloneURL string `json:"clone_url"` Description string `json:"description"` Website string `json:"website"` Watchers int `json:"watchers"` From 4b541faeee020042ccbbd517b38d236818d817d9 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 5 Dec 2015 17:12:43 -0500 Subject: [PATCH 14/60] admin users --- .gitignore | 1 + admin_users.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ gogs.go | 2 +- utils.go | 9 +++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 admin_users.go create mode 100644 utils.go diff --git a/.gitignore b/.gitignore index daf913b..25e241a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ _testmain.go *.exe *.test *.prof +.idea diff --git a/admin_users.go b/admin_users.go new file mode 100644 index 0000000..0ececaf --- /dev/null +++ b/admin_users.go @@ -0,0 +1,70 @@ +// Copyright 2015 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" +) + +type CreateUserOption struct { + SourceID int64 `json:"source_id"` + LoginName string `json:"login_name"` + Username string `json:"username" binding:"Required;AlphaDashDot;MaxSize(35)"` + Email string `json:"email" binding:"Required;Email;MaxSize(254)"` + Password string `json:"password" binding:"MaxSize(255)"` + SendNotify bool `json:"send_notify"` +} + +func (c *Client) CreateUser(opt CreateUserOption) (*User, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + user := new(User) + return user, c.getParsedResponse("POST", "/admin/users", + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), user) +} + +type EditUserOption struct { + SourceID int64 `json:"source_id"` + LoginName string `json:"login_name"` + FullName string `json:"full_name" binding:"MaxSize(100)"` + Email string `json:"email" binding:"Required;Email;MaxSize(254)"` + Password string `json:"password" binding:"MaxSize(255)"` + Website string `json:"website" binding:"MaxSize(50)"` + Location string `json:"location" binding:"MaxSize(50)"` + Active *bool `json:"active"` + Admin *bool `json:"admin"` + AllowGitHook *bool `json:"allow_git_hook"` + AllowImportLocal *bool `json:"allow_import_local"` +} + +func (c *Client) EditUser(user string, opt EditUserOption) error { + body, err := json.Marshal(&opt) + if err != nil { + return err + } + _, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + return err +} + +func (c *Client) DeleteUser(user string) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s", user), nil, nil) + return err +} + +func (c *Client) CreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + key := new(PublicKey) + return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key) +} diff --git a/gogs.go b/gogs.go index 70c272d..f1ac3c5 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.3.0" + return "0.4.0" } // Client represents a Gogs API client. diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..134fd8e --- /dev/null +++ b/utils.go @@ -0,0 +1,9 @@ +// Copyright 2015 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 + +func Bool(v bool) *bool { + return &v +} From 6cc168ca78710e0ac0f8593faab41861ebb6bdaf Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 15 Dec 2015 22:56:44 -0500 Subject: [PATCH 15/60] add user_email --- gogs.go | 2 +- user_email.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 user_email.go diff --git a/gogs.go b/gogs.go index f1ac3c5..84599ac 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.4.0" + return "0.5.0" } // Client represents a Gogs API client. diff --git a/user_email.go b/user_email.go new file mode 100644 index 0000000..b346599 --- /dev/null +++ b/user_email.go @@ -0,0 +1,46 @@ +// Copyright 2015 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" + "net/http" +) + +type Email struct { + Email string `json:"email"` + Verified bool `json:"verified"` + Primary bool `json:"primary"` +} + +func (c *Client) ListEmails() ([]*Email, error) { + emails := make([]*Email, 0, 3) + return emails, c.getParsedResponse("GET", "/user/emails", nil, nil, &emails) +} + +type CreateEmailOption struct { + Emails []string `json:"emails"` +} + +func (c *Client) AddEmail(opt CreateEmailOption) ([]*Email, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + emails := make([]*Email, 0, 3) + return emails, c.getParsedResponse("POST", "/user/emails", + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), emails) +} + +func (c *Client) DeleteEmail(opt CreateEmailOption) error { + body, err := json.Marshal(&opt) + if err != nil { + return err + } + _, err = c.getResponse("DELETE", "/user/emails", + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + return err +} From 30a98edc71c88a8a90fba681fc86a72d5636cfd5 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 17 Dec 2015 02:15:29 -0500 Subject: [PATCH 16/60] add orgs --- admin_orgs.go | 30 ++++++++++++++++++++++++++++ gogs.go | 2 +- org.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ repo.go | 6 ++---- 4 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 admin_orgs.go create mode 100644 org.go diff --git a/admin_orgs.go b/admin_orgs.go new file mode 100644 index 0000000..9984ecc --- /dev/null +++ b/admin_orgs.go @@ -0,0 +1,30 @@ +// Copyright 2015 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" +) + +type CreateOrgOption struct { + UserName string `json:"username"` + FullName string `json:"full_name"` + Description string `json:"description"` + Website string `json:"website"` + Location string `json:"location"` +} + +func (c *Client) CreateOrg(user string, opt CreateOrgOption) (*Organization, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + org := new(Organization) + return org, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), org) +} diff --git a/gogs.go b/gogs.go index 84599ac..bd263a1 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.5.0" + return "0.6.0" } // Client represents a Gogs API client. diff --git a/org.go b/org.go new file mode 100644 index 0000000..4d31461 --- /dev/null +++ b/org.go @@ -0,0 +1,54 @@ +// Copyright 2015 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" +) + +type Organization struct { + ID int64 `json:"id"` + UserName string `json:"username"` + FullName string `json:"full_name"` + AvatarUrl string `json:"avatar_url"` + Description string `json:"description"` + Website string `json:"website"` + Location string `json:"location"` +} + +func (c *Client) ListMyOrgs() ([]*Organization, error) { + orgs := make([]*Organization, 0, 5) + return orgs, c.getParsedResponse("GET", "/user/orgs", nil, nil, &orgs) +} + +func (c *Client) ListUserOrgs(user string) ([]*Organization, error) { + orgs := make([]*Organization, 0, 5) + return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs", user), nil, nil, &orgs) +} + +func (c *Client) GetOrg(orgname string) (*Organization, error) { + org := new(Organization) + return org, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s", orgname), nil, nil, org) +} + +type EditOrgOption struct { + FullName string `json:"full_name"` + Description string `json:"description"` + Website string `json:"website"` + Location string `json:"location"` +} + +func (c *Client) EditOrg(orgname string, opt EditOrgOption) error { + body, err := json.Marshal(&opt) + if err != nil { + return err + } + _, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + return err +} diff --git a/repo.go b/repo.go index b79ea22..aa6808f 100644 --- a/repo.go +++ b/repo.go @@ -34,8 +34,7 @@ type Repository struct { // ListMyRepos lists all repositories for the authenticated user that has access to. func (c *Client) ListMyRepos() ([]*Repository, error) { repos := make([]*Repository, 0, 10) - err := c.getParsedResponse("GET", "/user/repos", nil, nil, &repos) - return repos, err + return repos, c.getParsedResponse("GET", "/user/repos", nil, nil, &repos) } type CreateRepoOption struct { @@ -73,8 +72,7 @@ func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, e // GetRepo returns information of a repository of given owner. func (c *Client) GetRepo(owner, reponame string) (*Repository, error) { repo := new(Repository) - return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), - http.Header{"content-type": []string{"application/json"}}, nil, repo) + return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), nil, nil, repo) } // DeleteRepo deletes a repository of user or organization. From ce7aab692f41706d88b1ee18bc0aed550dfaa095 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 17 Dec 2015 02:24:28 -0500 Subject: [PATCH 17/60] binding rule --- admin_orgs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin_orgs.go b/admin_orgs.go index 9984ecc..9774152 100644 --- a/admin_orgs.go +++ b/admin_orgs.go @@ -12,7 +12,7 @@ import ( ) type CreateOrgOption struct { - UserName string `json:"username"` + UserName string `json:"username" binding:"Required"` FullName string `json:"full_name"` Description string `json:"description"` Website string `json:"website"` From 78460e9b869bfc42ff3ba62c52343433da629c93 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 17 Dec 2015 22:51:53 -0500 Subject: [PATCH 18/60] admin creare repo --- admin_orgs.go | 2 +- admin_repos.go | 22 ++++++++++++++++++++++ admin_users.go | 8 ++++---- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 admin_repos.go diff --git a/admin_orgs.go b/admin_orgs.go index 9774152..ee29464 100644 --- a/admin_orgs.go +++ b/admin_orgs.go @@ -19,7 +19,7 @@ type CreateOrgOption struct { Location string `json:"location"` } -func (c *Client) CreateOrg(user string, opt CreateOrgOption) (*Organization, error) { +func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err diff --git a/admin_repos.go b/admin_repos.go new file mode 100644 index 0000000..8a213cb --- /dev/null +++ b/admin_repos.go @@ -0,0 +1,22 @@ +// Copyright 2015 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" +) + +func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + repo := new(Repository) + return repo, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo) +} diff --git a/admin_users.go b/admin_users.go index 0ececaf..f8e26d9 100644 --- a/admin_users.go +++ b/admin_users.go @@ -20,7 +20,7 @@ type CreateUserOption struct { SendNotify bool `json:"send_notify"` } -func (c *Client) CreateUser(opt CreateUserOption) (*User, error) { +func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err @@ -44,7 +44,7 @@ type EditUserOption struct { AllowImportLocal *bool `json:"allow_import_local"` } -func (c *Client) EditUser(user string, opt EditUserOption) error { +func (c *Client) AdminEditUser(user string, opt EditUserOption) error { body, err := json.Marshal(&opt) if err != nil { return err @@ -54,12 +54,12 @@ func (c *Client) EditUser(user string, opt EditUserOption) error { return err } -func (c *Client) DeleteUser(user string) error { +func (c *Client) AdminDeleteUser(user string) error { _, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s", user), nil, nil) return err } -func (c *Client) CreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, error) { +func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err From 2a795f081a1b780357d4809f3b1f0941f473e9b9 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 21 Dec 2015 04:12:20 -0800 Subject: [PATCH 19/60] user - follow --- gogs.go | 4 ++-- user_follow.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 user_follow.go diff --git a/gogs.go b/gogs.go index bd263a1..232e6ab 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.6.0" + return "0.7.0" } // Client represents a Gogs API client. @@ -66,7 +66,7 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re return nil, errors.New("404 Not Found") } - if resp.StatusCode != 200 && resp.StatusCode != 201 { + if resp.StatusCode%100 != 2 { errMap := make(map[string]interface{}) if err = json.Unmarshal(data, &errMap); err != nil { return nil, err diff --git a/user_follow.go b/user_follow.go new file mode 100644 index 0000000..36cc65d --- /dev/null +++ b/user_follow.go @@ -0,0 +1,47 @@ +// Copyright 2015 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 "fmt" + +func (c *Client) ListMyFollowers(page int) ([]*User, error) { + users := make([]*User, 0, 10) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/followers?page=%d", page), nil, nil, &users) +} + +func (c *Client) ListFollowers(user string, page int) ([]*User, error) { + users := make([]*User, 0, 10) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/followers?page=%d", user, page), nil, nil, &users) +} + +func (c *Client) ListMyFollowing(page int) ([]*User, error) { + users := make([]*User, 0, 10) + return users, c.getParsedResponse("GET", fmt.Sprintf("/user/following?page=%d", page), nil, nil, &users) +} + +func (c *Client) ListFollowing(user string, page int) ([]*User, error) { + users := make([]*User, 0, 10) + return users, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/following?page=%d", user, page), nil, nil, &users) +} + +func (c *Client) IsFollowing(target string) bool { + _, err := c.getResponse("GET", fmt.Sprintf("/user/following/%s", target), nil, nil) + return err == nil +} + +func (c *Client) IsUserFollowing(user, target string) bool { + _, err := c.getResponse("GET", fmt.Sprintf("/users/%s/following/%s", user, target), nil, nil) + return err == nil +} + +func (c *Client) Follow(target string) error { + _, err := c.getResponse("PUT", fmt.Sprintf("/user/following/%s", target), nil, nil) + return err +} + +func (c *Client) Unfollow(target string) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/user/following/%s", target), nil, nil) + return err +} From 7830cf2c516c17e8df2bcbab8ebd8f0050ed5fbb Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 4 Jan 2016 04:51:12 +0800 Subject: [PATCH 20/60] fix #13 --- gogs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gogs.go b/gogs.go index 232e6ab..134d028 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.7.0" + return "0.7.1" } // Client represents a Gogs API client. @@ -66,7 +66,7 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re return nil, errors.New("404 Not Found") } - if resp.StatusCode%100 != 2 { + if resp.StatusCode/100 != 2 { errMap := make(map[string]interface{}) if err = json.Unmarshal(data, &errMap); err != nil { return nil, err From 2f4342d6deb4839427316c49042d90fdf601229f Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 6 Jan 2016 13:18:57 +0800 Subject: [PATCH 21/60] fix gogits/gogs#2329 --- gogs.go | 2 +- repo.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gogs.go b/gogs.go index 134d028..dba1328 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.7.1" + return "0.7.2" } // Client represents a Gogs API client. diff --git a/repo.go b/repo.go index aa6808f..7f703cb 100644 --- a/repo.go +++ b/repo.go @@ -38,7 +38,7 @@ func (c *Client) ListMyRepos() ([]*Repository, error) { } type CreateRepoOption struct { - Name string `json:"name" binding:"Required"` + Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"` Description string `json:"description" binding:"MaxSize(255)"` Private bool `json:"private"` AutoInit bool `json:"auto_init"` From 10296af427b9af7488ce80a6e82f3a55aaee8edf Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Thu, 14 Jan 2016 16:21:47 +0100 Subject: [PATCH 22/60] Create repo_branches.go --- repo_branches.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 repo_branches.go diff --git a/repo_branches.go b/repo_branches.go new file mode 100644 index 0000000..850fb93 --- /dev/null +++ b/repo_branches.go @@ -0,0 +1,16 @@ +package gogs + +type Branch struct { + name string `json:"name"` + commit *PayloadCommit `json:"commit"` +} + +func (c *Client) ListRepoBranches(user, repo string) ([]*Branch, error) { + branches := make([]*Branch, 0, 10) + return branches, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches", user, repo), nil, nil, &branches) +} + +func (c *Client) GetRepoBranch(user, repo, branch string) (*Branch, error) { + b := new(Branch) + return b, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/branches/%s", user, repo, branch), nil, nil, &b) +} From 3716eee47b7a20be8178e700eb26b2d5e41960a4 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Fri, 15 Jan 2016 14:30:47 +0100 Subject: [PATCH 23/60] Use uppercase for first letter --- repo_branches.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo_branches.go b/repo_branches.go index 850fb93..944c895 100644 --- a/repo_branches.go +++ b/repo_branches.go @@ -1,8 +1,8 @@ package gogs type Branch struct { - name string `json:"name"` - commit *PayloadCommit `json:"commit"` + Name string `json:"name"` + Commit *PayloadCommit `json:"commit"` } func (c *Client) ListRepoBranches(user, repo string) ([]*Branch, error) { From 25f8ed637da48c4d8003f7548f9849d2d724943b Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Fri, 15 Jan 2016 14:35:31 +0100 Subject: [PATCH 24/60] Fix import --- repo_branches.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/repo_branches.go b/repo_branches.go index 944c895..e6f5458 100644 --- a/repo_branches.go +++ b/repo_branches.go @@ -1,5 +1,9 @@ package gogs +import ( + "fmt" +) + type Branch struct { Name string `json:"name"` Commit *PayloadCommit `json:"commit"` From 4f98d4dc0dd1af1b7ca7ff462dca65c5347ca933 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 2 Feb 2016 16:54:14 -0500 Subject: [PATCH 25/60] Minor fix for #15 --- repo_branches.go => repo_branch.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) rename repo_branches.go => repo_branch.go (63%) diff --git a/repo_branches.go b/repo_branch.go similarity index 63% rename from repo_branches.go rename to repo_branch.go index e6f5458..1e58112 100644 --- a/repo_branches.go +++ b/repo_branch.go @@ -1,12 +1,17 @@ +// 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 ( "fmt" ) +// Branch represents a repository branch. type Branch struct { - Name string `json:"name"` - Commit *PayloadCommit `json:"commit"` + Name string `json:"name"` + Commit *PayloadCommit `json:"commit"` } func (c *Client) ListRepoBranches(user, repo string) ([]*Branch, error) { From af209485d57bd4af5e97083d2a1b787dd39f1146 Mon Sep 17 00:00:00 2001 From: Josh Frye Date: Fri, 12 Feb 2016 11:03:04 -0500 Subject: [PATCH 26/60] Add default branch to repo and change version --- gogs.go | 2 +- repo_hooks.go | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/gogs.go b/gogs.go index dba1328..8ec21d1 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.7.2" + return "0.7.3" } // Client represents a Gogs API client. diff --git a/repo_hooks.go b/repo_hooks.go index fa3a1e6..06c9687 100644 --- a/repo_hooks.go +++ b/repo_hooks.go @@ -92,16 +92,17 @@ type PayloadCommit struct { } type PayloadRepo struct { - ID int64 `json:"id"` - Name string `json:"name"` - URL string `json:"url"` - SSHURL string `json:"ssh_url"` - CloneURL string `json:"clone_url"` - Description string `json:"description"` - Website string `json:"website"` - Watchers int `json:"watchers"` - Owner *PayloadAuthor `json:"owner"` - Private bool `json:"private"` + ID int64 `json:"id"` + Name string `json:"name"` + URL string `json:"url"` + SSHURL string `json:"ssh_url"` + CloneURL string `json:"clone_url"` + Description string `json:"description"` + Website string `json:"website"` + Watchers int `json:"watchers"` + Owner *PayloadAuthor `json:"owner"` + Private bool `json:"private"` + DefaultBranch string `json:"default_branch"` } // _________ __ From 788ec59749df076b98e208909b44fdef02779deb Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 13 Mar 2016 23:18:27 -0400 Subject: [PATCH 27/60] 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 +} From 1da1834d366c2c0b0b4495d34089082f4e38aed5 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 21 Mar 2016 12:45:51 -0400 Subject: [PATCH 28/60] Add CreateTeamOption --- admin_orgs.go => admin_org.go | 8 -------- admin_repos.go => admin_repo.go | 0 admin_users.go => admin_user.go | 0 gogs.go | 2 +- org.go | 8 ++++++++ org_team.go | 18 ++++++++++++++++++ repo_hooks.go => repo_hook.go | 0 repo_keys.go => repo_key.go | 0 user_keys.go => user_key.go | 0 9 files changed, 27 insertions(+), 9 deletions(-) rename admin_orgs.go => admin_org.go (70%) rename admin_repos.go => admin_repo.go (100%) rename admin_users.go => admin_user.go (100%) create mode 100644 org_team.go rename repo_hooks.go => repo_hook.go (100%) rename repo_keys.go => repo_key.go (100%) rename user_keys.go => user_key.go (100%) diff --git a/admin_orgs.go b/admin_org.go similarity index 70% rename from admin_orgs.go rename to admin_org.go index ee29464..92f2cd9 100644 --- a/admin_orgs.go +++ b/admin_org.go @@ -11,14 +11,6 @@ import ( "net/http" ) -type CreateOrgOption struct { - UserName string `json:"username" binding:"Required"` - FullName string `json:"full_name"` - Description string `json:"description"` - Website string `json:"website"` - Location string `json:"location"` -} - func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) { body, err := json.Marshal(&opt) if err != nil { diff --git a/admin_repos.go b/admin_repo.go similarity index 100% rename from admin_repos.go rename to admin_repo.go diff --git a/admin_users.go b/admin_user.go similarity index 100% rename from admin_users.go rename to admin_user.go diff --git a/gogs.go b/gogs.go index 8e4596b..ce9e0a2 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.8.0" + return "0.8.1" } // Client represents a Gogs API client. diff --git a/org.go b/org.go index 4d31461..a68462d 100644 --- a/org.go +++ b/org.go @@ -36,6 +36,14 @@ func (c *Client) GetOrg(orgname string) (*Organization, error) { return org, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s", orgname), nil, nil, org) } +type CreateOrgOption struct { + UserName string `json:"username" binding:"Required"` + FullName string `json:"full_name"` + Description string `json:"description"` + Website string `json:"website"` + Location string `json:"location"` +} + type EditOrgOption struct { FullName string `json:"full_name"` Description string `json:"description"` diff --git a/org_team.go b/org_team.go new file mode 100644 index 0000000..89f09e3 --- /dev/null +++ b/org_team.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 + +type Team struct { + ID int64 `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Permission string `json:"permission"` +} + +type CreateTeamOption struct { + Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(30)"` + Description string `json:"description" binding:"MaxSize(255)"` + Permission string `json:"permission"` +} diff --git a/repo_hooks.go b/repo_hook.go similarity index 100% rename from repo_hooks.go rename to repo_hook.go diff --git a/repo_keys.go b/repo_key.go similarity index 100% rename from repo_keys.go rename to repo_key.go diff --git a/user_keys.go b/user_key.go similarity index 100% rename from user_keys.go rename to user_key.go From 01270dfe22e92d99ce9402fedba4530ad8810100 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 11 May 2016 12:18:31 -0400 Subject: [PATCH 29/60] issue: add field to indicate state when create --- gogs.go | 2 +- issue.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/gogs.go b/gogs.go index ce9e0a2..e3290d2 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.8.1" + return "0.8.2" } // Client represents a Gogs API client. diff --git a/issue.go b/issue.go index 236f41f..3204395 100644 --- a/issue.go +++ b/issue.go @@ -53,6 +53,7 @@ type CreateIssueOption struct { Assignee string `json:"assignee"` Milestone int64 `json:"milestone"` Labels []int64 `json:"labels"` + Closed bool `json:"closed"` } func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, error) { From e8d96b66be13760ef272f46f81cf202e8f9aac2f Mon Sep 17 00:00:00 2001 From: "Ahmed T. Youssef" Date: Sat, 2 Jul 2016 11:57:07 +0300 Subject: [PATCH 30/60] Update user_key.go (#29) Fixing ListMyPublicKeys --- user_key.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_key.go b/user_key.go index cb21757..99e8c09 100644 --- a/user_key.go +++ b/user_key.go @@ -25,9 +25,9 @@ func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) { return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys) } -func (c *Client) ListMyPublicKeys(user string) ([]*PublicKey, error) { +func (c *Client) ListMyPublicKeys() ([]*PublicKey, error) { keys := make([]*PublicKey, 0, 10) - return keys, c.getParsedResponse("GET", fmt.Sprintf("/user/keys", user), nil, nil, &keys) + return keys, c.getParsedResponse("GET", "/user/keys", nil, nil, &keys) } func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) { From 5ec1a1083059c3b267685eedb00a4922c6eafd17 Mon Sep 17 00:00:00 2001 From: lstahlman Date: Sun, 10 Jul 2016 13:57:18 -0700 Subject: [PATCH 31/60] Extend the API to include more repository properties (#33) Adds description, stars_count, forks_count, watchers_count and open_issues_count. --- repo.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/repo.go b/repo.go index 14a5a0b..f58d3af 100644 --- a/repo.go +++ b/repo.go @@ -23,11 +23,16 @@ type Repository struct { ID int64 `json:"id"` Owner *User `json:"owner"` FullName string `json:"full_name"` + Description string `json:"description"` Private bool `json:"private"` Fork bool `json:"fork"` HtmlUrl string `json:"html_url"` CloneUrl string `json:"clone_url"` SshUrl string `json:"ssh_url"` + Stars int `json:"stars_count"` + Forks int `json:"forks_count"` + Watchers int `json:"watchers_count"` + OpenIssues int `json:"open_issues_count"` Permissions Permission `json:"permissions"` } From 872cf281aac97429da02b6cdea942c9388eb65fb Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 11 Jul 2016 05:01:11 +0800 Subject: [PATCH 32/60] Bump version --- gogs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gogs.go b/gogs.go index e3290d2..1259929 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.8.2" + return "0.8.3" } // Client represents a Gogs API client. From ee68cd9eefff11615f336e9965762f6736eeecc8 Mon Sep 17 00:00:00 2001 From: lstahlman Date: Mon, 11 Jul 2016 14:02:42 -0700 Subject: [PATCH 33/60] Add timestamps to repository api response (#34) Add created_at and updated_at properties to repository api response --- repo.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/repo.go b/repo.go index f58d3af..adfb21a 100644 --- a/repo.go +++ b/repo.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "net/http" + "time" ) // Permission represents a API permission. @@ -33,6 +34,8 @@ type Repository struct { Forks int `json:"forks_count"` Watchers int `json:"watchers_count"` OpenIssues int `json:"open_issues_count"` + Created time.Time `json:"created_at"` + Updated time.Time `json:"updated_at"` Permissions Permission `json:"permissions"` } From f2f681f7cbaad80077a20f2e116b756a8ce8186c Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 17 Jul 2016 08:46:54 +0800 Subject: [PATCH 34/60] Simplify code --- admin_org.go | 3 +-- admin_repo.go | 3 +-- admin_user.go | 10 +++------- gogs.go | 2 +- issue.go | 5 ++--- org.go | 4 +--- repo.go | 10 +++------- repo_hook.go | 12 +++++++----- repo_key.go | 4 +--- user_email.go | 7 ++----- user_key.go | 4 +--- utils.go | 6 ++++++ 12 files changed, 29 insertions(+), 41 deletions(-) diff --git a/admin_org.go b/admin_org.go index 92f2cd9..be14062 100644 --- a/admin_org.go +++ b/admin_org.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" ) func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) { @@ -18,5 +17,5 @@ func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization } org := new(Organization) return org, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), org) + jsonHeader, bytes.NewReader(body), org) } diff --git a/admin_repo.go b/admin_repo.go index 8a213cb..50ba2be 100644 --- a/admin_repo.go +++ b/admin_repo.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" ) func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, error) { @@ -18,5 +17,5 @@ func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository } repo := new(Repository) return repo, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo) + jsonHeader, bytes.NewReader(body), repo) } diff --git a/admin_user.go b/admin_user.go index f8e26d9..4053150 100644 --- a/admin_user.go +++ b/admin_user.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" ) type CreateUserOption struct { @@ -26,8 +25,7 @@ func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) { return nil, err } user := new(User) - return user, c.getParsedResponse("POST", "/admin/users", - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), user) + return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user) } type EditUserOption struct { @@ -49,8 +47,7 @@ func (c *Client) AdminEditUser(user string, opt EditUserOption) error { if err != nil { return err } - _, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + _, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), jsonHeader, bytes.NewReader(body)) return err } @@ -65,6 +62,5 @@ func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*Pu return nil, err } key := new(PublicKey) - return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key) + return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key) } diff --git a/gogs.go b/gogs.go index 1259929..e3cbe41 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.8.3" + return "0.9.0" } // Client represents a Gogs API client. diff --git a/issue.go b/issue.go index 3204395..69e5c2e 100644 --- a/issue.go +++ b/issue.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" "time" ) @@ -63,7 +62,7 @@ func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, } 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) + jsonHeader, bytes.NewReader(body), issue) } type EditIssueOption struct { @@ -80,5 +79,5 @@ func (c *Client) EditIssue(owner, repo string, index int, opt EditIssueOption) ( } 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) + jsonHeader, bytes.NewReader(body), issue) } diff --git a/org.go b/org.go index a68462d..08d0088 100644 --- a/org.go +++ b/org.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" ) type Organization struct { @@ -56,7 +55,6 @@ func (c *Client) EditOrg(orgname string, opt EditOrgOption) error { if err != nil { return err } - _, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + _, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname), jsonHeader, bytes.NewReader(body)) return err } diff --git a/repo.go b/repo.go index adfb21a..4f48592 100644 --- a/repo.go +++ b/repo.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" "time" ) @@ -62,8 +61,7 @@ func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) { return nil, err } repo := new(Repository) - return repo, c.getParsedResponse("POST", "/user/repos", - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo) + return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo) } // CreateOrgRepo creates an organization repository for authenticated user. @@ -73,8 +71,7 @@ func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, e return nil, err } repo := new(Repository) - return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo) + return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo) } // GetRepo returns information of a repository of given owner. @@ -111,6 +108,5 @@ func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) { return nil, err } repo := new(Repository) - return repo, c.getParsedResponse("POST", "/repos/migrate", - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo) + return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo) } diff --git a/repo_hook.go b/repo_hook.go index 348458c..47fa13e 100644 --- a/repo_hook.go +++ b/repo_hook.go @@ -9,7 +9,6 @@ import ( "encoding/json" "errors" "fmt" - "net/http" "strings" "time" ) @@ -47,8 +46,7 @@ func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, return nil, err } h := new(Hook) - return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h) + return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h) } type EditHookOption struct { @@ -62,8 +60,12 @@ func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) e if err != nil { return err } - _, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + _, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body)) + return err +} + +func (c *Client) DeleteRepoHook(user, repo string, id int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil) return err } diff --git a/repo_key.go b/repo_key.go index dfd15c9..2201602 100644 --- a/repo_key.go +++ b/repo_key.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" "time" ) @@ -42,8 +41,7 @@ func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*Deplo return nil, err } key := new(DeployKey) - return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key) + return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key) } func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error { diff --git a/user_email.go b/user_email.go index b346599..02dd402 100644 --- a/user_email.go +++ b/user_email.go @@ -7,7 +7,6 @@ package gogs import ( "bytes" "encoding/json" - "net/http" ) type Email struct { @@ -31,8 +30,7 @@ func (c *Client) AddEmail(opt CreateEmailOption) ([]*Email, error) { return nil, err } emails := make([]*Email, 0, 3) - return emails, c.getParsedResponse("POST", "/user/emails", - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), emails) + return emails, c.getParsedResponse("POST", "/user/emails", jsonHeader, bytes.NewReader(body), emails) } func (c *Client) DeleteEmail(opt CreateEmailOption) error { @@ -40,7 +38,6 @@ func (c *Client) DeleteEmail(opt CreateEmailOption) error { if err != nil { return err } - _, err = c.getResponse("DELETE", "/user/emails", - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + _, err = c.getResponse("DELETE", "/user/emails", jsonHeader, bytes.NewReader(body)) return err } diff --git a/user_key.go b/user_key.go index 99e8c09..c0278e0 100644 --- a/user_key.go +++ b/user_key.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" "time" ) @@ -41,8 +40,7 @@ func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) { return nil, err } key := new(PublicKey) - return key, c.getParsedResponse("POST", "/user/keys", - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key) + return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key) } func (c *Client) DeletePublicKey(keyID int64) error { diff --git a/utils.go b/utils.go index 0083e05..a4d673e 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,12 @@ package gogs +import ( + "net/http" +) + +var jsonHeader = http.Header{"content-type": []string{"application/json"}} + func Bool(v bool) *bool { return &v } From 442b4e5ddc8029932c91872f8322fb7a2c2de858 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Tue, 26 Jul 2016 12:42:24 -0600 Subject: [PATCH 35/60] Added Full Name to CreateUserOption (#36) --- admin_user.go | 1 + 1 file changed, 1 insertion(+) diff --git a/admin_user.go b/admin_user.go index 4053150..d235a57 100644 --- a/admin_user.go +++ b/admin_user.go @@ -14,6 +14,7 @@ type CreateUserOption struct { SourceID int64 `json:"source_id"` LoginName string `json:"login_name"` Username string `json:"username" binding:"Required;AlphaDashDot;MaxSize(35)"` + FullName string `json:"full_name" binding:"MaxSize(100)"` Email string `json:"email" binding:"Required;Email;MaxSize(254)"` Password string `json:"password" binding:"MaxSize(255)"` SendNotify bool `json:"send_notify"` From 84314343cb1788f09147c03dc46f40a7f5fddda0 Mon Sep 17 00:00:00 2001 From: lstahlman Date: Tue, 2 Aug 2016 11:51:02 -0700 Subject: [PATCH 36/60] API Label Support (#35) --- issue_label.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/issue_label.go b/issue_label.go index 9e00ef7..1a59b15 100644 --- a/issue_label.go +++ b/issue_label.go @@ -4,7 +4,93 @@ package gogs +import ( + "bytes" + "encoding/json" + "fmt" +) + type Label struct { + ID int64 `json:"id"` Name string `json:"name"` Color string `json:"color"` } + +type LabelOption struct { + Name string `json:"name"` + Color string `json:"color"` +} + +func (c *Client) ListRepoLabels(owner, repo string) ([]*Label, error) { + labels := make([]*Label, 0) + return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels", owner, repo), nil, nil, &labels) +} + +func (c *Client) GetRepoLabel(owner, repo string, index int64) (*Label, error) { + label := new(Label) + return label, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index), nil, nil, label) +} + +func (c *Client) CreateLabel(owner, repo string, opt LabelOption) (*Label, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + label := new(Label) + return label, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/labels", owner, repo), + jsonHeader, bytes.NewReader(body), label) +} + +func (c *Client) EditLabel(owner, repo string, index int64, opt LabelOption) (*Label, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + label := new(Label) + return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index), + jsonHeader, bytes.NewReader(body), label) +} + +func (c *Client) DeleteLabel(owner, repo string, index int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index), nil, nil) + return err +} + +type IssueLabelsOption struct { + Labels []int64 `json:"labels"` +} + +func (c *Client) GetIssueLabels(owner, repo string, index int64) ([]*Label, error) { + labels := make([]*Label, 0) + return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil, &labels) +} + +func (c *Client) AddIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + labels := make([]*Label, 0) + return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), + jsonHeader, bytes.NewReader(body), &labels) +} + +func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + labels := make([]*Label, 0) + return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), + jsonHeader, bytes.NewReader(body), &labels) +} + +func (c *Client) DeleteIssueLabel(owner, repo string, index int64, label int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, index, label), nil, nil) + return err +} + +func (c *Client) ClearIssueLabels(owner, repo string, index int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil) + return err +} From 181ccc696caa21211e80f29d36925a69d9b85218 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 2 Aug 2016 11:59:14 -0700 Subject: [PATCH 37/60] #35 code cleanup --- gogs.go | 2 +- issue_label.go | 25 +++++++++++-------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/gogs.go b/gogs.go index e3cbe41..f6c6648 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.9.0" + return "0.10.0" } // Client represents a Gogs API client. diff --git a/issue_label.go b/issue_label.go index 1a59b15..7c80bb2 100644 --- a/issue_label.go +++ b/issue_label.go @@ -22,13 +22,13 @@ type LabelOption struct { } func (c *Client) ListRepoLabels(owner, repo string) ([]*Label, error) { - labels := make([]*Label, 0) + labels := make([]*Label, 0, 10) return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels", owner, repo), nil, nil, &labels) } -func (c *Client) GetRepoLabel(owner, repo string, index int64) (*Label, error) { +func (c *Client) GetRepoLabel(owner, repo string, id int64) (*Label, error) { label := new(Label) - return label, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index), nil, nil, label) + return label, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil, label) } func (c *Client) CreateLabel(owner, repo string, opt LabelOption) (*Label, error) { @@ -41,18 +41,17 @@ func (c *Client) CreateLabel(owner, repo string, opt LabelOption) (*Label, error jsonHeader, bytes.NewReader(body), label) } -func (c *Client) EditLabel(owner, repo string, index int64, opt LabelOption) (*Label, error) { +func (c *Client) EditLabel(owner, repo string, id int64, opt LabelOption) (*Label, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err } label := new(Label) - return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index), - jsonHeader, bytes.NewReader(body), label) + return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), label) } -func (c *Client) DeleteLabel(owner, repo string, index int64) error { - _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, index), nil, nil) +func (c *Client) DeleteLabel(owner, repo string, id int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil) return err } @@ -61,7 +60,7 @@ type IssueLabelsOption struct { } func (c *Client) GetIssueLabels(owner, repo string, index int64) ([]*Label, error) { - labels := make([]*Label, 0) + labels := make([]*Label, 0, 5) return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil, &labels) } @@ -71,8 +70,7 @@ func (c *Client) AddIssueLabels(owner, repo string, index int64, opt IssueLabels return nil, err } labels := make([]*Label, 0) - return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), - jsonHeader, bytes.NewReader(body), &labels) + return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels) } func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) { @@ -81,11 +79,10 @@ func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLa return nil, err } labels := make([]*Label, 0) - return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), - jsonHeader, bytes.NewReader(body), &labels) + return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels) } -func (c *Client) DeleteIssueLabel(owner, repo string, index int64, label int64) error { +func (c *Client) DeleteIssueLabel(owner, repo string, index, label int64) error { _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, index, label), nil, nil) return err } From e3ad13b2f318e0ed5272c5b213417868617001d8 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 2 Aug 2016 11:59:54 -0700 Subject: [PATCH 38/60] BREAK CHANGES: rename argument type for issue APIs int -> int64 --- issue.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/issue.go b/issue.go index 69e5c2e..da0ca83 100644 --- a/issue.go +++ b/issue.go @@ -41,7 +41,7 @@ func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Iss 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) { +func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, error) { issue := new(Issue) return issue, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue) } @@ -72,7 +72,7 @@ type EditIssueOption struct { Milestone *int64 `json:"milestone"` } -func (c *Client) EditIssue(owner, repo string, index int, opt EditIssueOption) (*Issue, error) { +func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err From d725743594dfcd8eea25024f8456c9c103dadb1a Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 3 Aug 2016 11:50:33 -0700 Subject: [PATCH 39/60] #35 improve code structure --- gogs.go | 2 +- issue_label.go | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/gogs.go b/gogs.go index f6c6648..d614e83 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.10.0" + return "0.10.1" } // Client represents a Gogs API client. diff --git a/issue_label.go b/issue_label.go index 7c80bb2..ce6a64d 100644 --- a/issue_label.go +++ b/issue_label.go @@ -16,11 +16,6 @@ type Label struct { Color string `json:"color"` } -type LabelOption struct { - Name string `json:"name"` - Color string `json:"color"` -} - func (c *Client) ListRepoLabels(owner, repo string) ([]*Label, error) { labels := make([]*Label, 0, 10) return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels", owner, repo), nil, nil, &labels) @@ -31,7 +26,12 @@ func (c *Client) GetRepoLabel(owner, repo string, id int64) (*Label, error) { return label, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil, label) } -func (c *Client) CreateLabel(owner, repo string, opt LabelOption) (*Label, error) { +type CreateLabelOption struct { + Name string `json:"name" binding:"Required"` + Color string `json:"color" binding:"Required;Size(7)"` +} + +func (c *Client) CreateLabel(owner, repo string, opt CreateLabelOption) (*Label, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err @@ -41,7 +41,12 @@ func (c *Client) CreateLabel(owner, repo string, opt LabelOption) (*Label, error jsonHeader, bytes.NewReader(body), label) } -func (c *Client) EditLabel(owner, repo string, id int64, opt LabelOption) (*Label, error) { +type EditLabelOption struct { + Name *string `json:"name"` + Color *string `json:"color"` +} + +func (c *Client) EditLabel(owner, repo string, id int64, opt EditLabelOption) (*Label, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err From 3a3b83760f362b667bc06d7d291bead04a15c4be Mon Sep 17 00:00:00 2001 From: Robin Lambertz Date: Sat, 6 Aug 2016 05:52:27 +0200 Subject: [PATCH 40/60] Add the AddOrgMembership API endpoint (#21) --- org_members.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 org_members.go diff --git a/org_members.go b/org_members.go new file mode 100644 index 0000000..9ff57ba --- /dev/null +++ b/org_members.go @@ -0,0 +1,26 @@ +// Copyright 2015 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" +) + +type AddOrgMembershipOption struct { + Role string `json:"role"` +} + +func (c *Client) AddOrgMembership(orgname string, username string, opt AddOrgMembershipOption) error { + body, err := json.Marshal(&opt) + if err != nil { + return err + } + _, err = c.getResponse("PUT", fmt.Sprintf("/orgs/%s/membership/%s", orgname, username), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + return err +} From 58af7d8521ea52ba81adcffbc18cc90e61383512 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 5 Aug 2016 20:56:36 -0700 Subject: [PATCH 41/60] #21 code cleanup --- gogs.go | 2 +- org_members.go => org_member.go | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) rename org_members.go => org_member.go (54%) diff --git a/gogs.go b/gogs.go index d614e83..0b19afa 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.10.1" + return "0.10.2" } // Client represents a Gogs API client. diff --git a/org_members.go b/org_member.go similarity index 54% rename from org_members.go rename to org_member.go index 9ff57ba..d7668bb 100644 --- a/org_members.go +++ b/org_member.go @@ -1,4 +1,4 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. +// 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. @@ -12,15 +12,14 @@ import ( ) type AddOrgMembershipOption struct { - Role string `json:"role"` + Role string `json:"role"` } -func (c *Client) AddOrgMembership(orgname string, username string, opt AddOrgMembershipOption) error { +func (c *Client) AddOrgMembership(org, user string, opt AddOrgMembershipOption) error { body, err := json.Marshal(&opt) if err != nil { return err } - _, err = c.getResponse("PUT", fmt.Sprintf("/orgs/%s/membership/%s", orgname, username), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body)) + _, err = c.getResponse("PUT", fmt.Sprintf("/orgs/%s/membership/%s", org, user), jsonHeader, bytes.NewReader(body)) return err } From 28d53c48e2e645f8ac996bb1fab1c92e88ef7fcd Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 5 Aug 2016 21:12:21 -0700 Subject: [PATCH 42/60] Fix compile error --- org_member.go | 1 - 1 file changed, 1 deletion(-) diff --git a/org_member.go b/org_member.go index d7668bb..8d8e208 100644 --- a/org_member.go +++ b/org_member.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "net/http" ) type AddOrgMembershipOption struct { From 9408f9de81df904cecceb1adb75d3b758e49ec9d Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 5 Aug 2016 21:25:42 -0700 Subject: [PATCH 43/60] Add binding validation rule --- org_member.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org_member.go b/org_member.go index 8d8e208..d9cdada 100644 --- a/org_member.go +++ b/org_member.go @@ -11,7 +11,7 @@ import ( ) type AddOrgMembershipOption struct { - Role string `json:"role"` + Role string `json:"role" binding:"Required"` } func (c *Client) AddOrgMembership(org, user string, opt AddOrgMembershipOption) error { From 9cf52efdffdc49acbf6d978191c202f712c7bf28 Mon Sep 17 00:00:00 2001 From: lstahlman Date: Tue, 9 Aug 2016 16:24:17 -0700 Subject: [PATCH 44/60] Add committer information to webhook payload. (#40) --- repo_hook.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/repo_hook.go b/repo_hook.go index 47fa13e..ec5c385 100644 --- a/repo_hook.go +++ b/repo_hook.go @@ -80,6 +80,12 @@ type PayloadAuthor struct { UserName string `json:"username"` } +type PayloadCommitter struct { + Name string `json:"name"` + Email string `json:"email"` + UserName string `json:"username"` +} + type PayloadUser struct { UserName string `json:"login"` ID int64 `json:"id"` @@ -87,10 +93,11 @@ type PayloadUser struct { } type PayloadCommit struct { - ID string `json:"id"` - Message string `json:"message"` - URL string `json:"url"` - Author *PayloadAuthor `json:"author"` + ID string `json:"id"` + Message string `json:"message"` + URL string `json:"url"` + Author *PayloadAuthor `json:"author"` + Committer *PayloadCommitter `json:"committer"` } type PayloadRepo struct { From d1020b4da5474f7533f5b11084dcfd5536cf2e71 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 9 Aug 2016 18:26:35 -0700 Subject: [PATCH 45/60] Add Timestamp to PayloadCommit --- gogs.go | 2 +- repo_hook.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/gogs.go b/gogs.go index 0b19afa..c34a7ed 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.10.2" + return "0.10.3" } // Client represents a Gogs API client. diff --git a/repo_hook.go b/repo_hook.go index ec5c385..2be59e1 100644 --- a/repo_hook.go +++ b/repo_hook.go @@ -98,6 +98,7 @@ type PayloadCommit struct { URL string `json:"url"` Author *PayloadAuthor `json:"author"` Committer *PayloadCommitter `json:"committer"` + Timestamp time.Time `json:"timestamp"` } type PayloadRepo struct { From f286f035269992bd38f397a9d080a8550baac907 Mon Sep 17 00:00:00 2001 From: Robin Lambertz Date: Thu, 11 Aug 2016 18:07:42 +0200 Subject: [PATCH 46/60] Add AddCollaborator API Endpoint (#24) --- repo_collaborator.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 repo_collaborator.go diff --git a/repo_collaborator.go b/repo_collaborator.go new file mode 100644 index 0000000..ba1d0c0 --- /dev/null +++ b/repo_collaborator.go @@ -0,0 +1,24 @@ +// 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 ( + "encoding/json" + "bytes" + "fmt" +) + +type AddCollaboratorOption struct { + Permission *string `json:"permission"` +} + +func (c *Client) AddCollaborator(user, repo, collaborator string, opt AddCollaboratorOption) error { + body, err := json.Marshal(&opt) + if err != nil { + return err + } + _, err = c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/collaborators/%s", user, repo, collaborator), nil, bytes.NewReader(body)) + return err +} From 1fef67c1910680405ebb3a2f1aecce35e1575b4d Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 11 Aug 2016 11:34:16 -0700 Subject: [PATCH 47/60] gofmt --- gogs.go | 2 +- repo_collaborator.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gogs.go b/gogs.go index c34a7ed..1626441 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.10.3" + return "0.10.4" } // Client represents a Gogs API client. diff --git a/repo_collaborator.go b/repo_collaborator.go index ba1d0c0..2a0052d 100644 --- a/repo_collaborator.go +++ b/repo_collaborator.go @@ -5,8 +5,8 @@ package gogs import ( - "encoding/json" "bytes" + "encoding/json" "fmt" ) From 87e433464c1f5e98e7208123c15b78119776fb83 Mon Sep 17 00:00:00 2001 From: Robin Lambertz Date: Thu, 11 Aug 2016 20:46:49 +0200 Subject: [PATCH 48/60] Add optional MaxRepoCreation field to EditUserOption (#22) --- admin_user.go | 1 + 1 file changed, 1 insertion(+) diff --git a/admin_user.go b/admin_user.go index d235a57..459031d 100644 --- a/admin_user.go +++ b/admin_user.go @@ -41,6 +41,7 @@ type EditUserOption struct { Admin *bool `json:"admin"` AllowGitHook *bool `json:"allow_git_hook"` AllowImportLocal *bool `json:"allow_import_local"` + MaxRepoCreation *int `json:"max_repo_creation"` } func (c *Client) AdminEditUser(user string, opt EditUserOption) error { From 5799eb65e0d83e14d548c8d82d50e779d9a47b46 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 14 Aug 2016 03:32:53 -0700 Subject: [PATCH 49/60] Add pull request --- gogs.go | 2 +- issue.go | 32 +++++++++++++-------- issue_milestone.go | 2 +- pull.go | 30 ++++++++++++++++++++ repo.go | 35 ++++++++++++----------- repo_hook.go | 69 +++++++++++++++++++++++++++++++++++++++------- 6 files changed, 130 insertions(+), 40 deletions(-) create mode 100644 pull.go diff --git a/gogs.go b/gogs.go index 1626441..3a9af8e 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.10.4" + return "0.11.0" } // Client represents a Gogs API client. diff --git a/issue.go b/issue.go index da0ca83..bd00755 100644 --- a/issue.go +++ b/issue.go @@ -11,25 +11,33 @@ import ( "time" ) +type StateType string + +const ( + STATE_OPEN StateType = "open" + STATE_CLOSED StateType = "closed" +) + 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"` + ID int64 `json:"id"` + Index int64 `json:"number"` + State StateType `json:"state"` + Title string `json:"title"` + Body string `json:"body"` + User *User `json:"user"` + Labels []*Label `json:"labels"` + Milestone *Milestone `json:"milestone"` + Assignee *User `json:"assignee"` + Comments int `json:"comments"` + Created time.Time `json:"created_at"` + Updated time.Time `json:"updated_at"` + PullRequest *PullRequestMeta `json:"pull_request"` - Created time.Time `json:"created_at"` - Updated time.Time `json:"updated_at"` } type ListIssueOption struct { diff --git a/issue_milestone.go b/issue_milestone.go index c6c0704..266583e 100644 --- a/issue_milestone.go +++ b/issue_milestone.go @@ -8,7 +8,7 @@ import "time" type Milestone struct { ID int64 `json:"id"` - State string `json:"state"` + State StateType `json:"state"` Title string `json:"title"` Description string `json:"description"` OpenIssues int `json:"open_issues"` diff --git a/pull.go b/pull.go new file mode 100644 index 0000000..d4a5405 --- /dev/null +++ b/pull.go @@ -0,0 +1,30 @@ +// 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" +) + +// PullRequest represents a pull reqesut API object. +type PullRequest struct { + // Copied from issue.go + ID int64 `json:"id"` + Index int64 `json:"number"` + State StateType `json:"state"` + Title string `json:"title"` + Body string `json:"body"` + User *User `json:"user"` + Labels []*Label `json:"labels"` + Milestone *Milestone `json:"milestone"` + Assignee *User `json:"assignee"` + Comments int `json:"comments"` + + Mergeable *bool `json:"mergeable"` + HasMerged bool `json:"merged"` + Merged *time.Time `json:"merged_at"` + MergedCommitID *string `json:"merge_commit_sha"` + MergedBy *User `json:"merged_by"` +} diff --git a/repo.go b/repo.go index 4f48592..280eaba 100644 --- a/repo.go +++ b/repo.go @@ -20,22 +20,25 @@ type Permission struct { // Repository represents a API repository. type Repository struct { - ID int64 `json:"id"` - Owner *User `json:"owner"` - FullName string `json:"full_name"` - Description string `json:"description"` - Private bool `json:"private"` - Fork bool `json:"fork"` - HtmlUrl string `json:"html_url"` - CloneUrl string `json:"clone_url"` - SshUrl string `json:"ssh_url"` - Stars int `json:"stars_count"` - Forks int `json:"forks_count"` - Watchers int `json:"watchers_count"` - OpenIssues int `json:"open_issues_count"` - Created time.Time `json:"created_at"` - Updated time.Time `json:"updated_at"` - Permissions Permission `json:"permissions"` + ID int64 `json:"id"` + Owner *User `json:"owner"` + Name string `json:"name"` + FullName string `json:"full_name"` + Description string `json:"description"` + Private bool `json:"private"` + Fork bool `json:"fork"` + HTMLURL string `json:"html_url"` + SSHURL string `json:"ssh_url"` + CloneURL string `json:"clone_url"` + Website string `json:"website"` + Stars int `json:"stars_count"` + Forks int `json:"forks_count"` + Watchers int `json:"watchers_count"` + OpenIssues int `json:"open_issues_count"` + DefaultBranch string `json:"default_branch"` + Created time.Time `json:"created_at"` + Updated time.Time `json:"updated_at"` + Permissions *Permission `json:"permissions,omitempty"` } // ListMyRepos lists all repositories for the authenticated user that has access to. diff --git a/repo_hook.go b/repo_hook.go index 2be59e1..2743e4f 100644 --- a/repo_hook.go +++ b/repo_hook.go @@ -118,6 +118,7 @@ type PayloadRepo struct { var ( _ Payloader = &CreatePayload{} _ Payloader = &PushPayload{} + _ Payloader = &PullRequestPayload{} ) // _________ __ @@ -140,11 +141,7 @@ func (p *CreatePayload) SetSecret(secret string) { } func (p *CreatePayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(p, "", " ") - if err != nil { - return []byte{}, err - } - return data, nil + return json.MarshalIndent(p, "", " ") } // ParseCreateHook parses create event hook content. @@ -192,11 +189,7 @@ func (p *PushPayload) SetSecret(secret string) { } func (p *PushPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(p, "", " ") - if err != nil { - return []byte{}, err - } - return data, nil + return json.MarshalIndent(p, "", " ") } // ParsePushHook parses push event hook content. @@ -219,3 +212,59 @@ func ParsePushHook(raw []byte) (*PushPayload, error) { func (p *PushPayload) Branch() string { return strings.Replace(p.Ref, "refs/heads/", "", -1) } + +// .___ +// | | ______ ________ __ ____ +// | |/ ___// ___/ | \_/ __ \ +// | |\___ \ \___ \| | /\ ___/ +// |___/____ >____ >____/ \___ > +// \/ \/ \/ + +type HookIssueAction string + +const ( + HOOK_ISSUE_OPENED HookIssueAction = "opened" + HOOK_ISSUE_CLOSED HookIssueAction = "closed" + HOOK_ISSUE_REOPENED HookIssueAction = "reopened" + HOOK_ISSUE_EDITED HookIssueAction = "edited" + HOOK_ISSUE_ASSIGNED HookIssueAction = "assigned" + HOOK_ISSUE_UNASSIGNED HookIssueAction = "unassigned" + HOOK_ISSUE_LABEL_UPDATED HookIssueAction = "label_updated" + HOOK_ISSUE_LABEL_CLEARED HookIssueAction = "label_cleared" + HOOK_ISSUE_SYNCHRONIZED HookIssueAction = "synchronized" +) + +type ChangesFromPayload struct { + From string `json:"from"` +} + +type ChangesPayload struct { + Title *ChangesFromPayload `json:"title,omitempty"` + Body *ChangesFromPayload `json:"body,omitempty"` +} + +// __________ .__ .__ __________ __ +// \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_ +// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\ +// | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | | +// |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__| +// \/ \/ |__| \/ \/ + +// PullRequestPayload represents a payload information of pull request event. +type PullRequestPayload struct { + Secret string `json:"secret"` + Action HookIssueAction `json:"action"` + Index int64 `json:"number"` + Changes *ChangesPayload `json:"changes,omitempty"` + PullRequest *PullRequest `json:"pull_request"` + Repository *Repository `json:"repository"` + Sender *User `json:"sender"` +} + +func (p *PullRequestPayload) SetSecret(secret string) { + p.Secret = secret +} + +func (p *PullRequestPayload) JSONPayload() ([]byte, error) { + return json.MarshalIndent(p, "", " ") +} From e363d3ff8f70d0fe813324eedf228684af41c29c Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 14 Aug 2016 04:16:36 -0700 Subject: [PATCH 50/60] Remove unused payload types --- gogs.go | 2 +- repo_hook.go | 63 ++++++++++++++++------------------------------------ 2 files changed, 20 insertions(+), 45 deletions(-) diff --git a/gogs.go b/gogs.go index 3a9af8e..0950a1b 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.11.0" + return "0.12.0" } // Client represents a Gogs API client. diff --git a/repo_hook.go b/repo_hook.go index 2743e4f..7bec411 100644 --- a/repo_hook.go +++ b/repo_hook.go @@ -74,45 +74,20 @@ type Payloader interface { JSONPayload() ([]byte, error) } -type PayloadAuthor struct { - Name string `json:"name"` - Email string `json:"email"` - UserName string `json:"username"` -} - -type PayloadCommitter struct { - Name string `json:"name"` - Email string `json:"email"` - UserName string `json:"username"` -} - type PayloadUser struct { - UserName string `json:"login"` - ID int64 `json:"id"` - AvatarUrl string `json:"avatar_url"` + Name string `json:"name"` + Email string `json:"email"` + UserName string `json:"username"` } +// FIXME: consider use same format as API when commits API are added. type PayloadCommit struct { - ID string `json:"id"` - Message string `json:"message"` - URL string `json:"url"` - Author *PayloadAuthor `json:"author"` - Committer *PayloadCommitter `json:"committer"` - Timestamp time.Time `json:"timestamp"` -} - -type PayloadRepo struct { - ID int64 `json:"id"` - Name string `json:"name"` - URL string `json:"url"` - SSHURL string `json:"ssh_url"` - CloneURL string `json:"clone_url"` - Description string `json:"description"` - Website string `json:"website"` - Watchers int `json:"watchers"` - Owner *PayloadAuthor `json:"owner"` - Private bool `json:"private"` - DefaultBranch string `json:"default_branch"` + ID string `json:"id"` + Message string `json:"message"` + URL string `json:"url"` + Author *PayloadUser `json:"author"` + Committer *PayloadUser `json:"committer"` + Timestamp time.Time `json:"timestamp"` } var ( @@ -129,11 +104,11 @@ var ( // \/ \/ \/ \/ type CreatePayload struct { - Secret string `json:"secret"` - Ref string `json:"ref"` - RefType string `json:"ref_type"` - Repo *PayloadRepo `json:"repository"` - Sender *PayloadUser `json:"sender"` + Secret string `json:"secret"` + Ref string `json:"ref"` + RefType string `json:"ref_type"` + Repo *Repository `json:"repository"` + Sender *User `json:"sender"` } func (p *CreatePayload) SetSecret(secret string) { @@ -177,11 +152,11 @@ type PushPayload struct { Ref string `json:"ref"` Before string `json:"before"` After string `json:"after"` - CompareUrl string `json:"compare_url"` + CompareURL string `json:"compare_url"` Commits []*PayloadCommit `json:"commits"` - Repo *PayloadRepo `json:"repository"` - Pusher *PayloadAuthor `json:"pusher"` - Sender *PayloadUser `json:"sender"` + Repo *Repository `json:"repository"` + Pusher *User `json:"pusher"` + Sender *User `json:"sender"` } func (p *PushPayload) SetSecret(secret string) { From 51c4df8c350b32f095c8eb236aae2e306025eead Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 16 Aug 2016 10:16:51 -0700 Subject: [PATCH 51/60] Rename Issue.User -> Issue.Poster --- gogs.go | 2 +- issue.go | 4 ++-- pull.go | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/gogs.go b/gogs.go index 0950a1b..bf8e847 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.12.0" + return "0.12.1" } // Client represents a Gogs API client. diff --git a/issue.go b/issue.go index bd00755..0a5e8ce 100644 --- a/issue.go +++ b/issue.go @@ -26,13 +26,13 @@ type PullRequestMeta struct { type Issue struct { ID int64 `json:"id"` Index int64 `json:"number"` - State StateType `json:"state"` + Poster *User `json:"user"` Title string `json:"title"` Body string `json:"body"` - User *User `json:"user"` Labels []*Label `json:"labels"` Milestone *Milestone `json:"milestone"` Assignee *User `json:"assignee"` + State StateType `json:"state"` Comments int `json:"comments"` Created time.Time `json:"created_at"` Updated time.Time `json:"updated_at"` diff --git a/pull.go b/pull.go index d4a5405..6e1ae6e 100644 --- a/pull.go +++ b/pull.go @@ -13,15 +13,17 @@ type PullRequest struct { // Copied from issue.go ID int64 `json:"id"` Index int64 `json:"number"` - State StateType `json:"state"` + Poster *User `json:"user"` Title string `json:"title"` Body string `json:"body"` - User *User `json:"user"` Labels []*Label `json:"labels"` Milestone *Milestone `json:"milestone"` Assignee *User `json:"assignee"` + State StateType `json:"state"` Comments int `json:"comments"` + HTMLURL string `json:"html_url"` + Mergeable *bool `json:"mergeable"` HasMerged bool `json:"merged"` Merged *time.Time `json:"merged_at"` From 5e50f0292565471b41b3c73fcadcb886140f0082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Carlb=C3=A4cker?= Date: Fri, 19 Aug 2016 17:13:44 +0200 Subject: [PATCH 52/60] Add Status-flag to EditIssueOption (#41) * EditIssueOption takes Status-flag * json-element renamed 'state' to reflect GitHub-API * Renamed Status=>State for EditIssueOption --- issue.go | 1 + 1 file changed, 1 insertion(+) diff --git a/issue.go b/issue.go index 0a5e8ce..7bb50df 100644 --- a/issue.go +++ b/issue.go @@ -78,6 +78,7 @@ type EditIssueOption struct { Body *string `json:"body"` Assignee *string `json:"assignee"` Milestone *int64 `json:"milestone"` + State *string `json:"state"` } func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, error) { From 82d9ef436fefbdecee20f787815b0724e74a4f6b Mon Sep 17 00:00:00 2001 From: lstahlman Date: Wed, 24 Aug 2016 14:30:53 -0700 Subject: [PATCH 53/60] Additional API support for milestones (#39) --- issue_milestone.go | 88 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/issue_milestone.go b/issue_milestone.go index 266583e..3451886 100644 --- a/issue_milestone.go +++ b/issue_milestone.go @@ -4,7 +4,12 @@ package gogs -import "time" +import ( + "bytes" + "encoding/json" + "fmt" + "time" +) type Milestone struct { ID int64 `json:"id"` @@ -16,3 +21,84 @@ type Milestone struct { Closed *time.Time `json:"closed_at"` Deadline *time.Time `json:"due_on"` } + +type CreateMilestoneOption struct { + Title string `json:"title"` + Description string `json:"description"` + Deadline *time.Time `json:"due_on"` +} + +type EditMilestoneOption struct { + Title string `json:"title"` + Description string `json:"description"` + Deadline *time.Time `json:"due_on"` +} + +type SetIssueMilestoneOption struct { + ID int64 `json:"id"` +} + +func (c *Client) ListRepoMilestones(owner, repo string) ([]*Milestone, error) { + milestones := make([]*Milestone, 0, 10) + return milestones, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), nil, nil, &milestones) +} + +func (c *Client) GetRepoMilestone(owner, repo string, id int64) (*Milestone, error) { + milestone := new(Milestone) + return milestone, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil, milestone) +} + +func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption) (*Milestone, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + milestone := new(Milestone) + return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), + jsonHeader, bytes.NewReader(body), milestone) +} + +func (c *Client) EditMilestone(owner, repo string, id int64, opt EditMilestoneOption) (*Milestone, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + milestone := new(Milestone) + return milestone, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), milestone) +} + +func (c *Client) DeleteMilestone(owner, repo string, id int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil) + return err +} + +func (c *Client) ChangeMilestoneStatus(owner, repo string, id int64, open bool) (*Milestone, error) { + var action string + if open { + action = "open" + } else { + action = "close" + } + + milestone := new(Milestone) + return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones/%d/%s", owner, repo, id, action), nil, nil, milestone) +} + +func (c *Client) GetIssueMilestone(owner, repo string, index int64) (*Milestone, error) { + milestone := new(Milestone) + return milestone, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/milestone", owner, repo, index), nil, nil, &milestone) +} + +func (c *Client) SetIssueMilestone(owner, repo string, index int64, opt SetIssueMilestoneOption) (*Milestone, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + milestone := new(Milestone) + return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/milestone", owner, repo, index), jsonHeader, bytes.NewReader(body), &milestone) +} + +func (c *Client) DeleteIssueMilestone(owner, repo string, index int64) error { + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/milestone", owner, repo, index), nil, nil) + return err +} From cdfad4c78ca6a880476a8ce11db1364822959348 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 24 Aug 2016 14:36:36 -0700 Subject: [PATCH 54/60] #39 code clean up --- gogs.go | 2 +- issue_milestone.go | 73 ++++++++++++---------------------------------- 2 files changed, 20 insertions(+), 55 deletions(-) diff --git a/gogs.go b/gogs.go index bf8e847..8939918 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.12.1" + return "0.12.2" } // Client represents a Gogs API client. diff --git a/issue_milestone.go b/issue_milestone.go index 3451886..365cce6 100644 --- a/issue_milestone.go +++ b/issue_milestone.go @@ -13,49 +13,45 @@ import ( type Milestone struct { ID int64 `json:"id"` - State StateType `json:"state"` Title string `json:"title"` Description string `json:"description"` + State StateType `json:"state"` OpenIssues int `json:"open_issues"` ClosedIssues int `json:"closed_issues"` Closed *time.Time `json:"closed_at"` Deadline *time.Time `json:"due_on"` } +func (c *Client) ListRepoMilestones(owner, repo string) ([]*Milestone, error) { + milestones := make([]*Milestone, 0, 10) + return milestones, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), nil, nil, &milestones) +} + +func (c *Client) GetMilestone(owner, repo string, id int64) (*Milestone, error) { + milestone := new(Milestone) + return milestone, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil, milestone) +} + type CreateMilestoneOption struct { Title string `json:"title"` Description string `json:"description"` Deadline *time.Time `json:"due_on"` } -type EditMilestoneOption struct { - Title string `json:"title"` - Description string `json:"description"` - Deadline *time.Time `json:"due_on"` -} - -type SetIssueMilestoneOption struct { - ID int64 `json:"id"` -} - -func (c *Client) ListRepoMilestones(owner, repo string) ([]*Milestone, error) { - milestones := make([]*Milestone, 0, 10) - return milestones, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), nil, nil, &milestones) -} - -func (c *Client) GetRepoMilestone(owner, repo string, id int64) (*Milestone, error) { - milestone := new(Milestone) - return milestone, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil, milestone) -} - func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption) (*Milestone, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err } milestone := new(Milestone) - return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), - jsonHeader, bytes.NewReader(body), milestone) + return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), jsonHeader, bytes.NewReader(body), milestone) +} + +type EditMilestoneOption struct { + Title *string `json:"title"` + Description *string `json:"description"` + State *string `json:"state"` + Deadline *time.Time `json:"due_on"` } func (c *Client) EditMilestone(owner, repo string, id int64, opt EditMilestoneOption) (*Milestone, error) { @@ -71,34 +67,3 @@ func (c *Client) DeleteMilestone(owner, repo string, id int64) error { _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil) return err } - -func (c *Client) ChangeMilestoneStatus(owner, repo string, id int64, open bool) (*Milestone, error) { - var action string - if open { - action = "open" - } else { - action = "close" - } - - milestone := new(Milestone) - return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones/%d/%s", owner, repo, id, action), nil, nil, milestone) -} - -func (c *Client) GetIssueMilestone(owner, repo string, index int64) (*Milestone, error) { - milestone := new(Milestone) - return milestone, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/milestone", owner, repo, index), nil, nil, &milestone) -} - -func (c *Client) SetIssueMilestone(owner, repo string, index int64, opt SetIssueMilestoneOption) (*Milestone, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } - milestone := new(Milestone) - return milestone, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/milestone", owner, repo, index), jsonHeader, bytes.NewReader(body), &milestone) -} - -func (c *Client) DeleteIssueMilestone(owner, repo string, index int64) error { - _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/milestone", owner, repo, index), nil, nil) - return err -} From 2ffd4704c6f37d7fb10110450fe035fa6df08db8 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 24 Aug 2016 15:52:02 -0700 Subject: [PATCH 55/60] Change EditMilestoneOption.Title to non-pointer --- issue_milestone.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/issue_milestone.go b/issue_milestone.go index 365cce6..ad27a15 100644 --- a/issue_milestone.go +++ b/issue_milestone.go @@ -48,7 +48,7 @@ func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption) } type EditMilestoneOption struct { - Title *string `json:"title"` + Title string `json:"title"` Description *string `json:"description"` State *string `json:"state"` Deadline *time.Time `json:"due_on"` From 3d69f7ccdc33f3c54d296492e0c090c817314fc8 Mon Sep 17 00:00:00 2001 From: Iwan Budi Kusnanto Date: Thu, 25 Aug 2016 10:49:11 +0700 Subject: [PATCH 56/60] issues comment API client (#26) --- issue_comment.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 issue_comment.go diff --git a/issue_comment.go b/issue_comment.go new file mode 100644 index 0000000..1b9726f --- /dev/null +++ b/issue_comment.go @@ -0,0 +1,69 @@ +package gogs + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" +) + +// CommentType is type of a comment +type CommentType int + +// Comment represents a comment in commit and issue page. +type Comment struct { + ID int64 `json:"id"` + Type CommentType `json:"type"` + Poster *User `json:"poster"` + IssueID int64 `json:"issue_id"` + CommitID int64 `json:"commit_id"` + Line int64 `json:"line"` + Content string `json:"content"` + + Created time.Time `json:"created"` + CreatedUnix int64 `json:"created_unix"` + + // Reference issue in commit message + CommitSHA string `json:"commit_sha"` + + //Attachments []*Attachment `json:"attachments"` +} + +// ListRepoIssueComments list comments on an issue +func (c *Client) ListRepoIssueComments(owner, repo string, issueID int64) ([]*Comment, error) { + comments := make([]*Comment, 0, 10) + return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, issueID), nil, nil, &comments) +} + +// CreateIssueCommentOption is option when creating an issue comment +type CreateIssueCommentOption struct { + Content string `json:"content" binding:"required"` +} + +// CreateIssueComment create comment on an issue +func (c *Client) CreateIssueComment(owner, repo string, issueID int64, opt CreateIssueCommentOption) (*Comment, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + comment := new(Comment) + return comment, c.getParsedResponse("POST", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments", owner, repo, issueID), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), comment) +} + +// EditIssueCommentOption is option when editing an issue comment +type EditIssueCommentOption struct { + Content string `json:"content" binding:"required"` +} + +// EditIssueComment edits an issue comment +func (c *Client) EditIssueComment(owner, repo string, issueID, commentID int64, opt EditIssueCommentOption) (*Comment, error) { + body, err := json.Marshal(&opt) + if err != nil { + return nil, err + } + comment := new(Comment) + return comment, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments/%d", owner, repo, issueID, commentID), + http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), comment) +} From c52f7ee0cc58d3cd6e379025552873a8df6de322 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 24 Aug 2016 20:58:09 -0700 Subject: [PATCH 57/60] #26 code cleanup --- gogs.go | 2 +- issue_comment.go | 56 +++++++++++++++++++----------------------------- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/gogs.go b/gogs.go index 8939918..d27996e 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.12.2" + return "0.12.3" } // Client represents a Gogs API client. diff --git a/issue_comment.go b/issue_comment.go index 1b9726f..c766f55 100644 --- a/issue_comment.go +++ b/issue_comment.go @@ -1,69 +1,57 @@ +// 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" ) -// CommentType is type of a comment -type CommentType int - // Comment represents a comment in commit and issue page. type Comment struct { - ID int64 `json:"id"` - Type CommentType `json:"type"` - Poster *User `json:"poster"` - IssueID int64 `json:"issue_id"` - CommitID int64 `json:"commit_id"` - Line int64 `json:"line"` - Content string `json:"content"` - - Created time.Time `json:"created"` - CreatedUnix int64 `json:"created_unix"` - - // Reference issue in commit message - CommitSHA string `json:"commit_sha"` - - //Attachments []*Attachment `json:"attachments"` + ID int64 `json:"id"` + Poster *User `json:"user"` + Body string `json:"body"` + Created time.Time `json:"created_at"` + Updated time.Time `json:"updated_at"` } -// ListRepoIssueComments list comments on an issue -func (c *Client) ListRepoIssueComments(owner, repo string, issueID int64) ([]*Comment, error) { +// ListIssueComments list comments on an issue. +func (c *Client) ListIssueComments(owner, repo string, index int64) ([]*Comment, error) { comments := make([]*Comment, 0, 10) - return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, issueID), nil, nil, &comments) + return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), nil, nil, &comments) } -// CreateIssueCommentOption is option when creating an issue comment +// CreateIssueCommentOption is option when creating an issue comment. type CreateIssueCommentOption struct { - Content string `json:"content" binding:"required"` + Body string `json:"body" binding:"Required"` } -// CreateIssueComment create comment on an issue -func (c *Client) CreateIssueComment(owner, repo string, issueID int64, opt CreateIssueCommentOption) (*Comment, error) { +// CreateIssueComment create comment on an issue. +func (c *Client) CreateIssueComment(owner, repo string, index int64, opt CreateIssueCommentOption) (*Comment, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err } comment := new(Comment) - return comment, c.getParsedResponse("POST", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments", owner, repo, issueID), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), comment) + return comment, c.getParsedResponse("POST", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments", owner, repo, index), jsonHeader, bytes.NewReader(body), comment) } -// EditIssueCommentOption is option when editing an issue comment +// EditIssueCommentOption is option when editing an issue comment. type EditIssueCommentOption struct { - Content string `json:"content" binding:"required"` + Body string `json:"body" binding:"Required"` } -// EditIssueComment edits an issue comment -func (c *Client) EditIssueComment(owner, repo string, issueID, commentID int64, opt EditIssueCommentOption) (*Comment, error) { +// EditIssueComment edits an issue comment. +func (c *Client) EditIssueComment(owner, repo string, index, commentID int64, opt EditIssueCommentOption) (*Comment, error) { body, err := json.Marshal(&opt) if err != nil { return nil, err } comment := new(Comment) - return comment, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments/%d", owner, repo, issueID, commentID), - http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), comment) + return comment, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/:%s/:%s/issues/%d/comments/%d", owner, repo, index, commentID), jsonHeader, bytes.NewReader(body), comment) } From c317bcf8d1f8c0a2f85f09a21c69db6222c29858 Mon Sep 17 00:00:00 2001 From: Iwan Budi Kusnanto Date: Sat, 27 Aug 2016 00:52:08 +0700 Subject: [PATCH 58/60] Split Client.getResponse to Client.doRequest & Client.getResponse. (#44) There is some API that give empty response and we only need to check HTTP status code. example : https://developer.github.com/v3/issues/assignees/#check-assignee But there is no way to do this in current code because getResponse will parse the response body and not returning HTTP status code. Client.doRequest makes it possible to do this. --- gogs.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gogs.go b/gogs.go index d27996e..389439b 100644 --- a/gogs.go +++ b/gogs.go @@ -38,7 +38,7 @@ func (c *Client) SetHTTPClient(client *http.Client) { c.client = client } -func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) { +func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*http.Response, error) { req, err := http.NewRequest(method, c.url+"/api/v1"+path, body) if err != nil { return nil, err @@ -48,7 +48,11 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re req.Header[k] = v } - resp, err := c.client.Do(req) + return c.client.Do(req) +} + +func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) { + resp, err := c.doRequest(method, path, header, body) if err != nil { return nil, err } From 7bea055c4a353c98a948453ca969e4d37bec89a1 Mon Sep 17 00:00:00 2001 From: Kurt Madel Date: Sun, 28 Aug 2016 08:35:23 -0400 Subject: [PATCH 59/60] API support to list repos by org and user (#45) --- repo.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/repo.go b/repo.go index 280eaba..f3b393c 100644 --- a/repo.go +++ b/repo.go @@ -47,6 +47,16 @@ func (c *Client) ListMyRepos() ([]*Repository, error) { return repos, c.getParsedResponse("GET", "/user/repos", nil, nil, &repos) } +func (c *Client) ListUserRepos(user string) ([]*Repository, error) { + repos := make([]*Repository, 0, 10) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos", user), nil, nil, &repos) +} + +func (c *Client) ListOrgRepos(org string) ([]*Repository, error) { + repos := make([]*Repository, 0, 10) + return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos) +} + type CreateRepoOption struct { Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"` Description string `json:"description" binding:"MaxSize(255)"` From d8aff570fa22d4e38954c753ec8b21862239b31d Mon Sep 17 00:00:00 2001 From: Kurt Madel Date: Tue, 30 Aug 2016 08:34:33 -0400 Subject: [PATCH 60/60] Pull Request hook branch/head info (#47) * added additional branch/repo info to PR webhook * added additional branch/repo info to PR webhook --- pull.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pull.go b/pull.go index 6e1ae6e..be93b26 100644 --- a/pull.go +++ b/pull.go @@ -22,6 +22,11 @@ type PullRequest struct { State StateType `json:"state"` Comments int `json:"comments"` + HeadBranch string `json:"head_branch"` + HeadRepo *Repository `json:"head_repo"` + BaseBranch string `json:"base_branch"` + BaseRepo *Repository `json:"base_repo"` + HTMLURL string `json:"html_url"` Mergeable *bool `json:"mergeable"`