-
Notifications
You must be signed in to change notification settings - Fork 3
/
hockeyapp.go
184 lines (150 loc) · 5.31 KB
/
hockeyapp.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
package main
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
type HockeyNotificationType string
const (
HOCKEY_NOTIFICATION_CRASH_REASON HockeyNotificationType = "crash_reason"
HOCKEY_NOTIFICATION_APP_VERSION HockeyNotificationType = "app_version"
)
type HockeyNotification struct {
PublicIdentifier string `json:"public_identifier"`
Type HockeyNotificationType `json:"type"`
SentAt time.Time `json:"sent_at"`
Url string `json:"url"`
CrashReason HockeyCrashReason `json:"crash_reason"`
AppVersion HockeyAppVersion `json:"app_version"`
}
type HockeyCrashReason struct {
Id int `json:"id"`
AppId int `json:"app_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Status int `json:"status"`
Reason string `json:"reason"`
LastCrashAt time.Time `json:"last_crash_at"`
Fixed bool `json:"fixed"`
AppVersionId int `json:"app_version_id"`
BundleVersion string `json:"bundle_version"`
BundleShortVersion string `json:"bundle_short_version"`
NumberOfCrashes int `json:"number_of_crashes"`
Method string `json:"method"`
File string `json:"file"`
Class string `json:"class"`
Line string `json:"line"`
Crashes []HockeyCrash
}
type HockeyCrash struct {
Id int `json:"id"`
AppId int `json:"app_id"`
BundleVersion string `json:"bundle_version"`
BundleShortVersion string `json:"bundle_short_version"`
ContactString string `json:"contact_string"`
CrashReasonId int `json:"crash_reason_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
HasDescription bool `json:"has_description"`
HasLog bool `json:"has_log"`
Model string `json:"model"`
Oem string `json:"oem"`
OsVersion string `json:"os_version"`
UserString string `json:"user_string"`
}
type HockeyAppVersion struct {
Version string `json:"version"`
ShortVersion string `json:"shortversion"`
Title string `json:"title"`
Timestamp int `json:"timestamp"`
AppSize int `json:"appsize"`
Notes template.HTML `json:"notes"`
Mandatory bool `json:"mandatory"`
MinimumOsVersion string `json:"minimum_os_version"`
DeviceFamily *string `json:"device_family"`
Id int `json:"id"`
AppId int `json:"app_id"`
RestrictedToTags bool `json:"restricted_to_tags"`
Status int `json:"status"`
Tags *[]string `json:"tags"`
}
const HOST = "rink.hockeyapp.net"
const API_BASE = "/api/2"
func (app *App) apiGetCall(call string, args *url.Values) (*http.Response, error) {
client := &http.Client{}
path := fmt.Sprintf("%s/apps/%s%s", API_BASE, app.hockeyAppId, call)
url := &url.URL{"https", "", nil, HOST, path, args.Encode(), ""}
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("X-HockeyAppToken", app.hockeyApiToken)
return client.Do(req)
}
func (app *App) apiPostCall(call string, formArgs *url.Values) (*http.Response, error) {
client := &http.Client{}
path := fmt.Sprintf("%s/apps/%s%s", API_BASE, app.hockeyAppId, call)
url := &url.URL{"https", "", nil, HOST, path, "", ""}
req, err := http.NewRequest("POST", url.String(), strings.NewReader(formArgs.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("X-HockeyAppToken", app.hockeyApiToken)
return client.Do(req)
}
func (app *App) SetBugTrackerUrl(reason HockeyCrashReason, trackerUrl string) error {
var err error
call := fmt.Sprintf("/crash_reasons/%d", reason.Id)
args := &url.Values{}
args.Set("ticket_url", trackerUrl)
var resp *http.Response
if resp, err = app.apiPostCall(call, args); err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("HockeyApp error: %s", resp.Status))
}
return nil
}
func (app *App) GetCrashes(reason HockeyCrashReason) ([]HockeyCrash, error) {
var err error
call := fmt.Sprintf("/crash_reasons/%d", reason.Id)
var resp *http.Response
if resp, err = app.apiGetCall(call, &url.Values{}); err != nil {
return nil, err
}
defer resp.Body.Close()
var bytes []byte
if bytes, err = ioutil.ReadAll(resp.Body); err != nil {
return nil, err
}
var responseJson struct {
Crashes []HockeyCrash `json:"crashes"`
}
if err := json.Unmarshal(bytes, &responseJson); err != nil {
return nil, err
}
return responseJson.Crashes, nil
}
func (app *App) GetCrashLog(crash HockeyCrash) (string, error) {
call := fmt.Sprintf("/crashes/%d", crash.Id)
args := &url.Values{}
args.Set("format", "log")
var err error
var resp *http.Response
if resp, err = app.apiGetCall(call, args); err != nil {
return "", err
}
defer resp.Body.Close()
var bytes []byte
if bytes, err = ioutil.ReadAll(resp.Body); err != nil {
return "", err
}
return string(bytes), nil
}