Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notification feature #46

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/notify.yml
Original file line number Diff line number Diff line change
@@ -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}`
});
}
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
});
}
```
30 changes: 29 additions & 1 deletion src/mock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -28,4 +38,22 @@ describe('GitHub API Mock Test', () => {

scope.done();
});
});

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();
});
});
Loading