Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[MINOR] fix(ci): Break ties when selecting a pre-PR Fork CI base (#15068)

Qi Yu82f27aee5d1a
diff --git a/.github/scripts/resolve-fork-ci.cjs b/.github/scripts/resolve-fork-ci.cjs
index 305f2de7a1..a1057bf600 100644
--- a/.github/scripts/resolve-fork-ci.cjs
+++ b/.github/scripts/resolve-fork-ci.cjs
@@ -65,13 +65,21 @@ async function resolve({ github, context, core }) {
const releases = branches.map(b => b.name).filter(n => /^branch-\d+\.\d+$/.test(n))
.sort((a, b) => a.localeCompare(b, 'en', { numeric: true })).slice(-3);
const candidates = [...new Set([upstream.default_branch, ...releases])];
- let fewest = Infinity;
+ let fewestAhead = Infinity;
+ let fewestBehind = Infinity;
for (const candidate of candidates) {
const { data: comparison } = await github.rest.repos.compareCommitsWithBasehead({
owner: 'apache', repo: 'gravitino',
basehead: `${candidate}...${context.repo.owner}:${headSha}`, per_page: 1
});
- if (comparison.ahead_by < fewest) { fewest = comparison.ahead_by; baseRef = candidate; }
+ // Branches sharing a fork point can have the same head-only commits.
+ // Prefer the candidate with fewer commits since that fork point.
+ if (comparison.ahead_by < fewestAhead ||
+ (comparison.ahead_by === fewestAhead && comparison.behind_by < fewestBehind)) {
+ fewestAhead = comparison.ahead_by;
+ fewestBehind = comparison.behind_by;
+ baseRef = candidate;
+ }
}
core.notice(`No open PR: testing against ${baseRef}; results require a matching PR base.`);
}
diff --git a/.github/scripts/tests/fork-ci.test.cjs b/.github/scripts/tests/fork-ci.test.cjs
index a9ebe44ab7..ec3182c40f 100644
--- a/.github/scripts/tests/fork-ci.test.cjs
+++ b/.github/scripts/tests/fork-ci.test.cjs
@@ -190,17 +190,26 @@ test('fork pushes resolve the PR base once', async () => {
getBranch: async () => ({ data: { commit: { sha: B } } }) } }, paginate: async () => [pr()] } });
assert.equal(output['base-sha'], B); assert.equal(output['base-ref'], 'main'); assert.equal(output.sync, 'true');
});
-test('pre-PR base discovery paginates branches, including later release branches', async () => {
- const output = {}; let paginated = false;
+async function discoverBase(main, release) {
+ const output = {};
const listBranches = () => {};
await resolve({ context: context('fork/repo', true), core: coreFor(output), github: {
rest: { pulls: { list() {} }, repos: { listBranches,
get: async args => ({ data: args.owner === 'fork' ? { fork: true, source: { full_name: 'apache/gravitino' } } : { default_branch: 'main' } }),
- compareCommitsWithBasehead: async args => ({ data: { ahead_by: args.basehead.startsWith('branch-9.9...') ? 1 : 99 } }),
+ compareCommitsWithBasehead: async args => ({ data: args.basehead.startsWith('branch-2.0...') ? release : main }),
getBranch: async () => ({ data: { commit: { sha: B } } }) } },
- paginate: async endpoint => { if (endpoint === listBranches) { paginated = true; return [{ name: 'branch-9.9' }]; } return []; }
+ paginate: async endpoint => endpoint === listBranches ? [{ name: 'branch-2.0' }] : []
} });
- assert.equal(paginated, true); assert.equal(output['base-ref'], 'branch-9.9');
+ return output['base-ref'];
+}
+test('pre-PR base discovery prefers fewer head-only commits among paginated branches', async () => {
+ assert.equal(await discoverBase({ ahead_by: 2, behind_by: 0 }, { ahead_by: 1, behind_by: 5 }), 'branch-2.0');
+});
+test('pre-PR backports prefer the closer base when both have the same head-only commits', async () => {
+ assert.equal(await discoverBase({ ahead_by: 1, behind_by: 13 }, { ahead_by: 1, behind_by: 0 }), 'branch-2.0');
+});
+test('pre-PR base discovery keeps the default branch when both distances tie', async () => {
+ assert.equal(await discoverBase({ ahead_by: 1, behind_by: 0 }, { ahead_by: 1, behind_by: 0 }), 'main');
});
test('trusted dispatch rejects an arbitrary ref or a non-upstream repository', async () => {
const ctx = context('fork/repo');ctx.eventName = 'workflow_dispatch';ctx.payload.inputs = { 'pull-request': '7' };