From 359fe68aadcddb38d581f63372c664aab6f870ee Mon Sep 17 00:00:00 2001 From: Matt Cowley Date: Mon, 26 Sep 2022 22:39:35 +0100 Subject: [PATCH] fix: Allow trailing whitespace on front matter + thread delimiters (#210) --- .editorconfig | 3 + dist/index.js | 6 +- lib/common/parse-tweet-file-content.js | 4 +- .../event.json | 15 +++ .../test.js | 97 +++++++++++++++++++ .../tweets/hello-world.tweet | 9 ++ 6 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 test/push-main-has-tweet-with-trailing-whitespace/event.json create mode 100644 test/push-main-has-tweet-with-trailing-whitespace/test.js create mode 100644 test/push-main-has-tweet-with-trailing-whitespace/tweets/hello-world.tweet diff --git a/.editorconfig b/.editorconfig index 1c3d9065..eebd4ada 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,3 +4,6 @@ indent_style = space end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true + +[test/push-main-has-tweet-with-trailing-whitespace/tweets/hello-world.tweet] +trim_trailing_whitespace = false diff --git a/dist/index.js b/dist/index.js index 4df1712e..d13ffb93 100644 --- a/dist/index.js +++ b/dist/index.js @@ -42495,7 +42495,7 @@ Object.defineProperty(exports, '__esModule', { value: true }); /* 731 */ /***/ (function(module) { -module.exports = {"name":"twitter-together","version":"2.1.0","description":"A GitHub action to tweet together using pull requests","main":"lib/index.js","scripts":{"build":"ncc build lib/index.js -o dist","coverage":"nyc report --reporter=html && open coverage/index.html","lint":"prettier --check '{lib,test}/**/*.js' 'docs/*.md' 'tweets/README.md' README.md package.json","lint:fix":"prettier --write '{lib,test}/**/*.js' 'docs/*.md' 'tweets/README.md' README.md package.json","test":"tap --branches=50 --functions=50 --lines=50 --statements=50 test/*/test.js","posttest":"npm run -s lint"},"keywords":[],"author":"","license":"MIT","dependencies":{"@actions/core":"1.9.1","@octokit/action":"^4.0.8","js-yaml":"^4.1.0","mime-types":"^2.1.35","parse-diff":"^0.9.0","twitter":"^1.7.1","twitter-text":"^3.1.0"},"devDependencies":{"@semantic-release/git":"^10.0.1","@zeit/ncc":"^0.22.0","nock":"^13.2.9","prettier":"^2.7.1","semantic-release":"^19.0.5","tap":"^16.3.0"},"repository":"github:twitter-together/action","release":{"branches":["+([0-9]).x","main","next",{"name":"beta","prerelease":true},{"name":"debug","prerelease":true}],"plugins":["@semantic-release/commit-analyzer","@semantic-release/release-notes-generator","@semantic-release/github",["@semantic-release/npm",{"npmPublish":false}],["@semantic-release/git",{"assets":["package.json","dist/index.js"],"message":"build(release): compiled action for ${nextRelease.version}\n\n[skip ci]"}]]}}; +module.exports = {"name":"twitter-together","version":"2.1.1","description":"A GitHub action to tweet together using pull requests","main":"lib/index.js","scripts":{"build":"ncc build lib/index.js -o dist","coverage":"nyc report --reporter=html && open coverage/index.html","lint":"prettier --check '{lib,test}/**/*.js' 'docs/*.md' 'tweets/README.md' README.md package.json","lint:fix":"prettier --write '{lib,test}/**/*.js' 'docs/*.md' 'tweets/README.md' README.md package.json","test":"tap --branches=70 --functions=100 --lines=80 --statements=80 test/*/test.js","posttest":"npm run -s lint"},"keywords":[],"author":"","license":"MIT","dependencies":{"@actions/core":"1.9.1","@octokit/action":"^4.0.8","js-yaml":"^4.1.0","mime-types":"^2.1.35","parse-diff":"^0.9.0","twitter":"^1.7.1","twitter-text":"^3.1.0"},"devDependencies":{"@semantic-release/git":"^10.0.1","@zeit/ncc":"^0.22.0","nock":"^13.2.9","prettier":"^2.7.1","semantic-release":"^19.0.5","tap":"^16.3.0"},"repository":"github:twitter-together/action","release":{"branches":["+([0-9]).x","main","next",{"name":"beta","prerelease":true},{"name":"debug","prerelease":true}],"plugins":["@semantic-release/commit-analyzer","@semantic-release/release-notes-generator","@semantic-release/github",["@semantic-release/npm",{"npmPublish":false}],["@semantic-release/git",{"assets":["package.json","dist/index.js"],"message":"build(release): compiled action for ${nextRelease.version}\n\n[skip ci]"}]]}}; /***/ }), /* 732 */, @@ -43069,7 +43069,7 @@ const { load } = __webpack_require__(186); const OPTION_REGEX = /^\(\s?\)\s+/; const FRONT_MATTER_REGEX = new RegExp( - `^---${EOL}([\\s\\S]*?)${EOL}---(?:$|(?:${EOL})+)` + `^---[ \t]*${EOL}([\\s\\S]*?)${EOL}---[ \t]*(?:$|(?:${EOL})+)` ); function parseTweetFileContent(text, dir, isThread = false) { @@ -43098,7 +43098,7 @@ function parseTweetFileContent(text, dir, isThread = false) { // Handle threading if (options.threadDelimiter) { const threadIdx = text.match( - new RegExp(`(?:${EOL})+${options.threadDelimiter}(?:${EOL})+`) + new RegExp(`(?:${EOL})+${options.threadDelimiter}[ \t]*(?:${EOL})+`) ); if (threadIdx) { const threadText = text.slice(threadIdx.index + threadIdx[0].length); diff --git a/lib/common/parse-tweet-file-content.js b/lib/common/parse-tweet-file-content.js index 18f5e1da..e62b58ad 100644 --- a/lib/common/parse-tweet-file-content.js +++ b/lib/common/parse-tweet-file-content.js @@ -9,7 +9,7 @@ const { load } = require("js-yaml"); const OPTION_REGEX = /^\(\s?\)\s+/; const FRONT_MATTER_REGEX = new RegExp( - `^---${EOL}([\\s\\S]*?)${EOL}---(?:$|(?:${EOL})+)` + `^---[ \t]*${EOL}([\\s\\S]*?)${EOL}---[ \t]*(?:$|(?:${EOL})+)` ); function parseTweetFileContent(text, dir, isThread = false) { @@ -38,7 +38,7 @@ function parseTweetFileContent(text, dir, isThread = false) { // Handle threading if (options.threadDelimiter) { const threadIdx = text.match( - new RegExp(`(?:${EOL})+${options.threadDelimiter}(?:${EOL})+`) + new RegExp(`(?:${EOL})+${options.threadDelimiter}[ \t]*(?:${EOL})+`) ); if (threadIdx) { const threadText = text.slice(threadIdx.index + threadIdx[0].length); diff --git a/test/push-main-has-tweet-with-trailing-whitespace/event.json b/test/push-main-has-tweet-with-trailing-whitespace/event.json new file mode 100644 index 00000000..d670ea52 --- /dev/null +++ b/test/push-main-has-tweet-with-trailing-whitespace/event.json @@ -0,0 +1,15 @@ +{ + "ref": "refs/heads/main", + "before": "0000000000000000000000000000000000000001", + "after": "0000000000000000000000000000000000000002", + "head_commit": { + "id": "0000000000000000000000000000000000000002" + }, + "repository": { + "owner": { + "login": "twitter-together" + }, + "name": "action", + "default_branch": "main" + } +} diff --git a/test/push-main-has-tweet-with-trailing-whitespace/test.js b/test/push-main-has-tweet-with-trailing-whitespace/test.js new file mode 100644 index 00000000..c2dc5d60 --- /dev/null +++ b/test/push-main-has-tweet-with-trailing-whitespace/test.js @@ -0,0 +1,97 @@ +/** + * This test checks the happy path of a commit to the main branch + * which includes a new *.tweet file that has trailing whitespace on front matter and thread delimiters + */ + +const path = require("path"); +const fs = require("fs"); + +const nock = require("nock"); +const tap = require("tap"); + +// SETUP +process.env.GITHUB_EVENT_NAME = "push"; +process.env.GITHUB_TOKEN = "secret123"; +process.env.GITHUB_EVENT_PATH = require.resolve("./event.json"); +process.env.GITHUB_REF = "refs/heads/main"; +process.env.GITHUB_WORKSPACE = path.dirname(process.env.GITHUB_EVENT_PATH); + +// set other env variables so action-toolkit is happy +process.env.GITHUB_WORKFLOW = ""; +process.env.GITHUB_ACTION = "twitter-together"; +process.env.GITHUB_ACTOR = ""; +process.env.GITHUB_REPOSITORY = ""; +process.env.GITHUB_SHA = ""; + +// MOCK +nock("https://api.github.com", { + reqheaders: { + authorization: "token secret123", + }, +}) + // get changed files + .get( + "/repos/twitter-together/action/compare/0000000000000000000000000000000000000001...0000000000000000000000000000000000000002" + ) + .reply(200, { + files: [ + { + status: "added", + filename: "tweets/hello-world.tweet", + }, + ], + }) + + // post comment + .post( + "/repos/twitter-together/action/commits/0000000000000000000000000000000000000002/comments", + (body) => { + tap.equal( + body.body, + "Tweeted:\n\n- https://twitter.com/gr2m/status/0000000000000000002\n- https://twitter.com/gr2m/status/0000000000000000003" + ); + return true; + } + ) + .reply(201); + +nock("https://api.twitter.com") + .post("/1.1/statuses/update.json", (body) => { + tap.equal(body.status, "Hello, world!"); + tap.equal(body.in_reply_to_status_id, "0000000000000000001"); + tap.equal(body.auto_populate_reply_metadata, "true"); + return true; + }) + .reply(201, { + id_str: "0000000000000000002", + user: { + screen_name: "gr2m", + }, + }) + + .post("/1.1/statuses/update.json", (body) => { + tap.equal(body.status, "Second Tweet!"); + tap.equal(body.in_reply_to_status_id, "0000000000000000002"); + tap.equal(body.auto_populate_reply_metadata, "true"); + return true; + }) + .reply(201, { + id_str: "0000000000000000003", + user: { + screen_name: "gr2m", + }, + }); + +// Confirm there is whitespace +const contents = fs.readFileSync( + path.join(__dirname, "tweets/hello-world.tweet"), + "utf-8" +); +tap.match(contents, /^--- \n[\s\S]+\n--- \n[\s\S]+\n--- \n[\s\S]+$/); + +process.on("exit", (code) => { + tap.equal(code, 0); + tap.same(nock.pendingMocks(), []); +}); + +require("../../lib"); diff --git a/test/push-main-has-tweet-with-trailing-whitespace/tweets/hello-world.tweet b/test/push-main-has-tweet-with-trailing-whitespace/tweets/hello-world.tweet new file mode 100644 index 00000000..8f512775 --- /dev/null +++ b/test/push-main-has-tweet-with-trailing-whitespace/tweets/hello-world.tweet @@ -0,0 +1,9 @@ +--- +reply: https://twitter.com/gr2m/status/0000000000000000001 +--- + +Hello, world! + +--- + +Second Tweet!