laedit Jérémie Bertrand

Webhook creation on SourceHut

on sourcehut, webhook

I am now using SourceHut to host the sources of some of my projects and wanted to deploy a static HTML page to my web host Ikoula.
On my shared server, Plesk is used to configure the websites and has a git option which allows to git pull from a source to the website folder for each git push, if the source emits a web hook.
Sourcehut doesn't have a GUI to add de git web hook so you have to use their API. There is one dedicated to git but it is being replaced by the GraphQL API one.
The git graphql api have their own playground but the documentation is global.

To this day the api is still hunder development, it has the special version 0.0.0 which indicates that an API which is still undergoing its initial design work, and provides no stability guarantees whatsoever.

So now for the web hook creation, there is a CLI tool for sr.ht but I used curl to consume the graphql apis.
First it is necessary to create a personal access token on https://meta.sr.ht/oauth2 with at least read/write access to OBJECT and git.sr.ht/REPOSITORIES and read-only access to git.sr.ht/PROFILE.
The token is valid for a year but can be revoked at any time.

Then to simplify the other actions, set the token in a variable:

oauth_token=<token>

And check that all is working by querying the version of the git graphql api:

curl \
  --oauth2-bearer "$oauth_token" \
  -H 'Content-Type: application/json' \
  -d '{"query": "{ version { major, minor, patch } }"}' \
  https://git.sr.ht/query

If all is good, get the id of the git repository for which you want to add a web hook:

curl \
  --oauth2-bearer "$oauth_token" \
  -H 'Content-Type: application/json' \
  -d '{"query": "{ me { repositories { results { id, name } } } }"}' \
  https://git.sr.ht/query

And create a web hook:

curl \
  --oauth2-bearer "$oauth_token" \
  -H 'Content-Type: application/json' \
  -d '{"query": "mutation { createGitWebhook(config: { repositoryID: <repository_id> url: \"<webhook url>\" events:[GIT_POST_RECEIVE] query: \"query { webhook { uuid event date } }\" }) { id } }"}' \
  https://git.sr.ht/query

The query creates it with a post receive event, so the web hook will be called after all git process on the server have been handled but you can also use a pre receive event.
The web hook has it's own graphql which defines the data send to the web hook. It allows to send only the necessary data.
And in response you will have the id of the newly created web hook.

You can check if it has been added and get a sample payload if we want to test the web hook:

curl \
  --oauth2-bearer "$oauth_token" \
  -H 'Content-Type: application/json' \
  -d '{"query": "{ gitWebhooks(repositoryID: <repository_id>) { cursor results { id, events, url, sample(event: GIT_POST_RECEIVE) } } }"}' \
  https://git.sr.ht/query

And after some time and some git push you can check that it has been activated:

curl \
  --oauth2-bearer "$oauth_token" \
  -H 'Content-Type: application/json' \
  -d '{"query": "{ gitWebhooks(repositoryID: <repository_id>) { cursor results { id, events, url, deliveries() { results { uuid, date, responseStatus } } } } }"}' \
  https://git.sr.ht/query

Sources:

No comments (for now), but you can reach me on mastodon.