Skip to content
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

feat(bot): add CLI #5

Open
wants to merge 4 commits into
base: challenge-bot-v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apps/challenge-bot/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default_events:
# - deployment_status
# - fork
# - gollum
# - issue_comment
- issue_comment
- issues
# - label
# - milestone
Expand Down Expand Up @@ -128,7 +128,6 @@ default_permissions:

# The homepage of your GitHub App.
url: https://github.com/Sage-Bionetworks/challenge-registry

# A description of the GitHub App.
# description: A description of my awesome app

Expand Down
2 changes: 2 additions & 0 deletions apps/challenge-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
"dependencies": {
"@probot/adapter-aws-lambda-serverless": "^3.0.1",
"ajv": "^8.11.0",
"commander": "^9.4.1",
"deepmerge": "^4.2.2",
"probot": "^12.2.4",
"serverless-http": "^3.1.0",
"string-argv": "^0.3.1",
"tslib": "^2.0.0"
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions apps/challenge-bot/src/build-challenge-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Context, Probot } from 'probot';
import { ChallengeApp } from './challenge-app';

export const buildChallengeApp = async (app: Probot) => {
app.on('issues.opened', run);
// app.on('issues.opened', run);
app.on('issue_comment.created', run);
// app.on('pull_request.opened', processPullRequestEvent);

async function run(context: Context<'issues'>) {
async function run(context: Context<'issue_comment'>) {
const challengeApp = new ChallengeApp(context);
await challengeApp.run();
}
Expand Down
74 changes: 67 additions & 7 deletions apps/challenge-bot/src/challenge-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import pino from 'pino';
import { version } from '../package.json';
import { Server } from './server/server';
import { Configuration, getConfiguration } from './config';
import { Command } from 'commander';
import stringArgv from 'string-argv';

export const logger = pino();

export class ChallengeApp {
private context: Context<'issues'>;
private context: Context<'issue_comment'>;
private logger: pino.Logger;
public server: Server;
private config?: Configuration;
private program = new Command();
private description = 'Awesome bot';

constructor(context: Context<'issues'>) {
constructor(context: Context<'issue_comment'>) {
this.context = context;
this.logger = logger.child({
version,
Expand All @@ -22,20 +26,76 @@ export class ChallengeApp {
// sha: this.headSha,
});
this.server = new Server();

this.program.name('challenge-bot').description(this.description);
this.program.exitOverride();
this.program.configureOutput({
// Visibly override write routines as example!
writeOut: (str) => process.stdout.write(`[OUT] ${str}`),
writeErr: (str) => process.stdout.write(`[ERR] ${str}`),
// Highlight errors in color.
// outputError: (str, write) => write(errorColor(str))
});
// .usage('[global options] command')
// .version(Pkg.version, '-v, --version', 'output the current version')
// .description(Pkg.description);

this.program
.command('ping')
.description('ping the bot')
.action(() => this.ping());

this.program
.command('version')
.description('output the version number')
.action(() => console.log(version));

this.program
.command('description')
.description('output the description')
.action(() => console.log(this.description));
}

private async ping(): Promise<void> {
const issueComment = this.context.issue({
body: 'pong',
});
await this.context.octokit.issues.createComment(issueComment);
}

async run(): Promise<void> {
try {
this.config = await getConfiguration(this.context);
this.logger.info({ config: this.config }, 'Loaded config');
} catch (err) {
this.logger.error('An error occured', err);
this.logger.error(
'An error occured while loading the configuration',
err
);
}

const issueComment = this.context.issue({
body: 'Thanks for opening this issue! ' + this.config?.message,
});
await this.context.octokit.issues.createComment(issueComment);
// const { comment, issue, pull_request: pr } = this.context.payload;
// const command = (comment || issue || pr).body;

const body = this.context.payload.comment.body;
this.logger.info({ body }, 'body');
if (!!body && body.startsWith('challenge-bot')) {
this.logger.info({ body }, 'Received bot command');
const argv = stringArgv(body);
this.logger.info({ argv }, 'argv');
try {
await this.program.parseAsync(argv.slice(1), {
from: 'user',
});
} catch (err) {
this.logger.info({ err }, 'CLI error');
}
}

// const issueComment = this.context.issue({
// body: 'Thanks for opening this issue! ' + this.config?.message,
// });
// await this.context.octokit.issues.createComment(issueComment);
}

// get repo(): string {
Expand Down
2 changes: 1 addition & 1 deletion apps/challenge-bot/src/config/get-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const DEFAULT_CONFIGURATION: Configuration = {
};

export const getConfiguration = async (
context: Context<'issues'>
context: Context<'issue_comment'>
): Promise<Configuration> => {
const validate = ajv.compile(configSchema);

Expand Down
16 changes: 16 additions & 0 deletions apps/challenge-bot/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ __metadata:
"@probot/adapter-aws-lambda-serverless": ^3.0.1
"@types/node": 18.7.23
ajv: ^8.11.0
commander: ^9.4.1
deepmerge: ^4.2.2
js-yaml: ^4.1.0
nock: 13.2.9
Expand All @@ -1039,6 +1040,7 @@ __metadata:
serverless-http: ^3.1.0
serverless-offline: ^11.1.3
smee-client: 1.2.3
string-argv: ^0.3.1
tslib: ^2.0.0
typescript: 4.8.4
languageName: unknown
Expand Down Expand Up @@ -2157,6 +2159,13 @@ __metadata:
languageName: node
linkType: hard

"commander@npm:^9.4.1":
version: 9.4.1
resolution: "commander@npm:9.4.1"
checksum: bfb18e325a5bdf772763c2213d5c7d9e77144d944124e988bcd8e5e65fb6d45d5d4e86b09155d0f2556c9a59c31e428720e57968bcd050b2306e910a0bf3cf13
languageName: node
linkType: hard

"commander@npm:~4.1.1":
version: 4.1.1
resolution: "commander@npm:4.1.1"
Expand Down Expand Up @@ -6483,6 +6492,13 @@ __metadata:
languageName: node
linkType: hard

"string-argv@npm:^0.3.1":
version: 0.3.1
resolution: "string-argv@npm:0.3.1"
checksum: efbd0289b599bee808ce80820dfe49c9635610715429c6b7cc50750f0437e3c2f697c81e5c390208c13b5d5d12d904a1546172a88579f6ee5cbaaaa4dc9ec5cf
languageName: node
linkType: hard

"string-width@npm:^1.0.2 || 2 || 3 || 4, string-width@npm:^4.1.0, string-width@npm:^4.2.3":
version: 4.2.3
resolution: "string-width@npm:4.2.3"
Expand Down