-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitbucketAPI.go
93 lines (78 loc) · 2.44 KB
/
bitbucketAPI.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func putReport(URL string, token string, report Report) error {
body := &report
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
req, _ := http.NewRequest("PUT", URL, buf)
req.Header.Set("Authorization", token)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, e := client.Do(req)
if e != nil {
fmt.Println("putReport failed", e)
return e
}
defer res.Body.Close()
fmt.Println("putReport response Status:", res.Status)
io.Copy(os.Stdout, res.Body) // Print the body to the stdout
return nil
}
func deleteAnnotations(annotationsURL string, token string) error {
req, _ := http.NewRequest("DELETE", annotationsURL, new(bytes.Buffer))
req.Header.Set("Authorization", token)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, e := client.Do(req)
if e != nil {
fmt.Println("deleteAnnotations failed", e)
return e
}
defer res.Body.Close()
fmt.Println("deleteAnnotations response Status:", res.Status)
io.Copy(os.Stdout, res.Body) // Print the body to the stdout
return nil
}
func postAnnotations(annotationsURL string, token string, annotations []Annotation) error {
// filter annotations for onl errors because of annotation limit on Bitbucket server
var filteredAnnotations []Annotation
for i := 0; i < len(annotations); i++ {
if annotations[i].Severity == "HIGH" {
filteredAnnotations = append(filteredAnnotations, annotations[i])
}
}
// fill annotations with warnings until limit reached
for i := 0; i < len(annotations); i++ {
if len(filteredAnnotations) < 1000 {
if annotations[i].Severity != "HIGH" {
filteredAnnotations = append(filteredAnnotations, annotations[i])
}
}
}
body := &BitbucketAnnotations{Annotations: filteredAnnotations}
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
req, _ := http.NewRequest("POST", annotationsURL, buf)
req.Header.Set("Authorization", token)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, e := client.Do(req)
if e != nil {
fmt.Println("postAnnotations failed", e)
return e
}
defer res.Body.Close()
fmt.Println("postAnnotations response Status:", res.Status)
io.Copy(os.Stdout, res.Body) // Print the body to the stdout
return nil
}