forked from gfleury/go-bitbucket-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_response.go
715 lines (628 loc) · 21.6 KB
/
api_response.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/
package bitbucketv1
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/mitchellh/mapstructure"
)
// APIResponse contains generic data from API Response
type APIResponse struct {
*http.Response `json:"-"`
Message string `json:"message,omitempty"`
// Operation is the name of the swagger operation.
Operation string `json:"operation,omitempty"`
// RequestURL is the request URL. This value is always available, even if the
// embedded *http.Response is nil.
RequestURL string `json:"url,omitempty"`
// Method is the HTTP method used for the request. This value is always
// available, even if the embedded *http.Response is nil.
Method string `json:"method,omitempty"`
// Payload holds the contents of the response body (which may be nil or empty).
// This is provided here as the raw response.Body() reader will have already
// been drained.
Payload []byte `json:"-"`
Values map[string]interface{}
}
type SelfLink struct {
Href string `json:"href"`
}
type CloneLink struct {
Href string `json:"href"`
Name string `json:"name"`
}
type Links struct {
Self []SelfLink `json:"self,omitempty"`
}
type Project struct {
Key string `json:"key"`
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Public bool `json:"public"`
Type string `json:"type"`
Links Links `json:"links"`
}
// Repository contains data from a BitBucket Repository
type Repository struct {
Slug string `json:"slug,omitempty"`
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ScmID string `json:"scmId,omitempty"`
State string `json:"state,omitempty"`
StatusMessage string `json:"statusMessage,omitempty"`
Forkable bool `json:"forkable,omitempty"`
Project *Project `json:"project,omitempty"`
Public bool `json:"public,omitempty"`
Links *struct {
Clone []CloneLink `json:"clone,omitempty"`
Self []SelfLink `json:"self,omitempty"`
} `json:"links,omitempty"`
Owner *struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
ID int `json:"id"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
Slug string `json:"slug"`
Type string `json:"type"`
AvatarURL string `json:"avatarUrl"`
} `json:"owner,omitempty"`
Origin *Repository `json:"origin,omitempty"`
}
type UserWithNameEmail struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
}
type UserWithLinks struct {
Name string `json:"name,omitempty"`
EmailAddress string `json:"emailAddress,omitempty"`
ID int `json:"id,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Active bool `json:"active,omitempty"`
Slug string `json:"slug,omitempty"`
Type string `json:"type,omitempty"`
Links Links `json:"links,omitempty"`
}
type User struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
ID int `json:"id"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
Slug string `json:"slug"`
Type string `json:"type"`
DirectoryName string `json:"directoryName"`
Deletable bool `json:"deletable"`
LastAuthenticationTimestamp int64 `json:"lastAuthenticationTimestamp"`
MutableDetails bool `json:"mutableDetails"`
MutableGroups bool `json:"mutableGroups"`
}
type UserWithMetadata struct {
User UserWithLinks `json:"user,omitempty"`
Role string `json:"role,omitempty"`
Approved bool `json:"approved,omitempty"`
Status string `json:"status,omitempty"`
LastReviewedCommit string `json:"lastReviewedCommit,omitempty"`
}
// PermissionGlobal are global permissions
type PermissionGlobal string
const (
// PermissionGlobalLicensedUser represents the ability to log into the system
PermissionGlobalLicensedUser PermissionGlobal = "LICENSED_USER"
// PermissionGlobalProjectCreate allows project creation
PermissionGlobalProjectCreate PermissionGlobal = "PROJECT_CREATE"
// PermissionGlobalAdmin represents an administrator
PermissionGlobalAdmin PermissionGlobal = "ADMIN"
// PermissionGlobalSysAdmin represents a system administrator
PermissionGlobalSysAdmin PermissionGlobal = "SYS_ADMIN"
)
// PermissionProject are project level permissions
type PermissionProject string
const (
// PermissionProjectAdmin grants admin priviledges
PermissionProjectAdmin PermissionProject = "PROJECT_ADMIN"
// PermissionProjectRead grants read priviledges
PermissionProjectRead PermissionProject = "PROJECT_READ"
// PermissionProjectWrite grants write priviledges
PermissionProjectWrite PermissionProject = "PROJECT_WRITE"
)
// PermissionRepository are repository level permissions
type PermissionRepository string
const (
// PermissionRepositoryAdmin grants admin priviledges
PermissionRepositoryAdmin PermissionRepository = "REPO_ADMIN"
// PermissionRepositoryRead grants read priviledges
PermissionRepositoryRead PermissionRepository = "REPO_READ"
// PermissionRepositoryWrite grants write priviledges
PermissionRepositoryWrite PermissionRepository = "REPO_WRITE"
)
// UserPermission contains a user with its permission
type UserPermission struct {
User User `json:"user"`
Permission string `json:"permission"`
}
// Group represents a user group
type Group struct {
Name string `json:"name"`
}
// GroupPermission contains a group with its permission
type GroupPermission struct {
Group Group `json:"group"`
Permission string `json:"permission"`
}
type MergeResult struct {
Outcome string `json:"outcome"`
Current bool `json:"current"`
}
type MergeGetResponse struct {
CanMerge bool `json:"canMerge"`
Conflicted bool `json:"conflicted"`
Outcome string `json:"outcome"`
Vetoes []map[string]interface{} `json:"vetoes"`
}
type PullRequestRef struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
LatestCommit string `json:"latestCommit"`
Repository Repository `json:"repository"`
}
type PullRequest struct {
ID int `json:"id"`
Version int32 `json:"version"`
Title string `json:"title"`
Description string `json:"description"`
State string `json:"state"`
Open bool `json:"open"`
Closed bool `json:"closed"`
CreatedDate int64 `json:"createdDate"`
UpdatedDate int64 `json:"updatedDate"`
FromRef PullRequestRef `json:"fromRef"`
ToRef PullRequestRef `json:"toRef"`
Locked bool `json:"locked"`
Author *UserWithMetadata `json:"author,omitempty"`
Reviewers []UserWithMetadata `json:"reviewers"`
Participants []UserWithMetadata `json:"participants,omitempty"`
Properties struct {
MergeResult MergeResult `json:"mergeResult"`
ResolvedTaskCount int `json:"resolvedTaskCount"`
OpenTaskCount int `json:"openTaskCount"`
} `json:"properties"`
Links Links `json:"links"`
}
// SSHKey contains data from a SSHKey in the BitBucket Server
type SSHKey struct {
ID int `json:"id"`
Text string `json:"text"`
Label string `json:"label"`
}
// Commit contains data from a commit in BitBucket
type Commit struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Author User `json:"author"`
AuthorTimestamp int64 `json:"authorTimestamp"`
Committer User `json:"committer"`
CommitterTimestamp int64 `json:"committerTimestamp"`
Message string `json:"message"`
Parents []struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
} `json:"parents"`
}
type BuildStatus struct {
State string `json:"state"`
Key string `json:"key"`
Name string `json:"name"`
Url string `json:"url"`
Description string `json:"description"`
DateAdded int64 `json:"dateAdded"`
}
type Diff struct {
Diffs []struct {
Source struct {
Components []string `json:"components"`
Parent string `json:"parent"`
Name string `json:"name"`
Extension string `json:"extension"`
ToString string `json:"toString"`
} `json:"source"`
Destination struct {
Components []string `json:"components"`
Parent string `json:"parent"`
Name string `json:"name"`
Extension string `json:"extension"`
ToString string `json:"toString"`
} `json:"destination"`
Hunks []struct {
SourceLine int `json:"sourceLine"`
SourceSpan int `json:"sourceSpan"`
DestinationLine int `json:"destinationLine"`
DestinationSpan int `json:"destinationSpan"`
Segments []struct {
Type string `json:"type"`
Lines []struct {
Destination int `json:"destination"`
Source int `json:"source"`
Line string `json:"line"`
Truncated bool `json:"truncated"`
} `json:"lines"`
Truncated bool `json:"truncated"`
} `json:"segments"`
Truncated bool `json:"truncated"`
} `json:"hunks"`
Truncated bool `json:"truncated"`
ContextLines float64 `json:"contextLines"`
FromHash string `json:"fromHash"`
ToHash string `json:"toHash"`
WhiteSpace string `json:"whiteSpace"`
} `json:"diffs"`
Truncated bool `json:"truncated"`
ContextLines float64 `json:"contextLines"`
FromHash string `json:"fromHash"`
ToHash string `json:"toHash"`
WhiteSpace string `json:"whiteSpace"`
}
// Tag contaings git Tag information
type Tag struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type string `json:"type"`
LatestCommit string `json:"latestCommit"`
LatestChangeset string `json:"latestChangeset"`
Hash string `json:"hash"`
}
// Branch contains git Branch information
type Branch struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type string `json:"type"`
LatestCommit string `json:"latestCommit"`
LatestChangeset string `json:"latestChangeset"`
IsDefault bool `json:"isDefault"`
}
// Content contains repository content information (and files content)
type Content struct {
Children struct {
IsLastPage bool `json:"isLastPage"`
Limit int `json:"limit"`
Size int `json:"size"`
Start int `json:"start"`
Values []struct {
ContentID string `json:"contentId,omitempty"`
Path struct {
Components []string `json:"components"`
Extension string `json:"extension"`
Name string `json:"name"`
Parent string `json:"parent"`
ToString string `json:"toString"`
} `json:"path"`
Size int `json:"size,omitempty"`
Type string `json:"type"`
Node string `json:"node,omitempty"`
} `json:"values"`
} `json:"children"`
Path struct {
Components []string `json:"components"`
Name string `json:"name"`
ToString string `json:"toString"`
} `json:"path"`
Revision string `json:"revision"`
}
type WebhookConfiguration struct {
Secret string `json:"secret"`
}
type Webhook struct {
ID int `json:"id"`
Name string `json:"name"`
Events []string `json:"events"`
Url string `json:"url"`
Active bool `json:"active"`
Configuration WebhookConfiguration `json:"configuration"`
}
type Code struct {
Category string `json:"category,omitempty"`
IsLastPage bool `json:"isLastPage,omitempty"`
Count int `json:"count,omitempty"`
Start int `json:"start,omitempty"`
Limit int `json:"limit,omitempty"`
NextStart int `json:"nextStart,omitempty"`
Values []struct {
Repository *Repository `json:"repository,omitempty"`
File string `json:"file"`
HitContexts [][]struct {
Line int `json:"line"`
Text string `json:"text"`
} `json:"hitContexts"`
HitCount int `json:"hitCount"`
} `json:"values,omitempty"`
}
type Scope struct {
Type string `json:"type"`
}
type Query struct {
Substituted bool `json:"substituted"`
}
type SearchResult struct {
Scope Scope `json:"scope"`
Code *Code `json:"code,omitempty"`
Repositories *Code `json:"repositories,omitempty"`
Query Query `json:"query"`
}
type Limits struct {
Primary int `json:"primary"`
Secondary int `json:"secondary"`
}
type SearchQuery struct {
Query string `json:"query"`
Entities struct {
Code Code `json:"code"`
} `json:"entities"`
Limits Limits `json:"limits"`
}
type Parent struct {
ID int `json:"id"`
}
type DiffType string
const (
DiffTypeEffective DiffType = "EFFECTIVE"
DiffTypeCommit DiffType = "COMMIT"
DiffTypeRange DiffType = "RANGE"
)
type LineType string
const (
LineTypeAdded LineType = "ADDED"
LineTypeRemoved LineType = "REMOVED"
LineTypeContext LineType = "CONTEXT"
)
type FileType string
const (
FileTypeFrom FileType = "FROM"
FileTypeTo FileType = "TO"
)
type Anchor struct {
DiffType DiffType `json:"diffType,omitempty"`
Line int `json:"line,omitempty"`
LineType LineType `json:"lineType,omitempty"`
FileType FileType `json:"fileType,omitempty"`
FromHash string `json:"fromHash,omitempty"`
Path string `json:"path,omitempty"`
SrcPath string `json:"srcPath,omitempty"`
ToHash string `json:"toHash,omitempty"`
}
type Comment struct {
Text string `json:"text"`
Parent *Parent `json:"parent,omitempty"`
Anchor *Anchor `json:"anchor,omitempty"`
}
type Properties struct {
Key string `json:"key"`
}
type PermittedOperations struct {
Editable bool `json:"editable"`
Deletable bool `json:"deletable"`
}
type ActivityComment struct {
Properties Properties `json:"properties"`
ID int `json:"id"`
Version int `json:"version"`
Text string `json:"text"`
Author User `json:"author"`
CreatedDate int64 `json:"createdDate"`
UpdatedDate int64 `json:"updatedDate"`
Comments []ActivityComment `json:"comments"`
PermittedOperations PermittedOperations `json:"permittedOperations"`
}
type CommitsStats struct {
Commits []Commit `json:"commits"`
Total int `json:"total"`
}
type Action string
const (
ActionCommented Action = "COMMENTED"
ActionRescoped Action = "RESCOPED"
ActionMerged Action = "MERGED"
ActionApproved Action = "APPROVED"
ActionDeclined Action = "DECLINED"
ActionOpened Action = "OPENED"
)
type Activity struct {
ID int `json:"id"`
CreatedDate int `json:"createdDate"`
User User `json:"user"`
Action Action `json:"action"`
CommentAction string `json:"commentAction"`
Comment ActivityComment `json:"comment"`
CommentAnchor Anchor `json:"commentAnchor"`
FromHash string `json:"fromHash,omitempty"`
PreviousFromHash string `json:"previousFromHash,omitempty"`
PreviousToHash string `json:"previousToHash,omitempty"`
ToHash string `json:"toHash,omitempty"`
Added CommitsStats `json:"added"`
Removed CommitsStats `json:"removed"`
}
type Activities struct {
NextPageStart int `json:"nextPageStart"`
IsLastPage bool `json:"isLastPage"`
Limit int `json:"limit"`
Size int `json:"size"`
Start int `json:"start"`
Values []Activity `json:"values"`
}
// String converts global permission to its string representation
func (p PermissionGlobal) String() string {
return string(p)
}
// String converts project permission to its string representation
func (p PermissionProject) String() string {
return string(p)
}
// String converts repository permission to its string representation
func (p PermissionRepository) String() string {
return string(p)
}
func (k *SSHKey) String() string {
parts := make([]string, 1, 2)
parts[0] = strings.TrimSpace(k.Text)
return strings.Join(parts, " ")
}
// GetCommitsResponse cast Commits into structure
func GetCommitsResponse(r *APIResponse) ([]Commit, error) {
var m []Commit
err := mapstructure.Decode(r.Values["values"], &m)
return m, err
}
// GetTagsResponse cast Tags into structure
func GetTagsResponse(r *APIResponse) ([]Tag, error) {
var m []Tag
err := mapstructure.Decode(r.Values["values"], &m)
return m, err
}
// GetBranchesResponse cast Tags into structure
func GetBranchesResponse(r *APIResponse) ([]Branch, error) {
var m []Branch
err := mapstructure.Decode(r.Values["values"], &m)
return m, err
}
// GetRepositoriesResponse cast Repositories into structure
func GetRepositoriesResponse(r *APIResponse) ([]Repository, error) {
var m []Repository
err := mapstructure.Decode(r.Values["values"], &m)
return m, err
}
// GetRepositoryResponse cast Repositories into structure
func GetRepositoryResponse(r *APIResponse) (Repository, error) {
var m Repository
err := mapstructure.Decode(r.Values, &m)
return m, err
}
// GetDiffResponse cast Diff into structure
func GetDiffResponse(r *APIResponse) (Diff, error) {
var m Diff
err := mapstructure.Decode(r.Values, &m)
return m, err
}
// GetSSHKeysResponse cast SSHKeys into structure
func GetSSHKeysResponse(r *APIResponse) ([]SSHKey, error) {
var m []SSHKey
err := mapstructure.Decode(r.Values["values"], &m)
return m, err
}
// GetBuildStatusResponse cast BuildStatus into structure
func GetBuildStatusesResponse(r *APIResponse) ([]BuildStatus, error) {
var m []BuildStatus
err := mapstructure.Decode(r.Values["values"], &m)
return m, err
}
// GetPullRequestResponse cast PullRequest into structure
func GetPullRequestResponse(r *APIResponse) (PullRequest, error) {
var m PullRequest
err := mapstructure.Decode(r.Values, &m)
return m, err
}
// GetPullRequestResponse PullRequests into structure
func GetPullRequestsResponse(r *APIResponse) ([]PullRequest, error) {
var m []PullRequest
err := mapstructure.Decode(r.Values["values"], &m)
return m, err
}
// GetContentResponse cast Content into structure
func GetContentResponse(r *APIResponse) (Content, error) {
var c Content
err := mapstructure.Decode(r.Values, &c)
return c, err
}
// GetUserWithMetadataResponse casts users into structure
func GetUserWithMetadataResponse(r *APIResponse) (UserWithMetadata, error) {
var c UserWithMetadata
err := mapstructure.Decode(r.Values, &c)
return c, err
}
// GetUsersResponse casts users into structure
func GetUsersResponse(r *APIResponse) ([]User, error) {
var c []User
err := mapstructure.Decode(r.Values["values"], &c)
return c, err
}
// GetUsersPermissionResponse casts user permissions into structure
func GetUsersPermissionResponse(r *APIResponse) ([]UserPermission, error) {
var c []UserPermission
err := mapstructure.Decode(r.Values["values"], &c)
return c, err
}
// GetGroupsPermissionResponse casts group permissions into structure
func GetGroupsPermissionResponse(r *APIResponse) ([]GroupPermission, error) {
var c []GroupPermission
err := mapstructure.Decode(r.Values["values"], &c)
return c, err
}
// GetWebhooksResponse cast Webhooks into structure
func GetWebhooksResponse(r *APIResponse) ([]Webhook, error) {
var h []Webhook
err := mapstructure.Decode(r.Values["values"], &h)
return h, err
}
// GetSearchResultResponse cast Search results into structure
func GetSearchResultResponse(r *APIResponse) (SearchResult, error) {
var h SearchResult
err := mapstructure.Decode(r.Values, &h)
return h, err
}
// GetActivitiesResponse cast Activities results into structure
func GetActivitiesResponse(r *APIResponse) (Activities, error) {
var h Activities
err := json.Unmarshal(r.Payload, &h)
return h, err
}
// NewAPIResponse create new APIResponse from http.Response
func NewAPIResponse(r *http.Response) *APIResponse {
response := &APIResponse{Response: r}
return response
}
// NewAPIResponseWithError create new erroneous API response from http.response and error
func NewAPIResponseWithError(r *http.Response, bodyBytes []byte, err error) (*APIResponse, error) {
response := &APIResponse{Response: r, Message: strings.Replace(err.Error(), "\"", "", -1)}
if bodyBytes != nil {
parseErr := json.Unmarshal(bodyBytes, &response.Values)
if parseErr != nil {
err = fmt.Errorf("%s ParseError: %v", err.Error(), parseErr)
}
}
return response, err
}
// NewBitbucketAPIResponse create new API response from http.response
func NewBitbucketAPIResponse(r *http.Response) (*APIResponse, error) {
response := &APIResponse{Response: r}
err := json.NewDecoder(r.Body).Decode(&response.Values)
if err != nil {
return nil, err
}
return response, err
}
// NewRawAPIResponse create new API response from http.response with raw data
func NewRawAPIResponse(r *http.Response) (*APIResponse, error) {
response := &APIResponse{Response: r}
raw, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
response.Payload = raw
return response, nil
}
// HasNextPage returns if response is paged and has next page and where it does start
func HasNextPage(response *APIResponse) (isLastPage bool, nextPageStart int) {
isLastPage, ok := response.Values["isLastPage"].(bool)
if ok && !isLastPage {
floatStart, ok := response.Values["nextPageStart"].(float64)
if ok {
nextPageStart = int(floatStart)
}
} else if !ok {
isLastPage = true
}
return !isLastPage, nextPageStart
}