Created
August 16, 2023 03:32
-
-
Save Amitminer/1089176a1b83fdca45ebc14250d75d1f to your computer and use it in GitHub Desktop.
a script to remove all failed workflows(GitHub Actions).. written in PHP
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
<?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