Skip to content

Commit

Permalink
ci: get-changed-files-with-git-diff: stable branch names
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Sep 19, 2024
1 parent c38a9c4 commit 167babe
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions .circleci/scripts/git-diff-develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { promisify } from 'util';
const exec = promisify(execCallback);

const MAIN_BRANCH = 'develop';
const SOURCE_BRANCH = `refs/pull/${process.env.CIRCLE_PR_NUMBER}/head`;

/**
* Get the target branch for the given pull request.
Expand All @@ -29,8 +30,8 @@ async function getBaseRef(): Promise<string | null> {
*/
async function fetchWithDepth(depth: number): Promise<boolean> {
try {
await exec(`git fetch --depth ${depth} origin develop`);
await exec(`git fetch --depth ${depth} origin ${process.env.CIRCLE_BRANCH}`);
await exec(`git fetch --depth ${depth} origin ${MAIN_BRANCH}`);
await exec(`git fetch --depth ${depth} origin ${SOURCE_BRANCH}:${SOURCE_BRANCH}`);
return true;
} catch (error: unknown) {
console.error(`Failed to fetch with depth ${depth}:`, error);
Expand All @@ -57,6 +58,7 @@ async function fetchUntilMergeBaseFound() {
if (
error instanceof Error &&
Object.hasOwnProperty.call(error, 'code') &&
// @ts-expect-error
error.code === 1
) {
console.error(`Error 'no merge base' encountered with depth ${depth}. Incrementing depth...`);
Expand All @@ -65,7 +67,7 @@ async function fetchUntilMergeBaseFound() {
}
}
}
await exec(`git fetch --unshallow origin develop`);
await exec(`git fetch --unshallow origin ${MAIN_BRANCH}`);
}

/**
Expand All @@ -77,7 +79,7 @@ async function fetchUntilMergeBaseFound() {
*/
async function gitDiff(): Promise<string> {
await fetchUntilMergeBaseFound();
const { stdout: diffResult } = await exec(`git diff --name-only origin/HEAD...${process.env.CIRCLE_BRANCH}`);
const { stdout: diffResult } = await exec(`git diff --name-only origin/HEAD...${SOURCE_BRANCH}`);
if (!diffResult) {
throw new Error('Unable to get diff after full checkout.');
}
Expand Down

0 comments on commit 167babe

Please sign in to comment.