Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 123 additions & 49 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95707,10 +95707,41 @@ var __webpack_exports__ = {};
});
return runsResponse.data;
}
async hasArtifactsForCommit(commitHash) {
try {
const workflowRuns = await this.findAllWorkflowRunsByCommit(commitHash);
for (const workflowRun of workflowRuns)try {
const runArtifacts = await this.listArtifactsForWorkflowRun(workflowRun.id);
if (runArtifacts.artifacts && runArtifacts.artifacts.length > 0) return true;
} catch (error) {
continue;
}
return false;
} catch (error) {
return false;
}
}
async getParentCommit(commitHash) {
const { owner, repo } = this.repository;
try {
const commitResponse = await this.octokit.rest.repos.getCommit({
owner,
repo,
ref: commitHash
});
if (commitResponse.data.parents && commitResponse.data.parents.length > 0) return commitResponse.data.parents[0].sha.substring(0, 10);
return null;
} catch (error) {
const apiError = error;
console.warn(`⚠️ Failed to get parent commit for ${commitHash}: ${apiError.message}`);
return null;
}
}
async getTargetBranchLatestCommit() {
const targetBranch = this.getTargetBranch();
console.log(`🔍 Attempting to get latest commit for target branch: ${targetBranch}`);
console.log(`📋 Repository: ${this.repository.owner}/${this.repository.repo}`);
let latestCommitHash = null;
try {
console.log(`📡 Trying to get latest commit from GitHub API...`);
const { owner, repo } = this.repository;
Expand All @@ -95721,9 +95752,8 @@ var __webpack_exports__ = {};
branch: targetBranch
});
if (branchResponse.data && branchResponse.data.commit) {
const commitHash = branchResponse.data.commit.sha.substring(0, 10);
console.log(`✅ Found commit hash from GitHub API: ${commitHash}`);
return commitHash;
latestCommitHash = branchResponse.data.commit.sha.substring(0, 10);
console.log(`✅ Found commit hash from GitHub API: ${latestCommitHash}`);
}
} catch (error) {
const apiError = error;
Expand All @@ -95741,67 +95771,111 @@ var __webpack_exports__ = {};
branch: altBranch
});
if (altResponse.data && altResponse.data.commit) {
const commitHash = altResponse.data.commit.sha.substring(0, 10);
console.log(`✅ Found commit hash from alternative branch ${altBranch}: ${commitHash}`);
return commitHash;
latestCommitHash = altResponse.data.commit.sha.substring(0, 10);
console.log(`✅ Found commit hash from alternative branch ${altBranch}: ${latestCommitHash}`);
break;
}
} catch (error) {
const altError = error;
console.log(`❌ Alternative branch ${altBranch} also failed: ${altError.message}`);
}
}
console.log(`📋 Trying to get from workflow runs...`);
try {
const runs = await this.listWorkflowRuns({
branch: targetBranch,
status: 'completed',
limit: 10
});
if (runs.workflow_runs && runs.workflow_runs.length > 0) {
console.log(`Found ${runs.workflow_runs.length} workflow runs for ${targetBranch}`);
const successfulRun = runs.workflow_runs.find((run)=>'success' === run.conclusion);
if (successfulRun) {
console.log(`✅ Found successful workflow run for ${targetBranch}: ${successfulRun.head_sha}`);
return successfulRun.head_sha.substring(0, 10);
if (!latestCommitHash) {
console.log(`📋 Trying to get from workflow runs...`);
try {
const runs = await this.listWorkflowRuns({
branch: targetBranch,
status: 'completed',
limit: 10
});
if (runs.workflow_runs && runs.workflow_runs.length > 0) {
console.log(`Found ${runs.workflow_runs.length} workflow runs for ${targetBranch}`);
const successfulRun = runs.workflow_runs.find((run)=>'success' === run.conclusion);
if (successfulRun) {
latestCommitHash = successfulRun.head_sha.substring(0, 10);
console.log(`✅ Found successful workflow run for ${targetBranch}: ${latestCommitHash}`);
} else {
const latestRun = runs.workflow_runs[0];
latestCommitHash = latestRun.head_sha.substring(0, 10);
console.log(`⚠️ No successful runs found, using latest workflow run for ${targetBranch}: ${latestCommitHash}`);
}
}
const latestRun = runs.workflow_runs[0];
console.log(`⚠️ No successful runs found, using latest workflow run for ${targetBranch}: ${latestRun.head_sha}`);
return latestRun.head_sha.substring(0, 10);
} catch (error) {
const workflowError = error;
console.warn(`⚠️ Failed to get workflow runs: ${workflowError.message}`);
}
} catch (error) {
const workflowError = error;
console.warn(`⚠️ Failed to get workflow runs: ${workflowError.message}`);
}
console.log(`🔧 No workflow runs found for ${targetBranch}, trying to fetch from remote...`);
try {
console.log(`📥 Running: git fetch origin`);
(0, external_child_process_.execSync)('git fetch origin', {
encoding: 'utf8'
});
console.log(`📥 Running: git rev-parse --short=10 origin/${targetBranch}`);
const commitHash = (0, external_child_process_.execSync)(`git rev-parse --short=10 origin/${targetBranch}`, {
encoding: 'utf8'
}).trim();
console.log(`✅ Found commit hash from git: ${commitHash}`);
return commitHash;
} catch (gitError) {
console.warn(`❌ Git fetch failed: ${gitError}`);
if (!latestCommitHash) {
console.log(`🔧 No workflow runs found for ${targetBranch}, trying to fetch from remote...`);
try {
console.log(`📥 Trying alternative: git ls-remote origin ${targetBranch}`);
const remoteRef = (0, external_child_process_.execSync)(`git ls-remote origin ${targetBranch}`, {
console.log(`📥 Running: git fetch origin`);
(0, external_child_process_.execSync)('git fetch origin', {
encoding: 'utf8'
});
console.log(`📥 Running: git rev-parse --short=10 origin/${targetBranch}`);
latestCommitHash = (0, external_child_process_.execSync)(`git rev-parse --short=10 origin/${targetBranch}`, {
encoding: 'utf8'
}).trim();
if (remoteRef) {
const commitHash = remoteRef.split('\t')[0].substring(0, 10);
console.log(`✅ Found commit hash from git ls-remote: ${commitHash}`);
return commitHash;
console.log(`✅ Found commit hash from git: ${latestCommitHash}`);
} catch (gitError) {
console.warn(`❌ Git fetch failed: ${gitError}`);
try {
console.log(`📥 Trying alternative: git ls-remote origin ${targetBranch}`);
const remoteRef = (0, external_child_process_.execSync)(`git ls-remote origin ${targetBranch}`, {
encoding: 'utf8'
}).trim();
if (remoteRef) {
latestCommitHash = remoteRef.split('\t')[0].substring(0, 10);
console.log(`✅ Found commit hash from git ls-remote: ${latestCommitHash}`);
}
} catch (altError) {
console.warn(`❌ Alternative git command failed: ${altError}`);
}
} catch (altError) {
console.warn(`❌ Alternative git command failed: ${altError}`);
}
}
console.error(`❌ All methods to get target branch commit have failed`);
throw new Error(`Unable to get target branch (${targetBranch}) commit hash. Please ensure the branch exists and you have correct permissions.`);
if (!latestCommitHash) {
console.error(`❌ All methods to get target branch commit have failed`);
throw new Error(`Unable to get target branch (${targetBranch}) commit hash. Please ensure the branch exists and you have correct permissions.`);
}
console.log(`🔍 Checking if commit ${latestCommitHash} has baseline artifacts...`);
const hasArtifacts = await this.hasArtifactsForCommit(latestCommitHash);
if (hasArtifacts) {
console.log(`✅ Commit ${latestCommitHash} has baseline artifacts`);
return latestCommitHash;
}
console.log(`⚠️ Commit ${latestCommitHash} does not have baseline artifacts`);
console.log(`🔍 Looking for previous commits with baseline artifacts...`);
let currentCommit = latestCommitHash;
let checkedCommits = [
currentCommit
];
const maxDepth = 5;
for(let depth = 0; depth < maxDepth; depth++){
const parentCommit = await this.getParentCommit(currentCommit);
if (!parentCommit) {
console.log(`⚠️ Reached the beginning of the branch, no more parent commits`);
break;
}
if (checkedCommits.includes(parentCommit)) {
console.log(`⚠️ Detected circular reference, stopping search`);
break;
}
checkedCommits.push(parentCommit);
console.log(`🔍 Checking parent commit ${parentCommit}...`);
const parentHasArtifacts = await this.hasArtifactsForCommit(parentCommit);
if (parentHasArtifacts) {
console.log(`✅ Found commit ${parentCommit} with baseline artifacts`);
console.log(`\n⚠️ Note: The latest commit (${latestCommitHash}) does not have baseline artifacts.`);
console.log(` Using commit ${parentCommit} for baseline comparison instead.`);
console.log(" If this seems incorrect, please wait a few minutes and try rerunning the workflow.");
return parentCommit;
}
currentCommit = parentCommit;
}
console.log(`\n⚠️ No commits with baseline artifacts found in the last ${maxDepth} commits.`);
console.log(` Using latest commit ${latestCommitHash} anyway.`);
console.log(" Note: If baseline comparison fails, please wait a few minutes and try rerunning the workflow.");
return latestCommitHash;
} catch (error) {
console.error(`❌ Failed to get target branch commit: ${error}`);
console.error(`Repository: ${this.repository.owner}/${this.repository.repo}`);
Expand Down
Loading