Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 3b1d86c

Browse files
committed
GetUserInfo, CreateAccessToken, ListAccessTokens
1 parent a5c3262 commit 3b1d86c

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

repo_hooks.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ type CreateHookOption struct {
3737
Active bool `json:"active"`
3838
}
3939

40-
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) error {
40+
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, error) {
4141
body, err := json.Marshal(&opt)
4242
if err != nil {
43-
return err
43+
return nil, err
4444
}
45-
_, err = c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
46-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
47-
return err
45+
h := new(Hook)
46+
err = c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
47+
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
48+
return h, err
4849
}
4950

5051
type EditHookOption struct {

user.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@
44

55
package gogs
66

7+
import (
8+
"fmt"
9+
)
10+
711
// User represents a API user.
812
type User struct {
913
Id int64 `json:"id"`
1014
UserName string `json:"username"`
15+
FullName string `json:"full_name"`
16+
Email string `json:"email"`
1117
AvatarUrl string `json:"avatar_url"`
1218
}
19+
20+
func (c *Client) GetUserInfo(user string) (*User, error) {
21+
u := new(User)
22+
err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s", user), nil, nil, u)
23+
return u, err
24+
}

user_app.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gogs
6+
7+
import (
8+
"bytes"
9+
"encoding/base64"
10+
"encoding/json"
11+
"fmt"
12+
"net/http"
13+
)
14+
15+
func BasicAuthEncode(user, pass string) string {
16+
return base64.StdEncoding.EncodeToString([]byte(user + ":" + pass))
17+
}
18+
19+
// AccessToken represents a API access token.
20+
type AccessToken struct {
21+
Name string `json:"name"`
22+
Sha1 string `json:"sha1"`
23+
}
24+
25+
func (c *Client) ListAccessTokens(user, pass string) ([]*AccessToken, error) {
26+
tokens := make([]*AccessToken, 0, 10)
27+
return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens", user),
28+
http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}}, nil, &tokens)
29+
}
30+
31+
type CreateAccessTokenOption struct {
32+
Name string `json:"name"`
33+
}
34+
35+
func (c *Client) CreateAccessToken(user, pass string, opt CreateAccessTokenOption) (*AccessToken, error) {
36+
body, err := json.Marshal(&opt)
37+
if err != nil {
38+
return nil, err
39+
}
40+
t := new(AccessToken)
41+
return t, c.getParsedResponse("POST", fmt.Sprintf("/users/%s/tokens", user),
42+
http.Header{
43+
"content-type": []string{"application/json"},
44+
"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}},
45+
bytes.NewReader(body), t)
46+
}

0 commit comments

Comments
 (0)