From 143f28b7358583398b7104e1b87734ca0e6c656b Mon Sep 17 00:00:00 2001 From: networkException Date: Fri, 10 Feb 2023 11:24:22 +0100 Subject: [PATCH] CI: Add script to post mastodon toots for commits on master This patch adds the toot-commits script (mirroring tweet-commits), posting new commits on the master branch to https://serenityos.social/@commits. --- .../{twitter.yml => social-media.yml} | 19 ++++++++++- Meta/toot-commits.js | 34 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) rename .github/workflows/{twitter.yml => social-media.yml} (56%) create mode 100644 Meta/toot-commits.js diff --git a/.github/workflows/twitter.yml b/.github/workflows/social-media.yml similarity index 56% rename from .github/workflows/twitter.yml rename to .github/workflows/social-media.yml index 0c14279a05..e0eb778494 100644 --- a/.github/workflows/twitter.yml +++ b/.github/workflows/social-media.yml @@ -1,4 +1,4 @@ -name: Twitter notifications +name: Social media notifications on: [ push ] @@ -22,3 +22,20 @@ jobs: CONSUMER_SECRET: ${{ secrets.CONSUMER_SECRET }} ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }} + + notify_mastodon: + runs-on: ubuntu-22.04 + if: always() && github.repository == 'SerenityOS/serenity' && github.ref == 'refs/heads/master' + + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: '14' + - run: npm i mastodon + - run: | + node ${{ github.workspace }}/Meta/toot-commits.js << 'EOF' + ${{ toJSON(github.event) }} + EOF + env: + ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }} diff --git a/Meta/toot-commits.js b/Meta/toot-commits.js new file mode 100644 index 0000000000..0db3060ce5 --- /dev/null +++ b/Meta/toot-commits.js @@ -0,0 +1,34 @@ +const fs = require("fs"); +const Mastodon = require("mastodon"); +const { ACCESS_TOKEN } = process.env; +const tootLength = 500; +// Mastodon always considers links to be 23 chars, see https://docs.joinmastodon.org/user/posting/#links +const mastodonLinkLength = 23; + +const mastodon = new Mastodon({ + access_token: ACCESS_TOKEN, + timeout_ms: 60 * 1000, + api_url: "https://serenityos.social/api/v1/", +}); + +(async () => { + const githubEvent = JSON.parse(fs.readFileSync(0).toString()); + const toots = []; + for (const commit of githubEvent["commits"]) { + const authorLine = `Author: ${commit["author"]["name"]}`; + const maxMessageLength = tootLength - authorLine.length - mastodonLinkLength - 2; // -2 for newlines + const commitMessage = + commit["message"].length > maxMessageLength + ? commit["message"].substring(0, maxMessageLength - 2) + "…" // Ellipsis counts as 2 characters + : commit["message"]; + + toots.push(`${commitMessage}\n${authorLine}\n${commit["url"]}`); + } + for (const toot of toots) { + try { + await mastodon.post("statuses", { status: toot, visibility: "unlisted" }); + } catch (e) { + console.error("Failed to post a toot!", e.message); + } + } +})();