gitdeploy/internal/webhooks/bitbucket/bitbucket.go

153 lines
4.2 KiB
Go
Raw Permalink Normal View History

2020-09-29 08:08:35 +00:00
package bitbucket
import (
"crypto/subtle"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
2020-12-01 00:48:09 +00:00
"git.rootprojects.org/root/gitdeploy/internal/log"
2020-10-09 09:22:06 +00:00
"git.rootprojects.org/root/gitdeploy/internal/options"
"git.rootprojects.org/root/gitdeploy/internal/webhooks"
2020-09-29 08:08:35 +00:00
"github.com/go-chi/chi"
2021-02-22 22:48:33 +00:00
"github.com/google/go-github/v33/github"
2020-09-29 08:08:35 +00:00
)
func init() {
var secret string
name := "bitbucket"
options.ServerFlags.StringVar(
&secret, fmt.Sprintf("%s-secret", name), "",
fmt.Sprintf(
"secret for %s webhooks (same as %s_SECRET=)",
name, strings.ToUpper(name)),
)
webhooks.AddProvider("bitbucket", InitWebhook("bitbucket", &secret, "BITBUCKET_SECRET"))
}
// InitWebhook prepares the webhook router.
// It should be called after arguments are parsed and ENVs are set.InitWebhook
func InitWebhook(providername string, secretList *string, envname string) func() {
2020-09-29 08:08:35 +00:00
return func() {
secrets := webhooks.ParseSecrets(providername, *secretList, envname)
if 0 == len(secrets) {
fmt.Fprintf(os.Stderr, "skipped route for missing %q\n", envname)
2020-09-29 08:08:35 +00:00
return
}
2020-09-29 08:08:35 +00:00
webhooks.AddRouteHandler(providername, func(router chi.Router) {
router.Post("/", func(w http.ResponseWriter, r *http.Request) {
2020-09-29 09:57:49 +00:00
r.Body = http.MaxBytesReader(w, r.Body, options.DefaultMaxBodySize)
2020-09-29 08:08:35 +00:00
accessToken := r.URL.Query().Get("access_token")
if len(accessToken) > 0 {
var valid bool
accessTokenB := []byte(accessToken)
for _, secret := range secrets {
if 1 == subtle.ConstantTimeCompare(accessTokenB, secret) {
valid = true
break
}
}
if !valid {
log.Printf("invalid %q access_token\n", providername)
http.Error(w, fmt.Sprintf("invalid %q access_token", providername), http.StatusBadRequest)
2020-09-29 08:08:35 +00:00
return
}
}
payload, err := ioutil.ReadAll(r.Body)
if err != nil {
// if there's a read error, it should have been handled
// already by the MaxBytesReader
return
}
if 0 == len(accessToken) {
2020-09-29 08:08:35 +00:00
sig := r.Header.Get("X-Hub-Signature")
for _, secret := range secrets {
// TODO replace with generic X-Hub-Signature validation
if err = github.ValidateSignature(sig, payload, secret); nil != err {
continue
}
// err = nil
break
}
if nil != err {
log.Printf("invalid %q signature: error: %s\n", providername, err)
http.Error(w, fmt.Sprintf("invalid %q signature", providername), http.StatusBadRequest)
2020-09-29 08:08:35 +00:00
return
}
}
info := Webhook{}
if err := json.Unmarshal(payload, &info); nil != err {
log.Printf("invalid bitbucket payload: error: %s\n%s\n", err, string(payload))
http.Error(w, "invalid bitbucket payload", http.StatusBadRequest)
return
}
2021-02-21 11:25:42 +00:00
//var branch string
//var tag string
2020-09-29 08:08:35 +00:00
var ref string
n := len(info.Push.Changes)
if n < 1 {
log.Printf("invalid bitbucket changeset (n): %d\n%s\n", n, string(payload))
http.Error(w, "invalid bitbucket payload", http.StatusBadRequest)
return
} else if n > 1 {
log.Printf("more than one bitbucket changeset (n): %d\n%s\n", n, string(payload))
}
refName := info.Push.Changes[0].New.Name
refType := info.Push.Changes[0].New.Type
switch refType {
case "tag":
2021-02-21 11:25:42 +00:00
//tag = refName
2020-09-29 08:08:35 +00:00
ref = fmt.Sprintf("refs/tags/%s", refName)
case "branch":
2021-02-21 11:25:42 +00:00
//branch = refName
2020-09-29 08:08:35 +00:00
ref = fmt.Sprintf("refs/heads/%s", refName)
default:
2020-12-01 00:48:09 +00:00
log.Printf("unexpected bitbucket RefType %s\n", refType)
2020-09-29 08:08:35 +00:00
ref = fmt.Sprintf("refs/UNKNOWN/%s", refName)
}
switch refType {
case "tags":
refType = "tag"
2021-02-21 11:25:42 +00:00
//tag = refName
2020-09-29 08:08:35 +00:00
case "heads":
refType = "branch"
2021-02-21 11:25:42 +00:00
//branch = refName
2020-09-29 08:08:35 +00:00
}
var rev string
if len(info.Push.Changes[0].Commits) > 0 {
// TODO first or last?
// TODO shouldn't tags have a Commit as well?
rev = info.Push.Changes[0].Commits[0].Hash
}
webhooks.Hook(webhooks.Ref{
2021-02-21 11:25:42 +00:00
// appears to be missing timestamp
2020-09-29 08:08:35 +00:00
HTTPSURL: info.Repository.Links.HTML.Href,
Rev: rev,
Ref: ref,
RefType: refType,
RefName: refName,
Repo: info.Repository.Name,
Owner: info.Repository.Workspace.Slug,
2021-02-21 11:25:42 +00:00
//Branch: branch,
//Tag: tag,
2020-09-29 08:08:35 +00:00
})
})
})
}
}