Skip to content

Commit b598643

Browse files
authored
🐛 Fix SKIP_PR option for runs with installation token (#232)
The original implementation for signed commits in #153 did not consider the `SKIP_PR` flag - if used together with an installation token it fails now. This change adds support by: 1) comparing changes against the remote branch, as base branch and HEAD are the same 2) Skipping pr branch creation 3) using the base instead of the undefined prBranch as upload target
1 parent 637915e commit b598643

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

dist/index.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -21513,6 +21513,7 @@ const {
2151321513
COMMIT_PREFIX,
2151421514
GITHUB_REPOSITORY,
2151521515
OVERWRITE_EXISTING_PR,
21516+
SKIP_PR,
2151621517
PR_BODY,
2151721518
BRANCH_PREFIX,
2151821519
FORK
@@ -21785,7 +21786,7 @@ class Git {
2178521786
// Gets the commit list in chronological order
2178621787
async getCommitsToPush() {
2178721788
const output = await execCmd(
21788-
`git log --format=%H --reverse ${ this.baseBranch }..HEAD`,
21789+
`git log --format=%H --reverse ${ SKIP_PR === false ? `` : `origin/` }${ this.baseBranch }..HEAD`,
2178921790
this.workingDir
2179021791
)
2179121792

@@ -21820,30 +21821,32 @@ class Git {
2182021821
async createGithubVerifiedCommits() {
2182121822
const commitsData = await this.getCommitsDataToPush()
2182221823

21823-
// Creates the PR branch if doesn't exists
21824-
try {
21825-
await this.github.git.createRef({
21826-
owner: this.repo.user,
21827-
repo: this.repo.name,
21828-
sha: this.lastCommitSha,
21829-
ref: 'refs/heads/' + this.prBranch
21830-
})
21824+
if (SKIP_PR === false) {
21825+
// Creates the PR branch if doesn't exists
21826+
try {
21827+
await this.github.git.createRef({
21828+
owner: this.repo.user,
21829+
repo: this.repo.name,
21830+
sha: this.lastCommitSha,
21831+
ref: 'refs/heads/' + this.prBranch
21832+
})
2183121833

21832-
core.debug(`Created new branch ${ this.prBranch }`)
21833-
} catch (error) {
21834-
// If the branch exists ignores the error
21835-
if (error.message !== 'Reference already exists') throw error
21834+
core.debug(`Created new branch ${ this.prBranch }`)
21835+
} catch (error) {
21836+
// If the branch exists ignores the error
21837+
if (error.message !== 'Reference already exists') throw error
21838+
}
2183621839
}
2183721840

2183821841
for (const commitData of commitsData) {
2183921842
await this.createGithubTreeAndCommit(commitData.tree, commitData.commitMessage)
2184021843
}
2184121844

21842-
core.debug(`Updating branch ${ this.prBranch } ref`)
21845+
core.debug(`Updating branch ${ SKIP_PR === false ? this.prBranch : this.baseBranch } ref`)
2184321846
await this.github.git.updateRef({
2184421847
owner: this.repo.user,
2184521848
repo: this.repo.name,
21846-
ref: `heads/${ this.prBranch }`,
21849+
ref: `heads/${ SKIP_PR === false ? this.prBranch : this.baseBranch }`,
2184721850
sha: this.lastCommitSha,
2184821851
force: true
2184921852
})

src/git.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
COMMIT_PREFIX,
1717
GITHUB_REPOSITORY,
1818
OVERWRITE_EXISTING_PR,
19+
SKIP_PR,
1920
PR_BODY,
2021
BRANCH_PREFIX,
2122
FORK
@@ -288,7 +289,7 @@ class Git {
288289
// Gets the commit list in chronological order
289290
async getCommitsToPush() {
290291
const output = await execCmd(
291-
`git log --format=%H --reverse ${ this.baseBranch }..HEAD`,
292+
`git log --format=%H --reverse ${ SKIP_PR === false ? `` : `origin/` }${ this.baseBranch }..HEAD`,
292293
this.workingDir
293294
)
294295

@@ -323,30 +324,32 @@ class Git {
323324
async createGithubVerifiedCommits() {
324325
const commitsData = await this.getCommitsDataToPush()
325326

326-
// Creates the PR branch if doesn't exists
327-
try {
328-
await this.github.git.createRef({
329-
owner: this.repo.user,
330-
repo: this.repo.name,
331-
sha: this.lastCommitSha,
332-
ref: 'refs/heads/' + this.prBranch
333-
})
334-
335-
core.debug(`Created new branch ${ this.prBranch }`)
336-
} catch (error) {
337-
// If the branch exists ignores the error
338-
if (error.message !== 'Reference already exists') throw error
327+
if (SKIP_PR === false) {
328+
// Creates the PR branch if doesn't exists
329+
try {
330+
await this.github.git.createRef({
331+
owner: this.repo.user,
332+
repo: this.repo.name,
333+
sha: this.lastCommitSha,
334+
ref: 'refs/heads/' + this.prBranch
335+
})
336+
337+
core.debug(`Created new branch ${ this.prBranch }`)
338+
} catch (error) {
339+
// If the branch exists ignores the error
340+
if (error.message !== 'Reference already exists') throw error
341+
}
339342
}
340343

341344
for (const commitData of commitsData) {
342345
await this.createGithubTreeAndCommit(commitData.tree, commitData.commitMessage)
343346
}
344347

345-
core.debug(`Updating branch ${ this.prBranch } ref`)
348+
core.debug(`Updating branch ${ SKIP_PR === false ? this.prBranch : this.baseBranch } ref`)
346349
await this.github.git.updateRef({
347350
owner: this.repo.user,
348351
repo: this.repo.name,
349-
ref: `heads/${ this.prBranch }`,
352+
ref: `heads/${ SKIP_PR === false ? this.prBranch : this.baseBranch }`,
350353
sha: this.lastCommitSha,
351354
force: true
352355
})

0 commit comments

Comments
 (0)