-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_server.go
239 lines (207 loc) · 5.84 KB
/
task_server.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"bytes"
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"regexp"
"sort"
"strings"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/spf13/viper"
"github.com/uber/gonduit/requests"
)
//go:embed tasks_template.html
var tplData string
/*
"priority": "High",
"priority": "Low",
"priority": "Needs Triage",
"priority": "Normal",
"priority": "Unbreak Now!",
"priority": "Wishlist",
*/
var prioritiesSorted = []string{
"Unbreak Now!",
"High",
"Needs Triage",
"Normal",
"Low",
"Wishlist",
"*",
}
func mdToHTML(md string) string {
// create markdown parser with extensions
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse([]byte(md))
// create HTML renderer with extensions
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
return string(markdown.Render(doc, renderer))
}
func runTaskServer() {
mux := http.NewServeMux()
tpl := template.Must(template.New("tasks").Parse(tplData))
mux.HandleFunc("/get-phabri-file", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
infoReq := &GetFileInfoRequest{
ID: id,
}
var info GetFileInfoResp
err := phabricatorClient.Call("file.info", infoReq, &info)
if err != nil {
log.Printf("Error: %v", err)
fmt.Fprintf(w, "Error: %v", err)
return
}
///DDD
baseURL := viper.GetString("phabricator.url")
apiToken := viper.GetString("phabricator.token")
// Define the API endpoint and parameters
apiEndpoint := "/api/file.download"
phid := info.Phid
// Construct the full URL
fullURL := baseURL + apiEndpoint
// Prepare the request body
body := bytes.NewBufferString(fmt.Sprintf("api.token=%s&phid=%s", apiToken, phid))
// Make the HTTP POST request
response, err := http.Post(fullURL, "application/x-www-form-urlencoded", body)
if err != nil {
log.Printf("Error: %v", err)
fmt.Fprintf(w, "Error: %v", err)
return
}
defer response.Body.Close()
// Read the response body
responseBody, err := io.ReadAll(response.Body)
if err != nil {
log.Printf("Error: %v", err)
fmt.Fprintf(w, "Error: %v", err)
return
}
var download FileDownloadResp
err = json.Unmarshal(responseBody, &download)
if err != nil {
log.Printf("Error: %v", err)
fmt.Fprintf(w, "Error: %v", err)
return
}
////ENDDDD
data, err := base64.StdEncoding.DecodeString(download.Result)
if err != nil {
log.Printf("Error: %v", err)
fmt.Fprintf(w, "Error: %v", err)
return
}
w.Header().Set("Content-type", info.MimeType)
w.Write(data)
})
mux.HandleFunc("/phabri-tasks-for-dashboard", func(w http.ResponseWriter, r *http.Request) {
// query := r.URL.Query()
// if query.Get("token") != viper.GetString("server.token") {
// log.Printf("Invalid token: %s", query.Get("token"))
// fmt.Fprintf(w, "Invalid token: %s", query.Get("token"))
// return
// }
var tasks map[string]PhabricatorTask
req := &TasksQueryRequest{
Status: "status-open",
}
err := phabricatorClient.Call("maniphest.query", req, &tasks)
if err != nil {
log.Printf("Error: %v", err)
fmt.Fprintf(w, "Error: %v", err)
return
}
tasksPreSorted := make([]PhabricatorTask, 0, len(tasks))
for _, v := range tasks {
tasksPreSorted = append(tasksPreSorted, v)
}
sort.Slice(tasksPreSorted, func(i, j int) bool {
return tasksPreSorted[i].ID > tasksPreSorted[j].ID
})
var tasksSorted []ExtendedPhabricatorTask
phids := make([]string, 0, len(tasks)*2)
for _, v := range tasks {
phids = append(phids, v.AuthorPHID)
phids = append(phids, v.ProjectPHIDs...)
}
phids = removeDuplicates(phids)
lookedUpPhids, err := phabricatorClient.PHIDLookup(requests.PHIDLookupRequest{
Names: phids,
})
if err != nil {
log.Printf("Error: %v", err)
fmt.Fprintf(w, "Error: %v", err)
return
}
for _, priority := range prioritiesSorted {
for _, task := range tasksPreSorted {
author, ok := lookedUpPhids[task.AuthorPHID]
authorName := ""
if ok {
authorName = author.FullName
}
rendered := mdToHTML(task.Description)
// match tags like "{F602442}"
reg := regexp.MustCompile(`\{F\d+\}`)
rendered = reg.ReplaceAllStringFunc(rendered, func(s string) string {
s = strings.ReplaceAll(s, "{F", "")
s = strings.ReplaceAll(s, "}", "")
return fmt.Sprintf(`<img src="/get-phabri-file?id=%s" />`, s)
})
projectNames := make([]string, 0, len(task.ProjectPHIDs))
isImportant := false
for _, projectPHID := range task.ProjectPHIDs {
project, ok := lookedUpPhids[projectPHID]
if ok {
if project.FullName == "[SUBSKRYBCYJNY] Wszystkie taski" {
continue
}
if project.FullName == "Na monitor" {
isImportant = true
}
projectNames = append(projectNames, project.FullName)
}
}
if task.Priority == priority || priority == "*" {
// check if already added
found := false
for _, v := range tasksSorted {
if v.PhabricatorTask.Phid == task.Phid {
found = true
break
}
}
if !found {
tasksSorted = append(tasksSorted, ExtendedPhabricatorTask{
PhabricatorTask: task,
AuthorName: authorName,
RenderedDescription: template.HTML(rendered),
ProjectNames: projectNames,
IsImportant: isImportant,
})
}
}
}
}
// move is important to the top
sort.SliceStable(tasksSorted, func(i, j int) bool {
return tasksSorted[i].IsImportant
})
// pretty print json
// b, _ := json.MarshalIndent(tasks, "", " ")
// fmt.Fprintf(w, "%s", b)
tpl.Execute(w, tasksSorted)
})
http.ListenAndServe(":8080", mux)
}