Skip to content

Commit

Permalink
Merge pull request #165 from vito/rm-srv
Browse files Browse the repository at this point in the history
remove 'server mode' experimentation
  • Loading branch information
vito authored May 19, 2022
2 parents 33be765 + a6984cb commit 2a3cd1c
Show file tree
Hide file tree
Showing 9 changed files with 2 additions and 401 deletions.
106 changes: 2 additions & 104 deletions bass/github.bass
Original file line number Diff line number Diff line change
@@ -1,92 +1,8 @@
#!/usr/bin/env bass

(provide [event-handler start-check start-status]
(provide [start-status]
(def *memos* *dir*/memos.json)

(use (.hmac)
(.strings)
(.git (linux/alpine/git)))

; returns a module with a GitHub webhook event handler that dispatches events
; back to the repo that they came from
;
; Verifies webhook payloads with the provided webhook secret, returning an
; error if the signature doesn't match.
;
; Retrieves the repository from the event payload, clones it, loads
; project.bass from the root of the source tree, and calls (github-event)
; with the event type, event payload, and a module providing functions for
; interacting with GitHub (e.g. check creation).
(defn event-handler [app-id !hook-secret! !private-key!]
(module [handle]
; accepts webhook payloads and asynchronously dispatches events
(defn handle [request respond]
(let [{:headers {:X-Github-Delivery delivery
:X-Github-Event event
:X-Hub-Signature-256 signature}
:body body} request]
(verify! body signature)

(log "handling" :delivery delivery :event event)
(respond {:handling delivery})
(dispatch (decode-json body) delivery event)))

; verifies the HMAC signature and errors if the signature is invalid
(defn verify! [body signature]
(let [[scheme claim] (strings:split signature "=")]
(if (hmac:verify scheme !hook-secret! claim body)
:ok
(error "invalid signature"))))

; a module for interacting with GitHub on behalf of the app
(defn gh-client [auth]
(module [check]
(defn check [thunk name sha repo]
(start-check thunk name sha repo auth))))

; forwards the event to the repository it came from
;
; Clones the repository at its default branch so that pull requests
; cannot just zero-out tests or introduce malicious Bass code.
;
; Loads project.bass from the root of the repository and calls
; (github-event) with the event type, event payload, and a module
; providing functions for interacting with GitHub (e.g. check creation).
(defn dispatch [payload delivery event]
(let [{:repository
{:full-name repo-name
:clone-url url
:default-branch branch
:pushed-at pushed-at}
:installation {:id inst-id}} payload
client (gh-client {:app-id app-id
:installation-id inst-id
:private-key !private-key!})
sha (git:ls-remote url branch pushed-at)
src (git:checkout url sha)
project (load (src/project))]
(project:github-event event payload client)))))

; starts the thunk and reflects its status as a Check Run
(defn start-check [thunk name sha repo auth]
(let [check-run (create-check-run
name sha repo auth
:status "in_progress"
:started-at (now 0))]
(log "created check run"
:repo repo
:name name
:sha sha
:run check-run:id)
(start thunk
(fn [ok?]
(update-check-run
check-run:id repo auth
:status "completed"
:conclusion (if ok? "success" "failure")
:completed-at (now 0))
[name ok?]))))

; starts the thunk and reflects its status as a Commit Status
(defn start-status [thunk name sha repo auth]
(create-status sha repo auth {:context name
Expand Down Expand Up @@ -136,25 +52,7 @@
(read :json)
next))

; creates a check run
(defn create-check-run [name sha repo auth & kwargs]
(log "creating check" :repo repo :name name :sha sha)
(gh-api "POST" (str "repos/" repo "/check-runs")
(assoc {:name name :head-sha sha} & kwargs)
auth))

; updates a check run with new fields (e.g. status)
(defn update-check-run [run-id repo auth & kwargs]
(log "updating check" :run run-id :payload payload)
(gh-api "PATCH" (str "repos/" repo "/check-runs/" run-id)
(list->scope kwargs)
auth))

; creates or updates a commit status
(defn create-status [sha repo auth body]
(log "creating commit status" :repo repo :sha sha :body body)
(gh-api "POST" (str "repos/" repo "/statuses/" sha) body auth))

; returns the first JSON object encoded in the payload
(defn decode-json [payload]
(next (read (mkfile ./json payload) :json))))
(gh-api "POST" (str "repos/" repo "/statuses/" sha) body auth)))
27 changes: 0 additions & 27 deletions bass/server

This file was deleted.

40 changes: 0 additions & 40 deletions pkg/internal/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ package internal

import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"regexp"
"strings"
"time"

"github.com/vito/bass/pkg/bass"
"github.com/vito/bass/pkg/srv"
"github.com/vito/bass/pkg/zapctx"
)

Expand All @@ -39,25 +34,6 @@ func init() {
Scope.Set("string-split",
bass.Func("string-split", "[delim str]", strings.Split))

Scope.Set("http-listen",
bass.Func("http-listen", "[addr handler]", func(ctx context.Context, addr string, cb bass.Combiner) error {
server := &http.Server{
Addr: addr,
Handler: http.MaxBytesHandler(srv.Mux(&srv.CallHandler{
Cb: cb,
RunCtx: ctx,
}), MaxBytes),
}

go func() {
<-ctx.Done()
// just passing ctx along to immediately interrupt everything
server.Shutdown(ctx)
}()

return server.ListenAndServe()
}))

Scope.Set("time-measure",
bass.Op("time-measure", "[form]", func(ctx context.Context, cont bass.Cont, scope *bass.Scope, form bass.Value) bass.ReadyCont {
before := bass.Clock.Now()
Expand All @@ -68,22 +44,6 @@ func init() {
}))
}))

Scope.Set("hmac-verify-sha256",
bass.Func("hmac-verify-sha256", "[key claim msg]", func(key bass.Secret, claim string, msg []byte) (bool, error) {
claimSum, err := hex.DecodeString(claim)
if err != nil {
return false, err
}

mac := hmac.New(sha256.New, key.Reveal())
_, err = mac.Write(msg)
if err != nil {
return false, err
}

return hmac.Equal(mac.Sum(nil), claimSum), nil
}))

Scope.Set("regexp-case",
bass.Op("regexp-case", "[str & re-fn-pairs]", func(ctx context.Context, cont bass.Cont, scope *bass.Scope, haystackForm bass.Value, pairs ...bass.Value) bass.ReadyCont {
if len(pairs)%2 == 1 {
Expand Down
71 changes: 0 additions & 71 deletions pkg/srv/call.go

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/srv/mux.go

This file was deleted.

40 changes: 0 additions & 40 deletions pkg/srv/request.go

This file was deleted.

Loading

0 comments on commit 2a3cd1c

Please sign in to comment.