mirror of
https://github.com/documize/community.git
synced 2025-07-24 15:49:44 +02:00
restructure directories
This commit is contained in:
parent
7e4ed6545b
commit
a2ce777762
159 changed files with 320 additions and 323 deletions
50
core/section/code/code.go
Normal file
50
core/section/code/code.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
// 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/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents code snippet
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
// 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 samples supporting 50+ languages"
|
||||
section.ContentType = "code"
|
||||
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
|
||||
}
|
379
core/section/gemini/gemini.go
Normal file
379
core/section/gemini/gemini.go
Normal file
|
@ -0,0 +1,379 @@
|
|||
// 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/section/provider"
|
||||
"github.com/documize/community/core/log"
|
||||
)
|
||||
|
||||
// Provider represents Gemini
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
// Meta describes us.
|
||||
func (*Provider) Meta() provider.TypeMeta {
|
||||
section := provider.TypeMeta{}
|
||||
section.ID = "23b133f9-4020-4616-9291-a98fb939735f"
|
||||
section.Title = "Gemini"
|
||||
section.Description = "Display work items and tickets from workspaces"
|
||||
section.ContentType = "gemini"
|
||||
|
||||
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 (*Provider) Refresh(ctx *provider.Context, config, data string) (newData string) {
|
||||
var c = geminiConfig{}
|
||||
err := json.Unmarshal([]byte(config), &c)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Unable to read Gemini config", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Clean(ctx)
|
||||
|
||||
if len(c.URL) == 0 {
|
||||
log.Info("Gemini.Refresh received empty URL")
|
||||
return
|
||||
}
|
||||
|
||||
if len(c.Username) == 0 {
|
||||
log.Info("Gemini.Refresh received empty username")
|
||||
return
|
||||
}
|
||||
|
||||
if len(c.APIKey) == 0 {
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
j, err := json.Marshal(items)
|
||||
|
||||
if err != nil {
|
||||
log.Error("unable to marshall 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 {
|
||||
fmt.Println(err)
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
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, err := getSecrets(ctx)
|
||||
log.IfErr(err)
|
||||
provider.WriteJSON(w, sec)
|
||||
}
|
123
core/section/gemini/model.go
Normal file
123
core/section/gemini/model.go
Normal file
|
@ -0,0 +1,123 @@
|
|||
// 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/core/section/provider"
|
||||
"github.com/documize/community/core/log"
|
||||
)
|
||||
|
||||
// the HTML that is rendered by this section.
|
||||
const renderTemplate = `
|
||||
{{if .Authenticated}}
|
||||
<p class="margin-left-20">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 }}' /> {{ $item.Type }}</td>
|
||||
<td class="bordered no-width"><img src='{{ $item.StatusImage }}' /> {{ $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)
|
||||
log.IfErr(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
|
||||
}
|
857
core/section/github/github.go
Normal file
857
core/section/github/github.go
Normal file
|
@ -0,0 +1,857 @@
|
|||
// 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"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/core/api/request"
|
||||
"github.com/documize/community/core/section/provider"
|
||||
"github.com/documize/community/core/log"
|
||||
|
||||
gogithub "github.com/google/go-github/github"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// 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.Callback = Callback
|
||||
}
|
||||
|
||||
// Provider represents GitHub
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
// Meta describes us.
|
||||
func (*Provider) Meta() provider.TypeMeta {
|
||||
return meta
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 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"
|
||||
log.ErrorString("github: " + msg)
|
||||
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 {
|
||||
msg := "Bad body"
|
||||
log.ErrorString("github: " + msg)
|
||||
provider.WriteMessage(w, "gitub", msg)
|
||||
return
|
||||
}
|
||||
|
||||
// get the secret token in the database
|
||||
ptoken := ctx.GetSecrets("token")
|
||||
|
||||
switch method {
|
||||
|
||||
case "saveSecret": // secret Token update code
|
||||
|
||||
// write the new one, direct from JS
|
||||
if err = ctx.SaveSecrets(string(body)); err != nil {
|
||||
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 {
|
||||
log.Error("github Command Unmarshal", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
|
||||
config.Clean()
|
||||
// always use DB version of the token
|
||||
config.Token = ptoken
|
||||
|
||||
client := p.githubClient(config)
|
||||
|
||||
switch method { // the main data handling switch
|
||||
|
||||
case "checkAuth":
|
||||
|
||||
if len(ptoken) == 0 {
|
||||
err = errors.New("empty github token")
|
||||
} else {
|
||||
err = validateToken(ptoken)
|
||||
}
|
||||
if err != nil {
|
||||
// token now invalid, so wipe it
|
||||
ctx.SaveSecrets("") // ignore error, already in an error state
|
||||
log.Error("github check token validation", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
provider.WriteEmpty(w)
|
||||
return
|
||||
|
||||
case tagCommitsData:
|
||||
|
||||
render, err := p.getCommits(client, config)
|
||||
if err != nil {
|
||||
log.Error("github getCommits:", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
|
||||
provider.WriteJSON(w, render)
|
||||
|
||||
case tagIssuesData:
|
||||
|
||||
render, err := p.getIssues(client, config)
|
||||
if err != nil {
|
||||
log.Error("github getIssues:", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
|
||||
provider.WriteJSON(w, render)
|
||||
|
||||
/*case "issuenum_data":
|
||||
|
||||
render, err := t.getIssueNum(client, config)
|
||||
if err != nil {
|
||||
log.Error("github getIssueNum:", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
|
||||
provider.WriteJSON(w, render)*/
|
||||
|
||||
case "owners":
|
||||
|
||||
me, _, err := client.Users.Get("")
|
||||
if err != nil {
|
||||
log.Error("github get user details:", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
|
||||
orgs, _, err := client.Organizations.List("", nil)
|
||||
if err != nil {
|
||||
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 "repos":
|
||||
|
||||
var render []githubRepo
|
||||
if config.Owner != "" {
|
||||
|
||||
me, _, err := client.Users.Get("")
|
||||
if err != nil {
|
||||
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 {
|
||||
log.Error("github get user/org repositories:", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
for _, vr := range repos {
|
||||
private := ""
|
||||
if *vr.Private {
|
||||
private = " (private)"
|
||||
}
|
||||
render = append(render,
|
||||
githubRepo{
|
||||
Name: config.Owner + "/" + *vr.Name + private,
|
||||
ID: fmt.Sprintf("%s:%s", config.Owner, *vr.Name),
|
||||
Owner: config.Owner,
|
||||
Repo: *vr.Name,
|
||||
Private: *vr.Private,
|
||||
URL: *vr.HTMLURL,
|
||||
})
|
||||
}
|
||||
}
|
||||
render = sortRepos(render)
|
||||
|
||||
provider.WriteJSON(w, render)
|
||||
|
||||
case "branches":
|
||||
|
||||
if config.Owner == "" || config.Repo == "" {
|
||||
provider.WriteJSON(w, []githubBranch{}) // we have nothing to return
|
||||
return
|
||||
}
|
||||
branches, _, err := client.Repositories.ListBranches(config.Owner, config.Repo,
|
||||
&gogithub.ListOptions{PerPage: 100})
|
||||
if err != nil {
|
||||
log.Error("github get branch details:", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
render := make([]githubBranch, len(branches))
|
||||
for kc, vb := range branches {
|
||||
render[kc] = githubBranch{
|
||||
Name: *vb.Name,
|
||||
ID: fmt.Sprintf("%s:%s:%s", config.Owner, config.Repo, *vb.Name),
|
||||
Included: false,
|
||||
URL: "https://github.com/" + config.Owner + "/" + config.Repo + "/tree/" + *vb.Name,
|
||||
}
|
||||
}
|
||||
|
||||
provider.WriteJSON(w, render)
|
||||
|
||||
case "labels":
|
||||
|
||||
if config.Owner == "" || config.Repo == "" {
|
||||
provider.WriteJSON(w, []githubBranch{}) // we have nothing to return
|
||||
return
|
||||
}
|
||||
labels, _, err := client.Issues.ListLabels(config.Owner, config.Repo,
|
||||
&gogithub.ListOptions{PerPage: 100})
|
||||
if err != nil {
|
||||
log.Error("github get labels:", err)
|
||||
provider.WriteError(w, "github", err)
|
||||
return
|
||||
}
|
||||
render := make([]githubBranch, len(labels))
|
||||
for kc, vb := range labels {
|
||||
render[kc] = githubBranch{
|
||||
Name: *vb.Name,
|
||||
ID: fmt.Sprintf("%s:%s:%s", config.Owner, config.Repo, *vb.Name),
|
||||
Included: false,
|
||||
Color: *vb.Color,
|
||||
}
|
||||
}
|
||||
|
||||
provider.WriteJSON(w, render)
|
||||
|
||||
default:
|
||||
|
||||
log.ErrorString("Github connector unknown method: " + method)
|
||||
provider.WriteEmpty(w)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
/*
|
||||
func (*Provider) getIssueNum(client *gogithub.Client, config githubConfig) ([]githubIssueActivity, error) {
|
||||
|
||||
ret := []githubIssueActivity{}
|
||||
|
||||
issue, _, err := client.Issues.Get(config.Owner, config.Repo, config.IssueNum)
|
||||
|
||||
if err == nil {
|
||||
n := ""
|
||||
a := ""
|
||||
p := issue.User
|
||||
if p != nil {
|
||||
if p.Login != nil {
|
||||
n = *p.Login
|
||||
}
|
||||
if p.AvatarURL != nil {
|
||||
a = *p.AvatarURL
|
||||
}
|
||||
}
|
||||
ret = append(ret, githubIssueActivity{
|
||||
Name: n,
|
||||
Event: "created",
|
||||
Message: template.HTML(*issue.Title),
|
||||
Date: "on " + issue.UpdatedAt.Format("January 2 2006, 15:04"),
|
||||
Avatar: a,
|
||||
URL: template.URL(*issue.HTMLURL),
|
||||
})
|
||||
ret = append(ret, githubIssueActivity{
|
||||
Name: n,
|
||||
Event: "described",
|
||||
Message: template.HTML(*issue.Body),
|
||||
Date: "on " + issue.UpdatedAt.Format("January 2 2006, 15:04"),
|
||||
Avatar: a,
|
||||
URL: template.URL(*issue.HTMLURL),
|
||||
})
|
||||
ret = append(ret, githubIssueActivity{
|
||||
Name: "",
|
||||
Event: "Note",
|
||||
Message: template.HTML("the issue timeline below is in reverse order"),
|
||||
Date: "",
|
||||
Avatar: githubGravatar,
|
||||
URL: template.URL(*issue.HTMLURL),
|
||||
})
|
||||
} else {
|
||||
return ret, err
|
||||
}
|
||||
|
||||
opts := &gogithub.ListOptions{PerPage: config.BranchLines}
|
||||
|
||||
guff, _, err := client.Issues.ListIssueTimeline(config.Owner, config.Repo, config.IssueNum, opts)
|
||||
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
|
||||
for _, v := range guff {
|
||||
if config.SincePtr == nil || v.CreatedAt.After(*config.SincePtr) {
|
||||
var n, a, m, u string
|
||||
|
||||
p := v.Actor
|
||||
if p != nil {
|
||||
if p.Name != nil {
|
||||
n = *p.Name
|
||||
}
|
||||
if p.AvatarURL != nil {
|
||||
a = *p.AvatarURL
|
||||
}
|
||||
}
|
||||
|
||||
u = fmt.Sprintf("https://github.com/%s/%s/issues/%d#event-%d",
|
||||
config.Owner, config.Repo, config.IssueNum, *v.ID)
|
||||
|
||||
switch *v.Event {
|
||||
case "commented":
|
||||
ic, _, err := client.Issues.GetComment(config.Owner, config.Repo, *v.ID)
|
||||
if err != nil {
|
||||
log.ErrorString("github error fetching issue event comment: " + err.Error())
|
||||
} else {
|
||||
m = *ic.Body
|
||||
u = *ic.HTMLURL
|
||||
p := ic.User
|
||||
if p != nil {
|
||||
if p.Login != nil {
|
||||
n = *p.Login
|
||||
}
|
||||
if p.AvatarURL != nil {
|
||||
a = *p.AvatarURL
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret = append(ret, githubIssueActivity{
|
||||
Name: n,
|
||||
Event: *v.Event,
|
||||
Message: template.HTML(m),
|
||||
Date: "on " + v.CreatedAt.Format("January 2 2006, 15:04"),
|
||||
Avatar: a,
|
||||
URL: template.URL(u),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
func wrapLabels(labels []gogithub.Label) string {
|
||||
l := ""
|
||||
for _, ll := range labels {
|
||||
l += `<span class="github-issue-label" style="background-color:#` + *ll.Color + `">` + *ll.Name + `</span> `
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (*Provider) getIssues(client *gogithub.Client, config githubConfig) ([]githubIssue, error) {
|
||||
|
||||
ret := []githubIssue{}
|
||||
|
||||
isRequired := make([]int, 0, 10)
|
||||
for _, s := range strings.Split(strings.Replace(config.IssuesText, "#", "", -1), ",") {
|
||||
i, err := strconv.Atoi(strings.TrimSpace(s))
|
||||
if err == nil {
|
||||
isRequired = append(isRequired, i)
|
||||
}
|
||||
}
|
||||
if len(isRequired) > 0 {
|
||||
|
||||
for _, i := range isRequired {
|
||||
|
||||
issue, _, err := client.Issues.Get(config.Owner, config.Repo, i)
|
||||
|
||||
if err == nil {
|
||||
n := ""
|
||||
p := issue.User
|
||||
if p != nil {
|
||||
if p.Login != nil {
|
||||
n = *p.Login
|
||||
}
|
||||
}
|
||||
l := wrapLabels(issue.Labels)
|
||||
ret = append(ret, githubIssue{
|
||||
Name: n,
|
||||
Message: *issue.Title,
|
||||
Date: issue.CreatedAt.Format("January 2 2006, 15:04"),
|
||||
Updated: issue.UpdatedAt.Format("January 2 2006, 15:04"),
|
||||
URL: template.URL(*issue.HTMLURL),
|
||||
Labels: template.HTML(l),
|
||||
ID: *issue.Number,
|
||||
IsOpen: *issue.State == "open",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
opts := &gogithub.IssueListByRepoOptions{
|
||||
Sort: "updated",
|
||||
State: config.IssueState.ID,
|
||||
ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}}
|
||||
|
||||
if config.SincePtr != nil {
|
||||
opts.Since = *config.SincePtr
|
||||
}
|
||||
|
||||
for _, lab := range config.Lists {
|
||||
if lab.Included {
|
||||
opts.Labels = append(opts.Labels, lab.Name)
|
||||
}
|
||||
}
|
||||
|
||||
guff, _, err := client.Issues.ListByRepo(config.Owner, config.Repo, opts)
|
||||
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
|
||||
for _, v := range guff {
|
||||
n := ""
|
||||
ptr := v.User
|
||||
if ptr != nil {
|
||||
if ptr.Login != nil {
|
||||
n = *ptr.Login
|
||||
}
|
||||
}
|
||||
l := wrapLabels(v.Labels)
|
||||
ret = append(ret, githubIssue{
|
||||
Name: n,
|
||||
Message: *v.Title,
|
||||
Date: v.CreatedAt.Format("January 2 2006, 15:04"),
|
||||
Updated: v.UpdatedAt.Format("January 2 2006, 15:04"),
|
||||
URL: template.URL(*v.HTMLURL),
|
||||
Labels: template.HTML(l),
|
||||
ID: *v.Number,
|
||||
IsOpen: *v.State == "open",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
||||
}
|
||||
|
||||
func (*Provider) getCommits(client *gogithub.Client, config githubConfig) ([]githubBranchCommits, error) {
|
||||
|
||||
opts := &gogithub.CommitsListOptions{
|
||||
SHA: config.Branch,
|
||||
ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}}
|
||||
|
||||
if config.SincePtr != nil {
|
||||
opts.Since = *config.SincePtr
|
||||
}
|
||||
|
||||
guff, _, err := client.Repositories.ListCommits(config.Owner, config.Repo, opts)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(guff) == 0 {
|
||||
return []githubBranchCommits{}, nil
|
||||
}
|
||||
|
||||
day := ""
|
||||
newDay := ""
|
||||
ret := []githubBranchCommits{}
|
||||
|
||||
for k, v := range guff {
|
||||
|
||||
if guff[k].Commit != nil {
|
||||
if guff[k].Commit.Committer.Date != nil {
|
||||
y, m, d := (*guff[k].Commit.Committer.Date).Date()
|
||||
newDay = fmt.Sprintf("%s %d, %d", m.String(), d, y)
|
||||
}
|
||||
}
|
||||
if day != newDay {
|
||||
day = newDay
|
||||
ret = append(ret, githubBranchCommits{
|
||||
Name: fmt.Sprintf("%s/%s:%s", config.Owner, config.Repo, config.Branch),
|
||||
Day: day,
|
||||
})
|
||||
}
|
||||
|
||||
var a, d, l, m, u string
|
||||
if v.Commit != nil {
|
||||
if v.Commit.Committer.Date != nil {
|
||||
// d = fmt.Sprintf("%v", *v.Commit.Committer.Date)
|
||||
d = v.Commit.Committer.Date.Format("January 2 2006, 15:04")
|
||||
}
|
||||
if v.Commit.Message != nil {
|
||||
m = *v.Commit.Message
|
||||
}
|
||||
}
|
||||
if v.Committer != nil {
|
||||
if v.Committer.Login != nil {
|
||||
l = *v.Committer.Login
|
||||
}
|
||||
if v.Committer.AvatarURL != nil {
|
||||
a = *v.Committer.AvatarURL
|
||||
}
|
||||
}
|
||||
if a == "" {
|
||||
a = githubGravatar
|
||||
}
|
||||
if v.HTMLURL != nil {
|
||||
u = *v.HTMLURL
|
||||
}
|
||||
ret[len(ret)-1].Commits = append(ret[len(ret)-1].Commits, githubCommit{
|
||||
Name: l,
|
||||
Message: m,
|
||||
Date: d,
|
||||
Avatar: a,
|
||||
URL: template.URL(u),
|
||||
})
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
||||
}
|
||||
|
||||
// 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 {
|
||||
log.Error("unable to unmarshall github config", err)
|
||||
return "internal configuration error '" + err.Error() + "'"
|
||||
}
|
||||
|
||||
c.Clean()
|
||||
c.Token = ctx.GetSecrets("token")
|
||||
|
||||
switch c.ReportInfo.ID {
|
||||
/*case "issuenum_data":
|
||||
refreshed, err := t.getIssueNum(t.githubClient(c), c)
|
||||
if err != nil {
|
||||
log.Error("unable to get github issue number activity", err)
|
||||
return data
|
||||
}
|
||||
j, err := json.Marshal(refreshed)
|
||||
if err != nil {
|
||||
log.Error("unable to marshall github issue number activity", err)
|
||||
return data
|
||||
}
|
||||
return string(j)*/
|
||||
|
||||
case tagIssuesData:
|
||||
refreshed, err := p.getIssues(p.githubClient(c), c)
|
||||
if err != nil {
|
||||
log.Error("unable to get github issues", err)
|
||||
return data
|
||||
}
|
||||
j, err := json.Marshal(refreshed)
|
||||
if err != nil {
|
||||
log.Error("unable to marshall github issues", err)
|
||||
return data
|
||||
}
|
||||
return string(j)
|
||||
|
||||
case tagCommitsData:
|
||||
refreshed, err := p.getCommits(p.githubClient(c), c)
|
||||
if err != nil {
|
||||
log.Error("unable to get github commits", err)
|
||||
return data
|
||||
}
|
||||
j, err := json.Marshal(refreshed)
|
||||
if err != nil {
|
||||
log.Error("unable to marshall github commits", err)
|
||||
return data
|
||||
}
|
||||
return string(j)
|
||||
|
||||
default:
|
||||
msg := "unknown data format: " + c.ReportInfo.ID
|
||||
log.ErrorString(msg)
|
||||
return "internal configuration error, " + msg
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 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 {
|
||||
log.Error("unable to unmarshall github config", err)
|
||||
return "Please delete and recreate this Github section."
|
||||
}
|
||||
|
||||
c.Clean()
|
||||
c.Token = ctx.GetSecrets("token")
|
||||
|
||||
payload.Config = c
|
||||
payload.Repo = c.RepoInfo
|
||||
payload.Limit = c.BranchLines
|
||||
if len(c.BranchSince) > 0 {
|
||||
payload.DateMessage = "created after " + c.BranchSince
|
||||
}
|
||||
|
||||
switch c.ReportInfo.ID {
|
||||
/* case "issuenum_data":
|
||||
payload.IssueNum = c.IssueNum
|
||||
raw := []githubIssueActivity{}
|
||||
|
||||
if len(data) > 0 {
|
||||
err = json.Unmarshal([]byte(data), &raw)
|
||||
if err != nil {
|
||||
log.Error("unable to unmarshall github issue activity data", err)
|
||||
return "Documize internal github json umarshall issue activity data error: " + err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
opt := &gogithub.MarkdownOptions{Mode: "gfm", Context: c.Owner + "/" + c.Repo}
|
||||
client := p.githubClient(c)
|
||||
for k, v := range raw {
|
||||
if v.Event == "commented" {
|
||||
output, _, err := client.Markdown(string(v.Message), opt)
|
||||
if err != nil {
|
||||
log.Error("convert commented text to markdown", err)
|
||||
} else {
|
||||
raw[k].Message = template.HTML(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
payload.IssueNumActivity = raw */
|
||||
|
||||
case tagIssuesData:
|
||||
raw := []githubIssue{}
|
||||
|
||||
if len(data) > 0 {
|
||||
err = json.Unmarshal([]byte(data), &raw)
|
||||
if err != nil {
|
||||
log.Error("unable to unmarshall github issue data", err)
|
||||
return "Documize internal github json umarshall open data error: " + err.Error() + "<BR>" + data
|
||||
}
|
||||
}
|
||||
payload.Issues = raw
|
||||
if strings.TrimSpace(c.IssuesText) != "" {
|
||||
payload.ShowIssueNumbers = true
|
||||
payload.DateMessage = c.IssuesText
|
||||
} else {
|
||||
if len(c.Lists) > 0 {
|
||||
for _, v := range c.Lists {
|
||||
if v.Included {
|
||||
payload.ShowList = true
|
||||
break
|
||||
}
|
||||
}
|
||||
payload.List = c.Lists
|
||||
}
|
||||
}
|
||||
|
||||
case tagCommitsData:
|
||||
raw := []githubBranchCommits{}
|
||||
err = json.Unmarshal([]byte(data), &raw)
|
||||
|
||||
if err != nil {
|
||||
log.Error("unable to unmarshall github commit data", err)
|
||||
return "Documize internal github json umarshall data error: " + err.Error() + "<BR>" + data
|
||||
}
|
||||
c.ReportInfo.ID = tagCommitsData
|
||||
payload.BranchCommits = raw
|
||||
for _, list := range raw {
|
||||
payload.CommitCount += len(list.Commits)
|
||||
}
|
||||
|
||||
default:
|
||||
msg := "unknown data format: " + c.ReportInfo.ID
|
||||
log.ErrorString(msg)
|
||||
return "internal configuration error, " + msg
|
||||
|
||||
}
|
||||
|
||||
t := template.New("github")
|
||||
|
||||
tmpl, ok := renderTemplates[c.ReportInfo.ID]
|
||||
if !ok {
|
||||
msg := "github render template not found for: " + c.ReportInfo.ID
|
||||
log.ErrorString(msg)
|
||||
return "Documize internal error: " + msg
|
||||
}
|
||||
|
||||
t, err = t.Parse(tmpl)
|
||||
|
||||
if err != nil {
|
||||
log.Error("github render template.Parse error:", err)
|
||||
return "Documize internal github template.Parse error: " + err.Error()
|
||||
}
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
err = t.Execute(buffer, payload)
|
||||
if err != nil {
|
||||
log.Error("github render template.Execute error:", err)
|
||||
return "Documize internal github template.Execute error: " + err.Error()
|
||||
}
|
||||
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// 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(>)
|
||||
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 + "?code=" + gt.AccessToken
|
||||
|
||||
http.Redirect(res, req, target, http.StatusTemporaryRedirect)
|
||||
|
||||
return nil
|
||||
}
|
268
core/section/github/model.go
Normal file
268
core/section/github/model.go
Normal file
|
@ -0,0 +1,268 @@
|
|||
// 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"
|
||||
"time"
|
||||
|
||||
"github.com/documize/community/core/log"
|
||||
)
|
||||
|
||||
const tagIssuesData = "issuesData"
|
||||
const tagCommitsData = "commitsData"
|
||||
|
||||
type githubRender struct {
|
||||
Config githubConfig
|
||||
Repo githubRepo
|
||||
List []githubBranch
|
||||
ShowList bool
|
||||
ShowIssueNumbers bool
|
||||
BranchCommits []githubBranchCommits
|
||||
CommitCount int
|
||||
Issues []githubIssue
|
||||
//IssueNum int
|
||||
//IssueNumActivity []githubIssueActivity
|
||||
Limit int
|
||||
DateMessage string
|
||||
}
|
||||
|
||||
var renderTemplates = map[string]string{
|
||||
tagCommitsData: `
|
||||
<div class="section-github-render">
|
||||
<p>
|
||||
There are {{ .CommitCount }} commits for branch <a href="{{.Config.BranchURL}}">{{.Config.Branch}}</a> of repository <a href="{{ .Repo.URL }}">{{.Repo.Name}}.</a>
|
||||
Showing {{ .Limit }} items {{ .DateMessage }}.
|
||||
</p>
|
||||
<div class="github-board">
|
||||
{{range $data := .BranchCommits}}
|
||||
<div class="github-group-title">
|
||||
Commits on {{ $data.Day }}
|
||||
</div>
|
||||
<ul class="github-list">
|
||||
{{range $commit := $data.Commits}}
|
||||
<li class="github-commit-item">
|
||||
<a class="link" href="{{$commit.URL}}">
|
||||
<div class="github-avatar">
|
||||
<img alt="@{{$commit.Name}}" src="{{$commit.Avatar}}" height="36" width="36">
|
||||
</div>
|
||||
<div class="github-commit-body">
|
||||
<div class="github-commit-title">{{$commit.Message}}</div>
|
||||
<div class="github-commit-meta">{{$commit.Name}} committed on {{$commit.Date}}</div>
|
||||
</div>
|
||||
</a>
|
||||
<div class="clearfix" />
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
`,
|
||||
tagIssuesData: `
|
||||
<div class="section-github-render">
|
||||
<p>
|
||||
{{if .ShowIssueNumbers}}
|
||||
Showing Selected Issues
|
||||
{{else}}
|
||||
{{ .Config.IssueState.Name }}
|
||||
{{end}}
|
||||
for repository <a href="{{ .Repo.URL }}/issues">{{.Repo.Name}}</a>
|
||||
{{if .ShowList}}
|
||||
labelled
|
||||
{{range $label := .List}}
|
||||
{{if $label.Included}}
|
||||
<span class="github-issue-label" style="background-color:#{{$label.Color}}">{{$label.Name}}</span>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{if .ShowIssueNumbers}}
|
||||
issue(s) {{ .DateMessage }}.
|
||||
{{else}}
|
||||
up to {{ .Limit }} items are shown{{ .DateMessage }}.
|
||||
{{end}}
|
||||
</p>
|
||||
<div class="github-board">
|
||||
<ul class="github-list">
|
||||
{{range $data := .Issues}}
|
||||
<li class="github-commit-item">
|
||||
<a class="link" href="{{$data.URL}}">
|
||||
<div class="issue-avatar">
|
||||
{{if $data.IsOpen}}
|
||||
<span title="Open issue">
|
||||
<svg height="16" version="1.1" viewBox="0 0 14 16" width="14"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
|
||||
</span>
|
||||
{{else}}
|
||||
<span title="Closed issue">
|
||||
<svg height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M7 10h2v2H7v-2zm2-6H7v5h2V4zm1.5 1.5l-1 1L12 9l4-4.5-1-1L12 7l-1.5-1.5zM8 13.7A5.71 5.71 0 0 1 2.3 8c0-3.14 2.56-5.7 5.7-5.7 1.83 0 3.45.88 4.5 2.2l.92-.92A6.947 6.947 0 0 0 8 1C4.14 1 1 4.14 1 8s3.14 7 7 7 7-3.14 7-7l-1.52 1.52c-.66 2.41-2.86 4.19-5.48 4.19v-.01z"></path></svg>
|
||||
</span>
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="github-commit-body">
|
||||
<div class="github-commit-title"><span class="label-name">{{$data.Message}}</span> {{$data.Labels}}</div>
|
||||
<div class="github-commit-meta">
|
||||
#{{$data.ID}} opened on {{$data.Date}} by {{$data.Name}}, last updated {{$data.Updated}}
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
<div class="clearfix" />
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
`,
|
||||
/* "issuenum_data": `
|
||||
<div class="section-github-render">
|
||||
<p>
|
||||
Activity for issue #{{.IssueNum}} in repository <a href="{{ .Repo.URL }}/issues">{{.Repo.Name}}.</a>
|
||||
Up to {{ .Limit }} items are shown{{ .DateMessage }}.
|
||||
</p>
|
||||
<div class="github-board">
|
||||
<ul class="github-list">
|
||||
{{range $data := .IssueNumActivity}}
|
||||
<li class="github-commit-item">
|
||||
<div class="github-avatar">
|
||||
<img alt="@{{$data.Name}}" src="{{$data.Avatar}}" height="36" width="36">
|
||||
</div>
|
||||
<div class="github-commit-meta">
|
||||
{{$data.Name}} <a class="link" href="{{$data.URL}}">{{$data.Event}}</a> {{$data.Date}}
|
||||
</div>
|
||||
<div class="github-commit-body">
|
||||
<div class="github-commit-title">
|
||||
{{$data.Message}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="clearfix" />
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
`,*/
|
||||
}
|
||||
|
||||
type githubReport struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type githubOwner struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type githubRepo struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Included bool `json:"included"`
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Private bool `json:"private"` // TODO review field use
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type githubBranch struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Included bool `json:"included"`
|
||||
URL string `json:"url"`
|
||||
Color string `json:"color,omitempty"`
|
||||
}
|
||||
|
||||
type githubBranchCommits struct {
|
||||
Name string `json:"name"`
|
||||
Day string `json:"day"`
|
||||
Commits []githubCommit
|
||||
}
|
||||
|
||||
type githubCommit struct {
|
||||
Date string `json:"date"`
|
||||
Message string `json:"message"`
|
||||
URL template.URL `json:"url"`
|
||||
Name string `json:"name"`
|
||||
Avatar string `json:"avatar"`
|
||||
}
|
||||
|
||||
type githubIssue struct {
|
||||
ID int `json:"id"`
|
||||
Date string `json:"date"`
|
||||
Updated string `json:"dated"`
|
||||
Message string `json:"message"`
|
||||
URL template.URL `json:"url"`
|
||||
Name string `json:"name"`
|
||||
Avatar string `json:"avatar"`
|
||||
Labels template.HTML `json:"labels"`
|
||||
IsOpen bool `json:"isopen"`
|
||||
}
|
||||
|
||||
/*
|
||||
type githubIssueActivity struct {
|
||||
Date string `json:"date"`
|
||||
Event string `json:"event"`
|
||||
Message template.HTML `json:"message"`
|
||||
URL template.URL `json:"url"`
|
||||
Name string `json:"name"`
|
||||
Avatar string `json:"avatar"`
|
||||
}
|
||||
*/
|
||||
|
||||
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"`
|
||||
Repo string `json:"repo_name"`
|
||||
Branch string `json:"branch"`
|
||||
BranchURL string `json:"branchURL"`
|
||||
BranchSince string `json:"branchSince,omitempty"`
|
||||
SincePtr *time.Time `json:"-"`
|
||||
BranchLines int `json:"branchLines,omitempty,string"`
|
||||
OwnerInfo githubOwner `json:"owner"`
|
||||
RepoInfo githubRepo `json:"repo"`
|
||||
ReportInfo githubReport `json:"report"`
|
||||
ClientID string `json:"clientId"`
|
||||
CallbackURL string `json:"callbackUrl"`
|
||||
Lists []githubBranch `json:"lists,omitempty"`
|
||||
IssueState githubReport `json:"state,omitempty"`
|
||||
IssuesText string `json:"issues,omitempty"`
|
||||
//IssueNum int `json:"issueNum,omitempty,string"`
|
||||
}
|
||||
|
||||
func (c *githubConfig) Clean() {
|
||||
c.Owner = c.OwnerInfo.Name
|
||||
c.Repo = c.RepoInfo.Repo
|
||||
for _, l := range c.Lists {
|
||||
if l.Included {
|
||||
c.Branch = l.Name
|
||||
c.BranchURL = l.URL
|
||||
break
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type githubCallbackT struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
44
core/section/github/sort.go
Normal file
44
core/section/github/sort.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
// 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 repos in order that that should be presented.
|
||||
type reposToSort []githubRepo
|
||||
|
||||
func (s reposToSort) Len() int { return len(s) }
|
||||
func (s reposToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s reposToSort) Less(i, j int) bool {
|
||||
return s[i].Name < s[j].Name
|
||||
}
|
||||
|
||||
func sortRepos(in []githubRepo) []githubRepo {
|
||||
sts := reposToSort(in)
|
||||
sort.Sort(sts)
|
||||
return []githubRepo(sts)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
53
core/section/markdown/markdown.go
Normal file
53
core/section/markdown/markdown.go
Normal 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 markdown
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/blackfriday"
|
||||
"github.com/documize/community/core/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents Markdown
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
// 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 markdown content with preview"
|
||||
section.ContentType = "markdown"
|
||||
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
|
||||
}
|
84
core/section/papertrail/model.go
Normal file
84
core/section/papertrail/model.go
Normal 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 class="margin-left-20">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"`
|
||||
}
|
299
core/section/papertrail/papertrail.go
Normal file
299
core/section/papertrail/papertrail.go
Normal file
|
@ -0,0 +1,299 @@
|
|||
// 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/section/provider"
|
||||
"github.com/documize/community/core/log"
|
||||
)
|
||||
|
||||
const me = "papertrail"
|
||||
|
||||
// Provider represents Gemini
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
// 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"
|
||||
|
||||
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(ctx, config, w, r)
|
||||
case "options":
|
||||
options(config, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh just sends back data as-is.
|
||||
func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData string) {
|
||||
var c = papertrailConfig{}
|
||||
err := json.Unmarshal([]byte(config), &c)
|
||||
|
||||
if err != nil {
|
||||
log.Error("unable to read Papertrail config", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Clean()
|
||||
|
||||
c.APIToken = ctx.GetSecrets("APIToken")
|
||||
|
||||
if len(c.APIToken) == 0 {
|
||||
log.Error("missing API token", err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := fetchEvents(c)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Papertrail fetchEvents failed", err)
|
||||
return
|
||||
}
|
||||
|
||||
j, err := json.Marshal(result)
|
||||
|
||||
if err != nil {
|
||||
log.Error("unable to marshal Papaertrail events", err)
|
||||
return
|
||||
}
|
||||
|
||||
newData = string(j)
|
||||
return
|
||||
}
|
||||
|
||||
func auth(ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, r *http.Request) {
|
||||
result, err := fetchEvents(config)
|
||||
|
||||
if result == nil {
|
||||
err = errors.New("nil result of papertrail query")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
log.IfErr(ctx.SaveSecrets(`{"APIToken":""}`)) // invalid token, so reset it
|
||||
|
||||
if err.Error() == "forbidden" {
|
||||
provider.WriteForbidden(w)
|
||||
} else {
|
||||
provider.WriteError(w, me, err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.IfErr(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(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 {
|
||||
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 {
|
||||
log.Error("message", err)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
log.Error("forbidden", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
dec := json.NewDecoder(res.Body)
|
||||
err = dec.Decode(&result)
|
||||
|
||||
if err != nil {
|
||||
log.Error("unable to read result", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
268
core/section/provider/provider.go
Normal file
268
core/section/provider/provider.go
Normal file
|
@ -0,0 +1,268 @@
|
|||
// 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"`
|
||||
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)
|
||||
}
|
41
core/section/register.go
Normal file
41
core/section/register.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
// 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/section/code"
|
||||
"github.com/documize/community/core/section/gemini"
|
||||
"github.com/documize/community/core/section/github"
|
||||
"github.com/documize/community/core/section/markdown"
|
||||
"github.com/documize/community/core/section/papertrail"
|
||||
"github.com/documize/community/core/section/provider"
|
||||
"github.com/documize/community/core/section/table"
|
||||
"github.com/documize/community/core/section/trello"
|
||||
"github.com/documize/community/core/section/wysiwyg"
|
||||
"github.com/documize/community/core/log"
|
||||
)
|
||||
|
||||
// Register sections
|
||||
func Register() {
|
||||
provider.Register("code", &code.Provider{})
|
||||
provider.Register("gemini", &gemini.Provider{})
|
||||
provider.Register("github", &github.Provider{})
|
||||
provider.Register("markdown", &markdown.Provider{})
|
||||
provider.Register("papertrail", &papertrail.Provider{})
|
||||
provider.Register("table", &table.Provider{})
|
||||
provider.Register("trello", &trello.Provider{})
|
||||
provider.Register("wysiwyg", &wysiwyg.Provider{})
|
||||
p := provider.List()
|
||||
log.Info(fmt.Sprintf("Documize registered %d smart sections", len(p)))
|
||||
}
|
82
core/section/section_test.go
Normal file
82
core/section/section_test.go
Normal 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")
|
||||
|
||||
}
|
50
core/section/table/table.go
Normal file
50
core/section/table/table.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
// 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/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents Table
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
// 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 and formatting for tabular data"
|
||||
section.ContentType = "table"
|
||||
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
|
||||
}
|
205
core/section/trello/model.go
Normal file
205
core/section/trello/model.go
Normal file
|
@ -0,0 +1,205 @@
|
|||
// 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 = `
|
||||
<p class="non-printable-message">Non-printable</p>
|
||||
<div class="section-trello-render non-printable">
|
||||
<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
|
||||
}
|
296
core/section/trello/trello.go
Normal file
296
core/section/trello/trello.go
Normal file
|
@ -0,0 +1,296 @@
|
|||
// 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/section/provider"
|
||||
"github.com/documize/community/core/log"
|
||||
)
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
// Provider represents Trello
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
// Meta describes us
|
||||
func (*Provider) Meta() provider.TypeMeta {
|
||||
return meta
|
||||
}
|
||||
|
||||
// Command stub.
|
||||
func (*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 {
|
||||
log.ErrorString("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 {
|
||||
log.IfErr(err)
|
||||
provider.WriteError(w, "trello", err)
|
||||
log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid
|
||||
return
|
||||
}
|
||||
|
||||
provider.WriteJSON(w, render)
|
||||
|
||||
case "boards":
|
||||
render, err := getBoards(config)
|
||||
|
||||
if err != nil {
|
||||
log.IfErr(err)
|
||||
provider.WriteError(w, "trello", err)
|
||||
log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid
|
||||
return
|
||||
}
|
||||
|
||||
provider.WriteJSON(w, render)
|
||||
|
||||
case "lists":
|
||||
render, err := getLists(config)
|
||||
|
||||
if err != nil {
|
||||
log.IfErr(err)
|
||||
provider.WriteError(w, "trello", err)
|
||||
log.IfErr(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:
|
||||
log.ErrorString("trello unknown method name: " + 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)
|
||||
log.IfErr(e)
|
||||
log.IfErr(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 (*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 {
|
||||
log.Error("unable to marshall trello cards", 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 {
|
||||
fmt.Println(err)
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
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
|
||||
}
|
50
core/section/wysiwyg/wysiwyg.go
Normal file
50
core/section/wysiwyg/wysiwyg.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
// 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/section/provider"
|
||||
)
|
||||
|
||||
// Provider represents WYSIWYG
|
||||
type Provider struct {
|
||||
}
|
||||
|
||||
// Meta describes us
|
||||
func (*Provider) Meta() provider.TypeMeta {
|
||||
section := provider.TypeMeta{}
|
||||
|
||||
section.ID = "0f024fa0-d017-4bad-a094-2c13ce6edad7"
|
||||
section.Title = "Rich Text"
|
||||
section.Description = "WYSIWYG editing with cut-paste image support"
|
||||
section.ContentType = "wysiwyg"
|
||||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue