diff --git a/pr/dbutil.go b/pr/dbutil.go index b8288b5..749a27c 100644 --- a/pr/dbutil.go +++ b/pr/dbutil.go @@ -3,15 +3,21 @@ package pr import ( "database/sql" "log" + "strings" _ "github.com/lib/pq" ) type Maintainer struct { - primary string - others []string - openMaintainer bool - noMaintainer bool + GithubHandle string + Email string +} + +type PortMaintainer struct { + Primary *Maintainer + Others []*Maintainer + OpenMaintainer bool + NoMaintainer bool } var tracDB *sql.DB @@ -20,6 +26,7 @@ var wwwDB *sql.DB // Create connections to DBs func init() { var err error + // TODO: use real dbname or read from env/flag tracDB, err = sql.Open("postgres", "host=/tmp dbname=l2dy") if err != nil { log.Fatal(err) @@ -47,8 +54,7 @@ func GetGitHubHandle(email string) (string, error) { // GetMaintainer returns the maintainers of a port, // the primary maintainer is always the first in the slice. -// TODO: parse multi identity per maintainer and email -func GetMaintainer(port string) (*Maintainer, error) { +func GetMaintainer(port string) (*PortMaintainer, error) { rows, err := wwwDB.Query("SELECT maintainer, is_primary "+ "FROM public.maintainers "+ "WHERE portfile = $1", port) @@ -57,7 +63,7 @@ func GetMaintainer(port string) (*Maintainer, error) { } defer rows.Close() - maintainer := new(Maintainer) + maintainer := new(PortMaintainer) maintainerCursor := "" isPrimary := false @@ -66,11 +72,27 @@ func GetMaintainer(port string) (*Maintainer, error) { return nil, err } if isPrimary { - maintainer.primary = maintainerCursor + maintainer.Primary = parseMaintainer(maintainerCursor) } else { - maintainer.others = append(maintainer.others, maintainerCursor) + maintainer.Others = append(maintainer.Others, parseMaintainer(maintainerCursor)) } } return maintainer, nil } + +func parseMaintainer(maintainerFullString string) *Maintainer { + maintainerStrings := strings.Split(maintainerFullString, " ") + maintainer := new(Maintainer) + for _, maintainerString := range maintainerStrings { + if strings.HasPrefix(maintainerString, "@") { + maintainer.GithubHandle = maintainerString[1:] + } else if strings.Count(maintainerString, ":") == 1 { + emailParts := strings.Split(maintainerString, ":") + maintainer.Email = emailParts[1] + "@" + emailParts[0] + } else { + maintainer.Email = maintainerString + "@macports.org" + } + } + return maintainer +} diff --git a/pr/githubapi/pull_request.go b/pr/githubapi/pull_request.go new file mode 100644 index 0000000..c984131 --- /dev/null +++ b/pr/githubapi/pull_request.go @@ -0,0 +1,23 @@ +package githubapi + +import ( + "context" + "github.com/google/go-github/github" + "regexp" +) + +func ListChangedPorts(number int) []string { + client := github.NewClient(nil) + files, _, err := client.PullRequests.ListFiles(context.Background(), "macports-staging", "macports-ports", number, nil) + if err != nil { + return nil + } + 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 +} diff --git a/pr/prbot/main.go b/pr/prbot/main.go index 5ca0dc4..711d49c 100644 --- a/pr/prbot/main.go +++ b/pr/prbot/main.go @@ -2,9 +2,10 @@ package main import ( "flag" - "github.com/macports/mpbot-github/pr/webhook" "log" "os" + + "github.com/macports/mpbot-github/pr/webhook" ) // Entry point of the PR bot