Skip to content

Commit 8a800ff

Browse files
committed
feat: skip PR when one already exists
1 parent 23908ce commit 8a800ff

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

lib/github.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ module.exports.getBranch = async function getBranch (owner, repo, branch) {
132132
}
133133
}
134134

135+
module.exports.getPullsForBranch = async function getBranch (owner, repo, branch) {
136+
const { data } = await octokit.pulls.list({ owner, repo, head: `${owner}:${branch}` })
137+
138+
return data
139+
}
140+
135141
module.exports.getChecks = async function getChecks (owner, repo, branch) {
136142
return octokit.checks.listForRef({
137143
owner,

lib/test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ async function createPR (dependentOwner, dependentRepo, parentBranchName, parent
8181
const body = `Wiby has raised this PR to kick off automated jobs.
8282
You are dependent upon ${parentDependencyLink}
8383
`
84-
const head = context.getTestingBranchName(parentBranchName)
84+
const testingBranchName = context.getTestingBranchName(parentBranchName)
85+
86+
const pullsForTestingBranch = await github.getPullsForBranch(dependentOwner, dependentRepo, testingBranchName)
87+
if (pullsForTestingBranch.length) {
88+
debug(`Existing PRs updated: ${pullsForTestingBranch.map((data) => data.html_url).join(', ')}`)
89+
return
90+
}
91+
8592
const draft = true
8693
/*
8794
from the conversation on wiby PR 93 https://github.com/pkgjs/wiby/pull/93#discussion_r615362448
@@ -92,11 +99,11 @@ async function createPR (dependentOwner, dependentRepo, parentBranchName, parent
9299
const preExistingOnDependent = await github.getBranch(dependentOwner, dependentRepo, parentBranchName)
93100
let result
94101
if (preExistingOnDependent) {
95-
result = await github.createPR(dependentOwner, dependentRepo, title, head, parentBranchName, draft, body)
102+
result = await github.createPR(dependentOwner, dependentRepo, title, testingBranchName, parentBranchName, draft, body)
96103
debug(`PR raised upon ${result.data.html_url} (base branch: ${parentBranchName})`)
97104
} else {
98105
const defaultBranch = await github.getDefaultBranch(dependentOwner, dependentRepo)
99-
result = await github.createPR(dependentOwner, dependentRepo, title, head, defaultBranch, draft, body)
106+
result = await github.createPR(dependentOwner, dependentRepo, title, testingBranchName, defaultBranch, draft, body)
100107
debug(`PR raised upon ${result.data.html_url} (base branch: ${defaultBranch})`)
101108
}
102109
return result

test/cli/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ tap.test('test command', async (tap) => {
6363
tap.match(result, 'Pushed a new commit to https://github.com/wiby-test/pass#wiby-existing-branch')
6464
tap.match(result, 'Pushed a new commit to https://github.com/wiby-test/fail#wiby-existing-branch')
6565
tap.match(result, 'Pushed a new commit to https://github.com/wiby-test/partial#wiby-existing-branch')
66-
tap.match(result, 'PR raised upon https://github.com/pkgjs/wiby/pull/1 (base branch: main)')
66+
tap.match(result, 'Existing PRs updated: https://github.com/pkgjs/wiby/pull/1')
6767
})
6868

6969
tap.test('test command should not add `wiby-` prefix when branch already has it', async (tap) => {

test/fixtures/http/test-command-positive.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ function nockRepo (nockInstance, repo) {
119119
})
120120
.patch(`/repos/wiby-test/${repo}/git/refs/heads%2Fwiby-existing-branch`, { sha: 'fake_sha' })
121121
.reply(204)
122+
.get(`/repos/wiby-test/${repo}/pulls?head=wiby-test%3Awiby-running-unit-tests`)
123+
.reply(200, [])
124+
.get(`/repos/wiby-test/${repo}/pulls?head=wiby-test%3Awiby-existing-branch`)
125+
.reply(200, [
126+
{
127+
html_url: 'https://github.com/pkgjs/wiby/pull/1'
128+
}
129+
])
122130
}
123131

124132
function buildNock () {

0 commit comments

Comments
 (0)