Skip to content

Commit

Permalink
fix: fetch valid URL for notification items
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebonsignori committed Aug 1, 2022
1 parent 3a27951 commit 0455d3d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Requires access to a Slack Bot with proper `write` permissions to the Slack chan

Forwarded notifications can be filtered by their [reason](#reason-filtering), [repository](#repository-filtering), [participation](#filter-only-participating), or [read status](#filter-only-unread).

After a notification is forwarded, it can be [marked as read](#mark-as-read)
After a notification is forwarded, it can be [marked as read](#mark-as-read).

## TOC

Expand Down
6 changes: 2 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ function run(getCore, getOctokit, getSlack) {
if (!notifications.length) {
return core.info(`No new notifications since last run after running through all filters: ${displayFilters(inputs)}`);
}
// Get the `html_url` for each notification and add it as `notification_html_url`
notifications = yield Promise.all(notifications.map((notification) => __awaiter(this, void 0, void 0, function* () {
var _a;
let notification_html_url;
Expand All @@ -152,11 +153,8 @@ function run(getCore, getOctokit, getSlack) {
notification_html_url = (_a = notificationSubject === null || notificationSubject === void 0 ? void 0 : notificationSubject.data) === null || _a === void 0 ? void 0 : _a.html_url;
}
catch (error) {
core.warning(`Unable to fetch URL fo notification\mid:${notification.id}\nsubject:${JSON.stringify(notification.subject, null, 2)}`);
core.warning(`Unable to fetch URL for notification\nid:${notification.id}\nsubject:${JSON.stringify(notification.subject, null, 2)}`);
}
console.log("Item:");
console.log(notification.subject);
console.log(notification_html_url);
return Object.assign(Object.assign({}, notification), { notification_html_url });
})));
// Default return is DESC, we want ASC to show oldest first
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ function setMockEnv(envMap: { [key: string]: any }) {
}

function mockGetCore() {
const core = {
info: sinon.stub().callsFake((args) => console.log(args)),
error: sinon.stub().callsFake((args) => console.log(args)),
getInput: CoreLibrary.getInput,
getBooleanInput: CoreLibrary.getBooleanInput,
setFailed: sinon.stub().callsFake((args) => console.log(args)),
};
// const core = {
// info: sinon.stub().callsFake((args) => {}),
// error: sinon.stub().callsFake((args) => {}),
// info: sinon.stub().callsFake((args) => console.log(args)),
// error: sinon.stub().callsFake((args) => console.log(args)),
// getInput: CoreLibrary.getInput,
// getBooleanInput: CoreLibrary.getBooleanInput,
// setFailed: sinon.stub().callsFake((args) => {}),
// setFailed: sinon.stub().callsFake((args) => console.log(args)),
// };
const core = {
info: sinon.stub().callsFake((args) => {}),
error: sinon.stub().callsFake((args) => {}),
getInput: CoreLibrary.getInput,
getBooleanInput: CoreLibrary.getBooleanInput,
setFailed: sinon.stub().callsFake((args) => {}),
};
return () => core;
}

function mockGetOctokit(
notifications?: Endpoints["GET /notifications"]["response"]["data"]
) {
const octokit = {
request: sinon.stub().resolves((arg) => ({ data: { html_url: `<${arg} html_url>` } })),
request: sinon.stub().callsFake(async (arg) => ({ data: { html_url: `<${arg} html_url>` } })),
rest: {
activity: {
listNotificationsForAuthenticatedUser: sinon
Expand Down Expand Up @@ -99,6 +99,7 @@ function createMockNotification(title: string, repository: string, reason: REASO
reason: reason,
subject: {
title: title,
url: `<subject url for - "${title}">`,
},
repository: {
full_name: repository,
Expand Down Expand Up @@ -238,6 +239,9 @@ test("sends slack message of notifications using defaults", async (t) => {
t.false(octokit.rest.activity.listNotificationsForAuthenticatedUser.getCall(0).args[0].participating);
t.is(slack.chat.postMessage.callCount, 1);
t.true(slack.chat.postMessage.getCall(0).args[0].text.includes("<A notification>"));

// Valid `html_url` fetched via octokit.request()
t.true(slack.chat.postMessage.getCall(0).args[0].text.includes(`<<subject url for - "<A notification>"> html_url>`));
});

test("marks sent notifications as read when mark-as-read is true", async (t) => {
Expand Down
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ async function run(
);
}

// Get the `html_url` for each notification and add it as `notification_html_url`
notifications = await Promise.all(notifications.map(
async (
notification: Endpoints["GET /notifications"]["response"]["data"][0]
Expand All @@ -156,14 +157,11 @@ async function run(
notification_html_url = notificationSubject?.data?.html_url;
} catch (error) {
core.warning(
`Unable to fetch URL fo notification\mid:${
`Unable to fetch URL for notification\nid:${
notification.id
}\nsubject:${JSON.stringify(notification.subject, null, 2)}`
);
}
console.log("Item:");
console.log(notification.subject);
console.log(notification_html_url);
return {
...notification,
notification_html_url,
Expand Down

0 comments on commit 0455d3d

Please sign in to comment.