-
Notifications
You must be signed in to change notification settings - Fork 11
feat: issue opened event #52
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
Open
rojen11
wants to merge
5
commits into
askbuddie:main
Choose a base branch
from
rojen11:rojen/github-bot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d7fa1ca
feat: github bot initialized
rojen11 3737ac1
feat: issue opened event
rojen11 4932780
Merge remote-tracking branch 'origin/main' into rojen/github-bot
rojen11 4cb57a1
fix: types and issue ref
rojen11 883e9ff
refactor: request handler, name
rojen11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,4 @@ | ||
| PRIVATE_REPO_NAME= | ||
| PUBLIC_REPO_NAME= | ||
| ORGANIZATION_NAME= | ||
| TOKEN= |
This file contains hidden or 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 hidden or 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,8 @@ | ||
| type Payload = { | ||
| action: string; | ||
| sender: Record<string, unknown>; | ||
| repository: Record<string, unknown>; | ||
| organization: Record<string, unknown>; | ||
| installation: Record<string, unknown>; | ||
| [key: string]: unknown; | ||
| }; |
This file contains hidden or 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,54 @@ | ||
| import AskBuddieBot from 'src/libs/askbuddiebot'; | ||
| import express from 'express'; | ||
|
|
||
| const app = express(); | ||
| const port = 3000; | ||
|
|
||
| const askBuddieBot = AskBuddieBot.getInstance(); | ||
|
|
||
| app.use(express.json()); | ||
|
|
||
| app.post('/payload', async (req, res) => { | ||
| const payload = req.body as Payload; | ||
|
|
||
| // get event name and action from the request | ||
| const event = req.headers['x-github-event'] ?? null; | ||
|
|
||
| if (event === 'ping') return res.status(200).send('OK'); | ||
|
|
||
| if (event === null) return res.status(400).send('Unknown event.'); | ||
|
|
||
| const eventName = `${event}.${payload.action}`; | ||
|
|
||
| // find the registered event in the bot | ||
| let botEvent; | ||
| try { | ||
| botEvent = askBuddieBot.getEvent(eventName); | ||
| } catch (e: unknown) { | ||
| if (e instanceof Error) { | ||
| console.error(e.message); | ||
| } else { | ||
| console.error('Something went wrong!'); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // check if the event is from registered repository | ||
| if ( | ||
| !askBuddieBot.isValidRepository( | ||
| (payload.repository?.name as string) ?? '' | ||
| ) | ||
| ) | ||
| return res.status(400).send('Not allowed.'); | ||
|
|
||
| // execute the event | ||
| const success = await botEvent?.handleEvent(payload); | ||
|
|
||
| if (!success) return res.status(500).send('Something went wrong!'); | ||
|
|
||
| res.status(200).send('OK'); | ||
| }); | ||
|
|
||
| app.listen(port, () => { | ||
| console.log(`App started on port ${port}.`); | ||
| }); | ||
This file contains hidden or 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,15 @@ | ||
| type Config = { | ||
| PRIVATE_REPO_NAME?: string; | ||
| PUBLIC_REPO_NAME?: string; | ||
| ORGANIZATION_NAME?: string; | ||
| TOKEN?: string; | ||
| }; | ||
|
|
||
| const config: Config = { | ||
| PRIVATE_REPO_NAME: process.env.PRIVATE_REPO_NAME, | ||
| PUBLIC_REPO_NAME: process.env.PUBLIC_REPO_NAME, | ||
| ORGANIZATION_NAME: process.env.ORGANIZATION_NAME, | ||
| TOKEN: process.env.TOKEN | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or 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 @@ | ||
| export { default as IssuesOpened } from './issues-opened'; |
This file contains hidden or 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,26 @@ | ||
| import Event from '../libs/event'; | ||
| import AskBuddieBot from 'src/libs/askbuddiebot'; | ||
|
|
||
| type Issue = { | ||
| body: string | null; | ||
| title: string; | ||
| [key: string]: unknown; | ||
| }; | ||
|
|
||
| class IssuesOpened implements Event { | ||
| name = 'issues.opened'; | ||
|
|
||
| async handleEvent(payload: Payload): Promise<boolean> { | ||
| const issue = payload.issue as Issue; | ||
|
|
||
| console.info(`Issue opened: ${issue.title}`); | ||
|
|
||
| const body = issue.body + `<br/><br/>Ref: ${issue.title}-${issue.id}`; | ||
|
|
||
| return await AskBuddieBot.getInstance() | ||
| .getRepository() | ||
| .createIssue(issue.title, body); | ||
| } | ||
| } | ||
|
|
||
| export default IssuesOpened; |
This file contains hidden or 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,73 @@ | ||
| import * as Events from '../events'; | ||
| import Event from './event'; | ||
| import config from 'src/config'; | ||
| import GithubRepository from './github_repository'; | ||
|
|
||
| type EventList = { | ||
| [key: string]: Event; | ||
| }; | ||
|
|
||
| class AskBuddieBot { | ||
| private events: EventList = {}; | ||
| private static instance: AskBuddieBot; | ||
| private repository: GithubRepository | undefined; | ||
|
|
||
| private constructor() { | ||
| console.info('Loading config...'); | ||
| Object.entries(config).forEach(([, value]) => { | ||
| if (value == undefined) throw new Error('Invalid env file.'); | ||
| }); | ||
|
|
||
| console.info('Config loaded.'); | ||
|
|
||
| console.info('Loading events...'); | ||
| this.loadEvents(); | ||
| console.info('Events loaded.'); | ||
|
|
||
| console.info('Loading repositories'); | ||
| this.loadRepository().then(() => { | ||
| console.info('Repository loaded'); | ||
| }); | ||
| } | ||
|
|
||
| public static getInstance(): AskBuddieBot { | ||
| if (!this.instance) this.instance = new AskBuddieBot(); | ||
| return this.instance; | ||
| } | ||
|
|
||
| // load all the events from events directory with the key ${event}.{action} | ||
| private loadEvents(): void { | ||
| Object.entries(Events).forEach(([, Event]) => { | ||
| const e = new Event(); | ||
| this.events[e.name] = e; | ||
| }); | ||
| } | ||
|
|
||
| // get id of the github repos and create a graphql repository for all the requests | ||
| private async loadRepository(): Promise<void> { | ||
| const repo = new GithubRepository(); | ||
| await repo.init(); | ||
| this.repository = repo; | ||
| } | ||
|
|
||
| public getRepository(): GithubRepository { | ||
| if (!this.repository) throw new Error('Repository is loading!'); | ||
|
|
||
| return this.repository; | ||
| } | ||
|
|
||
| // find the event from the key | ||
| public getEvent(e: string): Event { | ||
| if (!(e in this.events)) { | ||
| throw new Error(e + ' is not registered in events!'); | ||
| } | ||
|
|
||
| return this.events[e]; | ||
| } | ||
|
|
||
| public isValidRepository(name: string): boolean { | ||
| return name === config.PRIVATE_REPO_NAME; | ||
| } | ||
| } | ||
|
|
||
| export default AskBuddieBot; |
This file contains hidden or 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,6 @@ | ||
| interface Event { | ||
| name: string; | ||
| handleEvent(payload: Payload): Promise<boolean>; | ||
| } | ||
|
|
||
| export default Event; |
This file contains hidden or 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,86 @@ | ||
| import config from 'src/config'; | ||
| import RequestHandler from './request_handler'; | ||
|
|
||
| type OrganizationRepo = { | ||
| private: { | ||
| id: string; | ||
| }; | ||
| public: { | ||
| id: string; | ||
| }; | ||
| }; | ||
|
|
||
| class GithubRepository { | ||
| private publicRepoId = ''; | ||
| private privateRepoId = ''; | ||
| private requestHandler: RequestHandler; | ||
|
|
||
| constructor(requestHanlder: RequestHandler = new RequestHandler()) { | ||
| this.requestHandler = requestHanlder; | ||
| } | ||
|
|
||
| public async init() { | ||
| await this.getRepositoryId(); | ||
| } | ||
|
|
||
| public async getRepositoryId(): Promise<void> { | ||
| const origanization: string = config.ORGANIZATION_NAME ?? ''; | ||
| const privateRepo: string = config.PRIVATE_REPO_NAME ?? ''; | ||
| const publicRepo: string = config.PUBLIC_REPO_NAME ?? ''; | ||
|
|
||
| const query = ` | ||
| query GetRepo($org:String!, $private:String!, $public:String!) { | ||
| organization(login: $org) { | ||
| private: repository(name: $private) { | ||
| id | ||
| } | ||
| public: repository(name: $public) { | ||
| id | ||
| } | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const res = await this.requestHandler.post(query, { | ||
| org: origanization, | ||
| private: privateRepo, | ||
| public: publicRepo | ||
| }); | ||
|
|
||
| if (res.data.errors || !res.data.data) | ||
| throw new Error(res.data.errors.message ?? 'Something went wrong!'); | ||
|
|
||
| const repo = res.data.data.organization as OrganizationRepo; | ||
|
|
||
| this.privateRepoId = repo.private.id; | ||
| this.publicRepoId = repo.public.id; | ||
| } | ||
|
|
||
| public async createIssue(title: string, body: string): Promise<boolean> { | ||
| const repo = this.publicRepoId; | ||
|
|
||
| const query = ` | ||
| mutation CreateIssue($repositoryId: ID!, $title:String!, $body:String!) { | ||
| createIssue(input:{repositoryId: $repositoryId, title: $title, body: $body}) { | ||
| issue { | ||
| id | ||
| } | ||
| } | ||
| } | ||
|
|
||
| `; | ||
|
|
||
| const res = await this.requestHandler.post(query, { | ||
| repositoryId: repo, | ||
| title, | ||
| body | ||
| }); | ||
|
|
||
| if (res.data.errors || !res.data.data) | ||
| throw new Error('Something went wrong!'); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| export default GithubRepository; |
This file contains hidden or 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,18 @@ | ||
| import Axios from 'axios'; | ||
| import config from 'src/config'; | ||
|
|
||
| class RequestHandler { | ||
| private endpoint = 'https://api.github.com/graphql'; | ||
|
|
||
| public async post(query: string, variables: Record<string, unknown>) { | ||
| return await Axios.post( | ||
| this.endpoint, | ||
| { query, variables }, | ||
| { | ||
| headers: { Authorization: `Bearer ${config.TOKEN}` } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default RequestHandler; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i get this error without typing it as string.