// Copyright 2016 Documize Inc. . All rights reserved. // // This software (Documize Community Edition) is licensed under // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html // // You can operate outside the AGPL restrictions by purchasing // Documize Enterprise Edition and obtaining a commercial license // by contacting . // // https://documize.com package github import ( "sort" "time" "github.com/documize/community/core/log" gogithub "github.com/google/go-github/github" ) const ( tagPullRequestData = "pullRequestData" //pullRequestTimeFormat = "January 2 2006" ) type githubPullRequest struct { Repo string `json:"repo"` Name string `json:"name"` URL string `json:"url"` IsOpen bool `json:"isopen"` UpdatedAt string `json:"updatedAt"` } // sort pull requests in order that that should be presented - by date updated, closed first. type prToSort []githubPullRequest func (s prToSort) Len() int { return len(s) } func (s prToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s prToSort) Less(i, j int) bool { if !s[i].IsOpen && s[j].IsOpen { return true } if s[i].IsOpen && !s[j].IsOpen { return false } // TODO this seems a very slow approach iDate, iErr := time.Parse(milestonesTimeFormat, s[i].UpdatedAt) log.IfErr(iErr) jDate, jErr := time.Parse(milestonesTimeFormat, s[j].UpdatedAt) log.IfErr(jErr) return iDate.Before(jDate) } func init() { reports[tagPullRequestData] = report{refreshPullReqs, renderPullReqs, `

Pull Requests

During the period since {{.Config.Since}}{{.Config.DateMessage}}, {{.ClosedPRs}} pull requests were closed, while {{.OpenPRs}} remain open.

`} } func getPullReqs(client *gogithub.Client, config *githubConfig) ([]githubPullRequest, error) { ret := []githubPullRequest{} hadRepo := make(map[string]bool) for _, orb := range config.Lists { rName := orb.Owner + "/" + orb.Repo if !hadRepo[rName] { for _, state := range []string{"open", "closed"} { opts := &gogithub.PullRequestListOptions{ Sort: "updated", State: state, ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}} guff, _, err := client.PullRequests.List(orb.Owner, orb.Repo, opts) if err != nil { return ret, err } for _, v := range guff { include := true if state == "closed" { if config.SincePtr != nil { if (*config.SincePtr).After(*v.ClosedAt) { include = false } } } if include { up := "" if v.UpdatedAt != nil { up = (*v.UpdatedAt).Format(milestonesTimeFormat) } ret = append(ret, githubPullRequest{ Repo: rName, Name: *v.Title, URL: *v.HTMLURL, IsOpen: *v.State == "open", UpdatedAt: up, }) } } } } hadRepo[rName] = true } sort.Stable(prToSort(ret)) return ret, nil } func refreshPullReqs(gr *githubRender, config *githubConfig, client *gogithub.Client) (err error) { gr.PullRequests, err = getPullReqs(client, config) if err != nil { log.Error("unable to get github Pull Requests", err) return err } gr.OpenPRs = 0 gr.ClosedPRs = 0 for _, v := range gr.PullRequests { if v.IsOpen { gr.OpenPRs++ } else { gr.ClosedPRs++ } } return nil } func renderPullReqs(payload *githubRender, c *githubConfig) error { return nil }