UNPKG

6.13 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<Event extends WebhookEvents = WebhookEvents> {
54 name: WebhookEvents;
55 id: string;
56 payload: {
57 [K in Event]: K extends WebhookEvents ? WebhookEvent<K> : never;
58 }[Event]["payload"];
59 octokit: ProbotOctokit;
60 log: Logger;
61 constructor(event: WebhookEvent<Event>, octokit: ProbotOctokit, log: Logger);
62 /**
63 * Return the `owner` and `repo` params for making API requests against a
64 * repository.
65 *
66 * ```js
67 * const params = context.repo({path: '.github/config.yml'})
68 * // Returns: {owner: 'username', repo: 'reponame', path: '.github/config.yml'}
69 * ```
70 *
71 * @param object - Params to be merged with the repo params.
72 *
73 */
74 repo<T>(object?: T): RepoResultType<Event> & T;
75 /**
76 * Return the `owner`, `repo`, and `issue_number` params for making API requests
77 * against an issue. The object passed in will be merged with the repo params.
78 *
79 *
80 * ```js
81 * const params = context.issue({body: 'Hello World!'})
82 * // Returns: {owner: 'username', repo: 'reponame', issue_number: 123, body: 'Hello World!'}
83 * ```
84 *
85 * @param object - Params to be merged with the issue params.
86 */
87 issue<T>(object?: T): RepoResultType<Event> & {
88 issue_number: RepoIssueNumberType<Event>;
89 } & T;
90 /**
91 * Return the `owner`, `repo`, and `pull_number` params for making API requests
92 * against a pull request. The object passed in will be merged with the repo params.
93 *
94 *
95 * ```js
96 * const params = context.pullRequest({body: 'Hello World!'})
97 * // Returns: {owner: 'username', repo: 'reponame', pull_number: 123, body: 'Hello World!'}
98 * ```
99 *
100 * @param object - Params to be merged with the pull request params.
101 */
102 pullRequest<T>(object?: T): RepoResultType<Event> & {
103 pull_number: RepoIssueNumberType<Event>;
104 } & T;
105 /**
106 * Returns a boolean if the actor on the event was a bot.
107 * @type {boolean}
108 */
109 get isBot(): boolean;
110 /**
111 * Reads the app configuration from the given YAML file in the `.github`
112 * directory of the repository.
113 *
114 * For example, given a file named `.github/config.yml`:
115 *
116 * ```yml
117 * close: true
118 * comment: Check the specs on the rotary girder.
119 * ```
120 *
121 * Your app can read that file from the target repository:
122 *
123 * ```js
124 * // Load config from .github/config.yml in the repository
125 * const config = await context.config('config.yml')
126 *
127 * if (config.close) {
128 * context.octokit.issues.comment(context.issue({body: config.comment}))
129 * context.octokit.issues.edit(context.issue({state: 'closed'}))
130 * }
131 * ```
132 *
133 * You can also use a `defaultConfig` object:
134 *
135 * ```js
136 * // Load config from .github/config.yml in the repository and combine with default config
137 * const config = await context.config('config.yml', {comment: 'Make sure to check all the specs.'})
138 *
139 * if (config.close) {
140 * context.octokit.issues.comment(context.issue({body: config.comment}));
141 * context.octokit.issues.edit(context.issue({state: 'closed'}))
142 * }
143 * ```
144 *
145 * Config files can also specify a base that they extend. `deepMergeOptions` can be used
146 * to configure how the target config, extended base, and default configs are merged.
147 *
148 * For security reasons, configuration is only loaded from the repository's default branch,
149 * changes made in pull requests from different branches or forks are ignored.
150 *
151 * If you need more lower-level control over reading and merging configuration files,
152 * you can `context.octokit.config.get(options)`, see https://github.com/probot/octokit-plugin-config.
153 *
154 * @param fileName - Name of the YAML file in the `.github` directory
155 * @param defaultConfig - An object of default config options
156 * @param deepMergeOptions - Controls merging configs (from the [deepmerge](https://github.com/TehShrike/deepmerge) module)
157 * @return Configuration object read from the file
158 */
159 config<T>(fileName: string, defaultConfig?: T, deepMergeOptions?: MergeOptions): Promise<T | null>;
160}
161export {};
162
\No newline at end of file