Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Commit 0510227

Browse files
committed
update to probot 7
1 parent ab4fff2 commit 0510227

File tree

12 files changed

+176
-119
lines changed

12 files changed

+176
-119
lines changed

functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"firebase-admin": "5.12.0",
66
"firebase-functions": "1.0.0",
77
"minimatch": "3.0.4",
8-
"probot-ts": "6.1.0-ts"
8+
"probot": "7.0.0-typescript.2"
99
},
1010
"private": true
1111
}

functions/src/dev.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {createProbot} from "probot-ts";
1+
import {createProbot} from "probot";
22
import {credential, firestore, initializeApp} from "firebase-admin";
33
import {consoleStream, registerTasks} from "./util";
4-
import {Probot} from './typings';
54

65
console.warn(`Starting dev mode`);
76

@@ -14,7 +13,7 @@ initializeApp({
1413
const store: FirebaseFirestore.Firestore = firestore();
1514

1615
// Probot setup
17-
const bot: Probot = createProbot(config);
16+
const bot = createProbot(config);
1817

1918
// disable probot logging
2019
bot.logger.streams.splice(0, 1);

functions/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {firestore as database, https} from 'firebase-functions';
22
import {Request, Response} from "express";
3-
import {createProbot, Options} from "probot-ts";
3+
import {createProbot, Options} from "probot";
44
import {consoleStream, registerTasks, Tasks} from "./util";
55
import {credential, firestore, initializeApp} from "firebase-admin";
6-
import {Probot} from "./typings";
76
import {DocumentSnapshot} from "firebase-functions/lib/providers/firestore";
87
import {EventContext} from "firebase-functions/lib/cloud-functions";
98

@@ -25,7 +24,7 @@ if(probotConfig) {
2524

2625
const store: FirebaseFirestore.Firestore = firestore();
2726
// Create the bot using Firebase's probot config (see Readme.md)
28-
const bot: Probot = createProbot(probotConfig);
27+
const bot = createProbot(probotConfig);
2928
// disable probot logging
3029
bot.logger.streams.splice(0, 1);
3130
// Use node console as the output stream

functions/src/plugins/common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {Context, Robot} from "probot-ts";
1+
import {Context, Robot} from "probot";
22
import * as Github from '@octokit/rest';
33
import * as minimatch from "minimatch";
44
import {AdminConfig} from "../default";
55
import {Task} from "./task";
6-
import {OctokitWithPagination} from "probot-ts/lib/github";
6+
import {OctokitWithPagination} from "probot/lib/github";
77
import * as firebase from "firebase";
88

99
export class CommonTask extends Task {
@@ -51,7 +51,7 @@ export class CommonTask extends Task {
5151
*/
5252
async triggeredInit(data: firebase.firestore.DocumentData): Promise<void> {
5353
const repository = data as Repository & { installationId: number };
54-
const authGithub = await this.robot.auth(repository.installationId);
54+
const authGithub = await this.robot.auth(repository.installationId.toString());
5555
return this.init(authGithub, [repository]);
5656
}
5757

functions/src/plugins/merge.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Github from '@octokit/rest';
2-
import {Context, Robot} from "probot-ts";
3-
import {appConfig, MergeConfig} from "../default";
2+
import {Context, Robot} from "probot";
3+
import {AppConfig, appConfig, MergeConfig} from "../default";
44
import {addComment, getGhLabels, getLabelsNames, matchAny, matchAnyFile} from "./common";
55
import {Task} from "./task";
66
import {REVIEW_STATE, STATUS_STATE} from "../typings";
@@ -46,7 +46,7 @@ export class MergeTask extends Task {
4646
*/
4747
async onLabeled(context: Context): Promise<void> {
4848
const newLabel = context.payload.label.name;
49-
const pr: Github.PullRequest = context.payload.pull_request;
49+
const pr = context.payload.pull_request;
5050
const config = await this.getConfig(context);
5151
const {owner, repo} = context.repo();
5252
// we need the list of labels from Github because we might be adding multiple labels at once
@@ -534,11 +534,8 @@ export class MergeTask extends Task {
534534
* Gets the config for the merge plugin from Github or uses default if necessary
535535
*/
536536
async getConfig(context: Context): Promise<MergeConfig> {
537-
let repositoryConfig = await context.config(CONFIG_FILE);
538-
if(!repositoryConfig || !repositoryConfig.merge) {
539-
repositoryConfig = {merge: {}};
540-
}
541-
return {...appConfig.merge, ...repositoryConfig.merge};
537+
const repositoryConfig = await context.config<AppConfig>(CONFIG_FILE, appConfig);
538+
return repositoryConfig.merge;
542539
}
543540
}
544541

functions/src/plugins/task.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {Context, Robot} from "probot-ts";
2-
import {OctokitWithPagination} from "probot-ts/lib/github";
1+
import {Context, Robot} from "probot";
2+
import {OctokitWithPagination} from "probot/lib/github";
33

44
export class Task {
55
repositories: FirebaseFirestore.CollectionReference;
@@ -28,7 +28,7 @@ export class Task {
2828

2929
// wrapper for this.robot.on
3030
dispatch(events: string | string[], callback: (context: Context) => any) {
31-
this.robot.on(events, (context: Context) => {
31+
this.robot.on(events, (context: any) => {
3232
this.log({context}, "Event received");
3333
return callback(context);
3434
});

functions/src/plugins/triage.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Context, Robot} from "probot-ts";
2-
import {OctokitWithPagination} from "probot-ts/lib/github";
1+
import {Context, Robot} from "probot";
32
import {Task} from "./task";
43
import {CONFIG_FILE} from "./merge";
5-
import {AdminConfig, appConfig, TriageConfig} from "../default";
4+
import {AdminConfig, AppConfig, appConfig, TriageConfig} from "../default";
65
import {getGhLabels, getLabelsNames, matchAllOfAny} from "./common";
76
import * as Github from '@octokit/rest';
7+
import {GitHubApi} from "../typings";
88

99
export class TriageTask extends Task {
1010
constructor(robot: Robot, db: FirebaseFirestore.Firestore) {
@@ -25,10 +25,10 @@ export class TriageTask extends Task {
2525
const github = await this.robot.auth();
2626
const installations = await github.paginate(github.apps.getInstallations({}), pages => pages.data);
2727
await Promise.all(installations.map(async installation => {
28-
const authGithub: OctokitWithPagination = await this.robot.auth(installation.id);
28+
const authGithub = await this.robot.auth(installation.id) as GitHubApi;
2929
const repositories = await authGithub.apps.getInstallationRepositories({});
3030
await Promise.all(repositories.data.repositories.map(async (repository: Github.Repository) => {
31-
const context: Context = new Context({payload: {repository}}, authGithub);
31+
const context = new Context({payload: {repository}}, authGithub, this.robot.log);
3232
const config = await this.getConfig(context);
3333
const {owner, repo} = context.repo();
3434
const issues = await authGithub.paginate(authGithub.issues.getForRepo({
@@ -69,7 +69,7 @@ export class TriageTask extends Task {
6969
}
7070

7171
async checkTriage(context: Context): Promise<any> {
72-
const issue = context.payload.issue;
72+
const issue: any = context.payload.issue;
7373
const config = await this.getConfig(context);
7474
const {owner, repo} = context.repo();
7575
// getting labels from Github because we might be adding multiple labels at once
@@ -94,7 +94,7 @@ export class TriageTask extends Task {
9494
}
9595
}
9696

97-
setMilestone(milestoneNumber: number | null, github: OctokitWithPagination, owner: string, repo: string, issue: Github.Issue): Promise<any> {
97+
setMilestone(milestoneNumber: number | null, github: Github, owner: string, repo: string, issue: Github.Issue): Promise<any> {
9898
if(milestoneNumber) {
9999
this.log(`Adding milestone ${milestoneNumber} to issue ${issue.html_url}`);
100100
} else {
@@ -113,13 +113,10 @@ export class TriageTask extends Task {
113113
* Gets the config for the merge plugin from Github or uses default if necessary
114114
*/
115115
async getConfig(context: Context): Promise<TriageConfig> {
116-
let repositoryConfig = await context.config(CONFIG_FILE);
117-
if(!repositoryConfig || !repositoryConfig.triage) {
118-
repositoryConfig = {triage: {}};
119-
}
120-
const config = {...appConfig.triage, ...repositoryConfig.triage};
121-
config.defaultMilestone = parseInt(config.defaultMilestone, 10);
122-
config.needsTriageMilestone = parseInt(config.needsTriageMilestone, 10);
116+
const repositoryConfig = await context.config<AppConfig>(CONFIG_FILE, appConfig);
117+
const config = repositoryConfig.triage;
118+
config.defaultMilestone = parseInt(<any>config.defaultMilestone, 10);
119+
config.needsTriageMilestone = parseInt(<any>config.needsTriageMilestone, 10);
123120
return config;
124121
}
125122
}

functions/src/typings.ts

Lines changed: 128 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,131 @@
1-
// Type definitions for probot v3.0.0 (WIP)
2-
// Project: github.com/probot/probot
3-
// Definitions by: sirMerr <github.com/sirMerr>
4-
// Definitions: DefinitelyTyped/DefinitelyTyped
5-
/// <reference types="express" />
1+
import {Logger} from "probot";
2+
import {LoggerWithTarget} from "probot/lib/wrap-logger";
3+
import {OctokitWithPagination, Variables} from "probot/lib/github";
4+
import {Issue, PullRequest, Repository} from "@octokit/rest";
65

7-
import {Application} from "express";
8-
import {WebhookEvent} from "probot-ts/lib/robot";
9-
import {Logger, Robot} from "probot-ts";
10-
import {Plugin} from "probot-ts/lib";
6+
// override this for now until the types are fixed
7+
declare module "probot/lib/wrap-logger" {
8+
9+
interface LoggerWithTarget extends Logger {
10+
(str: string): void;
11+
12+
target: Logger;
13+
child: (attrs: ChildArgs) => LoggerWithTarget;
14+
trace: Logger;
15+
debug: Logger;
16+
info: Logger;
17+
warn: Logger;
18+
error: Logger;
19+
fatal: Logger;
20+
}
21+
}
22+
23+
export interface GitHubApi extends OctokitWithPagination {
24+
query: (string, variables: Variables, headers?: any) => any;
25+
}
26+
27+
export interface Payload {
28+
[x: string]: any;
29+
repository: Repository;
30+
issue: Issue;
31+
pull_request: PullRequest;
32+
sender: {
33+
type: string;
34+
};
35+
action: string;
36+
installation: {
37+
id: string;
38+
};
39+
}
40+
41+
declare module "probot" {
42+
export class Context {
43+
id: string;
44+
github: GitHubApi;
45+
log: LoggerWithTarget;
46+
payload: Payload;
47+
event: string;
48+
49+
constructor(event: any, github: GitHubApi, log: LoggerWithTarget);
50+
51+
/**
52+
* Return the `owner` and `repo` params for making API requests against a
53+
* repository.
54+
*
55+
* @param {object} [object] - Params to be merged with the repo params.
56+
*
57+
* @example
58+
*
59+
* const params = context.repo({path: '.github/config.yml'})
60+
* // Returns: {owner: 'username', repo: 'reponame', path: '.github/config.yml'}
61+
*
62+
*/
63+
repo<T>(object?: T): {
64+
owner: string;
65+
repo: string;
66+
} & T;
67+
68+
/**
69+
* Return the `owner`, `repo`, and `number` params for making API requests
70+
* against an issue or pull request. The object passed in will be merged with
71+
* the repo params.
72+
*
73+
* @example
74+
*
75+
* const params = context.issue({body: 'Hello World!'})
76+
* // Returns: {owner: 'username', repo: 'reponame', number: 123, body: 'Hello World!'}
77+
*
78+
* @param {object} [object] - Params to be merged with the issue params.
79+
*/
80+
issue<T>(object?: T): {
81+
number: number;
82+
} & {
83+
owner: string;
84+
repo: string;
85+
} & T;
86+
87+
/**
88+
* Returns a boolean if the actor on the event was a bot.
89+
* @type {boolean}
90+
*/
91+
readonly isBot: boolean;
92+
93+
/**
94+
* Reads the app configuration from the given YAML file in the `.github`
95+
* directory of the repository.
96+
*
97+
* @example <caption>Contents of <code>.github/config.yml</code>.</caption>
98+
*
99+
* close: true
100+
* comment: Check the specs on the rotary girder.
101+
*
102+
* @example <caption>App that reads from <code>.github/config.yml</code>.</caption>
103+
*
104+
* // Load config from .github/config.yml in the repository
105+
* const config = await context.config('config.yml')
106+
*
107+
* if (config.close) {
108+
* context.github.issues.comment(context.issue({body: config.comment}))
109+
* context.github.issues.edit(context.issue({state: 'closed'}))
110+
* }
111+
*
112+
* @example <caption>Using a <code>defaultConfig</code> object.</caption>
113+
*
114+
* // Load config from .github/config.yml in the repository and combine with default config
115+
* const config = await context.config('config.yml', {comment: 'Make sure to check all the specs.'})
116+
*
117+
* if (config.close) {
118+
* context.github.issues.comment(context.issue({body: config.comment}));
119+
* context.github.issues.edit(context.issue({state: 'closed'}))
120+
* }
121+
*
122+
* @param {string} fileName - Name of the YAML file in the `.github` directory
123+
* @param {object} [defaultConfig] - An object of default config options
124+
* @return {Promise<Object>} - Configuration object read from the file
125+
*/
126+
config<T>(fileName: string, defaultConfig: T): Promise<T | null>;
127+
}
128+
}
11129

12130
declare module "@octokit/rest" {
13131
export interface Label {
@@ -107,7 +225,7 @@ declare module "@octokit/rest" {
107225
};
108226
requested_reviewers: User[];
109227
requested_teams: any[]; // need to check how the "team" json structure is
110-
mergeable: boolean|null;
228+
mergeable: boolean | null;
111229
}
112230

113231
export interface File {
@@ -160,13 +278,3 @@ export const enum REVIEW_STATE {
160278
Commented = 'COMMENTED',
161279
Dismissed = 'DISMISSED'
162280
}
163-
164-
export interface Probot {
165-
server: Application;
166-
webhook: any;
167-
receive: (event: WebhookEvent) => Promise<any[]>;
168-
logger: Logger;
169-
load: (plugin: string | Plugin) => Robot;
170-
setup: (apps: Array<string | Plugin>) => void;
171-
start: () => void;
172-
}

functions/src/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {Context, Robot} from "probot-ts";
1+
import {Robot} from "probot";
22
import {CommonTask} from "./plugins/common";
33
import {MergeTask} from "./plugins/merge";
44
import {TriageTask} from "./plugins/triage";
5-
import {OctokitWithPagination} from "probot-ts/lib/github";
5+
import {OctokitWithPagination} from "probot/lib/github";
66

77
/**
88
* Get all results in case of paginated Github request
@@ -47,7 +47,7 @@ class Stream {
4747

4848
let event = '';
4949
let extraData = '';
50-
const context: Context = data.context;
50+
const context = data.context;
5151
if(context) {
5252
event = context.event;
5353
const payload = data.context.payload;

0 commit comments

Comments
 (0)