Skip to content

Commit

Permalink
Prevent overwriting manual changes made to branch (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecsammon authored Jul 11, 2024
1 parent 8b92be3 commit 8cf5d47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

40 changes: 32 additions & 8 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,41 @@ export default class Git {
const prefix = BRANCH_PREFIX.replace('SOURCE_REPO_NAME', GITHUB_REPOSITORY.split('/')[1])

let newBranch = path.join(prefix, this.repo.branch).replace(/\\/g, '/').replace(/\/\./g, '/')
this.prBranch = newBranch

if (OVERWRITE_EXISTING_PR === false) {
newBranch += `-${ Math.round((new Date()).getTime() / 1000) }`
this.prBranch = newBranch

core.debug(`Creating PR Branch ${ newBranch }`)

await execCmd(
`git switch -b "${ newBranch }"`,
this.workingDir
)

return
}

core.debug(`Creating PR Branch ${ newBranch }`)
core.debug(`Switch/Create PR Branch ${ newBranch }`)

await execCmd(
`git checkout -b "${ newBranch }"`,
`git remote set-branches origin '*'`,
this.workingDir
)

this.prBranch = newBranch
await execCmd(
`git fetch -v --depth=1`,
this.workingDir
)

await execCmd(
`git switch "${ newBranch }" 2>/dev/null || git switch -c "${ newBranch }"`,
this.workingDir
)

await this.getLastCommitSha()

}

async add(file) {
Expand Down Expand Up @@ -304,7 +326,7 @@ export default class Git {
// Gets the commit list in chronological order
async getCommitsToPush() {
const output = await execCmd(
`git log --format=%H --reverse ${ SKIP_PR === false ? `` : `origin/` }${ this.baseBranch }..HEAD`,
`git log --format=%H --reverse ${ this.lastCommitSha }..HEAD`,
this.workingDir
)

Expand All @@ -321,7 +343,7 @@ export default class Git {

// A wrapper for running all the flow to generate all the pending commits using the GitHub API
async createGithubVerifiedCommits() {
core.debug(`Creating Commits using GitHub API`)
core.debug(`Creating commits using GitHub API`)
const commits = await this.getCommitsToPush()

if (SKIP_PR === false) {
Expand All @@ -338,6 +360,8 @@ export default class Git {
} catch (error) {
// If the branch exists ignores the error
if (error.message !== 'Reference already exists') throw error

core.debug(`Branch ${ this.prBranch } already exists`)
}
}

Expand Down Expand Up @@ -366,15 +390,15 @@ export default class Git {
async push() {
if (FORK) {
return execCmd(
`git push -u fork ${ this.prBranch } --force`,
`git push -u fork ${ this.prBranch } --force-with-lease`,
this.workingDir
)
}
if (IS_INSTALLATION_TOKEN) {
return await this.createGithubVerifiedCommits()
}
return execCmd(
`git push ${ this.gitUrl } --force`,
`git push ${ this.gitUrl } --force-with-lease`,
this.workingDir
)
}
Expand Down Expand Up @@ -428,7 +452,7 @@ export default class Git {
`)

if (this.existingPr) {
core.info(`Overwriting existing PR`)
core.info(`Updating existing PR`)

const { data } = await this.github.pulls.update({
owner: this.repo.user,
Expand Down

0 comments on commit 8cf5d47

Please sign in to comment.