-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
61 lines (48 loc) · 1.44 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/stargazer39/github-utility/utils"
)
func list(username string, ctx *GitHubContext) (*[]GithubRepo, string, error) {
var repos []GithubRepo
req, err := ctx.Client.
R().
SetHeader("Authorization", fmt.Sprintf("token %s", ctx.Token)).
SetResult(&repos).
Get(fmt.Sprintf("https://api.github.com/users/%s/repos", username))
if err != nil {
return nil, "", err
}
if req.StatusCode() != http.StatusOK {
return nil, utils.PrettyPrintJSONBytes(req.Body()), &GithubError{
message: "Error with github",
code: req.StatusCode(),
}
}
return &repos, utils.PrettyPrintJSONBytes(req.Body()), nil
}
func transfer(to string, repo GithubRepo, ctx *GitHubContext) (error, string) {
bytes, _ := json.Marshal(&NewOwner{NewOwner: to})
req, err := ctx.Client.
R().
SetHeader("Authorization", fmt.Sprintf("token %s", ctx.Token)).
SetBody(bytes).
Post(fmt.Sprintf("https://api.github.com/repos/%s/%s/transfer", repo.Owner.Login, repo.Name))
if err != nil {
return err, ""
}
if req.StatusCode() != http.StatusAccepted {
return &GithubError{
message: "Error with github " + req.Status(),
code: req.StatusCode(),
}, utils.PrettyPrintJSONBytes(req.Body())
}
return nil, utils.PrettyPrintJSONBytes(req.Body())
}
func displayRepos(repos []GithubRepo) {
for i, r := range repos {
fmt.Printf("%d. %s\t%s\t%s\n", i+1, r.FullName, r.Visibility, r.Owner.Login)
}
}