Skip to content

Commit 22c9590

Browse files
feat: readme update (shubhaamgupta11#3)
* feat: refactored new issue logic * feat: refactored new pr logic * slack and discord added * feat: updated entry point * updated readme
1 parent c7167a3 commit 22c9590

File tree

6 files changed

+137
-38
lines changed

6 files changed

+137
-38
lines changed

.github/workflows/example.yml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ on:
66
workflow_dispatch:
77

88
jobs:
9-
monitor:
9+
run-notifier:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- name: Checkout code
13-
uses: actions/checkout@v3
14-
- name: Run GitHub Monitor Action
15-
uses: ./
12+
- name: Set up Node.js
13+
uses: actions/setup-node@v3
1614
with:
17-
task: "monitor-issues" # or monitor-prs
18-
git_token: ${{ secrets.GITHUB_TOKEN }}
19-
slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
20-
slack_channel: ${{ secrets.SLACK_CHANNEL }}
15+
node-version: '16'
16+
- name: Run action
17+
uses: ./ # path to your action
18+
with:
19+
task: "monitor-issues"
20+
git_token: "${{ secrets.GITHUB_TOKEN }}"
21+
notifier: "discord" # or "slack"
22+
fetch_data_interval: 24 # You can set this in hours
23+
discord_webhook_url: "your_discord_webhook_url"
2124
repo_owner: "facebook"
2225
repo_name: "react-native"
23-
slack_id_type: "user"
24-
slack_id: "U01N6V7HKUJ"
26+

README.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,85 @@
1-
# GitHub Monitor Action
1+
# Monitor Issues and PRs GitHub Action
22

3-
Monitor GitHub issues and PRs, and post updates to Slack.
3+
## Overview
4+
A GitHub Action to monitor new issues and pull requests in a specified repository and send notifications to Slack or Discord. This action allows you to track activity in your repositories and get alerted when new issues or PRs are created.
5+
6+
## Features
7+
8+
- Monitor issues and PRs in a GitHub repository.
9+
- Support for multiple notification methods.
10+
- Easy integration with GitHub workflows.
11+
12+
## Inputs
13+
14+
| Input | Description | Required | Default |
15+
| ----- | ----------- | -------- | ------- |
16+
| task | The task to run (monitor-issues or monitor-prs). | Yes | None |
17+
| git_secret | GitHub token for authentication. | Yes | None |
18+
| repo_owner | The owner of the GitHub repository (`facebook`). | Yes | None |
19+
| repo_name | The name of the GitHub repository (`react-native`). | Yes | None |
20+
| fetch_data_interval | The time interval to fetch data for (e.g., 1 hour, 24 hours). This should align with the cron schedule. | Yes | None |
21+
| notifier | Notification method (**slack** or **discord**). | Yes | None |
22+
| slack_bot_token | Slack bot token to send notifications (required if notifier=`slack`). | No | None |
23+
| slack_channel | The Slack channel id to send notifications to (required if notifier=`slack`). | No | None |
24+
| slack_id_type | Type of Slack ID (user or group, required if notifier=`slack`). This is needed to ping someone directly. | No | None |
25+
| slack_id | user id or group id as per `slack_id_type` (required if notifier=`slack`). | No | None |
26+
| discord_webhook_url | Discord webhook URL to send notifications (required if notifier=`discord`). | No | None |
27+
28+
## Example Usage
29+
30+
```yml
31+
name: Monitor GitHub Repo
32+
33+
on:
34+
schedule:
35+
- cron: "0 * * * *" # Runs every hour
36+
workflow_dispatch:
37+
38+
jobs:
39+
run-notifier:
40+
runs-on: ubuntu-latest
41+
steps:
42+
- name: Set up Node.js
43+
uses: actions/setup-node@v3
44+
with:
45+
node-version: '16'
46+
- name: Run action
47+
uses: ./ # Path to your action
48+
with:
49+
task: "monitor-issues"
50+
git_secret: "${{ secrets.GITHUB_TOKEN }}"
51+
notifier: "slack" # Or "discord"
52+
fetch_data_interval: 24 # Hours (must align with your cron schedule)
53+
slack_bot_token: "${{ secrets.SLACK_BOT_TOKEN }}"
54+
slack_channel: "#your-channel"
55+
repo_owner: "facebook"
56+
repo_name: "react-native"
57+
```
58+
59+
## How It Works
60+
- The action listens for new issues or PRs in the specified GitHub repository.
61+
- Depending on the `task` input, it will either monitor new issues (`monitor-issues`) or pull requests (`monitor-prs`).
62+
- Once a new issue or PR is detected, it sends a notification via Slack or Discord based on the `notifier` input.
63+
- The action checks for issues or PRs within the time period defined by `fetch_data_interval`, which should align with the cron schedule. For example, if the cron schedule is set to run every hour `(0 * * * *)`, set `fetch_data_interval` to 1 hour.
64+
65+
## Setting up Slack
66+
If you choose Slack as the notification method, you will need to create a Slack app and retrieve the **Slack Bot Token** and **Channel**:
67+
68+
- Create a Slack app.
69+
- Generate a **Slack Bot Token**.
70+
- Get the **Channel I**D.
71+
- Get the **Slack User ID** or **Group ID**.
72+
- Add these tokens/IDs to the GitHub secrets (`SLACK_BOT_TOKEN`, `SLACK_CHANNEL`, `SLACK_ID_TYPE`, and `SLACK_ID`).
73+
74+
`slack_id_type` Explanation
75+
- `user`: Use this if you want to send a notification to an individual user.
76+
- `group`: Use this if you want to send a notification to a group of users (e.g., a Slack channel or a group).
77+
78+
## Setting up Discord
79+
If you choose Discord as the notification method, you will need a Discord Webhook URL:
80+
81+
- Create a **Discord Webhook**.
82+
- Copy the Webhook URL and add it to the GitHub secrets (`DISCORD_WEBHOOK_URL`).
483

584
## Inputs
685

action/action.yml

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,42 @@ branding:
66
color: blue
77

88
inputs:
9+
task:
10+
description: Task to run (monitor-issues or monitor-prs).
11+
required: true
912
git_secret:
1013
description: GitHub token for authentication.
1114
required: true
12-
slack_bot_token:
13-
description: Slack bot token to send notifications.
14-
required: true
15-
slack_channel:
16-
description: The Slack channel to send notifications to.
17-
required: true
1815
repo_owner:
1916
description: Owner of the repository.
2017
required: true
2118
repo_name:
2219
description: Name of the repository.
2320
required: true
24-
slack_id_type:
25-
description: Type of Slack ID (user or channel).
21+
fetch_data_interval:
22+
description: How far back to fetch data (e.g., 1 hour, 24 hours).
2623
required: true
27-
slack_id:
28-
description: Slack ID of the recipient (user or channel).
24+
notifier:
25+
description: Notification method (slack or discord).
2926
required: true
27+
# Slack-specific inputs
28+
slack_bot_token:
29+
description: Slack bot token to send notifications.
30+
required: false
31+
slack_channel:
32+
description: The Slack channel to send notifications to.
33+
required: false
34+
slack_id_type:
35+
description: Type of Slack ID (user or group).
36+
required: false
37+
slack_id:
38+
description: Slack ID of the recipient (user or group).
39+
required: false
40+
# Discord-specific inputs
41+
discord_webhook_url:
42+
description: Discord webhook URL to send notifications.
43+
required: false
44+
3045

3146
runs:
3247
using: node16

action/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const core = require("@actions/core");
66
async function run() {
77
try {
88
const task = core.getInput("task");
9-
const gitToken = core.getInput("git_token");
9+
const gitToken = core.getInput("git_secret");
1010
const notifier = core.getInput("notifier"); // 'slack' or 'discord'
11+
const alertTime = core.getInput("fetch_data_interval"); // in hours
1112

1213
const owner = core.getInput("repo_owner");
1314
const repo = core.getInput("repo_name");
@@ -30,6 +31,7 @@ async function run() {
3031
notifier,
3132
slackConfig: { slackToken, slackChannel, slackIDType, slackID },
3233
discordConfig: { discordWebhookUrl },
34+
alertTime,
3335
});
3436
break;
3537

@@ -41,6 +43,7 @@ async function run() {
4143
notifier,
4244
slackConfig: { slackToken, slackChannel, slackIDType, slackID },
4345
discordConfig: { discordWebhookUrl },
46+
alertTime,
4447
});
4548
break;
4649

@@ -52,4 +55,4 @@ async function run() {
5255
}
5356
}
5457

