1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-02 20:15:26 +02:00

refactored smart section code

This commit is contained in:
Harvey Kandola 2017-07-21 13:39:53 +01:00
parent 5acfae3d0d
commit 3defc062bd
40 changed files with 172 additions and 306 deletions

View file

@ -0,0 +1,65 @@
// 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 airtable
import (
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
)
// Provider represents Airtable
type Provider struct {
Runtime env.Runtime
}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "3cfa411e-73bf-474c-841a-effd6b00fdd8"
section.Title = "Airtable"
section.Description = "Databases, tables, views"
section.ContentType = "airtable"
section.PageType = "tab"
return section
}
// Command stub.
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render converts markdown data into HTML suitable for browser rendering.
func (*Provider) Render(ctx *provider.Context, config, data string) string {
return embed(config, data)
}
// Refresh just sends back data as-is.
func (*Provider) Refresh(ctx *provider.Context, config, data string) string {
return embed(config, data)
}
func embed(config, data string) string {
return data
// return `
// <iframe class="airtable-embed"
// src="https://airtable.com/embed/shrFOcQ6BYrlUe62L?backgroundColor=yellow&viewControls=on"
// frameborder="0"
// onmousewheel=""
// width="100%"
// height="533"
// style="background: transparent; border: 1px solid #ccc;"></iframe>
// `
}

View file

@ -0,0 +1,53 @@
// 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 code
import (
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
)
// Provider represents code snippet
type Provider struct {
Runtime env.Runtime
}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "4f6f2b02-8397-483d-9bb9-eea1fef13304"
section.Title = "Code"
section.Description = "Formatted code snippets"
section.ContentType = "code"
section.PageType = "section"
section.Order = 9997
return section
}
// Command stub.
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render just sends back HMTL as-is.
func (*Provider) Render(ctx *provider.Context, config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*Provider) Refresh(ctx *provider.Context, config, data string) string {
return data
}

View file

@ -0,0 +1,374 @@
// 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 gemini
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
)
// Provider represents Gemini
type Provider struct {
Runtime env.Runtime
}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "23b133f9-4020-4616-9291-a98fb939735f"
section.Title = "Gemini"
section.Description = "Work items and tickets"
section.ContentType = "gemini"
section.PageType = "tab"
return section
}
// Render converts Gemini data into HTML suitable for browser rendering.
func (*Provider) Render(ctx *provider.Context, config, data string) string {
var items []geminiItem
var payload = geminiRender{}
var c = geminiConfig{}
json.Unmarshal([]byte(data), &items)
json.Unmarshal([]byte(config), &c)
c.ItemCount = len(items)
payload.Items = items
payload.Config = c
payload.Authenticated = c.UserID > 0
t := template.New("items")
t, _ = t.Parse(renderTemplate)
buffer := new(bytes.Buffer)
t.Execute(buffer, payload)
return buffer.String()
}
// Command handles authentication, workspace listing and items retrieval.
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
provider.WriteMessage(w, "gemini", "missing method name")
return
}
switch method {
case "secrets":
secs(ctx, w, r)
case "auth":
auth(ctx, w, r)
case "workspace":
workspace(ctx, w, r)
case "items":
items(ctx, w, r)
}
}
// Refresh just sends back data as-is.
func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData string) {
var c = geminiConfig{}
err := json.Unmarshal([]byte(config), &c)
if err != nil {
p.Runtime.Log.Error("Unable to read Gemini config", err)
return
}
c.Clean(ctx)
if len(c.URL) == 0 {
p.Runtime.Log.Info("Gemini.Refresh received empty URL")
return
}
if len(c.Username) == 0 {
p.Runtime.Log.Info("Gemini.Refresh received empty username")
return
}
if len(c.APIKey) == 0 {
p.Runtime.Log.Info("Gemini.Refresh received empty API key")
return
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/items/card/%d", c.URL, c.WorkspaceID), nil)
// req.Header.Set("Content-Type", "application/json")
creds := []byte(fmt.Sprintf("%s:%s", c.Username, c.APIKey))
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
if res.StatusCode != http.StatusOK {
return
}
defer res.Body.Close()
var items []geminiItem
dec := json.NewDecoder(res.Body)
err = dec.Decode(&items)
if err != nil {
p.Runtime.Log.Error("unable to Decode gemini items", err)
return
}
j, err := json.Marshal(items)
if err != nil {
p.Runtime.Log.Error("unable to marshal gemini items", err)
return
}
newData = string(j)
return
}
func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
var config = geminiConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean(nil) // don't look at the database for the parameters
if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
creds := []byte(fmt.Sprintf("%s:%s", config.Username, config.APIKey))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/users/username/%s", config.URL, config.Username), nil)
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
provider.WriteForbidden(w)
return
}
config.SaveSecrets(ctx)
defer res.Body.Close()
var g = geminiUser{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&g)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
provider.WriteJSON(w, g)
}
func workspace(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
var config = geminiConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean(ctx)
if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
if config.UserID == 0 {
provider.WriteMessage(w, "gemini", "Missing UserId value")
return
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/navigationcards/users/%d", config.URL, config.UserID), nil)
creds := []byte(fmt.Sprintf("%s:%s", config.Username, config.APIKey))
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
provider.WriteForbidden(w)
return
}
defer res.Body.Close()
var workspace interface{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&workspace)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
provider.WriteJSON(w, workspace)
}
func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
var config = geminiConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
provider.WriteMessage(w, "gemini", "Bad payload")
return
}
config.Clean(ctx)
if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value")
return
}
if len(config.Username) == 0 {
provider.WriteMessage(w, "gemini", "Missing Username value")
return
}
if len(config.APIKey) == 0 {
provider.WriteMessage(w, "gemini", "Missing APIKey value")
return
}
creds := []byte(fmt.Sprintf("%s:%s", config.Username, config.APIKey))
filter, err := json.Marshal(config.Filter)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
var jsonFilter = []byte(string(filter))
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/items/filtered", config.URL), bytes.NewBuffer(jsonFilter))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString(creds))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
provider.WriteError(w, "gemini", err)
return
}
if res.StatusCode != http.StatusOK {
provider.WriteForbidden(w)
return
}
defer res.Body.Close()
var items interface{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&items)
if err != nil {
fmt.Println(err)
provider.WriteError(w, "gemini", err)
return
}
provider.WriteJSON(w, items)
}
func secs(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
sec, _ := getSecrets(ctx)
provider.WriteJSON(w, sec)
}

View file

@ -0,0 +1,122 @@
// 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 gemini
import (
"strings"
"github.com/documize/community/domain/section/provider"
)
// the HTML that is rendered by this section.
const renderTemplate = `
{{if .Authenticated}}
<p>The Gemini workspace <a href="{{.Config.URL}}/workspace/{{.Config.WorkspaceID}}/items">{{.Config.WorkspaceName}}</a> contains {{.Config.ItemCount}} items.</p>
<table class="basic-table section-gemini-table">
<thead>
<tr>
<th class="bordered no-width">Item Key</th>
<th class="bordered">Title</th>
<th class="bordered no-width">Type</th>
<th class="bordered no-width">Status</th>
</tr>
</thead>
<tbody>
{{$wid := .Config.WorkspaceID}}
{{$app := .Config.URL}}
{{range $item := .Items}}
<tr>
<td class="bordered no-width"><a href="{{ $app }}/workspace/{{ $wid }}/item/{{ $item.ID }}">{{ $item.IssueKey }}</a></td>
<td class="bordered">{{ $item.Title }}</td>
<td class="bordered no-width"><img src='{{ $item.TypeImage }}' />&nbsp;{{ $item.Type }}</td>
<td class="bordered no-width"><img src='{{ $item.StatusImage }}' />&nbsp;{{ $item.Status }}</td>
</tr>
{{end}}
</tbody>
</table>
{{else}}
<p>Authenticate with Gemini to see items.</p>
{{end}}
`
// Gemini helpers
type geminiRender struct {
Config geminiConfig
Items []geminiItem
Authenticated bool
}
type geminiItem struct {
ID int64
IssueKey string
Title string
Type string
TypeImage string
Status string
StatusImage string
}
type geminiUser struct {
BaseEntity struct {
ID int `json:"id"`
Username string `json:"username"`
Firstname string `json:"firstname"`
Surname string `json:"surname"`
Email string `json:"email"`
}
}
type geminiConfig struct {
URL string `json:"url"`
Username string `json:"username"`
APIKey string `json:"apikey"`
UserID int64 `json:"userId"`
WorkspaceID int64 `json:"workspaceId"`
WorkspaceName string `json:"workspaceName"`
ItemCount int `json:"itemCount"`
Filter map[string]interface{} `json:"filter"`
}
func (c *geminiConfig) Clean(ctx *provider.Context) {
if ctx != nil {
sec, err := getSecrets(ctx)
if err == nil {
if len(sec.APIKey) > 0 && len(sec.Username) > 0 && len(sec.URL) > 0 {
c.APIKey = strings.TrimSpace(sec.APIKey)
c.Username = strings.TrimSpace(sec.Username)
c.URL = strings.TrimSpace(sec.URL)
}
}
}
c.APIKey = strings.TrimSpace(c.APIKey)
c.Username = strings.TrimSpace(c.Username)
c.URL = strings.TrimSpace(c.URL)
}
func (c *geminiConfig) SaveSecrets(ctx *provider.Context) {
var sec secrets
sec.APIKey = strings.TrimSpace(c.APIKey)
sec.Username = strings.TrimSpace(c.Username)
sec.URL = strings.TrimSpace(c.URL)
ctx.MarshalSecrets(sec)
}
type secrets struct {
URL string `json:"url"`
Username string `json:"username"`
APIKey string `json:"apikey"`
}
func getSecrets(ctx *provider.Context) (sec secrets, err error) {
err = ctx.UnmarshalSecrets(&sec)
return
}

View file

