You've already forked mpbot-github
mirror of
https://github.com/macports/mpbot-github.git
synced 2026-03-31 14:46:03 -07:00
83 lines
1.7 KiB
Go
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) ListChangedPortsAndLines(number int) (ports []string, changes []int, err error) {
|
|
files, _, err := client.PullRequests.ListFiles(context.Background(), "macports-staging", "macports-ports", number, nil)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
portfileRegexp := regexp.MustCompile(`[^\._/][^/]*/([^/]+)/Portfile`)
|
|
for _, file := range files {
|
|
if match := portfileRegexp.FindStringSubmatch(*file.Filename); match != nil {
|
|
ports = append(ports, match[1])
|
|
changes = append(changes, *file.Changes)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|