-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add classes, object_oriented code
- Loading branch information
1 parent
93a0f41
commit ca257f7
Showing
20 changed files
with
314 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.