probot
Version:
A framework for building GitHub Apps to automate and improve your workflow
97 lines • 3.65 kB
JavaScript
import fs from "node:fs";
import path from "node:path";
import { npxImport } from "npx-import-light";
import yaml from "js-yaml";
import { ProbotOctokit } from "./octokit/probot-octokit.js";
import { loadPackageJson } from "./helpers/load-package-json.js";
import { updateEnv } from "./helpers/update-env.js";
export class ManifestCreation {
#updateEnv;
constructor(options = { updateEnv }) {
this.#updateEnv = options.updateEnv || updateEnv;
}
get pkg() {
return loadPackageJson();
}
async createWebhookChannel({ SmeeClient: SmeeClientParam, log = console } = {}) {
let SmeeClient;
try {
SmeeClient =
SmeeClientParam ||
(await npxImport("smee-client@4.3.1", { onlyPackageRunner: true }))
.SmeeClient;
}
catch {
log.warn("SmeeClient is not available");
return void 0;
}
try {
const WEBHOOK_PROXY_URL = await SmeeClient.createChannel();
this.#updateEnv({
WEBHOOK_PROXY_URL,
});
return WEBHOOK_PROXY_URL;
}
catch {
log.warn("Unable to connect to smee.io, try restarting your server.");
return void 0;
}
}
getManifest(options) {
let manifest = {};
const { pkg, baseUrl, readFileSync = fs.readFileSync } = options;
try {
const file = readFileSync(path.join(process.cwd(), "app.yml"), "utf8");
manifest = yaml.load(file);
}
catch (error) {
// App config does not exist, which is ok.
if (error.code !== "ENOENT") {
throw error;
}
}
const generatedManifest = JSON.stringify({
description: manifest.description || pkg.description,
hook_attributes: {
url: process.env.WEBHOOK_PROXY_URL || `${baseUrl}/`,
},
name: process.env.PROJECT_DOMAIN || manifest.name || pkg.name,
public: manifest.public || true,
redirect_url: `${baseUrl}/probot/setup`,
// TODO: add setup url
// setup_url:`${baseUrl}/probot/success`,
url: manifest.url || pkg.homepage || pkg.repository,
version: "v1",
...manifest,
});
return generatedManifest;
}
async createAppFromCode(code, probotOptions) {
const octokit = new ProbotOctokit(probotOptions);
const options = {
...probotOptions,
code,
mediaType: {
previews: ["fury"], // needed for GHES 2.20 and older
},
...(process.env.GHE_HOST && {
baseUrl: `${process.env.GHE_PROTOCOL || "https"}://${process.env.GHE_HOST}/api/v3`,
}),
};
const response = await octokit.request("POST /app-manifests/:code/conversions", options);
const { id, client_id, client_secret, webhook_secret, pem } = response.data;
this.#updateEnv({
APP_ID: id.toString(),
PRIVATE_KEY: `"${pem}"`,
WEBHOOK_SECRET: webhook_secret,
GITHUB_CLIENT_ID: client_id,
GITHUB_CLIENT_SECRET: client_secret,
});
return response.data.html_url;
}
get createAppUrl() {
const githubHost = process.env.GHE_HOST || `github.com`;
return `${process.env.GHE_PROTOCOL || "https"}://${githubHost}${process.env.GH_ORG ? `/organizations/${process.env.GH_ORG}` : ""}/settings/apps/new`;
}
}
//# sourceMappingURL=manifest-creation.js.map