@ -0,0 +1,110 @@
// 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 (
"encoding/json"
"net/http"
"net/url"
"strings"
"github.com/documize/community/core/api/request"
gogithub "github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func clientID() string {
return request.ConfigString(meta.ConfigHandle(), "clientID")
}
func clientSecret() string {
return request.ConfigString(meta.ConfigHandle(), "clientSecret")
}
func authorizationCallbackURL() string {
// NOTE: URL value must have the path and query "/api/public/validate?section=github"
return request.ConfigString(meta.ConfigHandle(), "authorizationCallbackURL")
}
func validateToken(ptoken string) error {
// Github authorization check
authClient := gogithub.NewClient((&gogithub.BasicAuthTransport{
Username: clientID(),
Password: clientSecret(),
}).Client())
_, _, err := authClient.Authorizations.Check(clientID(), ptoken)
return err
}
func (*Provider) githubClient(config *githubConfig) *gogithub.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
return gogithub.NewClient(tc)
}
// Callback is called by a browser redirect from Github, via the validation endpoint
func Callback(res http.ResponseWriter, req *http.Request) error {
code := req.URL.Query().Get("code")
state := req.URL.Query().Get("state")
ghurl := "https://github.com/login/oauth/access_token"
vals := "client_id=" + clientID()
vals += "&client_secret=" + clientSecret()
vals += "&code=" + code
vals += "&state=" + state
req2, err := http.NewRequest("POST", ghurl+"?"+vals, strings.NewReader(vals))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req2.Header.Set("Accept", "application/json")
res2, err := http.DefaultClient.Do(req2)
if err != nil {
return err
}
var gt githubCallbackT
err = json.NewDecoder(res2.Body).Decode(&gt)
if err != nil {
return err
}
err = res2.Body.Close()
if err != nil {
return err
}
returl, err := url.QueryUnescape(state)
if err != nil {
return err
}
up, err := url.Parse(returl)
if err != nil {
return err
}
target := up.Scheme + "://" + up.Host + up.Path + "?mode=edit&code=" + gt.AccessToken
http.Redirect(res, req, target, http.StatusTemporaryRedirect)
return nil
}

View file

