Skip to content

Instantly share code, notes, and snippets.

@Amitminer
Created August 16, 2023 03:32
Show Gist options
  • Save Amitminer/1089176a1b83fdca45ebc14250d75d1f to your computer and use it in GitHub Desktop.
Save Amitminer/1089176a1b83fdca45ebc14250d75d1f to your computer and use it in GitHub Desktop.
a script to remove all failed workflows(GitHub Actions).. written in PHP
<?php
class GitHubAPI {
private $token;
private $owner;
private $repo;
public function __construct($token, $owner, $repo) {
$this->token = $token;
$this->owner = $owner;
$this->repo = $repo;
}
private function sendRequest($url, $method, $data = []) {
$headers = array(
"Authorization: token {$this->token}",
"Accept: application/vnd.github.v3+json"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($method === "POST") {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
} elseif ($method === "DELETE") {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
public function getFailures() {
$url = "https://api.github.com/repos/{$this->owner}/{$this->repo}/actions/runs";
$data = array(
"status" => "failure",
"per_page" => 10
);
return $this->sendRequest($url, "GET", $data);
}
public function deleteRun($runId) {
$url = "https://api.github.com/repos/{$this->owner}/{$this->repo}/actions/runs/{$runId}";
return $this->sendRequest($url, "DELETE");
}
}
$token = 'your-github-access-tokem';
$owner = 'your-username';
$repo = 'your-repo';
$githubAPI = new GitHubAPI($token, $owner, $repo);
$failures = $githubAPI->getFailures();
foreach ($failures["workflow_runs"] as $rec) {
echo "Deleting {$rec['id']}\n";
$githubAPI->deleteRun($rec['id']);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment