Skip to content

Commit 74836a0

Browse files
committed
optimize: find the parent commits when no latest commit's baseline
1 parent a442dd7 commit 74836a0

2 files changed

Lines changed: 280 additions & 98 deletions

File tree

dist/index.js

Lines changed: 123 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -95707,10 +95707,41 @@ var __webpack_exports__ = {};
9570795707
});
9570895708
return runsResponse.data;
9570995709
}
95710+
async hasArtifactsForCommit(commitHash) {
95711+
try {
95712+
const workflowRuns = await this.findAllWorkflowRunsByCommit(commitHash);
95713+
for (const workflowRun of workflowRuns)try {
95714+
const runArtifacts = await this.listArtifactsForWorkflowRun(workflowRun.id);
95715+
if (runArtifacts.artifacts && runArtifacts.artifacts.length > 0) return true;
95716+
} catch (error) {
95717+
continue;
95718+
}
95719+
return false;
95720+
} catch (error) {
95721+
return false;
95722+
}
95723+
}
95724+
async getParentCommit(commitHash) {
95725+
const { owner, repo } = this.repository;
95726+
try {
95727+
const commitResponse = await this.octokit.rest.repos.getCommit({
95728+
owner,
95729+
repo,
95730+
ref: commitHash
95731+
});
95732+
if (commitResponse.data.parents && commitResponse.data.parents.length > 0) return commitResponse.data.parents[0].sha.substring(0, 10);
95733+
return null;
95734+
} catch (error) {
95735+
const apiError = error;
95736+
console.warn(`⚠️ Failed to get parent commit for ${commitHash}: ${apiError.message}`);
95737+
return null;
95738+
}
95739+
}
9571095740
async getTargetBranchLatestCommit() {
9571195741
const targetBranch = this.getTargetBranch();
9571295742
console.log(`🔍 Attempting to get latest commit for target branch: ${targetBranch}`);
9571395743
console.log(`📋 Repository: ${this.repository.owner}/${this.repository.repo}`);
95744+
let latestCommitHash = null;
9571495745
try {
9571595746
console.log(`📡 Trying to get latest commit from GitHub API...`);
9571695747
const { owner, repo } = this.repository;
@@ -95721,9 +95752,8 @@ var __webpack_exports__ = {};
9572195752
branch: targetBranch
9572295753
});
9572395754
if (branchResponse.data && branchResponse.data.commit) {
95724-
const commitHash = branchResponse.data.commit.sha.substring(0, 10);
95725-
console.log(`✅ Found commit hash from GitHub API: ${commitHash}`);
95726-
return commitHash;
95755+
latestCommitHash = branchResponse.data.commit.sha.substring(0, 10);
95756+
console.log(`✅ Found commit hash from GitHub API: ${latestCommitHash}`);
9572795757
}
9572895758
} catch (error) {
9572995759
const apiError = error;
@@ -95741,67 +95771,111 @@ var __webpack_exports__ = {};
9574195771
branch: altBranch
9574295772
});
9574395773
if (altResponse.data && altResponse.data.commit) {
95744-
const commitHash = altResponse.data.commit.sha.substring(0, 10);
95745-
console.log(`✅ Found commit hash from alternative branch ${altBranch}: ${commitHash}`);
95746-
return commitHash;
95774+
latestCommitHash = altResponse.data.commit.sha.substring(0, 10);
95775+
console.log(`✅ Found commit hash from alternative branch ${altBranch}: ${latestCommitHash}`);
95776+
break;
9574795777
}
9574895778
} catch (error) {
9574995779
const altError = error;
9575095780
console.log(`❌ Alternative branch ${altBranch} also failed: ${altError.message}`);
9575195781
}
9575295782
}
95753-
console.log(`📋 Trying to get from workflow runs...`);
95754-
try {
95755-
const runs = await this.listWorkflowRuns({
95756-
branch: targetBranch,
95757-
status: 'completed',
95758-
limit: 10
95759-
});
95760-
if (runs.workflow_runs && runs.workflow_runs.length > 0) {
95761-
console.log(`Found ${runs.workflow_runs.length} workflow runs for ${targetBranch}`);
95762-
const successfulRun = runs.workflow_runs.find((run)=>'success' === run.conclusion);
95763-
if (successfulRun) {
95764-
console.log(`✅ Found successful workflow run for ${targetBranch}: ${successfulRun.head_sha}`);
95765-
return successfulRun.head_sha.substring(0, 10);
95783+
if (!latestCommitHash) {
95784+
console.log(`📋 Trying to get from workflow runs...`);
95785+
try {
95786+
const runs = await this.listWorkflowRuns({
95787+
branch: targetBranch,
95788+
status: 'completed',
95789+
limit: 10
95790+
});
95791+
if (runs.workflow_runs && runs.workflow_runs.length > 0) {
95792+
console.log(`Found ${runs.workflow_runs.length} workflow runs for ${targetBranch}`);
95793+
const successfulRun = runs.workflow_runs.find((run)=>'success' === run.conclusion);
95794+
if (successfulRun) {
95795+
latestCommitHash = successfulRun.head_sha.substring(0, 10);
95796+
console.log(`✅ Found successful workflow run for ${targetBranch}: ${latestCommitHash}`);
95797+
} else {
95798+
const latestRun = runs.workflow_runs[0];
95799+
latestCommitHash = latestRun.head_sha.substring(0, 10);
95800+
console.log(`⚠️ No successful runs found, using latest workflow run for ${targetBranch}: ${latestCommitHash}`);
95801+
}
9576695802
}
95767-
const latestRun = runs.workflow_runs[0];
95768-
console.log(`⚠️ No successful runs found, using latest workflow run for ${targetBranch}: ${latestRun.head_sha}`);
95769-
return latestRun.head_sha.substring(0, 10);
95803+
} catch (error) {
95804+
const workflowError = error;
95805+
console.warn(`⚠️ Failed to get workflow runs: ${workflowError.message}`);
9577095806
}
95771-
} catch (error) {
95772-
const workflowError = error;
95773-
console.warn(`⚠️ Failed to get workflow runs: ${workflowError.message}`);
9577495807
}
95775-
console.log(`🔧 No workflow runs found for ${targetBranch}, trying to fetch from remote...`);
95776-
try {
95777-
console.log(`📥 Running: git fetch origin`);
95778-
(0, external_child_process_.execSync)('git fetch origin', {
95779-
encoding: 'utf8'
95780-
});
95781-
console.log(`📥 Running: git rev-parse --short=10 origin/${targetBranch}`);
95782-
const commitHash = (0, external_child_process_.execSync)(`git rev-parse --short=10 origin/${targetBranch}`, {
95783-
encoding: 'utf8'
95784-
}).trim();
95785-
console.log(`✅ Found commit hash from git: ${commitHash}`);
95786-
return commitHash;
95787-
} catch (gitError) {
95788-
console.warn(`❌ Git fetch failed: ${gitError}`);
95808+
if (!latestCommitHash) {
95809+
console.log(`🔧 No workflow runs found for ${targetBranch}, trying to fetch from remote...`);
9578995810
try {
95790-
console.log(`📥 Trying alternative: git ls-remote origin ${targetBranch}`);
95791-
const remoteRef = (0, external_child_process_.execSync)(`git ls-remote origin ${targetBranch}`, {
95811+
console.log(`📥 Running: git fetch origin`);
95812+
(0, external_child_process_.execSync)('git fetch origin', {
95813+
encoding: 'utf8'
95814+
});
95815+
console.log(`📥 Running: git rev-parse --short=10 origin/${targetBranch}`);
95816+
latestCommitHash = (0, external_child_process_.execSync)(`git rev-parse --short=10 origin/${targetBranch}`, {
9579295817
encoding: 'utf8'
9579395818
}).trim();
95794-
if (remoteRef) {
95795-
const commitHash = remoteRef.split('\t')[0].substring(0, 10);
95796-
console.log(`✅ Found commit hash from git ls-remote: ${commitHash}`);
95797-
return commitHash;
95819+
console.log(`✅ Found commit hash from git: ${latestCommitHash}`);
95820+
} catch (gitError) {
95821+
console.warn(`❌ Git fetch failed: ${gitError}`);
95822+
try {
95823+
console.log(`📥 Trying alternative: git ls-remote origin ${targetBranch}`);
95824+
const remoteRef = (0, external_child_process_.execSync)(`git ls-remote origin ${targetBranch}`, {
95825+
encoding: 'utf8'
95826+
}).trim();
95827+
if (remoteRef) {
95828+
latestCommitHash = remoteRef.split('\t')[0].substring(0, 10);
95829+
console.log(`✅ Found commit hash from git ls-remote: ${latestCommitHash}`);
95830+
}
95831+
} catch (altError) {
95832+
console.warn(`❌ Alternative git command failed: ${altError}`);
9579895833
}
95799-
} catch (altError) {
95800-
console.warn(`❌ Alternative git command failed: ${altError}`);
9580195834
}
9580295835
}
95803-
console.error(`❌ All methods to get target branch commit have failed`);
95804-
throw new Error(`Unable to get target branch (${targetBranch}) commit hash. Please ensure the branch exists and you have correct permissions.`);
95836+
if (!latestCommitHash) {
95837+
console.error(`❌ All methods to get target branch commit have failed`);
95838+
throw new Error(`Unable to get target branch (${targetBranch}) commit hash. Please ensure the branch exists and you have correct permissions.`);
95839+
}
95840+
console.log(`🔍 Checking if commit ${latestCommitHash} has baseline artifacts...`);
95841+
const hasArtifacts = await this.hasArtifactsForCommit(latestCommitHash);
95842+
if (hasArtifacts) {
95843+
console.log(`✅ Commit ${latestCommitHash} has baseline artifacts`);
95844+
return latestCommitHash;
95845+
}
95846+
console.log(`⚠️ Commit ${latestCommitHash} does not have baseline artifacts`);
95847+
console.log(`🔍 Looking for previous commits with baseline artifacts...`);
95848+
let currentCommit = latestCommitHash;
95849+
let checkedCommits = [
95850+
currentCommit
95851+
];
95852+
const maxDepth = 5;
95853+
for(let depth = 0; depth < maxDepth; depth++){
95854+
const parentCommit = await this.getParentCommit(currentCommit);
95855+
if (!parentCommit) {
95856+
console.log(`⚠️ Reached the beginning of the branch, no more parent commits`);
95857+
break;
95858+
}
95859+
if (checkedCommits.includes(parentCommit)) {
95860+
console.log(`⚠️ Detected circular reference, stopping search`);
95861+
break;
95862+
}
95863+
checkedCommits.push(parentCommit);
95864+
console.log(`🔍 Checking parent commit ${parentCommit}...`);
95865+
const parentHasArtifacts = await this.hasArtifactsForCommit(parentCommit);
95866+
if (parentHasArtifacts) {
95867+
console.log(`✅ Found commit ${parentCommit} with baseline artifacts`);
95868+
console.log(`\n⚠️ Note: The latest commit (${latestCommitHash}) does not have baseline artifacts.`);
95869+
console.log(` Using commit ${parentCommit} for baseline comparison instead.`);
95870+
console.log(" If this seems incorrect, please wait a few minutes and try rerunning the workflow.");
95871+
return parentCommit;
95872+
}
95873+
currentCommit = parentCommit;
95874+
}
95875+
console.log(`\n⚠️ No commits with baseline artifacts found in the last ${maxDepth} commits.`);
95876+
console.log(` Using latest commit ${latestCommitHash} anyway.`);
95877+
console.log(" Note: If baseline comparison fails, please wait a few minutes and try rerunning the workflow.");
95878+
return latestCommitHash;
9580595879
} catch (error) {
9580695880
console.error(`❌ Failed to get target branch commit: ${error}`);
9580795881
console.error(`Repository: ${this.repository.owner}/${this.repository.repo}`);

0 commit comments

Comments
 (0)