Skip to content

Commit 60de800

Browse files
committed
refactor: Improve PR URL handling in createOrUpdatePR
1 parent 2837f76 commit 60de800

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

ghpr-cli.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ function log(message, color = 'reset') {
2121

2222
function execCommand(command, options = {}) {
2323
try {
24-
return execSync(command, {
24+
const result = execSync(command, {
2525
encoding: 'utf8',
2626
stdio: options.silent ? 'pipe' : 'inherit',
2727
...options
2828
});
29+
return result || '';
2930
} catch (error) {
3031
if (!options.silent) {
3132
log(`Error executing command: ${command}`, 'red');
@@ -67,7 +68,18 @@ async function checkExistingPR(branch) {
6768
}
6869
}
6970

70-
async function createOrUpdatePR(title, reviewers, assignees, existingPR) {
71+
async function getPRUrl(branch) {
72+
try {
73+
const prQuery = `gh pr list --head "${branch}" --json url --jq '.[0].url'`;
74+
const prUrlOutput = execCommand(prQuery, { silent: true });
75+
const prUrl = prUrlOutput.trim();
76+
return prUrl && prUrl !== 'null' ? prUrl : null;
77+
} catch (error) {
78+
return null;
79+
}
80+
}
81+
82+
async function createOrUpdatePR(title, reviewers, assignees, existingPR, currentBranch) {
7183
try {
7284
// Push the current branch
7385
log('Pushing current branch...', 'blue');
@@ -91,8 +103,13 @@ async function createOrUpdatePR(title, reviewers, assignees, existingPR) {
91103
execCommand(`gh pr edit ${existingPR.number} --add-assignee ${assignees.join(',')}`, { silent: true });
92104
}
93105

94-
prUrl = existingPR.url;
95-
log(`PR updated: ${prUrl}`, 'green');
106+
// Get the PR URL
107+
prUrl = await getPRUrl(currentBranch);
108+
if (prUrl) {
109+
log(`PR updated: ${prUrl}`, 'green');
110+
} else {
111+
log('Could not retrieve PR URL', 'yellow');
112+
}
96113
} else {
97114
log('Creating new PR...', 'blue');
98115

@@ -107,14 +124,15 @@ async function createOrUpdatePR(title, reviewers, assignees, existingPR) {
107124
createCommand += ` --assignee ${assignees.join(',')}`;
108125
}
109126

110-
const output = execCommand(createCommand);
111-
const urlMatch = output.match(/https:\/\/github\.com\/[^\s]*/);
112-
prUrl = urlMatch ? urlMatch[0] : null;
127+
// Create the PR (don't capture output as it might be empty)
128+
execCommand(createCommand);
113129

130+
// Get the PR URL
131+
prUrl = await getPRUrl(currentBranch);
114132
if (prUrl) {
115133
log(`PR created: ${prUrl}`, 'green');
116134
} else {
117-
log('PR created but could not extract URL from output', 'yellow');
135+
log('PR created but could not retrieve URL', 'yellow');
118136
}
119137
}
120138

@@ -209,7 +227,7 @@ async function main() {
209227
log(`Assignees: ${assignees.join(', ')}`, 'blue');
210228
}
211229

212-
const prUrl = await createOrUpdatePR(title, reviewers, assignees, existingPR);
230+
const prUrl = await createOrUpdatePR(title, reviewers, assignees, existingPR, currentBranch);
213231

214232
if (prUrl) {
215233
log('\n📋 Copy and paste the following in Slack:', 'bright');

0 commit comments

Comments
 (0)