From 58795a57913093cdfc38303858a7124aed84ce5c Mon Sep 17 00:00:00 2001 From: DonnieBLT <128622481+DonnieBLT@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:13:09 -0400 Subject: [PATCH] Add notification feature Related to #42 --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/OWASP-BLT/BLT-Action/issues/42?shareId=XXXX-XXXX-XXXX-XXXX). --- .github/workflows/notify.yml | 35 +++++++++++++++++++++++++++++ README.md | 43 ++++++++++++++++++++++++++++++++++++ USAGE.md | 40 +++++++++++++++++++++++++++++++++ src/mock-test.js | 30 ++++++++++++++++++++++++- 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/notify.yml diff --git a/.github/workflows/notify.yml b/.github/workflows/notify.yml new file mode 100644 index 0000000..005fc75 --- /dev/null +++ b/.github/workflows/notify.yml @@ -0,0 +1,35 @@ +name: Notify Team Members + +on: + issues: + types: [opened] + pull_request: + types: [opened] + issue_comment: + types: [created] + +jobs: + notify: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Notify team members + uses: actions/github-script@v4 + with: + script: | + const { context, github } = require('@actions/github'); + const { payload } = context; + const issue = payload.issue || payload.pull_request; + const comment = payload.comment; + const message = issue ? `New ${issue.pull_request ? 'pull request' : 'issue'}: ${issue.title}` : `New comment on ${comment.issue_url}: ${comment.body}`; + const teamMembers = ['team_member1', 'team_member2']; // Replace with actual team member usernames + for (const member of teamMembers) { + github.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + body: `@${member} ${message}` + }); + } diff --git a/README.md b/README.md index 523499b..033ff45 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ - **Assignment Validation**: The action prevents multiple assignments by checking if a user is already assigned to an issue. - **One Issue at a Time**: Limits users to be assigned to only one issue simultaneously. - **Time-Based Unassignment**: Automatically unassigns users from issues if they are not resolved within 5 days, keeping the issue flow active. +- **Automatic Notifications**: Automatically notifies team members of new issues, pull requests, or comments, improving communication and collaboration within the organization. ## Getting Started @@ -47,12 +48,54 @@ ``` +2. **Add the Notification Workflow to Your Repository**: + - Create a new YAML file inside the workflows directory (e.g., `notify.yml`). + - Add the following content to the YAML file: + + ```yml + name: Notify Team Members + + on: + issues: + types: [opened] + pull_request: + types: [opened] + issue_comment: + types: [created] + + jobs: + notify: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Notify team members + uses: actions/github-script@v4 + with: + script: | + const { context, github } = require('@actions/github'); + const { payload } = context; + const issue = payload.issue || payload.pull_request; + const comment = payload.comment; + const message = issue ? `New ${issue.pull_request ? 'pull request' : 'issue'}: ${issue.title}` : `New comment on ${comment.issue_url}: ${comment.body}`; + const teamMembers = ['team_member1', 'team_member2']; // Replace with actual team member usernames + for (const member of teamMembers) { + github.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + body: `@${member} ${message}` + }); + } + ``` ### Usage - To assign yourself to an issue, comment `/assign` on the issue. - To unassign yourself to an issue, comment `/unassign` on the issue. - The action will automatically check for your current assignments and assign you to the issue if you are eligible. +- The notification workflow will automatically notify team members of new issues, pull requests, or comments. ## Contributing diff --git a/USAGE.md b/USAGE.md index 8b761b4..92a1748 100644 --- a/USAGE.md +++ b/USAGE.md @@ -44,4 +44,44 @@ jobs: ``` +## Example usage of the notification feature +Here's an example flow that notifies team members of new issues, pull requests, or comments: + +```yml +name: Notify Team Members + +on: + issues: + types: [opened] + pull_request: + types: [opened] + issue_comment: + types: [created] + +jobs: + notify: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Notify team members + uses: actions/github-script@v4 + with: + script: | + const { context, github } = require('@actions/github'); + const { payload } = context; + const issue = payload.issue || payload.pull_request; + const comment = payload.comment; + const message = issue ? `New ${issue.pull_request ? 'pull request' : 'issue'}: ${issue.title}` : `New comment on ${comment.issue_url}: ${comment.body}`; + const teamMembers = ['team_member1', 'team_member2']; // Replace with actual team member usernames + for (const member of teamMembers) { + github.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + body: `@${member} ${message}` + }); + } +``` diff --git a/src/mock-test.js b/src/mock-test.js index 14b471b..d94cb0c 100644 --- a/src/mock-test.js +++ b/src/mock-test.js @@ -8,6 +8,16 @@ async function assignUserToIssue(owner, repo, issueNumber, username) { return response.data; } +async function notifyTeamMembers(owner, repo, issueNumber, message, teamMembers) { + const url = `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}/comments`; + for (const member of teamMembers) { + const response = await axios.post(url, { body: `@${member} ${message}` }); + if (response.status !== 201) { + throw new Error(`Failed to notify ${member}`); + } + } +} + describe('GitHub API Mock Test', () => { beforeEach(() => { nock.cleanAll(); @@ -28,4 +38,22 @@ describe('GitHub API Mock Test', () => { scope.done(); }); -}); \ No newline at end of file + + it('should notify team members of a new issue', async () => { + const owner = 'testowner'; + const repo = 'testrepo'; + const issueNumber = 1; + const message = 'New issue: Test issue'; + const teamMembers = ['team_member1', 'team_member2']; + + const scope = nock('https://api.github.com') + .post(`/repos/${owner}/${repo}/issues/${issueNumber}/comments`, { body: `@team_member1 ${message}` }) + .reply(201, { status: 'success' }) + .post(`/repos/${owner}/${repo}/issues/${issueNumber}/comments`, { body: `@team_member2 ${message}` }) + .reply(201, { status: 'success' }); + + await notifyTeamMembers(owner, repo, issueNumber, message, teamMembers); + + scope.done(); + }); +});