Serenity Operating System
1const fs = require("fs");
2const Twit = require("twit");
3const { CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET } = process.env;
4const tweetLength = 280;
5// Twitter always considers t.co links to be 23 chars, see https://help.twitter.com/en/using-twitter/how-to-tweet-a-link
6const twitterLinkLength = 23;
7
8const T = new Twit({
9 consumer_key: CONSUMER_KEY,
10 consumer_secret: CONSUMER_SECRET,
11 access_token: ACCESS_TOKEN,
12 access_token_secret: ACCESS_TOKEN_SECRET,
13});
14
15(async () => {
16 const githubEvent = JSON.parse(fs.readFileSync(0).toString());
17 const tweets = [];
18 for (const commit of githubEvent["commits"]) {
19 const authorLine = `Author: ${commit["author"]["name"]}`;
20 const maxMessageLength = tweetLength - authorLine.length - twitterLinkLength - 2; // -2 for newlines
21 const commitMessage =
22 commit["message"].length > maxMessageLength
23 ? commit["message"].substring(0, maxMessageLength - 2) + "…" // Ellipsis counts as 2 characters
24 : commit["message"];
25
26 tweets.push(`${commitMessage}\n${authorLine}\n${commit["url"]}`);
27 }
28 for (const tweet of tweets) {
29 try {
30 await T.post("statuses/update", { status: tweet });
31 } catch (e) {
32 console.error("Failed to post a tweet!", e.message);
33 }
34 }
35})();