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

Conversation

alecsammon
Copy link

We use the repo-file-sync-action for a number of tasks including syncing changes to our linting configuration.

When we make an update to the linting rules then manual changes are often required in addition to the synced file.

Currently if the sync action runs again then it overwrites any changes make in other files, including all the history.

This PR will adjust this, so that the existing PR is updated instead, and the changes added as a new commit.

Comment on lines 152 to +160
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
)
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.

@@ -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.

@@ -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.

@@ -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.

@alecsammon alecsammon marked this pull request as ready for review July 10, 2024 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant