/
model.go
123 lines (111 loc) · 3.54 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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/domain/section/provider"
"github.com/documize/community/domain/store"
)
// the HTML that is rendered by this section.
const renderTemplate = `
{{if .Authenticated}}
<p>The Gemini workspace <a href="{{.Config.URL}}/workspace/{{.Config.WorkspaceID}}/items">{{.Config.WorkspaceName}}</a> contains {{.Config.ItemCount}} items.</p>
<table class="basic-table section-gemini-table">
<thead>
<tr>
<th class="bordered no-width">Item Key</th>
<th class="bordered">Title</th>
<th class="bordered no-width">Type</th>
<th class="bordered no-width">Status</th>
</tr>
</thead>
<tbody>
{{$wid := .Config.WorkspaceID}}
{{$app := .Config.URL}}
{{range $item := .Items}}
<tr>
<td class="bordered no-width"><a href="{{ $app }}/workspace/{{ $wid }}/item/{{ $item.ID }}">{{ $item.IssueKey }}</a></td>
<td class="bordered">{{ $item.Title }}</td>
<td class="bordered no-width"><img src='{{ $item.TypeImage }}' /> {{ $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, store *store.Store) {
if ctx != nil {
sec, err := getSecrets(ctx, store)
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, store *store.Store) {
var sec secrets
sec.APIKey = strings.TrimSpace(c.APIKey)
sec.Username = strings.TrimSpace(c.Username)
sec.URL = strings.TrimSpace(c.URL)
ctx.MarshalSecrets(sec, store)
}
type secrets struct {
URL string `json:"url"`
Username string `json:"username"`
APIKey string `json:"apikey"`
}
func getSecrets(ctx *provider.Context, store *store.Store) (sec secrets, err error) {
err = ctx.UnmarshalSecrets(&sec, store)
return
}