55-
run();
58+
run();

action/monitor-new-issue.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ const sendDiscordNotification = require("./integrations/discord");
88
* @param {string} gitToken - GitHub API token for authentication.
99
* @param {string} owner - Repository owner.
1010
* @param {string} repo - Repository name.
11-
* @param {number} hoursAgo - Timeframe in hours to fetch issues created since then.
11+
* @param {number} alertTime - Timeframe in hours to fetch issues created since then.
1212
* @returns {Promise<Array>} - List of new issues created within the specified timeframe.
1313
*/
14-
const fetchNewIssues = async (gitToken, owner, repo, hoursAgo) => {
14+
const fetchNewIssues = async (gitToken, owner, repo, alertTime) => {
1515
const apiUrl = `https://api.github.com/repos/${owner}/${repo}/issues`;
16+
1617
const sinceDate = new Date(
17-
new Date().getTime() - hoursAgo * 60 * 60 * 1000
18+
new Date().getTime() - alertTime * 60 * 60 * 1000
1819
).toISOString();
1920

2021
let newIssues = [];
@@ -71,10 +72,9 @@ async function monitorIssues({
7172
notifier,
7273
slackConfig,
7374
discordConfig,
75+
alertTime,
7476
}) {
75-
const hoursAgo = 24;
76-
77-
const issues = await fetchNewIssues(gitToken, owner, repo, hoursAgo);
77+
const issues = await fetchNewIssues(gitToken, owner, repo, alertTime);
7878

7979
if (notifier === "slack") {
8080
const {

action/monitor-new-pr.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ const sendSlackNotification = require("./integrations/slack");
33
const sendDiscordNotification = require("./integrations/discord");
44

55
/**
6-
* Fetch new pull requests created within the last `daysAgo` days.
6+
* Fetch new pull requests created within the last `alertTime` days.
77
*
88
* @param {string} gitToken - GitHub API token for authentication.
99
* @param {string} owner - Repository owner.
1010
* @param {string} repo - Repository name.
11-
* @param {number} daysAgo - Timeframe in days to fetch PRs created since then. Default: 1 day.
11+
* @param {number} alertTime - Timeframe in days to fetch PRs created since then. Default: 1 day.
1212
* @returns {Promise<Array>} - List of new pull requests created within the specified timeframe.
1313
*/
14-
const fetchNewPRs = async (gitToken, owner, repo, daysAgo = 1) => {
14+
const fetchNewPRs = async (gitToken, owner, repo, alertTime) => {
1515
const apiUrl = `https://api.github.com/repos/${owner}/${repo}/pulls`;
1616
const cutoffDate = new Date(
17-
Date.now() - daysAgo * 24 * 60 * 60 * 1000
17+
Date.now() - alertTime * 60 * 60 * 1000
1818
).toISOString();
1919

2020
let newPRs = [];
2121
let page = 1;
2222

2323
console.log(
24-
`Fetching new PRs created in the last ${daysAgo} days from ${apiUrl}`
24+
`Fetching new PRs created in the last ${alertTime} days from ${apiUrl}`
2525
);
2626

2727
try {
@@ -76,11 +76,11 @@ async function monitorPRs({
7676
notifier,
7777
slackConfig,
7878
discordConfig,
79-
daysAgo = 1,
79+
alertTime,
8080
}) {
8181
console.log("Starting PR monitor...");
8282

83-
const prs = await fetchNewPRs(gitToken, owner, repo, daysAgo);
83+
const prs = await fetchNewPRs(gitToken, owner, repo, alertTime);
8484
console.log(
8585
`Found ${prs.length} new PRs:`,
8686
prs.map((pr) => pr.title)

0 commit comments

Comments
 (0)