UNPKG

pullcraft

Version:

A CLI tool to create pull requests on GitHub by comparing branches and using OpenAI to generate PR text.

108 lines 4.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GhClient = exports.OctokitClient = exports.GitHubClient = void 0; const child_process_1 = __importDefault(require("child_process")); let Octokit; class GitHubClient { async listPulls({ owner, repo, base, head }) { throw new Error('Not implemented'); } async updatePull({ owner, repo, pullNumber, title, body }) { throw new Error('Not implemented'); } async createPull({ owner, repo, base, head, title, body }) { throw new Error('Not implemented'); } } exports.GitHubClient = GitHubClient; class OctokitClient extends GitHubClient { octokit; constructor(githubToken) { super(); this.initializeOctokit(githubToken); } async initializeOctokit(githubToken) { const { Octokit } = await Promise.resolve().then(() => __importStar(require('@octokit/rest'))); this.octokit = new Octokit({ auth: githubToken }); } async listPulls({ owner, repo, base, head }) { return this.octokit.pulls.list({ owner, repo, base, head }); } async updatePull({ owner, repo, pullNumber, title, body }) { return this.octokit.pulls.update({ owner, repo, pullNumber, title, body }); } async createPull({ owner, repo, base, head, title, body }) { return this.octokit.pulls.create({ owner, repo, base, head, title, body }); } } exports.OctokitClient = OctokitClient; class GhClient { escapeShellArg(arg) { // Escape backticks first arg = arg.replace(/`/g, '\\`'); // Then escape single quotes // eslint-disable-next-line quotes return `'${arg.replace(/'/g, "'\\''")}'`; } async listPulls(params) { const { owner, repo, base, head } = params; const command = `gh pr list --json number,title,headRefName -R ${owner}/${repo} --base '${base}' --head '${head}'`; const output = child_process_1.default.execSync(command).toString().trim(); return output ? JSON.parse(output) : []; } async updatePull(params) { const { owner, repo, pullNumber, title, body } = params; let command = `gh pr edit ${pullNumber} -R ${owner}/${repo}`; if (title) command += ` --title ${this.escapeShellArg(title)}`; if (body) command += ` --body ${this.escapeShellArg(body)}`; child_process_1.default.execSync(command); } async createPull(params) { const { owner, repo, base, head, title, body } = params; let command = `gh pr create -R ${owner}/${repo} --base ${this.escapeShellArg(base)} --head ${this.escapeShellArg(head)} --title ${this.escapeShellArg(title)}`; if (body) command += ` --body ${this.escapeShellArg(body)}`; const output = child_process_1.default.execSync(command).toString().trim(); return { data: { html_url: output } }; } } exports.GhClient = GhClient; //# sourceMappingURL=githubClient.js.map