Skip to content

Commit

Permalink
refactor: add classes, object_oriented code
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun-Kolanu committed Jun 13, 2024
1 parent cf073e7 commit d90b27d
Show file tree
Hide file tree
Showing 20 changed files with 314 additions and 252 deletions.
15 changes: 6 additions & 9 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# The ID of your GitHub App
APP_ID=
WEBHOOK_SECRET=development

# Use `trace` to get verbose logging or `info` to show less
LOG_LEVEL=debug

# Go to https://smee.io/new set this to the URL that you are redirected to.
WEBHOOK_PROXY_URL=
WEBHOOK_PROXY_URL=https://smee.io/...
APP_ID=...
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n......-----END RSA PRIVATE KEY-----\n"
WEBHOOK_SECRET=....
GITHUB_CLIENT_ID=Iv23ct....
GITHUB_CLIENT_SECRET=...
64 changes: 30 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# GitHub Issue Assigner Bot

[contributing]: /docs/CONTRIBUTING.md

This GitHub bot helps manage issue assignments in a repository by automatically assigning or unassigning issues based on predefined rules and user commands in comments. The bot is implemented using Probot, a framework for building GitHub Apps.

If you find this project helpful, please consider giving it a star ⭐ on GitHub. It helps others discover the project and shows your appreciation for the work!
Expand All @@ -10,7 +12,7 @@ If you find this project helpful, please consider giving it a star ⭐ on GitHub
- Limit Assignees: Configure maximum number of assignees allowed per issue.
- Limit Assigned Issues: Set a limit on the number of issues a user can be assigned to at a time in the repository.
- Automatic unassignment: Users can request unassignment to an issue by commenting with a specific command.
- Issue opened comments: Bot greets the user when issue is opened
- Issue opened comments: Bot greets the user when issue is opened.
- Customizable Responses: Customize the bot's responses and prompts using a YAML configuration file.

# Usage
Expand Down Expand Up @@ -68,39 +70,33 @@ If you find this project helpful, please consider giving it a star ⭐ on GitHub
4. You can edit the values in the yml to customize the comments from the bot.
5. For example, commenting '@issue-asigner claim' will assign the issue and '@issue-assigner abandon' will remove the assignment.
# Contributing
If you have any suggestions or want to report a bug, open an issue or make a pull request.
## Prerequisites
1. Git installed
2. Node installed
## Setup
1. Clone the repository
```bash
git clone https://github.com/Varun-Kolanu/issue-assigner.git
```

2. Open the repo

```bash
cd issue-assigner
```

3. Install dependencies

```bash
npm i
```

