Skip to content

Commit

Permalink
feat: notify issues weekly
Browse files Browse the repository at this point in the history
  • Loading branch information
nicecui committed Oct 31, 2024
1 parent 682c8d2 commit 7478f4f
Show file tree
Hide file tree
Showing 5 changed files with 10,528 additions and 7,823 deletions.
97 changes: 97 additions & 0 deletions .github/scripts/issue-notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// import { Octokit } from "@octokit/rest";
// import axios from "axios";
(async () => {
const { Octokit } = await import("@octokit/rest")
const { default: axios } = await import('axios');

// Configuration
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const SLACK_WEBHOOK_URL = process.env.SLACK_ISSUE_NOTIFY_WEBHOOK_URL;
const MANAGER_SLACK_ID = process.env.DEVELOPER_MANAGER_SLACK_ID;
const REPO_OWNER = "GreptimeTeam";
const REPO_NAME = "docs";
const GITHUB_TO_SLACK = JSON.parse(process.env.GITHUBID_SLACKID_MAPPING);

const octokit = new Octokit({
auth: GITHUB_TOKEN
});

// Function to fetch open issues from the GitHub repository
async function fetchOpenIssues() {
try {
const issues = await octokit.issues.listForRepo({
owner: REPO_OWNER,
repo: REPO_NAME,
state: "open", // Only fetch open issues
per_page: 100 // Fetch 100 issues at a time
});
return issues.data;
} catch (error) {
console.error("Error fetching issues:", error);
return [];
}
}

// Function to count issues per assignee
function countIssuesByAssignee(issues) {
const issueCountByAssignee = {};

issues.forEach(issue => {
const assignees = issue.assignees;
assignees.forEach(assignee => {
const username = assignee.login;
if (!issueCountByAssignee[username]) {
issueCountByAssignee[username] = 0;
}
issueCountByAssignee[username] += 1;
});
});

const sortedIssueCounts = Object.entries(issueCountByAssignee).sort((a, b) => b[1] - a[1]);

const sortedIssueCountByAssignee = {};
sortedIssueCounts.forEach(([username, count]) => {
sortedIssueCountByAssignee[username] = count;
});

return sortedIssueCountByAssignee;
}

// Function to send a notification to Slack
async function sendSlackNotification(issueCounts) {
const messageLines = [`*🌼 Weekly GitHub Issue Report: 🌼* \n <@${MANAGER_SLACK_ID}>: Hey guys, let's close these issues together! 😉\n`];
for (const [githubUser, count] of Object.entries(issueCounts)) {
const slackUserId = GITHUB_TO_SLACK[githubUser];
const slackMention = slackUserId ? `<@${slackUserId}>` : githubUser;

const issueLink = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues?q=is%3Aissue+is%3Aopen+assignee%3A${githubUser}`;
const formattedLine = `${slackMention}: <${issueLink}|${count} open issues>`;

messageLines.push(formattedLine);
}

const message = messageLines.join("\n");

try {
const response = await axios.post(SLACK_WEBHOOK_URL, {
text: message
});

if (response.status !== 200) {
throw new Error(`Error sending Slack notification: ${response.status}`);
}
console.log("Notification sent to Slack successfully.");
} catch (error) {
console.error("Error sending notification to Slack:", error);
}
}

async function run() {
const issues = await fetchOpenIssues();
const issueCounts = countIssuesByAssignee(issues);
await sendSlackNotification(issueCounts);
}

run();
})()

35 changes: 35 additions & 0 deletions .github/workflows/issue-notifier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Weekly GitHub Issue Notifier

on:
schedule:
- cron: '0 2 * * 1' # Run every Monday at 2:00 AM UTC

jobs:
notify:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 8.6.0
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- run: pnpm install

- name: Run the notifier script
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # GitHub token for API access
SLACK_ISSUE_NOTIFY_WEBHOOK_URL: ${{ vars.SLACK_ISSUE_NOTIFY_WEBHOOK_URL }} # Slack webhook URL
DEVELOPER_MANAGER_SLACK_ID: ${{ vars.DEVELOPER_MANAGER_SLACK_ID }} # Slack ID of the manager
GITHUBID_SLACKID_MAPPING: ${{ vars.GITHUBID_SLACKID_MAPPING }} # JSON string mapping GitHub to Slack usernames
run: node .github/scripts/issue-notification.js

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.docusaurus
node_modules
build
build
.envrc
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@docusaurus/core": "3.4.0",
"@docusaurus/preset-classic": "3.4.0",
"@mdx-js/react": "^3.0.0",
"@octokit/rest": "^21.0.2",
"axios": "^1.7.7",
"clsx": "^2.0.0",
"prism-react-renderer": "^2.3.0",
"react": "^18.0.0",
Expand Down
Loading

0 comments on commit 7478f4f

Please sign in to comment.