-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
59 lines (50 loc) · 1.45 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
// Package github is http client for GitHub API.
// This package is wrapper for google/go-github package.
package github
import (
"context"
"fmt"
"github.com/google/go-github/v50/github"
"github.com/nao1215/leadtime/domain/model"
"golang.org/x/oauth2"
)
// Client is http client for GitHub API.
type Client struct {
// client is http client
client *github.Client
}
// NewClient return http client for GitHub API.
func NewClient(token string) *Client {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
client := oauth2.NewClient(context.Background(), tokenSource)
return &Client{client: github.NewClient(client)}
}
// ListRepositories return List the repositories for a user.
func (c *Client) ListRepositories(ctx context.Context) ([]*model.Repository, error) {
repos, resp, err := c.client.Repositories.List(ctx, "", nil)
if resp != nil {
defer func() error {
if err := resp.Body.Close(); err != nil {
return fmt.Errorf("failed to close response body: %w", err)
}
return nil
}()
}
if err != nil {
return nil, &APIError{StatusCode: resp.StatusCode, Message: "failed to gey repository list"}
}
repoList := make([]*model.Repository, 0)
for _, v := range repos {
repo := &model.Repository{
ID: v.ID,
Owner: &model.User{Name: v.Owner.Name},
Name: v.Name,
FullName: v.FullName,
Description: v.Description,
}
repoList = append(repoList, repo)
}
return repoList, nil
}