@ -0,0 +1,317 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize unity 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 (
"fmt"
"html/template"
"sort"
"time"
"github.com/documize/community/core/log"
gogithub "github.com/google/go-github/github"
)
const commitTimeFormat = "2006-01-02, 15:04"
type githubCommit struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
ShowRepo bool `json:"showRepo"`
Branch string `json:"branch"`
ShowBranch bool `json:"showBranch"`
Date string `json:"date"`
BinDate time.Time `json:"-"` // only used for sorting
ShowDate bool `json:"showDate"`
Login string `json:"login"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Message string `json:"message"`
URL template.URL `json:"url"`
}
type githubAuthorStats struct {
Author string `json:"author"`
Login string `json:"login"`
Avatar string `json:"avatar"`
CommitCount int `json:"commitCount"`
Repos []string `json:"repos"`
OpenIssues int `json:"openIssues"`
ClosedIssues int `json:"closedIssues"`
}
// order commits in a way that makes sense of the table
type orderCommits []githubCommit
func (s orderCommits) Len() int { return len(s) }
func (s orderCommits) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s orderCommits) Less(i, j int) bool {
if s[i].Repo == s[j].Repo {
if s[i].Branch == s[j].Branch {
if s[i].BinDate == s[j].BinDate {
return s[i].Name < s[j].Name
}
return s[i].BinDate.Before(s[j].BinDate)
}
return s[i].Branch < s[j].Branch
}
return s[i].Repo < s[j].Repo
}
// sort stats in order that that should be presented.
type asToSort []githubAuthorStats
func (s asToSort) Len() int { return len(s) }
func (s asToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s asToSort) Less(i, j int) bool {
return s[i].CommitCount > s[j].CommitCount
}
// sort branches in order that that should be presented.
type branchByID []githubBranch
func (s branchByID) Len() int { return len(s) }
func (s branchByID) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s branchByID) Less(i, j int) bool {
return s[i].ID < s[j].ID
}
const tagCommitsData = "commitsData"
func getCommits(client *gogithub.Client, config *githubConfig) ([]githubCommit, []githubAuthorStats, error) {
if !config.ShowCommits {
return nil, nil, nil
}
// first make sure we've got all the branches
for _, orb := range config.Lists {
if orb.Included {
branches, _, err := client.Repositories.ListBranches(orb.Owner, orb.Repo,
&gogithub.ListOptions{PerPage: 100})
if err == nil {
render := make([]githubBranch, len(branches))
for kc, vb := range branches {
for _, existing := range config.Lists {
if orb.Owner == existing.Owner && orb.Repo == existing.Repo && orb.Name == *vb.Name {
goto found
}
}
render[kc] = githubBranch{
Owner: orb.Owner,
Repo: orb.Repo,
Name: *vb.Name,
ID: fmt.Sprintf("%s:%s:%s", orb.Owner, orb.Repo, *vb.Name),
Included: true,
URL: "https://github.com/" + orb.Owner + "/" + orb.Repo + "/tree/" + *vb.Name,
}
found:
}
config.Lists = append(config.Lists, render...)
}
}
}
sort.Sort(branchByID(config.Lists))
config.UserNames = make(map[string]string)
authorStats := make(map[string]githubAuthorStats)
contribBranch := make(map[string]map[string]struct{})
overall := []githubCommit{}
for _, orb := range config.Lists {
if orb.Included {
opts := &gogithub.CommitsListOptions{
SHA: orb.Name,
ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}}
if config.SincePtr != nil {
opts.Since = *config.SincePtr
}
guff, _, err := client.Repositories.ListCommits(orb.Owner, orb.Repo, opts)
if err != nil {
return nil, nil, err
}
thisBranch := fmt.Sprintf("%s:%s", orb.Repo, orb.Name)
for _, v := range guff {
var d, m, u string
var bd time.Time
if v.Commit != nil {
if v.Commit.Committer.Date != nil {
d = v.Commit.Committer.Date.Format(commitTimeFormat)
bd = *v.Commit.Committer.Date
}
if v.Commit.Message != nil {
m = *v.Commit.Message
}
}
if v.HTMLURL != nil {
u = *v.HTMLURL
}
// author commits
al, an, aa := "", "", githubGravatar
if v.Author != nil {
if v.Author.Login != nil {
al = *v.Author.Login
an = getUserName(client, config, al)
}
if v.Author.AvatarURL != nil {
aa = *v.Author.AvatarURL
}
}
l := al // use author login
overall = append(overall, githubCommit{
Owner: orb.Owner,
Repo: orb.Repo,
Branch: orb.Name,
Name: an,
Login: l,
Message: m,
Date: d,
BinDate: bd,
Avatar: aa,
URL: template.URL(u),
})
if _, ok := contribBranch[l]; !ok {
contribBranch[l] = make(map[string]struct{})
}
contribBranch[l][thisBranch] = struct{}{}
cum := authorStats[l]
cum.Login = l
cum.Author = an
cum.Avatar = aa
cum.CommitCount++
// TODO review, this code removed as too slow
//cmt, _, err := client.Repositories.GetCommit(orb.Owner, orb.Repo, *v.SHA)
//if err == nil {
// if cmt.Stats != nil {
// if cmt.Stats.Total != nil {
// cum.TotalChanges += (*cmt.Stats.Total)
// }
// }
//}
//
authorStats[l] = cum
}
}
}
sort.Sort(orderCommits(overall))
for k := range overall {
overall[k].ShowRepo = true
overall[k].ShowBranch = true
overall[k].ShowDate = true
if k > 0 {
if overall[k].Repo == overall[k-1].Repo {
overall[k].ShowRepo = false
if overall[k].Branch == overall[k-1].Branch {
overall[k].ShowBranch = false
if overall[k].Date == overall[k-1].Date {
overall[k].ShowDate = false
}
}
}
}
}
retStats := make([]githubAuthorStats, 0, len(authorStats))
for _, v := range authorStats {
repos := contribBranch[v.Login]
v.Repos = make([]string, 0, len(repos))
for r := range repos {
v.Repos = append(v.Repos, r)
}
sort.Strings(v.Repos)
retStats = append(retStats, v)
}
sort.Sort(asToSort(retStats))
return overall, retStats, nil
}
func refreshCommits(gr *githubRender, config *githubConfig, client *gogithub.Client) (err error) {
if !config.ShowCommits {
return nil
}
gr.BranchCommits, gr.AuthorStats, err = getCommits(client, config)
if err != nil {
log.Error("github refreshCommits:", err)
return err
}
return nil
}
func renderCommits(payload *githubRender, c *githubConfig) error {
if !c.ShowCommits {
return nil
}
payload.CommitCount = 0
for range payload.BranchCommits {
payload.CommitCount++
}
payload.HasCommits = payload.CommitCount > 0
for i := range payload.Issues {
var author int
for a := range payload.AuthorStats {
if payload.AuthorStats[a].Login == payload.Issues[i].Name ||
(payload.AuthorStats[a].Login == "" && payload.Issues[i].Name == unassignedIssue) {
author = a
goto found
}
}
// no Author found for issue, so create one
payload.AuthorStats = append(payload.AuthorStats, githubAuthorStats{
Author: payload.Issues[i].Name,
Avatar: payload.Issues[i].Avatar,
})
author = len(payload.AuthorStats) - 1
found:
if payload.Issues[i].IsOpen {
payload.AuthorStats[author].OpenIssues++
} else {
payload.AuthorStats[author].ClosedIssues++
}
}
payload.HasAuthorStats = len(payload.AuthorStats) > 0
sort.Sort(asToSort(payload.AuthorStats))
payload.NumContributors = len(payload.AuthorStats) - 1
return nil
}
func init() {
reports[tagCommitsData] = report{refreshCommits, renderCommits, commitsTemplate}
}

View file

@ -0,0 +1,99 @@
// 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
const commitsTemplate = `
<div class="section-github-render">
<!--
{{if .HasAuthorStats}}
<div class="heading">Contributors</div>
<p>
There
{{if eq 1 .NumContributors}}is{{else}}are{{end}}
{{.NumContributors}}
{{if eq 1 .NumContributors}}contributor{{else}}contributors{{end}}
across {{.RepoCount}}
{{if eq 1 .RepoCount}} repository. {{else}} repositories. {{end}}
</p>
<table class="github-table">
<thead>
<tr>
<th class="title">Contributors</th>
<th></th>
</tr>
</thead>
<tbody>
{{range $stats := .AuthorStats}}
<tr>
<td class="no-width">
<img class="github-avatar" alt="@{{$stats.Author}}" src="{{$stats.Avatar}}" />
</td>
<td>
<div class="contributor-name">{{$stats.Author}}</div>
<div class="contributor-meta">
{{if gt $stats.OpenIssues 0}}
assigned {{$stats.OpenIssues}}
{{if eq 1 $stats.OpenIssues}} issue {{else}} issues {{end}}
{{end}}
{{if gt $stats.ClosedIssues 0}}
&middot; {{$stats.ClosedIssues}} closed
{{end}}
{{if gt $stats.CommitCount 0}}
{{if gt $stats.OpenIssues 0}} &middot; {{end}}
{{if gt $stats.ClosedIssues 0}} &middot; {{end}}
made {{$stats.CommitCount}}
{{if eq 1 $stats.CommitCount}} commit {{else}} commits {{end}}
on {{len $stats.Repos}} {{if eq 1 (len $stats.Repos)}} branch {{else}} branches {{end}}
{{range $repo := $stats.Repos}} &middot; {{$repo}} {{end}}
{{end}}
</div>
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
-->
{{if .HasCommits}}
<table class="github-table" style="width: 100%;">
<thead>
<tr>
<th class="title">Commits <span>&middot; {{len .BranchCommits}} commits</span>
</th>
<th></th>
</tr>
</thead>
<tbody>
{{range $commit := .BranchCommits}}
<tr>
<td>
<a href="{{$commit.URL}}">{{$commit.Message}}</a>
<span class="data"> {{$commit.Branch}}</span>
</td>
<td class="right-column">
<div class="contributor-meta">
{{$commit.Date}}
<img class="github-avatar" title="@{{$commit.Name}}" alt="@{{$commit.Name}}" src="{{$commit.Avatar}}" />
</div>
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
`

View file

@ -0,0 +1,253 @@
// 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 (
"bytes"
"encoding/json"
"errors"
"html/template"
"io/ioutil"
"net/http"
"strings"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
gogithub "github.com/google/go-github/github"
)
// TODO find a smaller image than the one below
const githubGravatar = "https://i2.wp.com/assets-cdn.github.com/images/gravatars/gravatar-user-420.png"
var meta provider.TypeMeta
func init() {
meta = provider.TypeMeta{}
meta.ID = "38c0e4c5-291c-415e-8a4d-262ee80ba5df"
meta.Title = "GitHub"
meta.Description = "Link code commits and issues"
meta.ContentType = "github"
meta.PageType = "tab"
meta.Callback = Callback
}
// Provider represents GitHub
type Provider struct {
Runtime env.Runtime
}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
return meta
}
// Command to run the various functions required...
func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
msg := "missing method name"
provider.WriteMessage(w, "gitub", msg)
return
}
if method == "config" {
var ret struct {
CID string `json:"clientID"`
URL string `json:"authorizationCallbackURL"`
}
ret.CID = clientID()
ret.URL = authorizationCallbackURL()
provider.WriteJSON(w, ret)
return
}
defer r.Body.Close() // ignore error
body, err := ioutil.ReadAll(r.Body)
if err != nil {
p.Runtime.Log.Error("bad body", errors.New("Missing body"))
provider.WriteMessage(w, "github", "bad body")
return
}
if method == "saveSecret" { // secret Token update code
// write the new one, direct from JS
if err = ctx.SaveSecrets(string(body)); err != nil {
p.Runtime.Log.Error("github settoken configuration", err)
provider.WriteError(w, "github", err)
return
}
provider.WriteEmpty(w)
return
}
// load the config from the client-side
config := githubConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
p.Runtime.Log.Error("github Command Unmarshal", err)
provider.WriteError(w, "github", err)
return
}
config.Clean()
// always use DB version of the token
config.Token = ctx.GetSecrets("token") // get the secret token in the database
client := p.githubClient(&config)
switch method {
case "checkAuth":
if len(config.Token) == 0 {
err = errors.New("empty github token")
} else {
err = validateToken(config.Token)
}
if err != nil {
// token now invalid, so wipe it
ctx.SaveSecrets("") // ignore error, already in an error state
p.Runtime.Log.Error("github check token validation", err)
provider.WriteError(w, "github", err)
return
}
provider.WriteEmpty(w)
default:
if listFailed(p.Runtime, method, config, client, w) {
gr := githubRender{}
for _, rep := range reports {
rep.refresh(&gr, &config, client)
}
provider.WriteJSON(w, &gr)
}
}
}
// Refresh ... gets the latest version
func (p *Provider) Refresh(ctx *provider.Context, configJSON, data string) string {
var c = githubConfig{}
err := json.Unmarshal([]byte(configJSON), &c)
if err != nil {
p.Runtime.Log.Error("unable to unmarshall github config", err)
return "internal configuration error '" + err.Error() + "'"
}
c.Clean()
c.Token = ctx.GetSecrets("token")
client := p.githubClient(&c)
byts, err := json.Marshal(refreshReportData(&c, client))
if err != nil {
p.Runtime.Log.Error("unable to marshall github data", err)
return "internal configuration error '" + err.Error() + "'"
}
return string(byts)
}
func refreshReportData(c *githubConfig, client *gogithub.Client) *githubRender {
var gr = githubRender{}
for _, rep := range reports {
rep.refresh(&gr, c, client)
}
return &gr
}
// Render ... just returns the data given, suitably formatted
func (p *Provider) Render(ctx *provider.Context, config, data string) string {
var err error
payload := githubRender{}
var c = githubConfig{}
err = json.Unmarshal([]byte(config), &c)
if err != nil {
p.Runtime.Log.Error("unable to unmarshall github config", err)
return "Please delete and recreate this Github section."
}
c.Clean()
c.Token = ctx.GetSecrets("token")
data = strings.TrimSpace(data)
if len(data) == 0 {
// TODO review why this error occurs & if it should be reported - seems to occur for new sections
// log.ErrorString(fmt.Sprintf("Rendered empty github JSON payload as '' for owner %s repos %#v", c.Owner, c.Lists))
return ""
}
err = json.Unmarshal([]byte(data), &payload)
if err != nil {
p.Runtime.Log.Error("unable to unmarshall github data", err)
return "Please delete and recreate this Github section."
}
payload.Config = c
payload.Limit = c.BranchLines
payload.List = c.Lists
ret := ""
for _, repID := range c.ReportOrder {
rep, ok := reports[repID]
if !ok {
msg := "github report not found for: " + repID
p.Runtime.Log.Info(msg)
return "Documize internal error: " + msg
}
if err = rep.render(&payload, &c); err != nil {
p.Runtime.Log.Error("unable to render "+repID, err)
return "Documize internal github render " + repID + " error: " + err.Error() + "<BR>" + data
}
t := template.New("github")
t, err = t.Parse(rep.template)
if err != nil {
p.Runtime.Log.Error("github render template.Parse error:", err)
//for k, v := range strings.Split(rep.template, "\n") {
// fmt.Println("DEBUG", k+1, v)
//}
return "Documize internal github template.Parse error: " + err.Error()
}
buffer := new(bytes.Buffer)
err = t.Execute(buffer, payload)
if err != nil {
p.Runtime.Log.Error("github render template.Execute error:", err)
return "Documize internal github template.Execute error: " + err.Error()
}
ret += buffer.String()
}
return ret
}

View file

@ -0,0 +1,244 @@
// 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"`
Creator string `json:"creator"`
Avatar string `json:"avatar"`
Labels template.HTML `json:"labels"`
LabelNames []string `json:"labelNames"`
LabelColors []string `json:"labelColors"`
IsOpen bool `json:"isopen"`
Repo string `json:"repo"`
Private bool `json:"private"`
Milestone string `json:"milestone"`
}
type githubSharedLabel struct {
Name string `json:"name"`
Count int `json:"count"`
Color string `json:"color"`
Repos template.HTML `json:"Repos"`
}
// 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].Milestone != noMilestone && s[j].Milestone == noMilestone {
return true
}
if s[i].Milestone == noMilestone && s[j].Milestone != noMilestone {
return false
}
if s[i].Milestone != s[j].Milestone {
// TODO should this order be by milestone completion?
return s[i].Milestone < s[j].Milestone
}
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)
}
// sort shared labels alphabetically
type sharedLabelsSort []githubSharedLabel
func (s sharedLabelsSort) Len() int { return len(s) }
func (s sharedLabelsSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sharedLabelsSort) Less(i, j int) bool { return s[i].Name < s[j].Name }
const (
tagIssuesData = "issuesData"
issuesTimeFormat = "January 2 2006, 15:04"
unassignedIssue = "(unassigned)"
)
func init() {
reports[tagIssuesData] = report{refreshIssues, renderIssues, issuesTemplate}
}
func wrapLabels(labels []gogithub.Label) (l string, labelNames []string, labelColors []string) {
labelNames = make([]string, 0, len(labels))
labelColors = make([]string, 0, len(labels))
for _, ll := range labels {
labelNames = append(labelNames, *ll.Name)
labelColors = append(labelColors, *ll.Color)
l += `<span class="issue-label" style="background-color:#` + *ll.Color + `">` + *ll.Name + `</span> `
}
return l, labelNames, labelColors
}
func getIssues(client *gogithub.Client, config *githubConfig) ([]githubIssue, error) {
if !config.ShowIssues {
return nil, nil
}
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
}
guff, _, err := client.Issues.ListByRepo(orb.Owner, orb.Repo, opts)
if err != nil {
return ret, err
}
for _, v := range guff {
n := unassignedIssue
av := githubGravatar
ptr := v.Assignee
if ptr != nil {
if ptr.Login != nil {
n = *ptr.Login
av = *ptr.AvatarURL
}
}
ms := noMilestone
if v.Milestone != nil {
if v.Milestone.Title != nil {
ms = *v.Milestone.Title
}
}
l, ln, lc := wrapLabels(v.Labels)
ret = append(ret, githubIssue{
Name: n,
Creator: getUserName(client, config, *v.User.Login),
Avatar: av,
Message: *v.Title,
Date: v.CreatedAt.Format(issuesTimeFormat),
Updated: v.UpdatedAt.Format(issuesTimeFormat),
URL: template.URL(*v.HTMLURL),
Labels: template.HTML(l),
LabelNames: ln,
LabelColors: lc,
ID: *v.Number,
IsOpen: *v.State == "open",
Repo: repoName(rName),
Private: orb.Private,
Milestone: ms,
})
}
}
}
hadRepo[rName] = true
}
}
sort.Sort(issuesToSort(ret))
return ret, nil
}
func refreshIssues(gr *githubRender, config *githubConfig, client *gogithub.Client) (err error) {
if !config.ShowIssues {
return nil
}
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)
sharedLabelColors := make(map[string]string)
for _, v := range gr.Issues {
if v.IsOpen {
gr.OpenIssues++
} else {
gr.ClosedIssues++
}
for i, lab := range v.LabelNames {
sharedLabels[lab] = append(sharedLabels[lab], v.Repo)
if _, exists := sharedLabelColors[lab]; !exists { // use the first one we see
sharedLabelColors[lab] = v.LabelColors[i]
}
}
}
gr.HasIssues = (gr.OpenIssues + gr.ClosedIssues) > 0
gr.SharedLabels = make([]githubSharedLabel, 0, len(sharedLabels)) // will usually be too big
for name, repos := range sharedLabels {
if len(repos) > 1 {
thisLab := githubSharedLabel{Name: name, Count: len(repos), Color: sharedLabelColors[name]}
show := ""
for i, r := range repos {
if i > 0 {
show += ", "
}
show += "<a href='https://github.com/" + config.Owner + "/" + r +
"/issues?q=is%3Aissue+label%3A" + name + "'>" + r + "</a>"
}
thisLab.Repos = template.HTML(show)
gr.SharedLabels = append(gr.SharedLabels, thisLab)
}
}
sort.Sort(sharedLabelsSort(gr.SharedLabels))
gr.HasSharedLabels = len(gr.SharedLabels) > 0
return nil
}
func renderIssues(payload *githubRender, c *githubConfig) error {
return nil
}

