mathieu/feature/Add Analytics UserProperties #76
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leka - iOS Monorepo | |
# Copyright APF France handicap | |
# SPDX-License-Identifier: Apache-2.0 | |
name: Cancel Workflows on PR Close or Label | |
on: | |
pull_request: | |
types: [closed, labeled] | |
jobs: | |
cancel-workflows: | |
runs-on: ubuntu-latest | |
permissions: | |
actions: write | |
pull-requests: read | |
steps: | |
- name: Cancel Running Workflows Based on Event | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const { owner, repo } = context.repo; | |
const eventName = context.eventName; | |
const action = context.payload.action; | |
const currentRunId = context.runId; | |
let prNumber; | |
let prHeadSha; | |
if (eventName === 'pull_request' && action === 'closed') { | |
// Event triggered by PR closure | |
prNumber = context.payload.pull_request.number; | |
prHeadSha = context.payload.pull_request.head.sha; | |
console.log(`Triggered by pull_request 'closed' event for PR #${prNumber}`); | |
} else if (eventName === 'pull_request' && action === 'labeled') { | |
// Event triggered by label addition | |
prNumber = context.payload.pull_request.number; | |
const labelName = context.payload.label.name; | |
console.log(`Triggered by pull_request 'labeled' event for PR #${prNumber}`); | |
console.log(`Label added: "${labelName}"`); | |
// Check if the label matches the one we're interested in | |
if (labelName !== '🛑️ cancel workflows') { | |
console.log('Label does not match. Exiting.'); | |
return; | |
} | |
prHeadSha = context.payload.pull_request.head.sha; | |
} else { | |
console.log('Unsupported event type or action. Exiting.'); | |
return; | |
} | |
console.log(`PR Number: ${prNumber}`); | |
console.log(`PR Head SHA: ${prHeadSha}`); | |
console.log(`Current Run ID: ${currentRunId}`); | |
// Fetch all in-progress and queued workflow runs | |
const statuses = ['in_progress', 'queued', 'requested', 'waiting', 'pending']; | |
let runs = []; | |
for (const status of statuses) { | |
const response = await github.rest.actions.listWorkflowRunsForRepo({ | |
owner: owner, | |
repo: repo, | |
status: status, | |
per_page: 100, | |
head_sha: prHeadSha, | |
}); | |
runs = runs.concat(response.data.workflow_runs); | |
} | |
console.log(`Total in-progress or queued runs: ${runs.length}`); | |
runs.forEach(run => { | |
const runInfo = [ | |
` - ID: ${run.id}`, | |
` Name: ${run.name}`, | |
` Head SHA: ${run.head_sha}`, | |
` Event: ${run.event}`, | |
` Status: ${run.status}` | |
]; | |
console.log(runInfo.join('\n')); | |
}); | |
// Cancel runs associated with the PR's head SHA, excluding the current run | |
for (const run of runs) { | |
if (run.id !== currentRunId && run.head_sha === prHeadSha) { | |
try { | |
await github.rest.actions.cancelWorkflowRun({ | |
owner, | |
repo, | |
run_id: run.id, | |
}); | |
console.log(`Cancelled workflow run: ${run.id}`); | |
} catch (error) { | |
console.error(`Failed to cancel workflow run: ${run.id}. Error: ${error.message}`); | |
} | |
} | |
} |