1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/github/github_repo.go
2018-04-19 16:10:24 -07:00

69 lines
1.3 KiB
Go

package github
import (
"context"
"net/http"
"os"
ghb "github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type GithubRepo struct {
apiKey string
Name string
Owner string
PullRequests []*ghb.PullRequest
}
func NewGithubRepo(name, owner string) *GithubRepo {
repo := GithubRepo{
apiKey: os.Getenv("WTF_GITHUB_TOKEN"),
Name: name,
Owner: owner,
}
repo.PullRequests, _ = repo.pullRequests()
return &repo
}
func (repo *GithubRepo) pullRequests() ([]*ghb.PullRequest, error) {
oauthClient := repo.oauthClient()
github := ghb.NewClient(oauthClient)
opts := &ghb.PullRequestListOptions{}
prs, _, err := github.PullRequests.List(context.Background(), repo.Owner, repo.Name, opts)
if err != nil {
return nil, err
}
return prs, nil
}
func (repo *GithubRepo) Repository() (*ghb.Repository, error) {
oauthClient := repo.oauthClient()
github := ghb.NewClient(oauthClient)
repository, _, err := github.Repositories.Get(context.Background(), repo.Owner, repo.Name)
if err != nil {
return nil, err
}
return repository, nil
}
/* -------------------- Unexported Functions -------------------- */
func (repo *GithubRepo) oauthClient() *http.Client {
tokenService := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: repo.apiKey},
)
return oauth2.NewClient(context.Background(), tokenService)
}