View file

@ -0,0 +1,68 @@
// 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
const (
openIsvg = `
<span class="issue-state" title="Open Issue">
<svg height="16" version="1.1" viewBox="0 0 14 16" width="14" class="color:#6cc644;">
<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 class="issue-state" title="Closed Issue">
<svg height="16" version="1.1" viewBox="0 0 16 16" width="16" class="color:#bd2c00;">
<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>
`
issuesTemplate = `
<div class="section-github-render">
{{if .HasIssues}}
<table class="github-table" style="width: 100%;">
<thead>
<tr>
<th class="title">
Issues <span>&middot; {{.ClosedIssues}} closed {{if eq 1 .ClosedIssues}}{{else}}issues{{end}} and {{.OpenIssues}} open
{{if eq 1 .OpenIssues}}issue{{else}}{{end}}</span>
</th>
<th></th>
</tr>
</thead>
<tbody>
{{range $data := .Issues}}
<tr>
<td>
{{if $data.IsOpen}}
` + openIsvg + `
{{else}}
` + closedIsvg + `
{{end}}
<a href="{{$data.URL}}">{{$data.Message}}</a> <span class="data">#{{$data.ID}}</span>
{{$data.Labels}}
</td>
<td class="right-column">
<div class="milestone-meta">
<span class="meta-milestone">{{$data.Milestone}}</span> &middot;
<span class="meta-creator">{{$data.Creator}}</span> &middot; <span class="meta-date">{{$data.Date}}</span>
</div>
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
`
)

View file

@ -0,0 +1,105 @@
// 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 (
"fmt"
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
gogithub "github.com/google/go-github/github"
)
func listFailed(rt env.Runtime, method string, config githubConfig, client *gogithub.Client, w http.ResponseWriter) (failed bool) {
switch method { // which list to choose?
case "owners":
me, _, err := client.Users.Get("")
if err != nil {
rt.Log.Error("github get user details:", err)
provider.WriteError(w, "github", err)
return
}
orgs, _, err := client.Organizations.List("", nil)
if err != nil {
rt.Log.Error("github get user's organisations:", err)
provider.WriteError(w, "github", err)
return
}
owners := make([]githubOwner, 1+len(orgs))
owners[0] = githubOwner{ID: *me.Login, Name: *me.Login}
for ko, vo := range orgs {
id := 1 + ko
owners[id].ID = *vo.Login
owners[id].Name = *vo.Login
}
owners = sortOwners(owners)
provider.WriteJSON(w, owners)
case "orgrepos":
var render []githubBranch
if config.Owner != "" {
me, _, err := client.Users.Get("")
if err != nil {
rt.Log.Error("github get user details:", err)
provider.WriteError(w, "github", err)
return
}
var repos []*gogithub.Repository
if config.Owner == *me.Login {
repos, _, err = client.Repositories.List(config.Owner, nil)
} else {
opt := &gogithub.RepositoryListByOrgOptions{
ListOptions: gogithub.ListOptions{PerPage: 100},
}
repos, _, err = client.Repositories.ListByOrg(config.Owner, opt)
}
if err != nil {
rt.Log.Error("github get user/org repositories:", err)
provider.WriteError(w, "github", err)
return
}
for _, vr := range repos {
render = append(render,
githubBranch{
Name: "master",
ID: fmt.Sprintf("%s:%s", config.Owner, *vr.Name),
Owner: config.Owner,
Repo: *vr.Name,
Private: *vr.Private,
Included: false,
URL: *vr.HTMLURL,
})
}
}
render = sortBranches(render)
provider.WriteJSON(w, render)
case "content":
provider.WriteJSON(w, refreshReportData(&config, client))
default:
return true // failed to get a list
}
return
}

View file

@ -0,0 +1,227 @@
// 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 (
"fmt"
"html/template"
"sort"
"github.com/documize/community/core/log"
gogithub "github.com/google/go-github/github"
)
type githubMilestone struct {
Repo string `json:"repo"`
Private bool `json:"private"`
Name string `json:"name"`
URL template.URL `json:"url"`
IsOpen bool `json:"isopen"`
OpenIssues int `json:"openIssues"`
ClosedIssues int `json:"closedIssues"`
CompleteMsg string `json:"completeMsg"`
DueDate string `json:"dueDate"`
UpdatedAt string `json:"updatedAt"`
Progress uint `json:"progress"`
IsMilestone bool `json:"isMilestone"`
}
// sort milestones in order that that should be presented.
type milestonesToSort []githubMilestone
func (s milestonesToSort) Len() int { return len(s) }
func (s milestonesToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s milestonesToSort) Less(i, j int) bool {
if s[i].Repo < s[j].Repo {
return true
}
if s[i].Repo > s[j].Repo {
return false
}
if !s[i].IsOpen && s[j].IsOpen {
return true
}
if s[i].IsOpen && !s[j].IsOpen {
return false
}
if s[i].Name != noMilestone && s[j].Name == noMilestone {
return true
}
if s[i].Name == noMilestone && s[j].Name != noMilestone {
return false
}
if s[i].Progress == s[j].Progress { // order equal progress milestones
return s[i].Name < s[j].Name
}
return s[i].Progress >= s[j].Progress // put more complete milestones first
}
const (
tagMilestonesData = "milestonesData"
milestonesTimeFormat = "January 2 2006"
noMilestone = "no milestone"
)
func init() {
reports[tagMilestonesData] = report{refreshMilestones, renderMilestones, milestonesTemplate}
}
func getMilestones(client *gogithub.Client, config *githubConfig) ([]githubMilestone, error) {
if !config.ShowMilestones {
return nil, nil
}
ret := []githubMilestone{}
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.MilestoneListOptions{
Sort: "updated",
State: state,
ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}}
guff, _, err := client.Issues.ListMilestones(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 {
dd := "no due date"
if v.DueOn != nil {
// TODO refactor to add message in red if the milestone is overdue
dd = "due " + (*v.DueOn).Format(milestonesTimeFormat) + ""
}
up := ""
if v.UpdatedAt != nil {
up = (*v.UpdatedAt).Format(milestonesTimeFormat)
}
progress := float64(*v.ClosedIssues*100) / float64(*v.OpenIssues+*v.ClosedIssues)
ret = append(ret, githubMilestone{
Repo: repoName(rName),
Private: orb.Private,
Name: *v.Title,
URL: template.URL(fmt.Sprintf(
"https://github.com/%s/%s/milestone/%d",
orb.Owner, orb.Repo, *v.Number)), // *v.HTMLURL does not give the correct value
IsOpen: *v.State == "open",
OpenIssues: *v.OpenIssues,
ClosedIssues: *v.ClosedIssues,
CompleteMsg: fmt.Sprintf("%2.0f%%", progress),
DueDate: dd,
UpdatedAt: up,
Progress: uint(progress),
IsMilestone: true,
})
}
}
}
}
hadRepo[rName] = true
}
}
return ret, nil
}
func refreshMilestones(gr *githubRender, config *githubConfig, client *gogithub.Client) (err error) {
if !config.ShowMilestones {
return nil
}
gr.Milestones, err = getMilestones(client, config)
if err != nil {
log.Error("unable to get github milestones", err)
return err
}
gr.OpenMS = 0
gr.ClosedMS = 0
for _, v := range gr.Milestones {
if v.IsOpen {
gr.OpenMS++
} else {
gr.ClosedMS++
}
}
gr.HasMilestones = (gr.OpenMS + gr.ClosedMS) > 0
return nil
}
func renderMilestones(payload *githubRender, c *githubConfig) error {
if !c.ShowMilestones {
return nil
}
hadRepo := make(map[string]bool)
payload.RepoCount = 0
for _, orb := range payload.List {
rName := orb.Owner + "/" + orb.Repo
if !hadRepo[rName] {
if orb.Included {
payload.RepoCount++
issuesOpen, issuesClosed := 0, 0
for _, iss := range payload.Issues {
if iss.Repo == repoName(rName) {
if iss.Milestone == noMilestone {
if iss.IsOpen {
issuesOpen++
} else {
issuesClosed++
}
}
}
}
if issuesClosed+issuesOpen > 0 {
//payload.Milestones = append(payload.Milestones, githubMilestone{
// Repo: orb.Repo, Private: orb.Private, Name: noMilestone, IsOpen: true,
// OpenIssues: issuesOpen, ClosedIssues: issuesClosed, URL: template.URL(orb.URL),
//})
}
hadRepo[rName] = true
}
}
}
sort.Sort(milestonesToSort(payload.Milestones))
return nil
}

View file

