Skip to content

Commit

Permalink
fix(github): Return remote commit hash for platformCommit=true (#16391
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zharinov authored Jul 3, 2022
1 parent 95e87cc commit c866fd0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1693,8 +1693,8 @@ export async function commitFiles(
config: CommitFilesConfig
): Promise<CommitSha | null> {
const commitResult = await git.prepareCommit(config); // Commit locally and don't push
const { branchName, files } = config;
if (!commitResult) {
const { branchName, files } = config;
logger.debug(
{ branchName, files: files.map(({ path }) => path) },
`Platform-native commit: unable to prepare for commit`
Expand All @@ -1706,7 +1706,9 @@ export async function commitFiles(
if (!pushResult) {
return null;
}
// Because the branch commit was done remotely via REST API, now we git fetch it locally.
// We also do this step when committing/pushing using local git tooling.
return git.fetchCommit(config);
// Replace locally created branch with the remotely created one
// and return the remote commit SHA
await git.resetToCommit(commitResult.parentCommitSha);
const commitSha = await git.fetchCommit(config);
return commitSha;
}
8 changes: 7 additions & 1 deletion lib/util/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ async function resetToBranch(branchName: string): Promise<void> {
await git.raw(['clean', '-fd']);
}

// istanbul ignore next
export async function resetToCommit(commit: string): Promise<void> {
logger.debug(`resetToCommit(${commit})`);
await git.raw(['reset', '--hard', commit]);
}

async function deleteLocalBranch(branchName: string): Promise<void> {
await git.branch(['-D', branchName]);
}
Expand Down Expand Up @@ -975,7 +981,7 @@ export async function fetchCommit({
logger.debug(`Fetching branch ${branchName}`);
try {
const ref = `refs/heads/${branchName}:refs/remotes/origin/${branchName}`;
await gitRetry(() => git.fetch(['origin', ref, '--force']));
await gitRetry(() => git.pull(['origin', ref, '--force']));
const commit = (await git.revparse([branchName])).trim();
config.branchCommits[branchName] = commit;
config.branchIsModified[branchName] = false;
Expand Down

0 comments on commit c866fd0

Please sign in to comment.