This is an automated email from the ASF dual-hosted git repository.

yuqi1129 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 82f27aee5d [MINOR] fix(ci): Break ties when selecting a pre-PR Fork CI 
base (#15068)
82f27aee5d is described below

commit 82f27aee5d1a2cb3d982773e8b7c6a1636a6dfac
Author: Qi Yu <[email protected]>
AuthorDate: Wed Nov 18 16:48:41 2026 +0800

    [MINOR] fix(ci): Break ties when selecting a pre-PR Fork CI base (#15068)
    
    ### What changes were proposed in this pull request?
    
    When a fork push has no open PR and multiple candidate bases have the
    same `ahead_by` count, prefer the candidate with the lowest `behind_by`
    count.
    
    ### Why are the changes needed?
    
    The first push for #15047 was one commit ahead of both `main` and
    `branch-2.0`, but it was 13 commits behind `main` and zero behind
    `branch-2.0`. Fork CI selected `main`, where the patch had already
    landed. The resulting empty diff caused the full build to be skipped.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No. This change only affects Fork CI base selection before a PR is
    opened.
    
    ### How was this patch tested?
    
    Ran `node --test .github/scripts/tests/fork-ci.test.cjs`. All 142 tests
    passed. The regression test uses the commit counts from #15047 and fails
    with the previous implementation.
---
 .github/scripts/resolve-fork-ci.cjs    | 12 ++++++++++--
 .github/scripts/tests/fork-ci.test.cjs | 19 ++++++++++++++-----
 2 files changed, 24 insertions(+), 7 deletions(-)

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' };

Reply via email to