karfau
A helper to get the raw data of a val, using the very nice implementation from @pomdtr.raw.
Usage: https://www.val.town/v/karfau.test_getRaw
Also look at @karfau.rawUrl to just get the raw url of another val inside a val.
Test runner
to be able to run a number of tests (e.g. on a different val). check the references for seeing how it is used. It is extracted into a val to avoid having all that clutter in the same val as your tests.
Each test is a named function (which can be async), the name is used as the name for the test.
- the input passed as the first argument is passed to each test, great for importing assertion methods, stubs, fixed values, ... everything that you do not mutate during a test
- if a function is async (it returns a promise) there is a timeout of 2 seconds before the test is marked as failed.
- all tests are called in the declared order, but async tests run in parallel afterwards, so don't assume any order
- if a test starts with
skip
it is not executed - if a test fails it throws the output, so it appears in the read box below the val and the evaluation/run is marked red
- if all tests pass it returns the output, so it appears in the grey box and the evaluation/run is marked green.
Note: If you are using the test runner to store the result in that val, as described above, it is considered a "JSON val" and has a run button, but it also means that another of your vals could update the val with just any other (JSON) state. Alternatively you can define a function val that calls the test runner and have a separete val to keep the curretn test results, but it means after updating the tests you need to fest save that val and then reevaluate to val storing the test state.
This is a great template if you want to solve the https://adventofcode.com puzzles in a TDD style:
- dump your puzzle input into the
input
function - copy/paste sample input and output from the page
- once all assertions pass, the solution is calculated
There is one function for each star that can be achieved.
Some helpers for common tasks are defined at the end of code: debug
, toInt
, exctractNumbers
, sum
, ... ?
PS: Did you know advent of code goes as far back as 2015 and you can still solve those puzzles?
This val has been created to avoid certain shortcomings of @vtdocs.verifyGithubWebhookSignature. So it was created as a mix/evolution of two sources:
This code is covered by tests which you can copy to run them, see @karfau.test_SignatureCheck
This val does not contain any val.town specific code (@
-imports, console.email
...), so it should be possible to run in Deno as is, potentially even in modern browsers (that support crypto
and TextEncoder
and modern ES syntax).
Usage
const myGithubWebhook = (req: Request) => {
const {verify} = @karfau.SignatureCheck(); // you have to call it to get the verify function!
const body = await req.text();
const signature = req.headers.get("X-Hub-Signature-256");
const verified = await verify(
{payload:body, signature},
@me.secrets.myGithubWebhookSecret,
// optionally provide fallback secrets (as many as needed)
// @me.secrets.myGithubWebhookSecretFallback
);
if (!verified) {
return new Response(`Not verified`, 401);
}
const payload = JSON.parse(body);
// actually do things in your webhook
};
By default the reason for failing verification is logged to console.error
, but you can pass it a different handler:
const {verify} = @karfau.SignatureCheck((reason) => { throw new Error(reason); });
(be aware that it will silently fail if you don't try catch it in an endpoint and the return code will be 502)
Why
@vtdocs.verifyGithubWebhookSignature has the following issues:
- it relies on the
verify
method of the outdated@octokit/webhooks-methods@3.0.2
which has (at least) two bugs that can make a difference when used in a webhook- it can throws errors instead of just returning
false
, which can be triggered by sending an invalid signature - it can be lured into checking a SHA1 signature if the signature header starts with
sha1=
- it can throws errors instead of just returning
- you need to pass the secret and payload as argument to the val, which makes them appear in the evaluation logs you produce (they are only visible for the author of the val if you run them as an API, but it still feels odd to see the secret in the evaluation logs.)
- parameters are all of type string and the order can be confused
- you can not use fallback secrets for rotating
Finds the top level named val that triggered the execution of this val. (Ignores any untitled vals, since those are sometimes created as an execution context.)
By passing a function as the first argument you receive all references as arguments, and can pick or transform them, e.g.
Possible Limitation: https://discord.com/channels/1020432421243592714/1143386413748994143
A type guard for vals that expect a WebApi Request
as the first parameter.
Since a val can be invoked by run, web, express or even email, the correct type for a web endpoint should be req?: Request | unknown
.
By using this type guard and handling the false
case, all later code can safely access the request:
export const myVal = (req?: Request | unknown) => {
if (!@karfau.isRequest(req)) {
return `This val has to be used with the web endpoint, see ${@neverstew.thisValUrl()}`;
}
if (req.method === 'GET') {
// ...
}
}
Usage
Create valimport {githubEmojiUrl} from "https://esm.town/v/karfau/githubEmoji";
console.log(githubEmojiUrl('+1'))
//"https://github.githubassets.com/images/icons/emoji/unicode/1f44d.png?v8"
or in a browser
<img src="https://karfau-githubEmoji.web.val.run/+1"></img>
Looks like in the preview.
curl https://karfau-githubEmoji.web.val.run/+1
(prints "https://github.githubassets.com/images/icons/emoji/unicode/1f44d.png?v8"
)
If the name you pass (as argument or in the request path) is not in the list, it returns
The list of names can be accessed using githubEmojiNames
or by calling https://karfau-githubemoji.web.val.run/names
A helper for creating the URL to request the raw data of a val, defaulting to use the very nice implementation from @pomdtr.raw. Also look at @karfau.getRaw to get the data of a val inside a val.
A webhook to approve dependency PRs created by bots
The webhook can be configured on the repo or on the org level
- it needs to have the Payload URL set to the "web endpoint" of the val ( ... -> Endpoints -> Copy web endpoint)
- it needs to POST a json payload
- it needs to receive the
workflow_runs
events - it needs to have the
webhookSecret
configured to the same value as in val town secrets (line 7)
(else response is 401: Not verified
)
It will only approve if all of the following conditions are met:
- the event
action
iscompleted
, theworkflow_run.conclusion
has to besuccess
, and the event is related to exactly one pull request
(else response is202: Ignored (event)
) - the PR is authored authored by one of the users listed in
allowedAuthors
(line 5)
(else response is202: Ignored (pr author)
) - the
githubApiToken
configured in line 9 needs to haverepo
access to the related repository
(else response is50x: some error message
) - a branch protection has to be configured that requires at least one review approval and at least one status check
(else response is202: Ignored (branch protection)
) - the PR has auto-merge enabled
(else response is202: Ignored (pr status)
) - the PR has any failing status checks (even if not required)
(else response is202: Ignored (pr checks)
) - the current value for
dryRun
isfalse
(line 3)
(else response is200: Would have been approved (dryRun)
)
If it approves the PR, it leaves a comment pointing to the website of this val.