Make dbname configurable

Read repo location from webhook event
This commit is contained in:
Zero King
2017-07-16 10:17:45 +00:00
parent 5ce138d477
commit bc693775f6
4 changed files with 25 additions and 23 deletions

View File

@@ -7,6 +7,7 @@ import (
"errors"
_ "github.com/lib/pq"
"os"
)
type Maintainer struct {
@@ -28,11 +29,11 @@ var wwwDB *sql.DB
func init() {
var err error
// TODO: use real dbname or read from env/flag
tracDB, err = sql.Open("postgres", "host=/tmp dbname=l2dy")
tracDB, err = sql.Open("postgres", "host=/tmp dbname="+os.Getenv("TRAC_DBNAME"))
if err != nil {
log.Fatal(err)
}
wwwDB, err = sql.Open("postgres", "host=/tmp dbname=l2dy")
wwwDB, err = sql.Open("postgres", "host=/tmp dbname="+os.Getenv("WWW_DBNAME"))
if err != nil {
log.Fatal(err)
}

View File

@@ -10,11 +10,10 @@ import (
type Client struct {
*github.Client
ctx context.Context
owner, repo string
ctx context.Context
}
func NewClient(botSecret, owner, repo string) *Client {
func NewClient(botSecret string) *Client {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: botSecret},
@@ -24,8 +23,6 @@ func NewClient(botSecret, owner, repo string) *Client {
return &Client{
Client: github.NewClient(tc),
ctx: ctx,
owner: owner,
repo: repo,
}
}
@@ -44,33 +41,33 @@ func (client *Client) ListChangedPorts(number int) ([]string, error) {
return ports, nil
}
func (client *Client) CreateComment(number int, body *string) error {
func (client *Client) CreateComment(owner, repo string, number int, body *string) error {
_, _, err := client.Issues.CreateComment(
client.ctx,
client.owner,
client.repo,
owner,
repo,
number,
&github.IssueComment{Body: body},
)
return err
}
func (client *Client) ReplaceLabels(number int, labels []string) error {
func (client *Client) ReplaceLabels(owner, repo string, number int, labels []string) error {
_, _, err := client.Issues.ReplaceLabelsForIssue(
client.ctx,
client.owner,
client.repo,
owner,
repo,
number,
labels,
)
return err
}
func (client *Client) ListLabels(number int) ([]string, error) {
func (client *Client) ListLabels(owner, repo string, number int) ([]string, error) {
labels, _, err := client.Issues.ListLabelsByIssue(
client.ctx,
client.owner,
client.repo,
owner,
repo,
number,
nil,
)

View File

@@ -11,9 +11,11 @@ import (
)
func (receiver *Receiver) handlePullRequest(body []byte) {
// Use &&= for isOpenmaintainer/isNomaintainer (init true) flag, isNomaintainer take precedence
// Loop over ports and aggregate related maintainers
// Use @_handle for now
defer func() {
if r := recover(); r != nil {
log.Println(r)
}
}()
event := &github.PullRequestEvent{}
err := json.Unmarshal(body, event)
@@ -22,6 +24,8 @@ func (receiver *Receiver) handlePullRequest(body []byte) {
return
}
number := *event.Number
owner := *event.Repo.Owner.Login
repo := *event.Repo.Name
isOpenmaintainer := true
isNomaintainer := true
isMaintainer := false
@@ -54,7 +58,7 @@ func (receiver *Receiver) handlePullRequest(body []byte) {
// Notify maintainers
if len(handles) > 0 {
body := "Notifying maintainers: @_" + strings.Join(handles, " @_")
err = receiver.githubClient.CreateComment(number, &body)
err = receiver.githubClient.CreateComment(owner, repo, number, &body)
if err != nil {
log.Println(err)
}
@@ -62,7 +66,7 @@ func (receiver *Receiver) handlePullRequest(body []byte) {
fallthrough
case "synchronize":
// Modify labels
labels, err := receiver.githubClient.ListLabels(number)
labels, err := receiver.githubClient.ListLabels(owner, repo, number)
newLabels := make([]string, len(labels))
copy(newLabels, labels)
if err != nil {
@@ -83,7 +87,7 @@ func (receiver *Receiver) handlePullRequest(body []byte) {
}
}
newLabels = append(newLabels, maintainerLabels...)
err = receiver.githubClient.ReplaceLabels(number, newLabels)
err = receiver.githubClient.ReplaceLabels(owner, repo, number, newLabels)
if err != nil {
log.Println(err)
}

View File

@@ -22,7 +22,7 @@ func NewReceiver(listenAddr string, hookSecret []byte, botSecret string) *Receiv
listenAddr: listenAddr,
hookSecret: hookSecret,
// TODO: canonical owner
githubClient: githubapi.NewClient(botSecret, "macports-staging", "macports-ports"),
githubClient: githubapi.NewClient(botSecret),
}
}