1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-20 21:59:42 +02:00
documize/core/section/github/issues.go
2016-08-19 12:42:01 +01:00

248 lines
6.7 KiB
Go

// Copyright 2016 Documize Inc. <legal@documize.com>. 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 <sales@documize.com>.
//
// https://documize.com
package github
import (
"html/template"
"sort"
"time"
"github.com/documize/community/core/log"
gogithub "github.com/google/go-github/github"
)
type githubIssue struct {
ID int `json:"id"`
Date string `json:"date"`
Updated string `json:"dated"`
Message string `json:"message"`
URL template.URL `json:"url"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Labels template.HTML `json:"labels"`
LabelNames []string `json:"labelNames"`
IsOpen bool `json:"isopen"`
Repo string `json:"repo"`
Milestone string `json:"milestone"`
}
// sort issues in order that that should be presented - by date updated.
type issuesToSort []githubIssue
func (s issuesToSort) Len() int { return len(s) }
func (s issuesToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s issuesToSort) 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(issuesTimeFormat, s[i].Updated)
log.IfErr(iErr)
jDate, jErr := time.Parse(issuesTimeFormat, s[j].Updated)
log.IfErr(jErr)
return iDate.Before(jDate)
}
const (
tagIssuesData = "issuesData"
issuesTimeFormat = "January 2 2006, 15:04"
openIsvg = `
<span title="Open issue">
<svg height="16" version="1.1" viewBox="0 0 14 16" width="14"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
</span>
`
closedIsvg = `
<span title="Closed issue">
<svg height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M7 10h2v2H7v-2zm2-6H7v5h2V4zm1.5 1.5l-1 1L12 9l4-4.5-1-1L12 7l-1.5-1.5zM8 13.7A5.71 5.71 0 0 1 2.3 8c0-3.14 2.56-5.7 5.7-5.7 1.83 0 3.45.88 4.5 2.2l.92-.92A6.947 6.947 0 0 0 8 1C4.14 1 1 4.14 1 8s3.14 7 7 7 7-3.14 7-7l-1.52 1.52c-.66 2.41-2.86 4.19-5.48 4.19v-.01z"></path></svg>
</span>
`
)
func init() {
reports[tagIssuesData] = report{refreshIssues, renderIssues, `
<div class="section-github-render">
<h3>Issues: {{.ClosedIssues}} closed, {{.OpenIssues}} open</h3>
<p>
{{if .ShowList}}
Including issues labelled
{{range $label := .List}}
{{if $label.Included}}
<span class="github-issue-label" style="background-color:#{{$label.Color}}">{{$label.Name}}</span>
{{end}}
{{end}}
{{end}}
</p>
<div class="github-board">
<ul class="github-list">
{{range $data := .Issues}}
<li class="github-commit-item">
<a class="link" href="{{$data.URL}}">
<div class="issue-avatar">
{{if $data.IsOpen}}
` + openIsvg + `
{{else}}
` + closedIsvg + `
{{end}}
</div>
<div class="github-commit-body">
<div class="github-commit-title"><span class="label-name">{{$data.Repo}} - {{$data.Message}}</span> :{{$data.Milestone}}: {{$data.Labels}}</div>
<div class="github-commit-meta">
#{{$data.ID}} opened on {{$data.Date}} by {{$data.Name}}, last updated {{$data.Updated}}
</div>
</div>
</a>
<div class="clearfix" />
</li>
{{end}}
</ul>
</div>
</div>
`}
}
func wrapLabels(labels []gogithub.Label) (l string, labelNames []string) {
labelNames = make([]string, 0, len(labels))
for _, ll := range labels {
labelNames = append(labelNames, *ll.Name)
l += `<span class="github-issue-label" style="background-color:#` + *ll.Color + `">` + *ll.Name + `</span> `
}
return l, labelNames
}
func getIssues(client *gogithub.Client, config *githubConfig) ([]githubIssue, error) {
ret := []githubIssue{}
hadRepo := make(map[string]bool)
for _, orb := range config.Lists {
if orb.Included {
rName := orb.Owner + "/" + orb.Repo
if !hadRepo[rName] {
for _, state := range []string{"open", "closed"} {
opts := &gogithub.IssueListByRepoOptions{
Sort: "updated",
State: state,
ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}}
if config.SincePtr != nil && state == "closed" /* we want all the open ones */ {
opts.Since = *config.SincePtr
}
/* TODO refactor to select certain lables
for _, lab := range config.Lists {
if lab.Included {
opts.Labels = append(opts.Labels, lab.Name)
}
}
*/
guff, _, err := client.Issues.ListByRepo(orb.Owner, orb.Repo, opts)
if err != nil {
return ret, err
}
for _, v := range guff {
n := ""
ptr := v.User
if ptr != nil {
if ptr.Login != nil {
n = *ptr.Login
}
}
ms := noMilestone
if v.Milestone != nil {
if v.Milestone.Title != nil {
ms = *v.Milestone.Title
}
}
l, ln := wrapLabels(v.Labels)
ret = append(ret, githubIssue{
Name: n,
Message: *v.Title,
Date: v.CreatedAt.Format(issuesTimeFormat),
Updated: v.UpdatedAt.Format(issuesTimeFormat),
URL: template.URL(*v.HTMLURL),
Labels: template.HTML(l),
LabelNames: ln,
ID: *v.Number,
IsOpen: *v.State == "open",
Repo: rName,
Milestone: ms,
})
}
}
}
hadRepo[rName] = true
}
}
sort.Stable(issuesToSort(ret))
return ret, nil
}
func refreshIssues(gr *githubRender, config *githubConfig, client *gogithub.Client) (err error) {
gr.Issues, err = getIssues(client, config)
if err != nil {
log.Error("unable to get github issues (cmd)", err)
return err
}
gr.OpenIssues = 0
gr.ClosedIssues = 0
sharedLabels := make(map[string][]string)
for _, v := range gr.Issues {
if v.IsOpen {
gr.OpenIssues++
} else {
gr.ClosedIssues++
}
for _, lab := range v.LabelNames {
sharedLabels[lab] = append(sharedLabels[lab], v.Repo)
}
}
gr.SharedLabels = make([]template.HTML, 0, len(sharedLabels)) // will usually be too big
for name, repos := range sharedLabels {
if len(repos) > 1 {
lab := name + ":["
for i, r := range repos {
if i > 0 {
lab += " "
}
lab += "<a href='https://github.com/" + r +
"/issues?q=is%3Aissue+label%3A" + name + "'>" + r + "</a>"
}
lab += "] "
gr.SharedLabels = append(gr.SharedLabels, template.HTML(lab))
}
}
return nil
}
func renderIssues(payload *githubRender, c *githubConfig) error {
return nil
}