Parse maintainer info

This commit is contained in:
Zero King
2017-07-13 12:36:54 +00:00
parent 6d3e032383
commit 8ff6f2dd07
3 changed files with 56 additions and 10 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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