/
public_json_api.go
149 lines (138 loc) · 4.51 KB
/
public_json_api.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
// Copyright 2021 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package main
import (
"encoding/json"
)
// publicApiBugDescription is used to serve the /bug HTTP requests
// and provide JSON description of the BUG. Backward compatible.
type publicAPIBugDescription struct {
Version int `json:"version"`
Title string `json:"title,omitempty"`
ID string `json:"id"`
FixCommits []vcsCommit `json:"fix-commits,omitempty"`
CauseCommit *vcsCommit `json:"cause-commit,omitempty"`
// links to the discussions
Discussions []string `json:"discussions,omitempty"`
Crashes []publicAPICrashDescription `json:"crashes,omitempty"`
}
type vcsCommit struct {
Title string `json:"title"`
Link string `json:"link,omitempty"`
Hash string `json:"hash,omitempty"`
Repo string `json:"repo,omitempty"`
Branch string `json:"branch,omitempty"`
}
type publicAPICrashDescription struct {
Title string `json:"title"`
SyzReproducer string `json:"syz-reproducer,omitempty"`
CReproducer string `json:"c-reproducer,omitempty"`
KernelConfig string `json:"kernel-config,omitempty"`
KernelSourceGit string `json:"kernel-source-git,omitempty"`
KernelSourceCommit string `json:"kernel-source-commit,omitempty"`
SyzkallerGit string `json:"syzkaller-git,omitempty"`
SyzkallerCommit string `json:"syzkaller-commit,omitempty"`
CompilerDescription string `json:"compiler-description,omitempty"`
Architecture string `json:"architecture,omitempty"`
CrashReport string `json:"crash-report-link,omitempty"`
}
func getExtAPIDescrForBugPage(bugPage *uiBugPage) *publicAPIBugDescription {
return &publicAPIBugDescription{
Version: 1,
Title: bugPage.Bug.Title,
ID: bugPage.Bug.ID,
Discussions: func() []string {
if bugPage.Bug.ExternalLink == "" {
return nil
}
return []string{bugPage.Bug.ExternalLink}
}(),
FixCommits: getBugFixCommits(bugPage.Bug),
CauseCommit: func() *vcsCommit {
if bugPage.BisectCause == nil || bugPage.BisectCause.Commit == nil {
return nil
}
bisectCause := bugPage.BisectCause
return &vcsCommit{
Title: bisectCause.Commit.Title,
Link: bisectCause.Commit.Link,
Hash: bisectCause.Commit.Hash,
Repo: bisectCause.KernelRepo,
Branch: bisectCause.KernelBranch}
}(),
Crashes: func() []publicAPICrashDescription {
var res []publicAPICrashDescription
for _, crash := range bugPage.Crashes.Crashes {
res = append(res, publicAPICrashDescription{
Title: crash.Title,
SyzReproducer: crash.ReproSyzLink,
CReproducer: crash.ReproCLink,
KernelConfig: crash.KernelConfigLink,
KernelSourceGit: crash.KernelCommitLink,
KernelSourceCommit: crash.KernelCommit,
SyzkallerGit: crash.SyzkallerCommitLink,
SyzkallerCommit: crash.SyzkallerCommit,
// TODO: add the CompilerDescription
// TODO: add the Architecture
CrashReport: crash.ReportLink,
})
}
return res
}(),
}
}
func getBugFixCommits(bug *uiBug) []vcsCommit {
var res []vcsCommit
for _, commit := range bug.Commits {
res = append(res, vcsCommit{
Title: commit.Title,
Link: commit.Link,
Hash: commit.Hash,
Repo: commit.Repo,
Branch: commit.Branch,
})
}
return res
}
type publicAPIBugGroup struct {
Version int `json:"version"`
Bugs []publicAPIBug
}
type publicAPIBug struct {
Title string `json:"title,omitempty"`
Link string `json:"link"`
LastUpdated string `json:"last-updated,omitempty"`
FixCommits []vcsCommit `json:"fix-commits,omitempty"`
}
func getExtAPIDescrForBugGroups(bugGroups []*uiBugGroup) *publicAPIBugGroup {
return &publicAPIBugGroup{
Version: 1,
Bugs: func() []publicAPIBug {
var res []publicAPIBug
for _, group := range bugGroups {
for _, bug := range group.Bugs {
res = append(res, publicAPIBug{
Title: bug.Title,
Link: bug.Link,
FixCommits: getBugFixCommits(bug),
})
}
}
return res
}(),
}
}
func GetJSONDescrFor(page interface{}) ([]byte, error) {
var res interface{}
switch i := page.(type) {
case *uiBugPage:
res = getExtAPIDescrForBugPage(i)
case *uiTerminalPage:
res = getExtAPIDescrForBugGroups([]*uiBugGroup{i.Bugs})
case *uiMainPage:
res = getExtAPIDescrForBugGroups(i.Groups)
default:
return nil, ErrClientNotFound
}
return json.MarshalIndent(res, "", "\t")
}