Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent overwriting manual changes made to branch #341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
Comment on lines 152 to +160
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code currently only checks out the default branch. We need to fetch the other branches so we can switch to as required.


await execCmd(
`git switch "${ newBranch }" 2>/dev/null || git switch -c "${ newBranch }"`,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempt to switch to the existing branch, if this doesn't exists (i.e. no PR already exists) then create a new branch.

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`,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base the commits on the difference from the last pushed commit to now.

This is safe as this reference is not updated until the

		for (const commit of commits) {
			await this.createGithubCommit(commit)
		}

that follows.

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`)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit/commits are not capitalised elsewhere in this code.

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`,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--force-with-lease adds an extra layer of security for preventing overwrite of changes when using the git push instead of the github API.

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