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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
package main
/*
This file is part of taler-dashboard
Copyright (C) 2023 Özgür Kesim
taler-dashboard is free software; you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any later
version.
taler-dashboard is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
License for more details.
You can receive a copy of the GNU Affero General Public License from
<http://www.gnu.org/licenses/>
@author Özgür Kesim <oec-taler@kesim.org>
*/
import (
"context"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"strings"
"sync"
"time"
)
type Data struct {
mux sync.RWMutex
url string
token string
num int
projectId int
minimumVersion string
tmpl *template.Template
ctx context.Context
Issues Issues
Projects Projects
Timestamp time.Time
Freq time.Duration
Lasterror error
}
func NewData(ctx context.Context, url, token string, num int) *Data {
data := &Data{
url: url,
token: token,
ctx: ctx,
num: num,
}
fm := map[string]any{
"shorten": func(max int, s string) string {
if len(s) <= max {
return s
}
return s[:max] + "⋯"
},
}
data.tmpl = template.Must(template.New("index").Funcs(fm).ParseFS(content, "*.tmpl"))
return data
}
const statusFilter = `status%5B%5D=10&status%5B%5D=20&status%5B%5D=30&status%5B%5D=40&status%5B%5D=50&severity%5B%5D=20`
var fields = []string{"id",
"description",
"summary",
"category",
"target_version",
"status",
"reporter",
"handler",
"resolution",
"priority",
"severity",
"created_at",
"updated_at",
"relationships",
"tags",
}
func (d *Data) update() {
url := fmt.Sprintf("%s/issues?project_id=%d&page_size=%d&%s&select=%s",
d.url, d.projectId, d.num, statusFilter,
strings.Join(fields, ","))
req, e := http.NewRequestWithContext(d.ctx, "GET", url, nil)
if nil != e {
d.mux.Lock()
defer d.mux.Unlock()
d.Lasterror = e
return
}
req.Header.Add("Authorization", d.token)
r, e := http.DefaultClient.Do(req)
if nil != e {
d.mux.Lock()
defer d.mux.Unlock()
d.Lasterror = e
return
} else if 200 != r.StatusCode {
d.mux.Lock()
defer d.mux.Unlock()
d.Lasterror = fmt.Errorf("Got unexpected status %s\n", r.Status)
return
}
iss := struct{ Issues Issues }{}
e = json.NewDecoder(r.Body).Decode(&iss)
d.mux.Lock()
defer d.mux.Unlock()
d.Lasterror = e
if nil != e {
return
}
d.Timestamp = time.Now()
// Filter issues with old target versions out
var issues = Issues{}
for _, issue := range iss.Issues {
if issue.Resolution.Name == "open" &&
strings.Compare(d.minimumVersion, issue.TargetVersion.Name) < 0 {
issues = append(issues, issue)
}
}
d.Issues = issues
fmt.Println("got", len(issues), "issues")
}
func (d *Data) Loop() {
d.update()
go func() {
var ticker = time.NewTicker(d.Freq)
for range ticker.C {
select {
case <-d.ctx.Done():
return
default:
fmt.Println("updating data")
d.update()
}
}
}()
}
func (d *Data) printJSON(w io.Writer) {
d.mux.RLock()
defer d.mux.RUnlock()
if nil == d.Issues {
fmt.Fprintln(w, "{}")
return
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(d.Issues)
}
func (d *Data) printTemplate(w io.Writer, name string) {
d.mux.RLock()
defer d.mux.RUnlock()
e := d.tmpl.ExecuteTemplate(w, name, d)
if nil != e {
log.Println(e)
}
}
|