-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (57 loc) · 1.87 KB
/
main.go
File metadata and controls
66 lines (57 loc) · 1.87 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)
// read env vars, no inference.
var secret string = os.Getenv("bitbucket_secret")
var appname string = os.Getenv("app_name")
var token string = os.Getenv("bitbucket_token")
var apiurl string = os.Getenv("apiurl")
var webhookurl string = os.Getenv("webhookurl")
type Payload struct {
Events []string `json:"events"`
Active bool `json:"active"`
Statistics Statistics `json:"statistics"`
Configuration Configuration `json:"configuration"`
URL string `json:"url"`
Name string `json:"name"`
}
type Statistics struct {
}
type Configuration struct {
CreatedBy string `json:"createdBy"`
Secret string `json:"secret"`
}
func main() {
data := Payload{
Events: []string{"repo:refs_changed"}, // the events that BBS should monitor, when this occurs the trigger is fired and a webhook call is made
Active: true,
Statistics: Statistics{},
Configuration: Configuration{"bitbucket", secret},
URL: webhookurl, // this is the URL that BBS should send a event payload to when a commit is made.
Name: "Argo Events", // the name given to the webhook that gets created in BBS
}
payloadBytes, err := json.Marshal(data)
if err != nil {
fmt.Println("Failure : ", err)
}
body := bytes.NewReader(payloadBytes)
// The request is sent to the api url with the repo-slug name appended
req, err := http.NewRequest("POST", apiurl+appname+"/webhooks", body)
if err != nil {
fmt.Println("Failure : ", err)
}
// set headers, grab token from env variable
req.Header.Set("Authorization", "Bearer "+token+"")
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
defer resp.Body.Close()
}