~sircmpwn/git.sr.ht

git.sr.ht/gitsrht-update-hook/webhooks.go -rw-r--r-- 3.6 KiB
5c21171dDrew DeVault API: Implement mutation { deleteArtifact } 22 hours ago
                                                                                
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
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"regexp"
	"strings"
	"time"
	"unicode/utf8"

	"git.sr.ht/~sircmpwn/core-go/crypto"
	"github.com/google/uuid"
	"github.com/mattn/go-runewidth"
)

type WebhookSubscription struct {
	Id     int
	Url    string
	Events string
}

// Note: unlike normal sr.ht services, we don't add webhook deliveries to the
// database until after the HTTP request has been completed, to reduce time
// spent blocking the user's terminal.
type WebhookDelivery struct {
	Headers         string
	Payload         string
	Response        string
	ResponseHeaders string
	ResponseStatus  int
	SubscriptionId  int
	UUID            string
	Url             string
}

type UpdatedRef struct {
	Tag  *AnnotatedTag `json:"annotated_tag",omitempty`
	Name string        `json:"name"`
	Old  *Commit       `json:"old"`
	New  *Commit       `json:"new"`
}

type WebhookPayload struct {
	Push     string            `json:"push"`
	PushOpts map[string]string `json:"push-options"`
	Pusher   UserContext       `json:"pusher"`
	Refs     []UpdatedRef      `json:"refs"`
}

var ansi = regexp.MustCompile("\x1B\\[[0-?]*[ -/]*[@-~]")

func deliverWebhooks(subs []WebhookSubscription,
	payload []byte, printResponse bool) []WebhookDelivery {

	var deliveries []WebhookDelivery
	client := &http.Client{Timeout: 5 * time.Second}

	for _, sub := range subs {
		nonce, signature := crypto.SignWebhook(payload)

		deliveryUuid := uuid.New().String()
		body := bytes.NewBuffer(payload)
		req, err := http.NewRequest("POST", sub.Url, body)
		req.Header.Add("Content-Type", "application/json")
		req.Header.Add("X-Webhook-Event", "repo:post-update")
		req.Header.Add("X-Webhook-Delivery", deliveryUuid)
		req.Header.Add("X-Payload-Nonce", nonce)
		req.Header.Add("X-Payload-Signature", signature)

		var requestHeaders bytes.Buffer
		for name, values := range req.Header {
			requestHeaders.WriteString(fmt.Sprintf("%s: %s\n",
				name, strings.Join(values, ", ")))
		}

		delivery := WebhookDelivery{
			Headers:        requestHeaders.String(),
			Payload:        string(payload),
			ResponseStatus: -1,
			SubscriptionId: sub.Id,
			UUID:           deliveryUuid,
			Url:            sub.Url,
		}

		resp, err := client.Do(req)
		if err != nil {
			delivery.Response = fmt.Sprintf("Error sending webhook: %v", err)
			log.Printf(delivery.Response)
			deliveries = append(deliveries, delivery)
			continue
		}
		defer resp.Body.Close()
		respBody, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			delivery.Response = fmt.Sprintf("Error reading webhook "+
				"response: %v", err)
			log.Printf(delivery.Response)
			deliveries = append(deliveries, delivery)
			continue
		}
		if !utf8.Valid(respBody) {
			delivery.Response = "Webhook response is not valid UTF-8"
			log.Printf(delivery.Response)
			deliveries = append(deliveries, delivery)
			continue
		}
		if printResponse {
			u, _ := url.Parse(sub.Url) // Errors will have happened earlier
			log.Println("Response from %s:", u.Host)
			log.Println(runewidth.Truncate(ansi.ReplaceAllString(
				string(respBody), ""), 1024, "..."))
		}
		logger.Printf("Delivered webhook to %s (sub %d), got %d",
			sub.Url, sub.Id, resp.StatusCode)

		var responseHeaders bytes.Buffer
		for name, values := range resp.Header {
			responseHeaders.WriteString(fmt.Sprintf("%s: %s\n",
				name, strings.Join(values, ", ")))
		}

		delivery.ResponseStatus = resp.StatusCode
		delivery.ResponseHeaders = responseHeaders.String()
		if len(respBody) > 65535 {
			delivery.Response = string(respBody)[:65535]
		} else {
			delivery.Response = string(respBody)
		}
		deliveries = append(deliveries, delivery)
	}

	return deliveries
}