Skip to content

Commit 16512d2

Browse files
authored
Added git push functionality if branch not on remote.
2 parents b9c92b0 + 65e5b19 commit 16512d2

File tree

3 files changed

+90
-19
lines changed

3 files changed

+90
-19
lines changed

README.md

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
# 🚀 GitHub PR Maker [![Tests Status](https://github.com/user/github-pr-builder/workflows/Tests/badge.svg)](https://github.com/user/github-pr-builder/actions)
1+
# 🚀 GitHub PR Maker
22

3-
🎯 GitHub PR Maker automatically generates polished PRs using your commit history and a standardized template!
3+
[![Tests](https://github.com/user/github-pr-builder/workflows/test/badge.svg)]
44

5-
## ✨ Features
6-
7-
- 🔍 Scans your recent commits and lets you pick which ones to include
8-
- ✏️ Edit commit messages for clearer PR descriptions
9-
- 🧩 Uses your PR template to generate a consistent PR format
10-
- 🤖 Creates the PR directly via GitHub CLI
5+
🎯 GitHub PR Maker generates PRs using your commit history and a standardized template.
116

127
## 🛠️ How It Works
138

@@ -20,8 +15,6 @@ The app will prompt you for:
2015

2116
It then:
2217
- 📋 Generates a PR using your template with all provided information
23-
- 🔗 Creates the PR with title in format: `[TICKET-123] Your PR Title`
24-
- 📊 Shows you a preview before submitting
2518

2619
## 🚀 Usage
2720

@@ -33,14 +26,7 @@ npm install
3326
npm start
3427
```
3528

36-
## 🧰 Tech Stack
37-
38-
- 💬 [`@inquirer/prompts`](https://github.com/SBoudrias/Inquirer.js) - Interactive CLI prompts
39-
- 🧪 [`jest`](https://jestjs.io/) - Testing framework
40-
- 🧹 [`eslint`](https://eslint.org/) - Code quality
41-
- 📝 [`twig`](https://github.com/twigjs/twig.js) - Templating engine
42-
43-
## 🧪 Testing
29+
## 🧪 Development
4430

4531
```bash
4632
# Run tests

index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,37 @@ export function checkGitRepository() {
3838
}
3939
}
4040

41+
// Get current branch name
42+
export function getCurrentBranch() {
43+
try {
44+
return execSync('git branch --show-current').toString().trim();
45+
} catch {
46+
return null;
47+
}
48+
}
49+
50+
// Check if branch is pushed to remote
51+
export function isBranchPushedToRemote(branchName) {
52+
try {
53+
// Check if the branch exists on the remote
54+
execSync(`git ls-remote --heads origin ${branchName}`, { stdio: 'ignore' });
55+
return true;
56+
} catch {
57+
return false;
58+
}
59+
}
60+
61+
// Push branch to remote
62+
export function pushBranchToRemote(branchName) {
63+
try {
64+
execSync(`git push -u origin ${branchName}`);
65+
return true;
66+
} catch (error) {
67+
console.error('Failed to push branch to remote:', error.message);
68+
return false;
69+
}
70+
}
71+
4172
// Check if gh CLI is installed
4273
export function checkGhCli() {
4374
try {
@@ -163,13 +194,40 @@ export async function main() {
163194
});
164195

165196
if (confirmCreate) {
197+
// Get current branch name
198+
const currentBranch = getCurrentBranch();
199+
200+
if (!currentBranch) {
201+
console.error('\n❌ Failed to determine current branch');
202+
process.exit(1);
203+
}
204+
205+
// Check if branch is pushed to remote
206+
let needsToPush = false;
207+
if (!isBranchPushedToRemote(currentBranch)) {
208+
console.log(`\n🔄 Branch '${currentBranch}' not found on remote. Pushing now...`);
209+
needsToPush = true;
210+
211+
const pushSucceeded = pushBranchToRemote(currentBranch);
212+
if (!pushSucceeded) {
213+
console.error('\n❌ Failed to push branch to remote. Cannot create PR.');
214+
process.exit(1);
215+
}
216+
console.log(`✅ Branch '${currentBranch}' successfully pushed to remote.`);
217+
}
218+
219+
// Create PR
166220
const fullTitle = ticketNumber ? `[${ticketNumber}] ${prTitle}` : prTitle;
167221
const result = await createPR(fullTitle, renderedTemplate);
168222

169223
if (result.success) {
170224
console.log(`\n✅ Pull Request created successfully: ${result.url}`);
171225
} else {
172226
console.error(`\n❌ Failed to create Pull Request: ${result.error}`);
227+
228+
if (needsToPush) {
229+
console.log('\n💡 Note: Your branch was pushed to remote, but PR creation failed. You can create a PR manually.');
230+
}
173231
}
174232
} else {
175233
console.log('\n❌ PR creation cancelled');

test/basic.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import path from 'path';
66
import { fileURLToPath } from 'url';
7-
import { getRecentCommits, getTemplatePath } from '../index';
7+
import { getRecentCommits, getTemplatePath, getCurrentBranch, isBranchPushedToRemote } from '../index';
88

99
const __filename = fileURLToPath(import.meta.url);
1010
const __dirname = path.dirname(__filename);
@@ -13,6 +13,8 @@ describe('GitHub PR Maker', () => {
1313
test('Module exports expected functions', () => {
1414
expect(typeof getRecentCommits).toBe('function');
1515
expect(typeof getTemplatePath).toBe('function');
16+
expect(typeof getCurrentBranch).toBe('function');
17+
expect(typeof isBranchPushedToRemote).toBe('function');
1618
});
1719

1820
// This test relies on implementation details, so it's a bit fragile
@@ -41,6 +43,20 @@ describe('GitHub PR Maker', () => {
4143
// Test without ticket number (undefined)
4244
expect(ticketNumberFormat(undefined, 'Add feature')).toBe('Add feature');
4345
});
46+
47+
test('Branch remote push checks work correctly', () => {
48+
// Test the logic for determining when to push a branch
49+
50+
// Branch exists on remote
51+
expect(branchPushNeeded('feature-branch', true)).toBe(false);
52+
53+
// Branch doesn't exist on remote
54+
expect(branchPushNeeded('feature-branch', false)).toBe(true);
55+
56+
// No branch name
57+
expect(branchPushNeeded(null, false)).toBe(false);
58+
expect(branchPushNeeded('', false)).toBe(false);
59+
});
4460
});
4561

4662
/**
@@ -49,3 +65,14 @@ describe('GitHub PR Maker', () => {
4965
function ticketNumberFormat(ticketNumber, prTitle) {
5066
return ticketNumber ? `[${ticketNumber}] ${prTitle}` : prTitle;
5167
}
68+
69+
/**
70+
* Helper function that mimics the branch push decision logic
71+
*/
72+
function branchPushNeeded(branchName, existsOnRemote) {
73+
// If no branch name, can't push
74+
if (!branchName) return false;
75+
76+
// If branch isn't on remote, need to push
77+
return !existsOnRemote;
78+
}

0 commit comments

Comments
 (0)