Skip to content

Commit

Permalink
fix: Allow trailing whitespace on front matter + thread delimiters (#210
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MattIPv4 authored Sep 26, 2022
1 parent c68e023 commit 359fe68
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/common/parse-tweet-file-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions test/push-main-has-tweet-with-trailing-whitespace/event.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
97 changes: 97 additions & 0 deletions test/push-main-has-tweet-with-trailing-whitespace/test.js
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
reply: https://twitter.com/gr2m/status/0000000000000000001
---

Hello, world!

---

Second Tweet!

0 comments on commit 359fe68

Please sign in to comment.