Created
August 19, 2022 14:47
-
-
Save maurorappa/b1a02c98d0e98c30323dcf9911439922 to your computer and use it in GitHub Desktop.
Bitbucket webhook parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Enable webhook for a Bitbucket repository, get the json of the events, parse the json and notify (here in Slack) | |
// This is well suited to run serverless | |
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
type AutoGenerated struct { | |
Push struct { | |
Changes []struct { | |
Forced bool `json:"forced"` | |
Old struct { | |
Name string `json:"name"` | |
Links struct { | |
Commits struct { | |
Href string `json:"href"` | |
} `json:"commits"` | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
} `json:"links"` | |
DefaultMergeStrategy string `json:"default_merge_strategy"` | |
MergeStrategies []string `json:"merge_strategies"` | |
Type string `json:"type"` | |
Target struct { | |
Rendered struct { | |
} `json:"rendered"` | |
Hash string `json:"hash"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
} `json:"links"` | |
Author struct { | |
Raw string `json:"raw"` | |
Type string `json:"type"` | |
} `json:"author"` | |
Summary struct { | |
Raw string `json:"raw"` | |
Markup string `json:"markup"` | |
HTML string `json:"html"` | |
Type string `json:"type"` | |
} `json:"summary"` | |
Parents []struct { | |
Hash string `json:"hash"` | |
Type string `json:"type"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
} `json:"links"` | |
} `json:"parents"` | |
Date time.Time `json:"date"` | |
Message string `json:"message"` | |
Type string `json:"type"` | |
Properties struct { | |
} `json:"properties"` | |
} `json:"target"` | |
} `json:"old"` | |
Links struct { | |
Commits struct { | |
Href string `json:"href"` | |
} `json:"commits"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
Diff struct { | |
Href string `json:"href"` | |
} `json:"diff"` | |
} `json:"links"` | |
Created bool `json:"created"` | |
Commits []struct { | |
Rendered struct { | |
} `json:"rendered"` | |
Hash string `json:"hash"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
Comments struct { | |
Href string `json:"href"` | |
} `json:"comments"` | |
Patch struct { | |
Href string `json:"href"` | |
} `json:"patch"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
Diff struct { | |
Href string `json:"href"` | |
} `json:"diff"` | |
Approve struct { | |
Href string `json:"href"` | |
} `json:"approve"` | |
Statuses struct { | |
Href string `json:"href"` | |
} `json:"statuses"` | |
} `json:"links"` | |
Author struct { | |
Raw string `json:"raw"` | |
Type string `json:"type"` | |
} `json:"author"` | |
Summary struct { | |
Raw string `json:"raw"` | |
Markup string `json:"markup"` | |
HTML string `json:"html"` | |
Type string `json:"type"` | |
} `json:"summary"` | |
Parents []struct { | |
Hash string `json:"hash"` | |
Type string `json:"type"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
} `json:"links"` | |
} `json:"parents"` | |
Date time.Time `json:"date"` | |
Message string `json:"message"` | |
Type string `json:"type"` | |
Properties struct { | |
} `json:"properties"` | |
} `json:"commits"` | |
Truncated bool `json:"truncated"` | |
Closed bool `json:"closed"` | |
New struct { | |
Name string `json:"name"` | |
Links struct { | |
Commits struct { | |
Href string `json:"href"` | |
} `json:"commits"` | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
} `json:"links"` | |
DefaultMergeStrategy string `json:"default_merge_strategy"` | |
MergeStrategies []string `json:"merge_strategies"` | |
Type string `json:"type"` | |
Target struct { | |
Rendered struct { | |
} `json:"rendered"` | |
Hash string `json:"hash"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
} `json:"links"` | |
Author struct { | |
Raw string `json:"raw"` | |
Type string `json:"type"` | |
} `json:"author"` | |
Summary struct { | |
Raw string `json:"raw"` | |
Markup string `json:"markup"` | |
HTML string `json:"html"` | |
Type string `json:"type"` | |
} `json:"summary"` | |
Parents []struct { | |
Hash string `json:"hash"` | |
Type string `json:"type"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
} `json:"links"` | |
} `json:"parents"` | |
Date time.Time `json:"date"` | |
Message string `json:"message"` | |
Type string `json:"type"` | |
Properties struct { | |
} `json:"properties"` | |
} `json:"target"` | |
} `json:"new"` | |
} `json:"changes"` | |
} `json:"push"` | |
Actor struct { | |
DisplayName string `json:"display_name"` | |
UUID string `json:"uuid"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
Avatar struct { | |
Href string `json:"href"` | |
} `json:"avatar"` | |
} `json:"links"` | |
Type string `json:"type"` | |
Nickname string `json:"nickname"` | |
AccountID string `json:"account_id"` | |
} `json:"actor"` | |
Repository struct { | |
Scm string `json:"scm"` | |
Website interface{} `json:"website"` | |
UUID string `json:"uuid"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
Avatar struct { | |
Href string `json:"href"` | |
} `json:"avatar"` | |
} `json:"links"` | |
Project struct { | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
Avatar struct { | |
Href string `json:"href"` | |
} `json:"avatar"` | |
} `json:"links"` | |
Type string `json:"type"` | |
Name string `json:"name"` | |
Key string `json:"key"` | |
UUID string `json:"uuid"` | |
} `json:"project"` | |
FullName string `json:"full_name"` | |
Owner struct { | |
Username string `json:"username"` | |
DisplayName string `json:"display_name"` | |
Type string `json:"type"` | |
UUID string `json:"uuid"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
Avatar struct { | |
Href string `json:"href"` | |
} `json:"avatar"` | |
} `json:"links"` | |
} `json:"owner"` | |
Workspace struct { | |
Slug string `json:"slug"` | |
Type string `json:"type"` | |
Name string `json:"name"` | |
Links struct { | |
Self struct { | |
Href string `json:"href"` | |
} `json:"self"` | |
HTML struct { | |
Href string `json:"href"` | |
} `json:"html"` | |
Avatar struct { | |
Href string `json:"href"` | |
} `json:"avatar"` | |
} `json:"links"` | |
UUID string `json:"uuid"` | |
} `json:"workspace"` | |
Type string `json:"type"` | |
IsPrivate bool `json:"is_private"` | |
Name string `json:"name"` | |
} `json:"repository"` | |
} | |
func main() { | |
log.Print("starting server...") | |
http.HandleFunc("/", handler) | |
// Determine port for HTTP service. | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "8080" | |
log.Printf("defaulting to port %s", port) | |
} | |
// Start HTTP server. | |
log.Printf("listening on port %s", port) | |
if err := http.ListenAndServe(":"+port, nil); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
data, _ := ioutil.ReadAll(r.Body) | |
var result AutoGenerated | |
if err := json.Unmarshal(data, &result); err != nil { // Parse []byte to go struct pointer | |
fmt.Println("Can not unmarshal JSON") | |
} | |
//body := string(data) | |
//fmt.Fprintf(w, "BB sent: %s\n\n", body) | |
tag := result.Push.Changes[0].New.Type | |
tag_name := result.Push.Changes[0].New.Name | |
fmt.Fprintf(w, "type: %s, tag: %s \n\n", tag, tag_name) | |
author := result.Actor.DisplayName | |
accountId := result.Actor.AccountID | |
fmt.Fprintf(w, "Author: %s, Id: %s \n\n", author, accountId) | |
//notify(author) | |
oldTag := result.Push.Changes[0].Old.Target.Hash | |
newTag := result.Push.Changes[0].New.Target.Hash | |
fmt.Fprintf(w, "oldTag: %s, newTag: %s \n\n", oldTag, newTag) | |
} | |
func notify(who string) { | |
url := "https://hooks.slack.com/services/YOUR_TOKEN" | |
postBody, _ := json.Marshal(map[string]string{ | |
"text": who + " pushed", | |
}) | |
responseBody := bytes.NewBuffer(postBody) | |
resp, err := http.Post( url , "application/json", responseBody) | |
if err != nil { | |
log.Printf("An Error Occured %v", err) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
sb := string(body) | |
fmt.Print("%s", sb) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment