UNPKG

6.03 kBTypeScriptView Raw
1import merge from "deepmerge";
2import type { EmitterWebhookEvent as WebhookEvent, EmitterWebhookEventName as WebhookEvents } from "@octokit/webhooks";
3import type { Logger } from "pino";
4import type { ProbotOctokit } from "./octokit/probot-octokit.js";
5export type MergeOptions = merge.Options;
6/** Repo owner type, either string or never depending on the context */
7type RepoOwnerType<T extends WebhookEvents> = WebhookEvent<T>["payload"] extends {
8 repository: {
9 owner: {
10 login: string;
11 };
12 };
13} ? string : never;
14/** Repo name type, either string or never depending on the context */
15type RepoNameType<T extends WebhookEvents> = WebhookEvent<T>["payload"] extends {
16 repository: {
17 name: string;
18 };
19} ? string : never;
20/** Issue type (also pull request number), either number or never depending on the context */
21type RepoIssueNumberType<T extends WebhookEvents> = WebhookEvent<T>["payload"] extends {
22 issue: {
23 number: number;
24 };
25} ? number : never | WebhookEvent<T>["payload"] extends {
26 pull_request: {
27 number: number;
28 };
29} ? number : never | WebhookEvent<T>["payload"] extends {
30 number: number;
31} ? number : never;
32/** Context.repo return type */
33type RepoResultType<E extends WebhookEvents> = {
34 owner: RepoOwnerType<E>;
35 repo: RepoNameType<E>;
36};
37/**
38 * The context of the event that was triggered, including the payload and
39 * helpers for extracting information can be passed to GitHub API calls.
40 *
41 * ```js
42 * export default app => {
43 * app.on('push', context => {
44 * context.log.info('Code was pushed to the repo, what should we do with it?');
45 * });
46 * };
47 * ```
48 *
49 * @property {octokit} octokit - An Octokit instance
50 * @property {payload} payload - The webhook event payload
51 * @property {log} log - A pino instance
52 */
53export declare class Context<E extends WebhookEvents = WebhookEvents> {
54 name: WebhookEvents;
55 id: string;
56 payload: WebhookEvent<E>["payload"];
57 octokit: ProbotOctokit;
58 log: Logger;
59 constructor(event: WebhookEvent<E>, octokit: ProbotOctokit, log: Logger);
60 /**
61 * Return the `owner` and `repo` params for making API requests against a
62 * repository.
63 *
64 * ```js
65 * const params = context.repo({path: '.github/config.yml'})
66 * // Returns: {owner: 'username', repo: 'reponame', path: '.github/config.yml'}
67 * ```
68 *
69 * @param object - Params to be merged with the repo params.
70 *
71 */
72 repo<T>(object?: T): RepoResultType<E> & T;
73 /**
74 * Return the `owner`, `repo`, and `issue_number` params for making API requests
75 * against an issue. The object passed in will be merged with the repo params.
76 *
77 *
78 * ```js
79 * const params = context.issue({body: 'Hello World!'})
80 * // Returns: {owner: 'username', repo: 'reponame', issue_number: 123, body: 'Hello World!'}
81 * ```
82 *
83 * @param object - Params to be merged with the issue params.
84 */
85 issue<T>(object?: T): RepoResultType<E> & {
86 issue_number: RepoIssueNumberType<E>;
87 } & T;
88 /**
89 * Return the `owner`, `repo`, and `pull_number` params for making API requests
90 * against a pull request. The object passed in will be merged with the repo params.
91 *
92 *
93 * ```js
94 * const params = context.pullRequest({body: 'Hello World!'})
95 * // Returns: {owner: 'username', repo: 'reponame', pull_number: 123, body: 'Hello World!'}
96 * ```
97 *
98 * @param object - Params to be merged with the pull request params.
99 */
100 pullRequest<T>(object?: T): RepoResultType<E> & {
101 pull_number: RepoIssueNumberType<E>;
102 } & T;
103 /**
104 * Returns a boolean if the actor on the event was a bot.
105 * @type {boolean}
106 */
107 get isBot(): boolean;
108 /**
109 * Reads the app configuration from the given YAML file in the `.github`
110 * directory of the repository.
111 *
112 * For example, given a file named `.github/config.yml`:
113 *
114 * ```yml
115 * close: true
116 * comment: Check the specs on the rotary girder.
117 * ```
118 *
119 * Your app can read that file from the target repository:
120 *
121 * ```js
122 * // Load config from .github/config.yml in the repository
123 * const config = await context.config('config.yml')
124 *
125 * if (config.close) {
126 * context.octokit.issues.comment(context.issue({body: config.comment}))
127 * context.octokit.issues.edit(context.issue({state: 'closed'}))
128 * }
129 * ```
130 *
131 * You can also use a `defaultConfig` object:
132 *
133 * ```js
134 * // Load config from .github/config.yml in the repository and combine with default config
135 * const config = await context.config('config.yml', {comment: 'Make sure to check all the specs.'})
136 *
137 * if (config.close) {
138 * context.octokit.issues.comment(context.issue({body: config.comment}));
139 * context.octokit.issues.edit(context.issue({state: 'closed'}))
140 * }
141 * ```
142 *
143 * Config files can also specify a base that they extend. `deepMergeOptions` can be used
144 * to configure how the target config, extended base, and default configs are merged.
145 *
146 * For security reasons, configuration is only loaded from the repository's default branch,
147 * changes made in pull requests from different branches or forks are ignored.
148 *
149 * If you need more lower-level control over reading and merging configuration files,
150 * you can `context.octokit.config.get(options)`, see https://github.com/probot/octokit-plugin-config.
151 *
152 * @param fileName - Name of the YAML file in the `.github` directory
153 * @param defaultConfig - An object of default config options
154 * @param deepMergeOptions - Controls merging configs (from the [deepmerge](https://github.com/TehShrike/deepmerge) module)
155 * @return Configuration object read from the file
156 */
157 config<T>(fileName: string, defaultConfig?: T, deepMergeOptions?: MergeOptions): Promise<T | null>;
158}
159export {};
160
\No newline at end of file