@ -0,0 +1,75 @@
// 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
const (
rawMSsvg = `<path d="M8 2H6V0h2v2zm4 5H2c-.55 0-1-.45-1-1V4c0-.55.45-1 1-1h10l2 2-2 2zM8 4H6v2h2V4zM6 16h2V8H6v8z"></path>`
openMSsvg = `
<span class="issue-state" title="Open Milestone">
<svg height="16" width="14" version="1.1" viewBox="0 0 14 16">
` + rawMSsvg + `
</svg>
</span>
`
closedMSsvg = `
<span class="issue-state" title="Closed Milestone">
<svg aria-hidden="true" class="octicon octicon-check" height="16" height="14" version="1.1" viewBox="0 0 12 16">
<path d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5z"></path>
</svg>
</span>
`
milestonesTemplate = `
<div class="section-github-render">
{{if .HasMilestones}}
<table class="github-table" style="width: 100%;">
<thead>
<tr>
<th class="title">Milestones <span>&middot; {{.ClosedMS}} closed and {{.OpenMS}} open</span>
</th>
<th></th>
</tr>
</thead>
<tbody>
{{range $data := .Milestones}}
<tr>
<td>
{{if $data.IsMilestone}}
{{if $data.IsOpen}}
` + openMSsvg + `
{{else}}
` + closedMSsvg + `
{{end}}
{{end}}
<a class="link" href="{{$data.URL}}">{{$data.Name}}</a>
<span class="data"> &middot; {{if $data.IsMilestone}} {{$data.DueDate}}{{end}} </span>
</td>
<td class="right-column">
{{if $data.IsMilestone}}
<span class="bold color-off-black">{{$data.CompleteMsg}}</span> complete
<span class="bold color-off-black">{{$data.OpenIssues}}</span> open
<span class="bold color-off-black">{{$data.ClosedIssues}}</span> closed
{{else}}
<span class="bold color-off-black">{{$data.OpenIssues}}</span> open <span class="bold color-off-black">{{$data.ClosedIssues}}</span> closed
{{end}}
<div class="progress-bar">
<div class="progress" style="width:{{$data.Progress}}%;"></div>
</div>
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
`
)

View file

@ -0,0 +1,200 @@
// 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 (
"sort"
"strings"
"time"
"github.com/documize/community/core/log"
gogithub "github.com/google/go-github/github"
)
type githubRender struct {
Config githubConfig `json:"config"`
List []githubBranch `json:"list"`
RepoCount int `json:"repoCount"`
ShowList bool `json:"showList"`
ShowIssueNumbers bool `json:"showIssueNumbers"`
BranchCommits []githubCommit `json:"branchCommits"`
HasCommits bool `json:"hasCommits"`
CommitCount int `json:"commitCount"`
Issues []githubIssue `json:"issues"`
HasIssues bool `json:"hasIssues"`
SharedLabels []githubSharedLabel `json:"sharedLabels"`
HasSharedLabels bool `json:"hasSharedLabels"`
OpenIssues int `json:"openIssues"`
ClosedIssues int `json:"closedIssues"`
Limit int `json:"limit"`
Milestones []githubMilestone `json:"milestones"`
HasMilestones bool `json:"hasMilestones"`
OpenMS int `json:"openMS"`
ClosedMS int `json:"closedMS"`
OpenPRs int `json:"openPRs"`
ClosedPRs int `json:"closedPRs"`
AuthorStats []githubAuthorStats `json:"authorStats"`
HasAuthorStats bool `json:"hasAuthorStats"`
NumContributors int `json:"numContributors"`
}
type report struct {
refresh func(*githubRender, *githubConfig, *gogithub.Client) error
render func(*githubRender, *githubConfig) error
template string
}
var reports = make(map[string]report)
type githubOwner struct {
ID string `json:"id"`
Name string `json:"name"`
}
type githubBranch struct {
ID string `json:"id"`
Owner string `json:"owner"`
Repo string `json:"repo"`
Name string `json:"name"`
Included bool `json:"included"`
URL string `json:"url"`
Color string `json:"color,omitempty"`
Comma bool `json:"comma"`
Private bool `json:"private"`
}
type githubLabel struct {
ID string `json:"id"`
Owner string `json:"owner"`
Repo string `json:"repo"`
Name string `json:"name"`
Included bool `json:"included"`
URL string `json:"url"`
Color string `json:"color,omitempty"`
}
type githubConfig struct {
Token string `json:"-"` // NOTE very important that the secret Token is not leaked to the client side, so "-"
UserID string `json:"userId"`
PageID string `json:"pageId"`
Owner string `json:"owner_name"`
BranchSince string `json:"branchSince,omitempty"`
SincePtr *time.Time `json:"-"`
Since string `json:"-"`
BranchLines int `json:"branchLines,omitempty,string"`
OwnerInfo githubOwner `json:"owner"`
ClientID string `json:"clientId"`
CallbackURL string `json:"callbackUrl"`
Lists []githubBranch `json:"lists,omitempty"`
ReportOrder []string `json:"-"`
DateMessage string `json:"-"`
UserNames map[string]string `json:"UserNames"`
ShowMilestones bool `json:"showMilestones,omitempty"`
ShowIssues bool `json:"showIssues,omitempty"`
ShowCommits bool `json:"showCommits,omitempty"`
}
func (c *githubConfig) Clean() {
c.Owner = c.OwnerInfo.Name
if len(c.BranchSince) >= len("yyyy/mm/dd hh:ss") {
var since time.Time
tt := []byte("yyyy-mm-ddThh:mm:00Z")
for _, i := range []int{0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15} {
tt[i] = c.BranchSince[i]
}
err := since.UnmarshalText(tt)
if err != nil {
log.ErrorString("Date unmarshall '" + c.BranchSince + "'->'" + string(tt) + "' error: " + err.Error())
} else {
c.SincePtr = &since
}
}
if c.SincePtr == nil {
c.DateMessage = " (the last 7 days)"
since := time.Now().AddDate(0, 0, -7)
c.SincePtr = &since
} else {
c.DateMessage = ""
}
c.Since = (*c.SincePtr).Format(issuesTimeFormat)
c.ReportOrder = []string{tagSummaryData}
if c.ShowMilestones {
c.ReportOrder = append(c.ReportOrder, tagMilestonesData)
}
if c.ShowIssues {
c.ReportOrder = append(c.ReportOrder, tagIssuesData)
}
if c.ShowCommits {
c.ReportOrder = append(c.ReportOrder, tagCommitsData)
}
c.BranchLines = 100 // overide any existing value with maximum allowable in one call
sort.Sort(branchesToSort(c.Lists)) // get the configured branches in a sensible order for display
lastItem := 0
for i := range c.Lists {
c.Lists[i].Comma = true
if c.Lists[i].Included {
lastItem = i
}
}
if lastItem < len(c.Lists) {
c.Lists[lastItem].Comma = false
}
if c.UserNames == nil {
c.UserNames = make(map[string]string)
}
}
type githubCallbackT struct {
AccessToken string `json:"access_token"`
}
func repoName(branchName string) string {
bits := strings.Split(branchName, "/")
if len(bits) != 2 {
return branchName + "?repo"
}
pieces := strings.Split(bits[1], ":")
if len(pieces) == 0 {
return branchName + "?repo:?branch"
}
return pieces[0]
}
func getUserName(client *gogithub.Client, config *githubConfig, login string) (fullName string) {
an := login
if content, found := config.UserNames[login]; found {
if len(content) > 0 {
an = content
}
} else {
usr, _, err := client.Users.Get(login)
if err == nil {
if usr.Name != nil {
if len(*usr.Name) > 0 {
config.UserNames[login] = *usr.Name
an = *usr.Name
}
}
} else {
config.UserNames[login] = login // don't look again for a missing name
}
}
return an
}

View file

@ -0,0 +1,36 @@
// 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 "sort"
// sort owners in order that that should be presented.
type ownersToSort []githubOwner
func (s ownersToSort) Len() int { return len(s) }
func (s ownersToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ownersToSort) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
func sortOwners(in []githubOwner) []githubOwner {
sts := ownersToSort(in)
sort.Sort(sts)
return []githubOwner(sts)
}
// sort branches in order that that should be presented.
func sortBranches(in []githubBranch) []githubBranch {
sts := branchesToSort(in)
sort.Sort(sts)
return []githubBranch(sts)
}

View file

@ -0,0 +1,42 @@
// 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 (
gogithub "github.com/google/go-github/github"
)
const (
tagSummaryData = "summaryData"
)
// sort branches in order that that should be presented.
type branchesToSort []githubBranch
func (s branchesToSort) Len() int { return len(s) }
func (s branchesToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s branchesToSort) Less(i, j int) bool {
return s[i].URL < s[j].URL
}
func init() {
reports[tagSummaryData] = report{refreshSummary, renderSummary, summaryTemplate}
}
func refreshSummary(gr *githubRender, config *githubConfig, client *gogithub.Client) (err error) {
return nil
}
func renderSummary(payload *githubRender, c *githubConfig) error {
return nil
}

View file

@ -0,0 +1,48 @@
// 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
const summaryTemplate = `
<div class="section-github-render">
<p>Activity since {{.Config.Since}}{{.Config.DateMessage}} for {{.Config.Owner}} repository
{{range $data := .Config.Lists}}
{{if $data.Included}}
<a class="link" href="{{$data.URL}}">
{{$data.Repo}}{{if $data.Comma}},{{end}}
</a>
{{end}}
{{end}}
</p>
<!--
{{if .HasSharedLabels}}
<div class="heading">Labels</div>
<p>There
{{if eq 1 (len .SharedLabels)}} is {{else}} are {{end}}
{{len .SharedLabels}}
shared
{{if eq 1 (len .SharedLabels)}} label {{else}} labels {{end}}
across the repositories.</p>
<table class="github-table">
<tbody>
{{range $slabel := .SharedLabels}}
<tr>
<td class="no-width"><span class="issue-label" style="background-color:#{{$slabel.Color}}">{{$slabel.Name}} ({{$slabel.Count}})</span></td>
<td>{{$slabel.Repos}}</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
-->
</div>
`

View file

@ -0,0 +1,56 @@
// 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 markdown
import (
"net/http"
"github.com/documize/blackfriday"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
)
// Provider represents Markdown
type Provider struct {
Runtime env.Runtime
}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "1470bb4a-36c6-4a98-a443-096f5658378b"
section.Title = "Markdown"
section.Description = "CommonMark based content"
section.ContentType = "markdown"
section.PageType = "section"
section.Order = 9998
return section
}
// Command stub.
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render converts markdown data into HTML suitable for browser rendering.
func (*Provider) Render(ctx *provider.Context, config, data string) string {
result := blackfriday.MarkdownCommon([]byte(data))
return string(result)
}
// Refresh just sends back data as-is.
func (*Provider) Refresh(ctx *provider.Context, config, data string) string {
return data
}

