-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
2,281 additions
and
95 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
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
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
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,46 +1,94 @@ | ||
interface WebhookContextParams { | ||
payload: Record<string, any>; | ||
import { Octokit } from '@octokit/rest'; | ||
import { | ||
GetIssueParams, | ||
GetIssueResponse, | ||
GetPullRequestParams, | ||
GetPullRequestResponse, | ||
ListPullRequestFiles, | ||
} from './github-webhook.const'; | ||
|
||
export class GithubClient extends Octokit {} | ||
|
||
interface WebhookContextParams<E> { | ||
github: GithubClient; | ||
payload: E; | ||
eventType: string; | ||
} | ||
|
||
export class WebhookContext { | ||
export class WebhookContext<E> { | ||
public github: GithubClient; | ||
public eventType: string; | ||
public payload: Record<string, any>; | ||
public scheduledComments: { context: string; comment: string }[] = []; | ||
public payload: E; | ||
public scheduledComments: { handler: string; comment: string }[] = []; | ||
public scheduledlabels: string[] = []; | ||
|
||
constructor(params: WebhookContextParams) { | ||
public _prFilesCache?: ListPullRequestFiles; | ||
private _issueRequestCache: { [key: string]: GetIssueResponse } = {}; | ||
private _pullRequestCache: { [key: string]: GetPullRequestResponse } = {}; | ||
|
||
constructor(params: WebhookContextParams<E>) { | ||
this.github = params.github; | ||
this.eventType = params.eventType; | ||
this.payload = params.payload; | ||
} | ||
|
||
public repo<T>(data?: T) { | ||
public get senderIsBot(): boolean { | ||
return ( | ||
(this.payload as any).sender.type === 'Bot' || | ||
(this.payload as any).sender.login === 'homeassistant' | ||
); | ||
} | ||
|
||
public repo<T>(data?: T): { owner: string; repo: string } & T { | ||
return { | ||
owner: this.payload.repository.owner.login, | ||
repo: this.payload.repository.name, | ||
owner: (this.payload as any).repository.owner.login, | ||
repo: (this.payload as any).repository.name, | ||
...data, | ||
}; | ||
} | ||
|
||
public issue<T>(data?: T) { | ||
return { | ||
issue_number: (this.payload.issue || this.payload.pull_request || this.payload).number, | ||
issue_number: ((this.payload as any).issue?.number || | ||
(this.payload as any).pull_request?.number || | ||
(this.payload as any)?.number) as number, | ||
...this.repo(data), | ||
}; | ||
} | ||
|
||
public pullRequest<T>(data?: T) { | ||
return { | ||
pull_number: (this.payload.issue || this.payload.pull_request || this.payload).number, | ||
pull_number: ( | ||
(this.payload as any).issue || | ||
(this.payload as any).pull_request || | ||
this.payload | ||
).number as number, | ||
...this.repo(data), | ||
}; | ||
} | ||
|
||
public scheduleIssueComment(context: string, comment: string): void { | ||
this.scheduledComments.push({ context, comment }); | ||
public scheduleIssueComment(handler: string, comment: string): void { | ||
this.scheduledComments.push({ handler, comment }); | ||
} | ||
|
||
public scheduleIssueLabel(label: string): void { | ||
this.scheduledlabels.push(label); | ||
} | ||
|
||
public async fetchIssueWithCache(params: GetIssueParams): Promise<GetIssueResponse> { | ||
const key = `${params.owner}/${params.repo}/${params.pull_number}`; | ||
if (!(key in this._issueRequestCache)) { | ||
this._issueRequestCache[key] = (await this.github.issues.get(params)).data; | ||
} | ||
return this._issueRequestCache[key]; | ||
} | ||
public async fetchPullRequestWithCache( | ||
params: GetPullRequestParams, | ||
): Promise<GetPullRequestResponse> { | ||
const key = `${params.owner}/${params.repo}/${params.pull_number}`; | ||
if (!(key in this._pullRequestCache)) { | ||
this._pullRequestCache[key] = (await this.github.pulls.get(params)).data; | ||
} | ||
return this._pullRequestCache[key]; | ||
} | ||
} |
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
Oops, something went wrong.