Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Remove "No newline" from previews #222

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/pull-request/get-new-tweets.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ async function getNewTweets({ octokit, toolkit, payload }) {
const newTweets = parseDiff(data)
.filter((file) => file.new && /^tweets\/.*\.tweet$/.test(file.to))
.map((file) =>
file.chunks[0].changes.map((line) => line.content.substr(1)).join("\n")
file.chunks[0].changes
.filter((line) => line.content.startsWith("+")) // ignore No newline at EOF
.map((line) => line.content.substr(1))
.join("\n")
);

toolkit.info(`New tweets found: ${newTweets.length}`);
Expand Down
22 changes: 22 additions & 0 deletions test/pull-request-has-tweet-no-newline/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"action": "opened",
"pull_request": {
"number": 123,
"base": {
"ref": "main"
},
"head": {
"sha": "0000000000000000000000000000000000000002",
"repo": {
"fork": false
}
}
},
"repository": {
"default_branch": "main",
"name": "action",
"owner": {
"login": "twitter-together"
}
}
}
80 changes: 80 additions & 0 deletions test/pull-request-has-tweet-no-newline/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* This test checks the happy path of pull request adding a new *.tweet file
*/

const nock = require("nock");
const tap = require("tap");

// SETUP
process.env.GITHUB_EVENT_NAME = "pull_request";
process.env.GITHUB_TOKEN = "secret123";
process.env.GITHUB_EVENT_PATH = require.resolve("./event.json");

// set other env variables so action-toolkit is happy
process.env.GITHUB_REF = "";
process.env.GITHUB_WORKSPACE = "";
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/pulls/123/files")
.reply(200, [
{
status: "added",
filename: "tweets/hello-world.tweet",
},
]);

// get pull request diff
nock("https://api.github.com", {
reqheaders: {
accept: "application/vnd.github.diff",
authorization: "token secret123",
},
})
.get("/repos/twitter-together/action/pulls/123")
.reply(
200,
`diff --git a/tweets/hello-world.tweet b/tweets/hello-world.tweet
new file mode 100644
index 0000000..0123456
--- /dev/null
+++ b/tweets/hello-world.tweet
@@ -0,0 +1 @@
+Hello, world!
\\ No newline at end of file
`
);

// create check run
nock("https://api.github.com")
// get changed files
.post("/repos/twitter-together/action/check-runs", (body) => {
tap.equal(body.name, "preview");
tap.equal(body.head_sha, "0000000000000000000000000000000000000002");
tap.equal(body.status, "completed");
tap.equal(body.conclusion, "success");
tap.same(body.output, {
title: "1 tweet(s)",
summary: "### ✅ Valid\n\n> Hello, world!",
});

return true;
})
.reply(201);

process.on("exit", (code) => {
tap.equal(code, 0);
tap.same(nock.pendingMocks(), []);
});

require("../../lib");