4. Run the app
```bash
npm start
```
See these issues for seeing all features practically: [#3](https://github.com/Varun-Kolanu/issue-assigner/issues/3) and [#4](https://github.com/Varun-Kolanu/issue-assigner/issues/4)
See [Contributing guide][contributing] for contributing to the project.
# Folder structure
```bash
.
├── .github/ ## configuration files
│ └── issue-assigner.yml
├── assets/ ## assets like images, icons, etc
├── src/ ## main source code
│ ├── classes/ # classes for event handlers defined
│ ├── handlers/ # functions to handle webhook events from github
│ │ ├── issue_comment.js
│ │ └── issue_opened.js
│ ├── helpers/ # utility functions to help handlers
│ ├── app.js # main file which exports probot app
│ └── server.js # server for the app
├── test/ ## tests for app
│ ├── fixtures/ # mock data for testing
│ ├── integration/ # integration tests
│ ├── unit/ # unit tests
│ └── utils/ # utility functions for tests
├── CONTRIBUTING.md ## Contributing Guide
└── README.md ## Readme
```

# License

Expand Down
File renamed without changes.
28 changes: 19 additions & 9 deletions CONTRIBUTING.md → docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
## Contributing

[fork]: /fork
[pr]: /compare
[code-of-conduct]: CODE_OF_CONDUCT.md

Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great.

Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms.

## Prerequisites

1. Git installed
2. Node installed

## Issues and PRs

If you have suggestions for how this project could be improved, or want to report a bug, open an issue! We'd love all and any contributions. If you have questions, too, we'd love to hear them.

We'd also love PRs. If you're thinking of a large PR, we advise opening up an issue first to talk about it, though! Look at the links below if you're not sure how to open a PR.

To get an issue assigned, use the issue-assigner bot:

1. Comment `@issue-assigner claim` to get an issue assigned.
2. Comment `@issue-assigner abandon` to get an issue unassigned.

## Submitting a pull request

1. [Fork][fork] and clone the repository.
1. Configure and install the dependencies: `npm install`.
1. Make sure the tests pass on your machine: `npm test`, note: these tests also apply the linter, so there's no need to lint separately.
1. Create a new branch: `git checkout -b my-branch-name`.
1. Make your change, add tests, and make sure the tests still pass.
1. Push to your fork and [submit a pull request][pr].
1. Pat your self on the back and wait for your pull request to be reviewed and merged.
1. Fork and clone the repository.
2. Configure and install the dependencies: `npm install`.
3. Make sure the tests pass on your machine: `npm test`.
4. Create a new branch: `git checkout -b my-branch-name`.
5. Make your change, add tests, and make sure the tests still pass.
6. Push to your fork and submit a pull request.
7. The pull request should contain a phrase like `fixes #1`, `closes #1` etc., to get the PR linked to the issue you re trying to solve.
8. Make the PR a Draft, if not ready for a review and make it Open when your PR is ready.
9. Pat your self on the back and wait for your pull request to be reviewed and merged.

Here are a few things you can do that will increase the likelihood of your pull request being accepted:

Expand Down
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"axios": "^1.7.2",
"body-parser": "^1.20.2",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"probot": "^13.0.1"
},
"devDependencies": {
Expand Down
33 changes: 16 additions & 17 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import bodyParser from "body-parser";
import issue_comment_created from "./handlers/issue_comment_created.js";
import issue_opened from "./handlers/issue_opened.js";
import {
IssueCommentHandler,
IssueOpenerHandler,
} from "./classes/event_handler.js";
import routes from "./routes/routes.js";

export default async (app, { getRouter }) => {
const router = getRouter("/issue-assigner");
router.use(bodyParser.json());
router.get("/", async (req, res) => {
res.status(200).send("Welcome to Issue Assigner");
});
// Define routes of server
routes(getRouter);

router.post("/webhook", (req, res) => {
console.log("Webhook from marketplace: ", req.body);
res.status(200).json({ success: "Webhook received successfully" });
});
/* Webhooks received */

// Issue opened by a user
app.on("issues.opened", issue_opened);
app.on("issues.opened", async (context) => {
const issueOpenerHandlerObj = new IssueOpenerHandler(context);
await issueOpenerHandlerObj.handleEvent();
});

// Comment is created in an issue by a user
app.on(
["issue_comment.created", "issue_comment.edited"],
issue_comment_created
);
app.on(["issue_comment.created", "issue_comment.edited"], async (context) => {
const issueCommentHandlerObj = new IssueCommentHandler(context);
await issueCommentHandlerObj.handleEvent();
});
};
59 changes: 59 additions & 0 deletions src/classes/event_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import issueCommentHandler from "../handlers/issue_comment.js";
import issueOpenerHandler from "../handlers/issue_opened.js";
import { getConfig } from "../helpers/config.js";

// Event handler for webhook events
class EventHandler {
// context contains the information of repo
constructor(context) {
this.context = context;
}

async init() {
// catching issue from the payload of webhook
const { issue } = this.context.payload;

// Fetching maintainers of the repository to be used later
const collaboratorsJson =
await this.context.octokit.repos.listCollaborators(this.context.repo({}));
const maintainers = collaboratorsJson.data
.filter((coll) => {
return (
coll.permissions.admin ||
coll.permissions.maintain ||
coll.permissions.triage
);
})
.map((coll) => coll.login);

// Fetching config, like yml from `.github` folder of repo.
const config = await getConfig(this.context);

// Saving
this.issue = issue;
this.maintainers = maintainers;
this.config = config;
}

handleEvent() {
throw new Error("handleEvent method must be implemented");
}
}

// Inheriting EventHandler
export class IssueCommentHandler extends EventHandler {
// Override
async handleEvent() {
await this.init();
await issueCommentHandler.call(this);
}
}

// Inheriting EventHandler
export class IssueOpenerHandler extends EventHandler {
// Override
async handleEvent() {
await this.init();
await issueOpenerHandler.call(this);
}
}
Loading

0 comments on commit d90b27d

Please sign in to comment.