// 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 ( "encoding/json" "html/template" "net/http" "strconv" "strings" "github.com/documize/community/core/log" "github.com/documize/community/core/section/provider" 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"` IsOpen bool `json:"isopen"` } const tagIssuesData = "issuesData" func init() { reports[tagIssuesData] = report{commandIssuesData, refreshIssues, renderIssues, `

{{if .ShowIssueNumbers}} Showing Selected Issues {{else}} {{ .Config.IssueState.Name }} {{end}} for repository {{.Repo.Name}} {{if .ShowList}} labelled {{range $label := .List}} {{if $label.Included}} {{$label.Name}} {{end}} {{end}} {{end}} {{if .ShowIssueNumbers}} issue(s) {{ .DateMessage }}. {{else}} up to {{ .Limit }} items are shown{{ .DateMessage }}. {{end}}

`} } func wrapLabels(labels []gogithub.Label) string { l := "" for _, ll := range labels { l += `` + *ll.Name + ` ` } return l } func (*Provider) getIssues(client *gogithub.Client, config githubConfig) ([]githubIssue, error) { ret := []githubIssue{} isRequired := make([]int, 0, 10) for _, s := range strings.Split(strings.Replace(config.IssuesText, "#", "", -1), ",") { i, err := strconv.Atoi(strings.TrimSpace(s)) if err == nil { isRequired = append(isRequired, i) } } if len(isRequired) > 0 { for _, i := range isRequired { issue, _, err := client.Issues.Get(config.Owner, config.Repo, i) if err == nil { n := "" p := issue.User if p != nil { if p.Login != nil { n = *p.Login } } l := wrapLabels(issue.Labels) ret = append(ret, githubIssue{ Name: n, Message: *issue.Title, Date: issue.CreatedAt.Format("January 2 2006, 15:04"), Updated: issue.UpdatedAt.Format("January 2 2006, 15:04"), URL: template.URL(*issue.HTMLURL), Labels: template.HTML(l), ID: *issue.Number, IsOpen: *issue.State == "open", }) } } } else { opts := &gogithub.IssueListByRepoOptions{ Sort: "updated", State: config.IssueState.ID, ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}} if config.SincePtr != nil { opts.Since = *config.SincePtr } for _, lab := range config.Lists { if lab.Included { opts.Labels = append(opts.Labels, lab.Name) } } guff, _, err := client.Issues.ListByRepo(config.Owner, config.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 } } l := wrapLabels(v.Labels) ret = append(ret, githubIssue{ Name: n, Message: *v.Title, Date: v.CreatedAt.Format("January 2 2006, 15:04"), Updated: v.UpdatedAt.Format("January 2 2006, 15:04"), URL: template.URL(*v.HTMLURL), Labels: template.HTML(l), ID: *v.Number, IsOpen: *v.State == "open", }) } } return ret, nil } func commandIssuesData(p *Provider, client *gogithub.Client, config githubConfig, w http.ResponseWriter) { render, err := p.getIssues(client, config) if err != nil { log.Error("github getIssues:", err) provider.WriteError(w, "github", err) return } provider.WriteJSON(w, render) } func refreshIssues(p *Provider, c githubConfig, data string) string { refreshed, err := p.getIssues(p.githubClient(c), c) if err != nil { log.Error("unable to get github issues", err) return data } j, err := json.Marshal(refreshed) if err != nil { log.Error("unable to marshall github issues", err) return data } return string(j) } func renderIssues(c *githubConfig, payload *githubRender, data string) error { raw := []githubIssue{} if len(data) > 0 { err := json.Unmarshal([]byte(data), &raw) if err != nil { return err } } payload.Issues = raw if strings.TrimSpace(c.IssuesText) != "" { payload.ShowIssueNumbers = true payload.DateMessage = c.IssuesText } else { if len(c.Lists) > 0 { for _, v := range c.Lists { if v.Included { payload.ShowList = true break } } payload.List = c.Lists } } return nil }