Skip to content

Commit

Permalink
refactor: move probot commands into src/bin/ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Nov 20, 2020
1 parent 74f1f16 commit 3f0974b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 100 deletions.
69 changes: 1 addition & 68 deletions bin/probot-receive.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,3 @@
#!/usr/bin/env node
// Usage: probot receive -e push -p path/to/payload app.js

require("dotenv").config();

const path = require("path");
const uuid = require("uuid");
const program = require("commander");
const { getPrivateKey } = require("@probot/get-private-key");

const {
logWarningsForObsoleteEnvironmentVariables,
} = require("../lib/helpers/log-warnings-for-obsolete-environment-variables");
const { Probot } = require("../");

logWarningsForObsoleteEnvironmentVariables();

program
.usage("[options] [path/to/app.js...]")
.option(
"-e, --event <event-name>",
"Event name",
process.env.GITHUB_EVENT_NAME
)
.option(
"-p, --payload-path <payload-path>",
"Path to the event payload",
process.env.GITHUB_EVENT_PATH
)
.option(
"-t, --token <access-token>",
"Access token",
process.env.GITHUB_TOKEN
)
.option("-a, --app <id>", "ID of the GitHub App", process.env.APP_ID)
.option(
"-P, --private-key <file>",
"Path to certificate of the GitHub App",
getPrivateKey
)
.parse(process.argv);

const githubToken = program.token;

if (!program.event || !program.payloadPath) {
program.help();
}

const privateKey = getPrivateKey();
if (!githubToken && (!program.app || !privateKey)) {
console.warn(
"No token specified and no certificate found, which means you will not be able to do authenticated requests to GitHub"
);
}

const payload = require(path.resolve(program.payloadPath));

const probot = new Probot({
id: program.app,
privateKey,
githubToken: githubToken,
});

probot.setup(program.args);

probot.logger.debug("Receiving event", program.event);
probot.receive({ name: program.event, payload, id: uuid.v4() }).catch(() => {
// Process must exist non-zero to indicate that the action failed to run
process.exit(1);
});
require("../lib/bin/probot-receive");
4 changes: 1 addition & 3 deletions bin/probot-run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

const { Probot } = require('../')

Probot.run(process.argv)
require("../lib/bin/probot-run");
30 changes: 1 addition & 29 deletions bin/probot.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
#!/usr/bin/env node

const semver = require('semver')
const program = require('commander')
const pkg = require('../package')
const updateNotifier = require('update-notifier')

if (!semver.satisfies(process.version, pkg.engines.node)) {
console.log(`Node.js version ${pkg.engines.node} is required. You have ${process.version}.`)
process.exit(1)
}

if (pkg.version !== '0.0.0-development') {
updateNotifier({pkg}).notify()
}

program
.version(pkg.version)
.usage('<command> [options]')
.command('run', 'run the bot')
.command('receive', 'Receive a single event and payload')
.on('command:*', (cmd) => {
if (!program.commands.find(c => c._name == cmd[0])) {
console.error(`Invalid command: ${program.args.join(' ')}\n`)
program.outputHelp()
process.exit(1)
}
})

program
.parse(process.argv)
require("../lib/bin/probot");
67 changes: 67 additions & 0 deletions src/bin/probot-receive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Usage: probot receive -e push -p path/to/payload app.js

require("dotenv").config();

import path from "path";
import { v4 as uuidv4 } from "uuid";
import program from "commander";
import { getPrivateKey } from "@probot/get-private-key";

import { Probot } from "../";
import { logWarningsForObsoleteEnvironmentVariables } from "../helpers/log-warnings-for-obsolete-environment-variables";

logWarningsForObsoleteEnvironmentVariables();

program
.usage("[options] [path/to/app.js...]")
.option(
"-e, --event <event-name>",
"Event name",
process.env.GITHUB_EVENT_NAME
)
.option(
"-p, --payload-path <payload-path>",
"Path to the event payload",
process.env.GITHUB_EVENT_PATH
)
.option(
"-t, --token <access-token>",
"Access token",
process.env.GITHUB_TOKEN
)
.option("-a, --app <id>", "ID of the GitHub App", process.env.APP_ID)
.option(
"-P, --private-key <file>",
"Path to certificate of the GitHub App",
process.env.PRIVATE_KEY_PATH
)
.parse(process.argv);

const githubToken = program.token;

if (!program.event || !program.payloadPath) {
program.help();
}

const privateKey = getPrivateKey();
if (!githubToken && (!program.app || !privateKey)) {
console.warn(
"No token specified and no certificate found, which means you will not be able to do authenticated requests to GitHub"
);
}

const payload = require(path.resolve(program.payloadPath));

const probot = new Probot({
id: program.app,
privateKey: String(privateKey),
githubToken: githubToken,
});

probot.setup(program.args);

probot.log.debug("Receiving event", program.event);
probot.receive({ name: program.event, payload, id: uuidv4() }).catch(() => {
// Process must exist non-zero to indicate that the action failed to run
process.exit(1);
});
3 changes: 3 additions & 0 deletions src/bin/probot-run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Probot } from "../";

Probot.run(process.argv);
31 changes: 31 additions & 0 deletions src/bin/probot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import semver from "semver";
import program from "commander";
import updateNotifier from "update-notifier";

const pkg = require("../../package");

if (!semver.satisfies(process.version, pkg.engines.node)) {
console.log(
`Node.js version ${pkg.engines.node} is required. You have ${process.version}.`
);
process.exit(1);
}

if (pkg.version !== "0.0.0-development") {
updateNotifier({ pkg }).notify();
}

program
.version(pkg.version)
.usage("<command> [options]")
.command("run", "run the bot")
.command("receive", "Receive a single event and payload")
.on("command:*", (cmd) => {
if (!program.commands.find((c) => c._name == cmd[0])) {
console.error(`Invalid command: ${program.args.join(" ")}\n`);
program.outputHelp();
process.exit(1);
}
});

program.parse(process.argv);

0 comments on commit 3f0974b

Please sign in to comment.