-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbug.go
More file actions
160 lines (135 loc) · 3.52 KB
/
bug.go
File metadata and controls
160 lines (135 loc) · 3.52 KB
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
// SPDX-License-Identifier: BSD-2-Clause
//
// Copyright (c) Lewis Cook <lcook@FreeBSD.org>
package command
import (
"encoding/json"
"fmt"
"net/http"
"github.com/bwmarrin/discordgo"
)
const (
bugzBase string = "https://bugs.freebsd.org"
bugz string = bugzBase + "/bugzilla/"
bugzRest string = bugz + "/rest"
bugzBug string = bugzRest + "/bug"
bugzBugID string = bugzBug + "?id=%s"
bugzSubExp string = "bugid"
)
type user struct {
ID json.Number
Name string
RealName string `json:"real_name"`
}
type bug struct {
ID json.Number
Status string
Resolution string
Summary string
Product string
Component string
Version string
Platform string
Assignee user `json:"assigned_to_detail"`
Creation string `json:"creation_time"`
Creator user `json:"creator_detail"`
}
type report struct {
Bugs []bug `json:"bugs"`
}
func getReport(id string) (report, error) {
req, err := http.NewRequest("GET", fmt.Sprintf(bugzBugID, id), nil)
if err != nil {
return report{}, err
}
req.Header.Set("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return report{}, err
}
defer resp.Body.Close()
var rep report
err = json.NewDecoder(resp.Body).Decode(&rep)
if err != nil {
return report{}, err
}
return rep, nil
}
func (h *Handler) Bug(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot || m.Author.ID == s.State.User.ID {
return
}
bugzRegex := `(?:(?i)\` + h.Settings.Prefix + `bug\s+|(?:https?://)?bugs\.freebsd\.org/bugzilla/show_bug\.cgi\?id=)(?P<bugid>\d{1,6})`
if bugID := messageMatchRegex(m, bugzRegex, bugzSubExp); bugID != "" {
s.ChannelTyping(m.ChannelID)
author := &discordgo.MessageEmbedAuthor{
Name: "Bugzilla: Bug " + bugID,
IconURL: "https://raw.githubusercontent.com/freebsd/freebsd-src/refs/heads/main/stand/images/freebsd-logo-rev.png",
}
report, err := getReport(bugID)
if err != nil {
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
Description: fmt.Sprintf(
"Unable to request data from Bugzilla: %v",
err,
),
Color: embedColorFreeBSD,
Author: author,
}, m.Reference())
return
}
if len(report.Bugs) < 1 {
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
Description: fmt.Sprintf(
"Unable to find Bugzilla report with ID matching **%s**",
bugID,
),
Color: embedColorFreeBSD,
Author: author,
}, m.Reference())
return
}
bug := &report.Bugs[0]
s.ChannelMessageSendEmbedReply(m.ChannelID, &discordgo.MessageEmbed{
Description: fmt.Sprintf(
"[%s](%s/%s)",
bug.Summary,
bugzBase,
bugID,
),
Timestamp: bug.Creation,
Color: embedColorFreeBSD,
Footer: &discordgo.MessageEmbedFooter{
Text: bug.Creator.RealName,
},
Author: author,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Status",
Value: func() string {
if bug.Resolution == "" {
return bug.Status
}
return bug.Status + " " + bug.Resolution
}(),
Inline: true,
},
{Name: "Product", Value: bug.Product, Inline: true},
{Name: "Component", Value: bug.Component, Inline: true},
{Name: "Version", Value: bug.Version, Inline: true},
{Name: "Platform", Value: bug.Platform, Inline: true},
{
Name: "Assignee",
Value: func() string {
if bug.Assignee.RealName == "" {
return "Nobody"
}
return bug.Assignee.RealName
}(),
Inline: true,
},
},
}, m.Reference())
}
}