tweet-commits.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. const fs = require("fs");
  2. const Twit = require("twit");
  3. const { CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET } = process.env;
  4. const 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
  6. const twitterLinkLength = 23;
  7. const T = new Twit({
  8. consumer_key: CONSUMER_KEY,
  9. consumer_secret: CONSUMER_SECRET,
  10. access_token: ACCESS_TOKEN,
  11. access_token_secret: ACCESS_TOKEN_SECRET,
  12. });
  13. (async () => {
  14. const githubEvent = JSON.parse(fs.readFileSync(0).toString());
  15. const tweets = [];
  16. for (const commit of githubEvent["commits"]) {
  17. const authorLine = `Author: ${commit["author"]["name"]}`;
  18. const maxMessageLength = tweetLength - authorLine.length - twitterLinkLength - 2; // -2 for newlines
  19. const commitMessage =
  20. commit["message"].length > maxMessageLength
  21. ? commit["message"].substring(0, maxMessageLength - 2) + "…" // Ellipsis counts as 2 characters
  22. : commit["message"];
  23. tweets.push(`${commitMessage}\n${authorLine}\n${commit["url"]}`);
  24. }
  25. for (const tweet of tweets) {
  26. try {
  27. await T.post("statuses/update", { status: tweet });
  28. } catch (e) {
  29. console.error("Failed to post a tweet!", e.message);
  30. }
  31. }
  32. })();