Skip to content

Commit a442dd7

Browse files
authored
fix: fix the logic of finding artifacts (#18)
1 parent a92db9e commit a442dd7

File tree

4 files changed

+298
-42
lines changed

4 files changed

+298
-42
lines changed

dist/index.js

Lines changed: 117 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95819,6 +95819,76 @@ var __webpack_exports__ = {};
9581995819
});
9582095820
return artifactsResponse.data;
9582195821
}
95822+
async findWorkflowRunByCommit(commitHash, status = 'completed') {
95823+
const { owner, repo } = this.repository;
95824+
try {
95825+
const runsResponse = await this.octokit.rest.actions.listWorkflowRunsForRepo({
95826+
owner,
95827+
repo,
95828+
head_sha: commitHash,
95829+
status,
95830+
per_page: 10
95831+
});
95832+
if (runsResponse.data.workflow_runs && runsResponse.data.workflow_runs.length > 0) {
95833+
const successfulRun = runsResponse.data.workflow_runs.find((run)=>'success' === run.conclusion);
95834+
return successfulRun || runsResponse.data.workflow_runs[0];
95835+
}
95836+
const shortHash = commitHash.substring(0, 10);
95837+
const allRunsResponse = await this.octokit.rest.actions.listWorkflowRunsForRepo({
95838+
owner,
95839+
repo,
95840+
status,
95841+
per_page: 100
95842+
});
95843+
const matchingRun = allRunsResponse.data.workflow_runs?.find((run)=>run.head_sha.startsWith(shortHash) || run.head_sha.startsWith(commitHash));
95844+
return matchingRun || null;
95845+
} catch (error) {
95846+
const apiError = error;
95847+
console.warn(`⚠️ Failed to find workflow run for commit ${commitHash}: ${apiError.message}`);
95848+
return null;
95849+
}
95850+
}
95851+
async findAllWorkflowRunsByCommit(commitHash, status = 'completed') {
95852+
const { owner, repo } = this.repository;
95853+
try {
95854+
const runsResponse = await this.octokit.rest.actions.listWorkflowRunsForRepo({
95855+
owner,
95856+
repo,
95857+
head_sha: commitHash,
95858+
status,
95859+
per_page: 30
95860+
});
95861+
if (runsResponse.data.workflow_runs && runsResponse.data.workflow_runs.length > 0) return runsResponse.data.workflow_runs;
95862+
const shortHash = commitHash.substring(0, 10);
95863+
const allRunsResponse = await this.octokit.rest.actions.listWorkflowRunsForRepo({
95864+
owner,
95865+
repo,
95866+
status,
95867+
per_page: 100
95868+
});
95869+
const matchingRuns = allRunsResponse.data.workflow_runs?.filter((run)=>run.head_sha.startsWith(shortHash) || run.head_sha.startsWith(commitHash)) || [];
95870+
return matchingRuns;
95871+
} catch (error) {
95872+
const apiError = error;
95873+
console.warn(`⚠️ Failed to find workflow runs for commit ${commitHash}: ${apiError.message}`);
95874+
return [];
95875+
}
95876+
}
95877+
async listArtifactsForWorkflowRun(runId) {
95878+
const { owner, repo } = this.repository;
95879+
try {
95880+
const artifactsResponse = await this.octokit.rest.actions.listWorkflowRunArtifacts({
95881+
owner,
95882+
repo,
95883+
run_id: runId
95884+
});
95885+
return artifactsResponse.data;
95886+
} catch (error) {
95887+
const apiError = error;
95888+
console.warn(`⚠️ Failed to list artifacts for workflow run ${runId}: ${apiError.message}`);
95889+
throw error;
95890+
}
95891+
}
9582295892
async findArtifactByNamePattern(pattern) {
9582395893
const artifacts = await this.listArtifacts();
9582495894
console.log(`Looking for artifacts matching pattern: ${pattern}`);
@@ -95981,30 +96051,62 @@ var __webpack_exports__ = {};
9598196051
console.log(`📋 Searching for artifact with path hash and commit hash: ${expectedArtifactName}`);
9598296052
console.log(` Path hash: ${pathHash}`);
9598396053
console.log(` File path: ${relativePath}`);
95984-
const artifacts = await githubService.listArtifacts();
95985-
const artifact = artifacts.artifacts.find((a)=>a.name === expectedArtifactName);
96054+
console.log(`🔍 Looking for workflow runs with commit hash: ${commitHash}`);
96055+
const workflowRuns = await githubService.findAllWorkflowRunsByCommit(commitHash);
96056+
let artifact = null;
96057+
let artifacts = null;
96058+
if (workflowRuns && workflowRuns.length > 0) {
96059+
console.log(`✅ Found ${workflowRuns.length} workflow run(s) for commit ${commitHash}`);
96060+
for (const workflowRun of workflowRuns){
96061+
console.log(`\n🔍 Checking workflow run: ${workflowRun.id} (${workflowRun.name || 'unnamed'})`);
96062+
console.log(` Status: ${workflowRun.status}, Conclusion: ${workflowRun.conclusion}`);
96063+
try {
96064+
const runArtifacts = await githubService.listArtifactsForWorkflowRun(workflowRun.id);
96065+
const foundArtifact = runArtifacts.artifacts?.find((a)=>a.name === expectedArtifactName);
96066+
if (foundArtifact) {
96067+
artifact = foundArtifact;
96068+
artifacts = runArtifacts;
96069+
console.log(`✅ Found artifact in workflow run ${workflowRun.id}: ${artifact.name} (ID: ${artifact.id})`);
96070+
break;
96071+
}
96072+
{
96073+
const artifactNames = runArtifacts.artifacts?.map((a)=>a.name).join(', ') || 'none';
96074+
console.log(` ⚠️ Artifact not found. Available artifacts: ${artifactNames}`);
96075+
}
96076+
} catch (runArtifactsError) {
96077+
console.warn(` ⚠️ Failed to get artifacts from workflow run ${workflowRun.id}: ${runArtifactsError}`);
96078+
continue;
96079+
}
96080+
}
96081+
if (!artifact) {
96082+
console.log(`\n⚠️ Artifact not found in any of the ${workflowRuns.length} workflow runs`);
96083+
console.log(`🔄 Falling back to listing all repository artifacts...`);
96084+
}
96085+
} else {
96086+
console.log(`⚠️ No workflow runs found for commit ${commitHash}`);
96087+
console.log(`🔄 Falling back to listing all repository artifacts...`);
96088+
}
96089+
if (!artifact) {
96090+
artifacts = await githubService.listArtifacts();
96091+
artifact = artifacts.artifacts.find((a)=>a.name === expectedArtifactName);
96092+
}
9598696093
if (!artifact) {
9598796094
console.log(`❌ No artifact found matching: ${expectedArtifactName}`);
95988-
console.log(` Available artifacts: ${artifacts.artifacts.map((a)=>a.name).join(', ')}`);
96095+
if (artifacts?.artifacts) console.log(` Available artifacts: ${artifacts.artifacts.map((a)=>a.name).join(', ')}`);
9598996096
console.log(`💡 This might mean:`);
9599096097
console.log(" - The target branch hasn't been built yet");
9599196098
console.log(" - The artifact name pattern doesn't match");
9599296099
console.log(" - The artifact has expired (GitHub artifacts expire after 90 days)");
9599396100
throw new Error(`No artifact found matching: ${expectedArtifactName}`);
9599496101
}
9599596102
console.log(`✅ Found exact match: ${artifact.name} (ID: ${artifact.id})`);
95996-
try {
95997-
const artifacts = await githubService.listArtifacts();
95998-
const artifactDetails = artifacts.artifacts.find((a)=>a.id === artifact.id);
95999-
if (artifactDetails) {
96000-
console.log(`📊 Artifact details:`);
96001-
console.log(` - Created: ${artifactDetails.created_at}`);
96002-
console.log(` - Expired: ${artifactDetails.expired_at || 'Not expired'}`);
96003-
console.log(` - Size: ${artifactDetails.size_in_bytes} bytes`);
96004-
if (artifactDetails.expired_at) console.log(`⚠️ Warning: This artifact has expired and may not be downloadable`);
96005-
}
96006-
} catch (detailError) {
96007-
console.warn(`⚠️ Could not get artifact details: ${detailError || 'Unknown error'}`);
96103+
const artifactDetails = artifact;
96104+
if (artifactDetails) {
96105+
console.log(`📊 Artifact details:`);
96106+
console.log(` - Created: ${artifactDetails.created_at}`);
96107+
console.log(` - Expired: ${artifactDetails.expired_at || 'Not expired'}`);
96108+
console.log(` - Size: ${artifactDetails.size_in_bytes} bytes`);
96109+
if (artifactDetails.expired_at) console.log(`⚠️ Warning: This artifact has expired and may not be downloadable`);
9600896110
}
9600996111
console.log(`📥 Downloading artifact...`);
9601096112
try {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@
3939
"@actions/artifact": "^2.3.2",
4040
"@actions/core": "^1.2.6",
4141
"@actions/github": "^4.0.0",
42-
"@rsdoctor/cli": "1.3.3-beta.2",
4342
"@playwright/test": "^1.42.1",
43+
"@rsdoctor/cli": "1.3.3-beta.2",
4444
"@rsdoctor/client": "1.3.3-beta.2",
4545
"@rslib/core": "^0.16.0",
4646
"@rstest/core": "^0.5.4",
4747
"@types/node": "^24.5.2",
4848
"@types/node-fetch": "^2.6.11",
4949
"@types/yauzl": "^2.10.3",
50-
"mock-fs": "^5.2.0",
5150
"fast-glob": "^3.3.3",
51+
"mock-fs": "^5.2.0",
5252
"nock": "^13.5.4",
5353
"yauzl": "^3.2.0"
5454
},

src/download.ts

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,60 @@ export async function downloadArtifactByCommitHash(
112112
console.log(` Path hash: ${pathHash}`);
113113
console.log(` File path: ${relativePath}`);
114114

115-
// List all artifacts and find the exact match
116-
const artifacts = await githubService.listArtifacts();
117-
const artifact = artifacts.artifacts.find((a: any) => a.name === expectedArtifactName);
115+
// Try to find all workflow runs by commit hash first (more efficient)
116+
console.log(`🔍 Looking for workflow runs with commit hash: ${commitHash}`);
117+
const workflowRuns = await githubService.findAllWorkflowRunsByCommit(commitHash);
118+
119+
let artifact: any = null;
120+
let artifacts: any = null;
121+
122+
if (workflowRuns && workflowRuns.length > 0) {
123+
console.log(`✅ Found ${workflowRuns.length} workflow run(s) for commit ${commitHash}`);
124+
125+
// Search through all workflow runs, starting with the highest priority ones
126+
for (const workflowRun of workflowRuns) {
127+
console.log(`\n🔍 Checking workflow run: ${workflowRun.id} (${workflowRun.name || 'unnamed'})`);
128+
console.log(` Status: ${workflowRun.status}, Conclusion: ${workflowRun.conclusion}`);
129+
130+
try {
131+
const runArtifacts = await githubService.listArtifactsForWorkflowRun(workflowRun.id);
132+
const foundArtifact = runArtifacts.artifacts?.find((a: any) => a.name === expectedArtifactName);
133+
134+
if (foundArtifact) {
135+
artifact = foundArtifact;
136+
artifacts = runArtifacts;
137+
console.log(`✅ Found artifact in workflow run ${workflowRun.id}: ${artifact.name} (ID: ${artifact.id})`);
138+
break; // Found it, stop searching
139+
} else {
140+
const artifactNames = runArtifacts.artifacts?.map((a: any) => a.name).join(', ') || 'none';
141+
console.log(` ⚠️ Artifact not found. Available artifacts: ${artifactNames}`);
142+
}
143+
} catch (runArtifactsError) {
144+
console.warn(` ⚠️ Failed to get artifacts from workflow run ${workflowRun.id}: ${runArtifactsError}`);
145+
continue; // Try next workflow run
146+
}
147+
}
148+
149+
if (!artifact) {
150+
console.log(`\n⚠️ Artifact not found in any of the ${workflowRuns.length} workflow runs`);
151+
console.log(`🔄 Falling back to listing all repository artifacts...`);
152+
}
153+
} else {
154+
console.log(`⚠️ No workflow runs found for commit ${commitHash}`);
155+
console.log(`🔄 Falling back to listing all repository artifacts...`);
156+
}
157+
158+
// Fallback: if not found in any workflow run, search all repository artifacts
159+
if (!artifact) {
160+
artifacts = await githubService.listArtifacts();
161+
artifact = artifacts.artifacts.find((a: any) => a.name === expectedArtifactName);
162+
}
118163

119164
if (!artifact) {
120165
console.log(`❌ No artifact found matching: ${expectedArtifactName}`);
121-
console.log(` Available artifacts: ${artifacts.artifacts.map((a: any) => a.name).join(', ')}`);
166+
if (artifacts?.artifacts) {
167+
console.log(` Available artifacts: ${artifacts.artifacts.map((a: any) => a.name).join(', ')}`);
168+
}
122169
console.log(`💡 This might mean:`);
123170
console.log(` - The target branch hasn't been built yet`);
124171
console.log(` - The artifact name pattern doesn't match`);
@@ -128,28 +175,24 @@ export async function downloadArtifactByCommitHash(
128175

129176
console.log(`✅ Found exact match: ${artifact.name} (ID: ${artifact.id})`);
130177

131-
try {
132-
const artifacts = await githubService.listArtifacts();
133-
interface Artifact {
134-
id: number;
135-
name: string;
136-
created_at: string;
137-
expired_at?: string;
138-
size_in_bytes: number;
139-
}
140-
const artifactDetails = artifacts.artifacts.find((a: Artifact) => a.id === artifact.id);
141-
if (artifactDetails) {
142-
console.log(`📊 Artifact details:`);
143-
console.log(` - Created: ${artifactDetails.created_at}`);
144-
console.log(` - Expired: ${artifactDetails.expired_at || 'Not expired'}`);
145-
console.log(` - Size: ${artifactDetails.size_in_bytes} bytes`);
146-
147-
if (artifactDetails.expired_at) {
148-
console.log(`⚠️ Warning: This artifact has expired and may not be downloadable`);
149-
}
178+
// Display artifact details
179+
interface Artifact {
180+
id: number;
181+
name: string;
182+
created_at: string;
183+
expired_at?: string;
184+
size_in_bytes: number;
185+
}
186+
const artifactDetails = artifact as Artifact;
187+
if (artifactDetails) {
188+
console.log(`📊 Artifact details:`);
189+
console.log(` - Created: ${artifactDetails.created_at}`);
190+
console.log(` - Expired: ${artifactDetails.expired_at || 'Not expired'}`);
191+
console.log(` - Size: ${artifactDetails.size_in_bytes} bytes`);
192+
193+
if (artifactDetails.expired_at) {
194+
console.log(`⚠️ Warning: This artifact has expired and may not be downloadable`);
150195
}
151-
} catch (detailError) {
152-
console.warn(`⚠️ Could not get artifact details: ${detailError || 'Unknown error'}`);
153196
}
154197

155198
console.log(`📥 Downloading artifact...`);

0 commit comments

Comments
 (0)