Skip to content

Commit

Permalink
fetch urls for notification threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebonsignori committed Aug 1, 2022
1 parent f65d5f2 commit d288895
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 35 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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)

## TOC

- [Example Usage](#example-usage)
Expand All @@ -26,6 +28,7 @@ Forwarded notifications can be filtered by their [reason](#reason-filtering), [r
- [`filter-only-participating`](#filter-only-participating)
- [`filter-only-unread`](#filter-only-unread)
- [Optional Configuration](#optional-configuration)
- [`mark-as-read`](#mark-as-read)
- [`sort-oldest-first`](#sort-oldest-first)
- [`timezone`](#timezone)
- [`date-format`](#date-format)
Expand Down Expand Up @@ -66,7 +69,14 @@ To find the channel's [destination](#destination), you can press on the channel'

## Inputs

All configuration for the action is set via inputs.
All configuration for the action is set via inputs, e.g.

```yml
with:
mark-as-read: "true"
```

Where `mark-as-read` is an _input_.

For true/false inputs, a "true" or "false" string is required.

Expand Down
24 changes: 17 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,21 @@ function run(getCore, getOctokit, getSlack) {
if (!notifications.length) {
return core.info(`No new notifications since last run after running through all filters: ${displayFilters(inputs)}`);
}
for (const notification of notifications) {
console.log(notification);
console.log(notification.subject.url);
const item = yield octokit.request(notification.subject.url);
console.log(item);
}
notifications = yield Promise.all(notifications.map((notification) => __awaiter(this, void 0, void 0, function* () {
var _a;
let notification_html_url;
try {
const notificationSubject = yield octokit.request(notification.subject.url);
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)}`);
}
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
if (inputs.sortOldestFirst) {
notifications = notifications.reverse();
Expand Down Expand Up @@ -359,7 +368,8 @@ function renderNotificationMessage(inputs, notification) {
const notificationDate = (0, dayjs_1.default)(notification.updated_at)
.tz(inputs.timezone)
.format(inputs.dateFormat);
return `*${notification.repository.full_name}* _${notificationDate}_\n<${notification.url}|${notification.subject.title}>`;
// @ts-expect-error notification_html_url is added and not typed on notification
return `*${notification.repository.full_name}* _${notificationDate}_\n<${notification.notification_html_url || notification.repository.html_url}|${notification.subject.title}>`;
}
/**
* Renders notifications for Slack and then sends them
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

22 changes: 11 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) => arg),
request: sinon.stub().resolves((arg) => ({ data: { html_url: `<${arg} html_url>` } })),
rest: {
activity: {
listNotificationsForAuthenticatedUser: sinon
Expand Down
57 changes: 43 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as CoreLibrary from "@actions/core";
import CronParser from "cron-parser";
import { Octokit } from "octokit";
import { throttling } from "@octokit/plugin-throttling";
import { Endpoints } from "@octokit/types";
import { WebClient } from "@slack/web-api";

import getInputs from "./lib/get-inputs";
Expand All @@ -19,7 +20,7 @@ if (require.main === module) {
};
const getSlack = (inputs): WebClient => {
return new WebClient(inputs.slackToken);
}
};
run(getCore, getOctokit, getSlack);
}

Expand Down Expand Up @@ -97,9 +98,11 @@ async function run(
return core.info(
`No new notifications fetched since last run with given filters:\n<filter-only-unread>: ${inputs.filterOnlyUnread}\n<filter-only-participating>: ${inputs.filterOnlyParticipating}`
);
}
}
let notifications = notificationsFetch;
core.info(`${notifications.length} notifications fetched before filtering.`);
core.info(
`${notifications.length} notifications fetched before filtering.`
);

// Filter notifications to include/exclude user defined "reason"s
if (inputs.filterIncludeReasons.length) {
Expand Down Expand Up @@ -135,16 +138,38 @@ async function run(

if (!notifications.length) {
return core.info(
`No new notifications since last run after running through all filters: ${displayFilters(inputs)}`
`No new notifications since last run after running through all filters: ${displayFilters(
inputs
)}`
);
}

for (const notification of notifications) {
console.log(notification)
console.log(notification.subject.url);
const item = await octokit.request(notification.subject.url);
console.log(item)
}
notifications = await Promise.all(notifications.map(
async (
notification: Endpoints["GET /notifications"]["response"]["data"][0]
) => {
let notification_html_url;
try {
const notificationSubject = await octokit.request(
notification.subject.url
);
notification_html_url = notificationSubject?.data?.html_url;
} catch (error) {
core.warning(
`Unable to fetch URL fo notification\mid:${
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,
};
}
));

// Default return is DESC, we want ASC to show oldest first
if (inputs.sortOldestFirst) {
Expand All @@ -163,7 +188,7 @@ async function run(
for (const notification of notifications) {
await octokit.rest.activity.markThreadAsRead({
thread_id: notification.id,
})
});
}
}

Expand All @@ -180,9 +205,13 @@ function displayFilters(inputs) {
<filter-only-participating>: ${inputs.filterOnlyParticipating}
<filter-include-reasons>: ${inputs.filterIncludeReasons?.join(", ") || "[]"}
<filter-exclude-reasons>: ${inputs.filterExcludeReasons?.join(", ") || "[]"}
<filter-include-repositories>: ${inputs.filterIncludeRepositories?.join(", ") || "[]"}
<filter-exclude-repositories>: ${inputs.filterExcludeRepositories?.join(", ") || "[]"}
`
<filter-include-repositories>: ${
inputs.filterIncludeRepositories?.join(", ") || "[]"
}
<filter-exclude-repositories>: ${
inputs.filterExcludeRepositories?.join(", ") || "[]"
}
`;
}

// export `run` function for testing
Expand Down
3 changes: 2 additions & 1 deletion src/lib/send-to-slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function renderNotificationMessage(
const notificationDate = dayjs(notification.updated_at)
.tz(inputs.timezone)
.format(inputs.dateFormat);
return `*${notification.repository.full_name}* _${notificationDate}_\n<${notification.url}|${notification.subject.title}>`;
// @ts-expect-error notification_html_url is added and not typed on notification
return `*${notification.repository.full_name}* _${notificationDate}_\n<${notification.notification_html_url || notification.repository.html_url}|${notification.subject.title}>`;
}

/**
Expand Down

0 comments on commit d288895

Please sign in to comment.