View file

@ -0,0 +1,84 @@
// 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 papertrail
import "strings"
// the HTML that is rendered by this section.
const renderTemplate = `
{{if .HasData}}
<p >The <a href="https://papertrailapp.com">Papertrail log</a> for query <em>{{.Config.Query}}</em> contains {{.Count}} entries.</p>
<table class="basic-table section-papertrail-table">
<thead>
<tr>
<th class="bordered no-width">Date</th>
<th class="bordered no-width">Severity</th>
<th class="bordered">Message</th>
</tr>
</thead>
<tbody>
{{range $item := .Events}}
<tr>
<td class="bordered no-width color-gray">{{ $item.Dated }}</td>
<td class="bordered no-width">{{ $item.Severity }}</td>
<td class="bordered width-90">{{ $item.Message }}</td>
</tr>
{{end}}
</tbody>
</table>
{{else}}
<p>There are no Papertrail log entries to see.</p>
{{end}}
`
// Papertrail helpers
type papertrailRender struct {
Config papertrailConfig
Events []papertrailEvent
Count int
Authenticated bool
HasData bool
}
type papertrailSearch struct {
Events []papertrailEvent `json:"events"`
}
type papertrailEvent struct {
ID string `json:"id"`
Dated string `json:"display_received_at"`
Message string `json:"message"`
Severity string `json:"severity"`
}
type papertrailConfig struct {
APIToken string `json:"APIToken"` // only contains the correct token just after it is typed in
Query string `json:"query"`
Max int `json:"max"`
Group papertrailOption `json:"group"`
System papertrailOption `json:"system"`
}
func (c *papertrailConfig) Clean() {
c.APIToken = strings.TrimSpace(c.APIToken)
c.Query = strings.TrimSpace(c.Query)
}
type papertrailOption struct {
ID int `json:"id"`
Name string `json:"name"`
}
type papertrailOptions struct {
Groups []papertrailOption `json:"groups"`
Systems []papertrailOption `json:"systems"`
}

View file

@ -0,0 +1,301 @@
// 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 papertrail
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/url"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
)
const me = "papertrail"
// Provider represents Papertrail
type Provider struct {
Runtime env.Runtime
}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "db0a3a0a-b5d4-4d00-bfac-ee28abba451d"
section.Title = "Papertrail"
section.Description = "Display log entries"
section.ContentType = "papertrail"
section.PageType = "tab"
return section
}
// Render converts Papertrail data into HTML suitable for browser rendering.
func (*Provider) Render(ctx *provider.Context, config, data string) string {
var search papertrailSearch
var events []papertrailEvent
var payload = papertrailRender{}
var c = papertrailConfig{}
json.Unmarshal([]byte(data), &search)
json.Unmarshal([]byte(config), &c)
c.APIToken = ctx.GetSecrets("APIToken")
max := len(search.Events)
if c.Max < max {
max = c.Max
}
events = search.Events[:max]
payload.Count = len(events)
payload.HasData = payload.Count > 0
payload.Events = events
payload.Config = c
payload.Authenticated = c.APIToken != ""
t := template.New("items")
t, _ = t.Parse(renderTemplate)
buffer := new(bytes.Buffer)
t.Execute(buffer, payload)
return buffer.String()
}
// Command handles authentication, workspace listing and items retrieval.
func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
provider.WriteMessage(w, me, "missing method name")
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, me, "Bad payload")
return
}
var config = papertrailConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
provider.WriteMessage(w, me, "Bad config")
return
}
config.Clean()
if config.APIToken == provider.SecretReplacement || config.APIToken == "" {
config.APIToken = ctx.GetSecrets("APIToken")
}
if len(config.APIToken) == 0 {
provider.WriteMessage(w, me, "Missing API token")
return
}
switch method {
case "auth":
auth(p.Runtime, ctx, config, w, r)
case "options":
options(config, w, r)
}
}
// Refresh just sends back data as-is.
func (p *Provider) Refresh(ctx *provider.Context, config, data string) (newData string) {
var c = papertrailConfig{}
err := json.Unmarshal([]byte(config), &c)
if err != nil {
p.Runtime.Log.Error("unable to read Papertrail config", err)
return
}
c.Clean()
c.APIToken = ctx.GetSecrets("APIToken")
if len(c.APIToken) == 0 {
p.Runtime.Log.Error("missing API token", err)
return
}
result, err := fetchEvents(p.Runtime, c)
if err != nil {
p.Runtime.Log.Error("Papertrail fetchEvents failed", err)
return
}
j, err := json.Marshal(result)
if err != nil {
p.Runtime.Log.Error("unable to marshal Papaertrail events", err)
return
}
newData = string(j)
return
}
func auth(rt env.Runtime, ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, r *http.Request) {
result, err := fetchEvents(rt, config)
if result == nil {
err = errors.New("nil result of papertrail query")
}
if err != nil {
ctx.SaveSecrets(`{"APIToken":""}`) // invalid token, so reset it
if err.Error() == "forbidden" {
provider.WriteForbidden(w)
} else {
provider.WriteError(w, me, err)
}
return
}
ctx.SaveSecrets(`{"APIToken":"` + config.APIToken + `"}`)
provider.WriteJSON(w, result)
}
func options(config papertrailConfig, w http.ResponseWriter, r *http.Request) {
// get systems
req, err := http.NewRequest("GET", "https://papertrailapp.com/api/v1/systems.json", nil)
req.Header.Set("X-Papertrail-Token", config.APIToken)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
provider.WriteError(w, me, err)
return
}
if res.StatusCode != http.StatusOK {
provider.WriteForbidden(w)
return
}
defer res.Body.Close()
var systems []papertrailOption
dec := json.NewDecoder(res.Body)
err = dec.Decode(&systems)
if err != nil {
fmt.Println(err)
provider.WriteError(w, me, err)
return
}
// get groups
req, err = http.NewRequest("GET", "https://papertrailapp.com/api/v1/groups.json", nil)
req.Header.Set("X-Papertrail-Token", config.APIToken)
client = &http.Client{}
res, err = client.Do(req)
if err != nil {
fmt.Println(err)
provider.WriteError(w, me, err)
return
}
if res.StatusCode != http.StatusOK {
provider.WriteForbidden(w)
return
}
defer res.Body.Close()
var groups []papertrailOption
dec = json.NewDecoder(res.Body)
err = dec.Decode(&groups)
if err != nil {
fmt.Println(err)
provider.WriteError(w, me, err)
return
}
var options = papertrailOptions{}
options.Groups = groups
options.Systems = systems
provider.WriteJSON(w, options)
}
func fetchEvents(rt env.Runtime, config papertrailConfig) (result interface{}, err error) {
var filter string
if len(config.Query) > 0 {
filter = fmt.Sprintf("q=%s", url.QueryEscape(config.Query))
}
if config.Group.ID > 0 {
prefix := ""
if len(filter) > 0 {
prefix = "&"
}
filter = fmt.Sprintf("%s%sgroup_id=%d", filter, prefix, config.Group.ID)
}
var req *http.Request
req, err = http.NewRequest("GET", "https://papertrailapp.com/api/v1/events/search.json?"+filter, nil)
if err != nil {
rt.Log.Error("new request", err)
return
}
req.Header.Set("X-Papertrail-Token", config.APIToken)
client := &http.Client{}
var res *http.Response
res, err = client.Do(req)
if err != nil {
rt.Log.Error("message", err)
return
}
if res.StatusCode != http.StatusOK {
rt.Log.Error("forbidden", err)
return
}
defer res.Body.Close()
dec := json.NewDecoder(res.Body)
err = dec.Decode(&result)
if err != nil {
rt.Log.Error("unable to read result", err)
}
return
}

View file

