Create GitHub Pull Request
Create a new pull request in a GitHub repository
Source Code
const [owner, repo, title, body = "", head, base = "main"] = process.argv.slice(2);
const token = process.env.GITHUB_TOKEN || "PLACEHOLDER_TOKEN";
async function createPR() {
const url = `https://api.github.com/repos/${owner}/${repo}/pulls`;
const payload = { title, body, head, base };
console.log(`Creating PR in ${owner}/${repo}: "${title}" (${head} → ${base})`);
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!response.ok) {
const error = await response.json();
console.error("ERROR: GitHub API request failed.");
console.error(` Status: ${response.status}`);
console.error(` Message: ${error.message || response.statusText}`);
console.error(" Check that the repository exists and you have push access.");
process.exit(1);
}
const remaining = response.headers.get("X-RateLimit-Remaining");
if (remaining && parseInt(remaining) < 100) {
console.log(`⚠️ Rate limit warning: ${remaining} requests remaining`);
}
const pr = await response.json();
const fs = await import("fs");
fs.writeFileSync("session/pr.json", JSON.stringify(pr, null, 2));
console.log(`✅ Created PR #${pr.number}: ${pr.title}`);
console.log(` URL: ${pr.html_url}`);
console.log(` Branch: ${pr.head.ref} → ${pr.base.ref}`);
console.log(` Wrote details to session/pr.json`);
}
try {
await createPR();
} catch (error) {
console.error("ERROR: Unexpected failure creating pull request.");
console.error(` ${error.message}`);
process.exit(1);
}