Skip to content

Fix artifacts url parsing #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules/

# Editors
.vscode
.idea/

# Logs
logs
Expand Down
48 changes: 39 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35124,15 +35124,45 @@ async function run() {
// e.g., https://circleci.com/gh/mne-tools/mne-python/53315
// e.g., https://circleci.com/gh/scientific-python/circleci-artifacts-redirector-action/94?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link
// Set the new status
const parts = payload.target_url.split('?')[0].split('/')
const orgId = parts.slice(-3)[0]
const repoId = parts.slice(-2)[0]
const buildId = parts.slice(-1)[0]
core.debug(`org: ${orgId}`)
core.debug(`repo: ${repoId}`)
core.debug(`build: ${buildId}`)
// Get the URLs
const artifacts_url = `https://circleci.com/api/v2/project/gh/${orgId}/${repoId}/${buildId}/artifacts`
let artifacts_url = '';
const target = payload.target_url.split('?')[0]; // strip any ?utm=…
if (target.includes('/pipelines/circleci/')) {
// ───── New GitHub‑App URL ───────────────────────────────────────────
// .../pipelines/circleci/<org‑id>/<project‑id>/<pipe‑seq>/workflows/<workflow‑id>
const workflowId = target.split('/').pop();
core.debug(`workflow: ${workflowId}`);

// 1. Get the jobs that belong to this workflow
const jobsRes = await fetch(
`https://circleci.com/api/v2/workflow/${workflowId}/job`,
{ headers: { 'Circle-Token': apiToken } }
);
const jobs = await jobsRes.json();
if (!jobs.items.length) {
core.setFailed(`No jobs returned for workflow ${workflowId}`);
return;
}

// 2. Pick the first job
const job = jobs.items[0];
const projectSlug = job.project_slug; // "circleci/<org‑id>/<project‑id>"
const jobNumber = job.job_number;

core.debug(`slug: ${projectSlug}`);
core.debug(`job#: ${jobNumber}`);

// 3. Construct the v2 artifacts endpoint
artifacts_url = `https://circleci.com/api/v2/project/${projectSlug}/${jobNumber}/artifacts`;
} else {
// ───── Legacy OAuth URL (…/gh/<org>/<repo>/<build>) ────────────────
const parts = target.split('/');
const orgId = parts.slice(-3)[0];
const repoId = parts.slice(-2)[0];
const buildId = parts.slice(-1)[0];

artifacts_url =
`https://circleci.com/api/v2/project/gh/${orgId}/${repoId}/${buildId}/artifacts`;
}
core.debug(`Fetching JSON: ${artifacts_url}`)
if (apiToken == null || apiToken == '') {
apiToken = 'null'
Expand Down
48 changes: 39 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,45 @@ async function run() {
// e.g., https://circleci.com/gh/mne-tools/mne-python/53315
// e.g., https://circleci.com/gh/scientific-python/circleci-artifacts-redirector-action/94?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link
// Set the new status
const parts = payload.target_url.split('?')[0].split('/')
const orgId = parts.slice(-3)[0]
const repoId = parts.slice(-2)[0]
const buildId = parts.slice(-1)[0]
core.debug(`org: ${orgId}`)
core.debug(`repo: ${repoId}`)
core.debug(`build: ${buildId}`)
// Get the URLs
const artifacts_url = `https://circleci.com/api/v2/project/gh/${orgId}/${repoId}/${buildId}/artifacts`
let artifacts_url = '';
const target = payload.target_url.split('?')[0]; // strip any ?utm=…
if (target.includes('/pipelines/circleci/')) {
// ───── New GitHub‑App URL ───────────────────────────────────────────
// .../pipelines/circleci/<org‑id>/<project‑id>/<pipe‑seq>/workflows/<workflow‑id>
const workflowId = target.split('/').pop();
core.debug(`workflow: ${workflowId}`);

// 1. Get the jobs that belong to this workflow
const jobsRes = await fetch(
`https://circleci.com/api/v2/workflow/${workflowId}/job`,
{ headers: { 'Circle-Token': apiToken } }
);
const jobs = await jobsRes.json();
if (!jobs.items.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthieutrs you had if (!jobs.items?.length) { (note the ?) and eslint complained, so I removed the ?

core.setFailed(`No jobs returned for workflow ${workflowId}`);
return;
}

// 2. Pick the first job
const job = jobs.items[0];
const projectSlug = job.project_slug; // "circleci/<org‑id>/<project‑id>"
const jobNumber = job.job_number;

core.debug(`slug: ${projectSlug}`);
core.debug(`job#: ${jobNumber}`);

// 3. Construct the v2 artifacts endpoint
artifacts_url = `https://circleci.com/api/v2/project/${projectSlug}/${jobNumber}/artifacts`;
} else {
// ───── Legacy OAuth URL (…/gh/<org>/<repo>/<build>) ────────────────
const parts = target.split('/');
const orgId = parts.slice(-3)[0];
const repoId = parts.slice(-2)[0];
const buildId = parts.slice(-1)[0];

artifacts_url =
`https://circleci.com/api/v2/project/gh/${orgId}/${repoId}/${buildId}/artifacts`;
}
core.debug(`Fetching JSON: ${artifacts_url}`)
if (apiToken == null || apiToken == '') {
apiToken = 'null'
Expand Down