@ -0,0 +1,269 @@
// 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 provider
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sort"
"strings"
"github.com/documize/community/core/api/request"
"github.com/documize/community/core/log"
)
// SecretReplacement is a constant used to replace secrets in data-structures when required.
// 8 stars.
const SecretReplacement = "********"
// sectionsMap is where individual sections register themselves.
var sectionsMap = make(map[string]Provider)
// TypeMeta details a "smart section" that represents a "page" in a document.
type TypeMeta struct {
ID string `json:"id"`
Order int `json:"order"`
ContentType string `json:"contentType"`
PageType string `json:"pageType"`
Title string `json:"title"`
Description string `json:"description"`
Preview bool `json:"preview"` // coming soon!
Callback func(http.ResponseWriter, *http.Request) error `json:"-"`
}
// ConfigHandle returns the key name for database config table
func (t *TypeMeta) ConfigHandle() string {
return fmt.Sprintf("SECTION-%s", strings.ToUpper(t.ContentType))
}
// Provider represents a 'page' in a document.
type Provider interface {
Meta() TypeMeta // Meta returns section details
Command(ctx *Context, w http.ResponseWriter, r *http.Request) // Command is general-purpose method that can return data to UI
Render(ctx *Context, config, data string) string // Render converts section data into presentable HTML
Refresh(ctx *Context, config, data string) string // Refresh returns latest data
}
// Context describes the environment the section code runs in
type Context struct {
OrgID string
UserID string
prov Provider
inCommand bool
}
// NewContext is a convenience function.
func NewContext(orgid, userid string) *Context {
if orgid == "" || userid == "" {
log.Error("NewContext incorrect orgid:"+orgid+" userid:"+userid, errors.New("bad section context"))
}
return &Context{OrgID: orgid, UserID: userid}
}
// Register makes document section type available
func Register(name string, p Provider) {
sectionsMap[name] = p
}
// List returns available types
func List() map[string]Provider {
return sectionsMap
}
// GetSectionMeta returns a list of smart sections.
func GetSectionMeta() []TypeMeta {
sections := []TypeMeta{}
for _, section := range sectionsMap {
sections = append(sections, section.Meta())
}
return sortSections(sections)
}
// Command passes parameters to the given section id, the returned bool indicates success.
func Command(section string, ctx *Context, w http.ResponseWriter, r *http.Request) bool {
s, ok := sectionsMap[section]
if ok {
ctx.prov = s
ctx.inCommand = true
s.Command(ctx, w, r)
}
return ok
}
// Callback passes parameters to the given section callback, the returned error indicates success.
func Callback(section string, w http.ResponseWriter, r *http.Request) error {
s, ok := sectionsMap[section]
if ok {
if cb := s.Meta().Callback; cb != nil {
return cb(w, r)
}
}
return errors.New("section not found")
}
// Render runs that operation for the given section id, the returned bool indicates success.
func Render(section string, ctx *Context, config, data string) (string, bool) {
s, ok := sectionsMap[section]
if ok {
ctx.prov = s
return s.Render(ctx, config, data), true
}
return "", false
}
// Refresh returns the latest data for a section.
func Refresh(section string, ctx *Context, config, data string) (string, bool) {
s, ok := sectionsMap[section]
if ok {
ctx.prov = s
return s.Refresh(ctx, config, data), true
}
return "", false
}
// WriteJSON writes data as JSON to HTTP response.
func WriteJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
j, err := json.Marshal(v)
if err != nil {
WriteMarshalError(w, err)
return
}
_, err = w.Write(j)
log.IfErr(err)
}
// WriteString writes string tp HTTP response.
func WriteString(w http.ResponseWriter, data string) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(data))
log.IfErr(err)
}
// WriteEmpty returns just OK to HTTP response.
func WriteEmpty(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("{}"))
log.IfErr(err)
}
// WriteMarshalError write JSON marshalling error to HTTP response.
func WriteMarshalError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}"))
log.IfErr(err2)
log.Error("JSON marshall failed", err)
}
// WriteMessage write string to HTTP response.
func WriteMessage(w http.ResponseWriter, section, msg string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("{Message: " + msg + "}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Error for section %s: %s", section, msg))
}
// WriteError write given error to HTTP response.
func WriteError(w http.ResponseWriter, section string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'Internal server error'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("Error for section %s", section), err)
}
// WriteForbidden write 403 to HTTP response.
func WriteForbidden(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte("{Error: 'Unauthorized'}"))
log.IfErr(err)
}
// Secrets handling
// SaveSecrets for the current user/org combination.
// The secrets must be in the form of a JSON format string, for example `{"mysecret":"lover"}`.
// An empty string signifies no valid secrets for this user/org combination.
// Note that this function can only be called within the Command method of a section.
func (c *Context) SaveSecrets(JSONobj string) error {
if !c.inCommand {
return errors.New("SaveSecrets() may only be called from within Command()")
}
m := c.prov.Meta()
return request.UserConfigSetJSON(c.OrgID, c.UserID, m.ContentType, JSONobj)
}
// MarshalSecrets to the database.
// Parameter the same as for json.Marshal().
func (c *Context) MarshalSecrets(sec interface{}) error {
if !c.inCommand {
return errors.New("MarshalSecrets() may only be called from within Command()")
}
byts, err := json.Marshal(sec)
if err != nil {
return err
}
return c.SaveSecrets(string(byts))
}
// GetSecrets for the current context user/org.
// For example (see SaveSecrets example): thisContext.GetSecrets("mysecret")
// JSONpath format is defined at https://dev.mysql.com/doc/refman/5.7/en/json-path-syntax.html .
// An empty JSONpath returns the whole JSON object, as JSON.
// Errors return the empty string.
func (c *Context) GetSecrets(JSONpath string) string {
m := c.prov.Meta()
return request.UserConfigGetJSON(c.OrgID, c.UserID, m.ContentType, JSONpath)
}
// ErrNoSecrets is returned if no secret is found in the database.
var ErrNoSecrets = errors.New("no secrets in database")
// UnmarshalSecrets from the database.
// Parameter the same as for "v" in json.Unmarshal().
func (c *Context) UnmarshalSecrets(v interface{}) error {
secTxt := c.GetSecrets("") // get all the json of the secrets
if len(secTxt) > 0 {
return json.Unmarshal([]byte(secTxt), v)
}
return ErrNoSecrets
}
// sort sections in order that that should be presented.
type sectionsToSort []TypeMeta
func (s sectionsToSort) Len() int { return len(s) }
func (s sectionsToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sectionsToSort) Less(i, j int) bool {
if s[i].Order == s[j].Order {
return s[i].Title < s[j].Title
}
return s[i].Order > s[j].Order
}
func sortSections(in []TypeMeta) []TypeMeta {
sts := sectionsToSort(in)
sort.Sort(sts)
return []TypeMeta(sts)
}

View file

@ -0,0 +1,45 @@
// 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 section
import (
"fmt"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/airtable"
"github.com/documize/community/domain/section/code"
"github.com/documize/community/domain/section/gemini"
"github.com/documize/community/domain/section/github"
"github.com/documize/community/domain/section/markdown"
"github.com/documize/community/domain/section/papertrail"
"github.com/documize/community/domain/section/provider"
"github.com/documize/community/domain/section/table"
"github.com/documize/community/domain/section/trello"
"github.com/documize/community/domain/section/wysiwyg"
)
// Register sections
func Register(rt env.Runtime) {
provider.Register("code", &code.Provider{Runtime: rt})
provider.Register("gemini", &gemini.Provider{Runtime: rt})
provider.Register("github", &github.Provider{Runtime: rt})
provider.Register("markdown", &markdown.Provider{Runtime: rt})
provider.Register("papertrail", &papertrail.Provider{Runtime: rt})
provider.Register("table", &table.Provider{Runtime: rt})
provider.Register("code", &code.Provider{Runtime: rt})
provider.Register("trello", &trello.Provider{Runtime: rt})
provider.Register("wysiwyg", &wysiwyg.Provider{Runtime: rt})
provider.Register("airtable", &airtable.Provider{Runtime: rt})
p := provider.List()
rt.Log.Info(fmt.Sprintf("Registered %d sections", len(p)))
}

View file

@ -0,0 +1,82 @@
// 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 section
import (
"net/http"
"testing"
"github.com/documize/community/core/section/provider"
)
type testsection provider.TypeMeta
var ts testsection
func init() {
provider.Register("testsection", &ts)
}
// Command is an end-point...
func (ts *testsection) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {}
var didRefresh bool
// Refresh existing data, returning data in the format of the target system
func (ts *testsection) Refresh(ctx *provider.Context, meta, data string) string {
didRefresh = true
return ""
}
// Render converts data in the target system format into HTML
func (*testsection) Render(ctx *provider.Context, meta, data string) string {
return "testsection " + data
}
func (*testsection) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "TestGUID"
section.Title = "TestSection"
section.Description = "A Test Section"
section.ContentType = "testsection"
return section
}
func TestSection(t *testing.T) {
ctx := provider.NewContext("_orgid_", "_userid_")
if _, ok := provider.Refresh("testsection", ctx, "", ""); !ok {
t.Error("did not find 'testsection' smart section (1)")
}
if !didRefresh {
t.Error("did not run the test Refresh method")
}
out, ok := provider.Render("testsection", ctx, "meta", "dingbat")
if !ok {
t.Error("did not find 'testsection' smart section (2)")
}
if out != "testsection dingbat" {
t.Error("wrong output from Render")
}
sects := provider.GetSectionMeta()
for _, v := range sects {
if v.Title == "TestSection" {
return
}
//t.Logf("%v %v", v.Order, v.Title)
}
t.Error("TestSection not in meta output")
}

View file

@ -0,0 +1,53 @@
// 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 table
import (
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
)
// Provider represents Table
type Provider struct {
Runtime env.Runtime
}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "81a2ea93-2dfc-434d-841e-54b832492c92"
section.Title = "Tabular"
section.Description = "Rows, columns for tabular data"
section.ContentType = "table"
section.PageType = "section"
section.Order = 9996
return section
}
// Command stub.
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render sends back data as-is (HTML).
func (*Provider) Render(ctx *provider.Context, config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*Provider) Refresh(ctx *provider.Context, config, data string) string {
return data
}

View file

