Files
mpbot-github/pr/githubapi/pull_request.go
Zero King bc693775f6 Make dbname configurable
Read repo location from webhook event
2017-07-16 10:17:45 +00:00

83 lines
1.7 KiB
Go

package githubapi
import (
"context"
"regexp"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type Client struct {
*github.Client
ctx context.Context
}
func NewClient(botSecret string) *Client {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: botSecret},
)
tc := oauth2.NewClient(ctx, ts)
return &Client{
Client: github.NewClient(tc),
ctx: ctx,
}
}
func (client *Client) ListChangedPorts(number int) ([]string, error) {
files, _, err := client.PullRequests.ListFiles(context.Background(), "macports-staging", "macports-ports", number, nil)
if err != nil {
return nil, err
}
ports := make([]string, 0, 1)
portfileRegexp := regexp.MustCompile(`[^\._/][^/]*/([^/]+)/Portfile`)
for _, file := range files {
if match := portfileRegexp.FindStringSubmatch(*file.Filename); match != nil {
ports = append(ports, match[1])
}
}
return ports, nil
}
func (client *Client) CreateComment(owner, repo string, number int, body *string) error {
_, _, err := client.Issues.CreateComment(
client.ctx,
owner,
repo,
number,
&github.IssueComment{Body: body},
)
return err
}
func (client *Client) ReplaceLabels(owner, repo string, number int, labels []string) error {
_, _, err := client.Issues.ReplaceLabelsForIssue(
client.ctx,
owner,
repo,
number,
labels,
)
return err
}
func (client *Client) ListLabels(owner, repo string, number int) ([]string, error) {
labels, _, err := client.Issues.ListLabelsByIssue(
client.ctx,
owner,
repo,
number,
nil,
)
if err != nil {
return nil, err
}
labelNames := make([]string, 0, 1)
for _, label := range labels {
labelNames = append(labelNames, *label.Name)
}
return labelNames, nil
}