@ -0,0 +1,204 @@
// 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 trello
import "strings"
const renderTemplate = `
<div class="section-trello-render">
<p>There are {{ .CardCount }} cards across {{ .ListCount }} lists for board <a href="{{ .Board.URL }}">{{.Board.Name}}.</a></p>
<div class="trello-board" style="background-color: {{.Board.Prefs.BackgroundColor}}">
<a href="{{ .Board.URL }}"><div class="trello-board-title">{{.Board.Name}}</div></a>
{{range $data := .Data}}
<div class="trello-list">
<div class="trello-list-title">{{ $data.List.Name }}</div>
{{range $card := $data.Cards}}
<a href="{{ $card.URL }}">
<div class="trello-card">
{{ $card.Name }}
</div>
</a>
{{end}}
</div>
{{end}}
</div>
</div>
`
type secrets struct {
Token string `json:"token"`
}
type trelloConfig struct {
AppKey string `json:"appKey"`
Token string `json:"token"`
Board trelloBoard `json:"board"`
Lists []trelloList `json:"lists"`
}
func (c *trelloConfig) Clean() {
c.AppKey = strings.TrimSpace(c.AppKey)
c.Token = strings.TrimSpace(c.Token)
}
// Trello objects based upon https://github.com/VojtechVitek/go-trello
type trelloMember struct {
ID string `json:"id"`
AvatarHash string `json:"avatarHash"`
Bio string `json:"bio"`
BioData struct {
Emoji interface{} `json:"emoji,omitempty"`
} `json:"bioData"`
Confirmed bool `json:"confirmed"`
FullName string `json:"fullName"`
PremOrgsAdminID []string `json:"idPremOrgsAdmin"`
Initials string `json:"initials"`
MemberType string `json:"memberType"`
Products []int `json:"products"`
Status string `json:"status"`
URL string `json:"url"`
Username string `json:"username"`
AvatarSource string `json:"avatarSource"`
Email string `json:"email"`
GravatarHash string `json:"gravatarHash"`
BoardsID []string `json:"idBoards"`
BoardsPinnedID []string `json:"idBoardsPinned"`
OrganizationsID []string `json:"idOrganizations"`
LoginTypes []string `json:"loginTypes"`
NewEmail string `json:"newEmail"`
OneTimeMessagesDismissed []string `json:"oneTimeMessagesDismissed"`
Prefs struct {
SendSummaries bool `json:"sendSummaries"`
MinutesBetweenSummaries int `json:"minutesBetweenSummaries"`
MinutesBeforeDeadlineToNotify int `json:"minutesBeforeDeadlineToNotify"`
ColorBlind bool `json:"colorBlind"`
Locale string `json:"locale"`
} `json:"prefs"`
Trophies []string `json:"trophies"`
UploadedAvatarHash string `json:"uploadedAvatarHash"`
PremiumFeatures []string `json:"premiumFeatures"`
}
type trelloBoard struct {
ID string `json:"id"`
Name string `json:"name"`
Closed bool `json:"closed"`
OrganizationID string `json:"idOrganization"`
Pinned bool `json:"pinned"`
URL string `json:"url"`
ShortURL string `json:"shortUrl"`
Desc string `json:"desc"`
DescData struct {
Emoji struct{} `json:"emoji"`
} `json:"descData"`
Prefs struct {
PermissionLevel string `json:"permissionLevel"`
Voting string `json:"voting"`
Comments string `json:"comments"`
Invitations string `json:"invitations"`
SelfJoin bool `json:"selfjoin"`
CardCovers bool `json:"cardCovers"`
CardAging string `json:"cardAging"`
CalendarFeedEnabled bool `json:"calendarFeedEnabled"`
Background string `json:"background"`
BackgroundColor string `json:"backgroundColor"`
BackgroundImage string `json:"backgroundImage"`
BackgroundImageScaled []trelloBoardBackground `json:"backgroundImageScaled"`
BackgroundTile bool `json:"backgroundTile"`
BackgroundBrightness string `json:"backgroundBrightness"`
CanBePublic bool `json:"canBePublic"`
CanBeOrg bool `json:"canBeOrg"`
CanBePrivate bool `json:"canBePrivate"`
CanInvite bool `json:"canInvite"`
} `json:"prefs"`
LabelNames struct {
Red string `json:"red"`
Orange string `json:"orange"`
Yellow string `json:"yellow"`
Green string `json:"green"`
Blue string `json:"blue"`
Purple string `json:"purple"`
} `json:"labelNames"`
}
type trelloBoardBackground struct {
Width int `json:"width"`
Height int `json:"height"`
URL string `json:"url"`
}
type trelloList struct {
ID string `json:"id"`
Name string `json:"name"`
Closed bool `json:"closed"`
BoardID string `json:"idBoard"`
Pos float32 `json:"pos"`
Included bool `json:"included"` // indicates whether we display cards from this list
}
type trelloCard struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
ShortID int `json:"idShort"`
AttachmentCoverID string `json:"idAttachmentCover"`
CheckListsID []string `json:"idCheckLists"`
BoardID string `json:"idBoard"`
ListID string `json:"idList"`
MembersID []string `json:"idMembers"`
MembersVotedID []string `json:"idMembersVoted"`
ManualCoverAttachment bool `json:"manualCoverAttachment"`
Closed bool `json:"closed"`
Pos float32 `json:"pos"`
ShortLink string `json:"shortLink"`
DateLastActivity string `json:"dateLastActivity"`
ShortURL string `json:"shortUrl"`
Subscribed bool `json:"subscribed"`
URL string `json:"url"`
Due string `json:"due"`
Desc string `json:"desc"`
DescData struct {
Emoji struct{} `json:"emoji"`
} `json:"descData"`
CheckItemStates []struct {
CheckItemID string `json:"idCheckItem"`
State string `json:"state"`
} `json:"checkItemStates"`
Badges struct {
Votes int `json:"votes"`
ViewingMemberVoted bool `json:"viewingMemberVoted"`
Subscribed bool `json:"subscribed"`
Fogbugz string `json:"fogbugz"`
CheckItems int `json:"checkItems"`
CheckItemsChecked int `json:"checkItemsChecked"`
Comments int `json:"comments"`
Attachments int `json:"attachments"`
Description bool `json:"description"`
Due string `json:"due"`
} `json:"badges"`
Labels []struct {
Color string `json:"color"`
Name string `json:"name"`
} `json:"labels"`
}
type trelloListCards struct {
List trelloList
Cards []trelloCard
}
type trelloRender struct {
Board trelloBoard
Data []trelloListCards
CardCount int
ListCount int
}

View file

@ -0,0 +1,300 @@
// 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 trello
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"github.com/documize/community/core/api/request"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
)
var meta provider.TypeMeta
func init() {
meta = provider.TypeMeta{}
meta.ID = "c455a552-202e-441c-ad79-397a8152920b"
meta.Title = "Trello"
meta.Description = "Embed cards from boards and lists"
meta.ContentType = "trello"
meta.PageType = "tab"
}
// Provider represents Trello
type Provider struct {
Runtime env.Runtime
}
// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
return meta
}
// Command stub.
func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, "trello", "Bad body")
return
}
var config = trelloConfig{}
err = json.Unmarshal(body, &config)
if err != nil {
provider.WriteError(w, "trello", err)
return
}
config.Clean()
config.AppKey = request.ConfigString(meta.ConfigHandle(), "appKey")
if len(config.AppKey) == 0 {
p.Runtime.Log.Info("missing trello App Key")
provider.WriteMessage(w, "trello", "Missing appKey")
return
}
if len(config.Token) == 0 {
config.Token = ctx.GetSecrets("token") // get a token, if we have one
}
if method != "config" {
if len(config.Token) == 0 {
provider.WriteMessage(w, "trello", "Missing token")
return
}
}
switch method {
case "cards":
render, err := getCards(config)
if err != nil {
p.Runtime.Log.Error("failed to render cards", err)
provider.WriteError(w, "trello", err)
ctx.SaveSecrets("") // failure means our secrets are invalid
return
}
provider.WriteJSON(w, render)
case "boards":
render, err := getBoards(config)
if err != nil {
p.Runtime.Log.Error("failed to render board", err)
provider.WriteError(w, "trello", err)
ctx.SaveSecrets("") // failure means our secrets are invalid
return
}
provider.WriteJSON(w, render)
case "lists":
render, err := getLists(config)
if err != nil {
p.Runtime.Log.Error("failed to get Trello lists", err)
provider.WriteError(w, "trello", err)
ctx.SaveSecrets("") // failure means our secrets are invalid
return
}
provider.WriteJSON(w, render)
case "config":
var ret struct {
AppKey string `json:"appKey"`
Token string `json:"token"`
}
ret.AppKey = config.AppKey
if config.Token != "" {
ret.Token = provider.SecretReplacement
}
provider.WriteJSON(w, ret)
return
default:
p.Runtime.Log.Info("unknown trello method called: " + method)
provider.WriteMessage(w, "trello", "missing method name")
return
}
// the token has just worked, so save it as our secret
var s secrets
s.Token = config.Token
b, e := json.Marshal(s)
if err != nil {
p.Runtime.Log.Error("failed save Trello secrets", e)
}
ctx.SaveSecrets(string(b))
}
// Render just sends back HMTL as-is.
func (*Provider) Render(ctx *provider.Context, config, data string) string {
raw := []trelloListCards{}
payload := trelloRender{}
var c = trelloConfig{}
json.Unmarshal([]byte(data), &raw)
json.Unmarshal([]byte(config), &c)
payload.Board = c.Board
payload.Data = raw
payload.ListCount = len(raw)
for _, list := range raw {
payload.CardCount += len(list.Cards)
}
t := template.New("trello")
t, _ = t.Parse(renderTemplate)
buffer := new(bytes.Buffer)
t.Execute(buffer, payload)
return buffer.String()
}
// Refresh just sends back data as-is.
func (p *Provider) Refresh(ctx *provider.Context, config, data string) string {
var c = trelloConfig{}
json.Unmarshal([]byte(config), &c)
refreshed, err := getCards(c)
if err != nil {
return data
}
j, err := json.Marshal(refreshed)
if err != nil {
p.Runtime.Log.Error("failed to marshal trello data", err)
return data
}
return string(j)
}
// Helpers
func getBoards(config trelloConfig) (boards []trelloBoard, err error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.trello.com/1/members/me/boards?fields=id,name,url,closed,prefs,idOrganization&key=%s&token=%s", config.AppKey, config.Token), nil)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: HTTP status code %d", res.StatusCode)
}
b := []trelloBoard{}
defer res.Body.Close()
dec := json.NewDecoder(res.Body)
err = dec.Decode(&b)
// we only show open, team boards (not personal)
for _, b := range b {
if !b.Closed && len(b.OrganizationID) > 0 {
boards = append(boards, b)
}
}
if err != nil {
return nil, err
}
return boards, nil
}
func getLists(config trelloConfig) (lists []trelloList, err error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.trello.com/1/boards/%s/lists/open?key=%s&token=%s", config.Board.ID, config.AppKey, config.Token), nil)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: HTTP status code %d", res.StatusCode)
}
defer res.Body.Close()
dec := json.NewDecoder(res.Body)
err = dec.Decode(&lists)
if err != nil {
return nil, err
}
return lists, nil
}
func getCards(config trelloConfig) (listCards []trelloListCards, err error) {
for _, list := range config.Lists {
// don't process lists that user excluded from rendering
if !list.Included {
continue
}
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.trello.com/1/lists/%s/cards?key=%s&token=%s", list.ID, config.AppKey, config.Token), nil)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: HTTP status code %d", res.StatusCode)
}
defer res.Body.Close()
var cards []trelloCard
dec := json.NewDecoder(res.Body)
err = dec.Decode(&cards)
if err != nil {
return nil, err
}
data := trelloListCards{}
data.Cards = cards
data.List = list
listCards = append(listCards, data)
}
return listCards, nil
}

View file

@ -0,0 +1,53 @@
// 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 wysiwyg
import (
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
)
// Provider represents WYSIWYG
type Provider struct {
Runtime env.Runtime
}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "0f024fa0-d017-4bad-a094-2c13ce6edad7"
section.Title = "Rich Text"
section.Description = "Rich text WYSIWYG"
section.ContentType = "wysiwyg"
section.PageType = "section"
section.Order = 9999
return section
}
// Command stub.
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}
// Render returns data as-is (HTML).
func (*Provider) Render(ctx *provider.Context, config, data string) string {
return data
}
// Refresh just sends back data as-is.
func (*Provider) Refresh(ctx *provider.Context, config, data string) string {
return data
}