Skip to content

Commit

Permalink
Merge pull request #1024 from hydephp/update-monorepo-scripts
Browse files Browse the repository at this point in the history
Internal: Update monorepo scripts
  • Loading branch information
caendesilva committed Feb 14, 2023
2 parents 0881bbe + 7440aba commit 4f58fd4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ jobs:

- name: Run Psalm
id: analysis
run: vendor/bin/psalm || echo EXIT_CODE=$? >> $GITHUB_OUTPUT || exit 0
run: vendor/bin/psalm > psalmout.txt || echo EXIT_CODE=$? >> $GITHUB_OUTPUT || exit 0

- name: Ping CI server with coverage results
run: php monorepo/scripts/ping-ci-server-with-type-coverage.php ${{ secrets.CI_SERVER_TOKEN }} ${{ github.event.pull_request.head.sha }} ${{ github.head_ref }}


# Since GitHub Actions for some reason doesn't support exiting with warnings, we work around this by
Expand Down
54 changes: 54 additions & 0 deletions monorepo/scripts/ping-ci-server-with-type-coverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

define('TIME_START', microtime(true));

/**
* @internal This script is used to ping the CI server with the type coverage results.
*
* @example php __FILE__ ${{ secrets.CI_SERVER_TOKEN }} ${{ github.event.pull_request.head.sha }} ${{ github.head_ref }}
*
* @uses vendor/bin/psalm > psalmout.txt
*/
echo "Pinging CI server\n";

$token = $argv[1] ?? exit(400);
$commit = $argv[2] ?? exit(400);
$branch = $argv[3] ?? 'master';

// Very inefficient, but it works fine for our purposes.
function getCoverage(string $contents): float
{
$lines = explode(PHP_EOL, $contents);
foreach ($lines as $line) {
if (str_starts_with($line, 'Psalm was able to infer types for ') && str_ends_with($line, '% of the codebase')) {
return (float) substr($line, 34, -16);
}
}
throw new \Exception('Could not find coverage in Psalm output');
}

$data = [
'commit' => $commit,
'coverage' => getCoverage(file_get_contents('psalmout.txt')),
'time_ms' => (microtime(true) - TIME_START) * 1000,
];

$url = 'https://ci.hydephp.se/api/github/actions/type-coverage';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = [
'Accept: application/json',
"Authorization: Bearer $token",
'Content-Type: application/json',
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
36 changes: 36 additions & 0 deletions monorepo/scripts/ping-openanalytics-testrunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @internal This script is used to ping the OpenAnalytics server with the test results.
*
* @example php ping.php 'Monorepo Smoke Tests' ${{ secrets.OPENANALYTICS_TOKEN }}
*
* @uses vendor/bin/pest --stop-on-failure --testdox-text testdox.txt
*/
echo "Pinging statistics server\n";

$runner = $argv[1] ?? exit(400);
$token = $argv[2] ?? exit(400);

$url = 'https://analytics.hydephp.se/api/test_runs';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = [
'Accept: application/json',
"Authorization: Bearer $token",
'Content-Type: application/json',
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
'runner' => json_encode($runner),
'tests' => substr_count(file_get_contents('testdox.txt') ?: exit(404), '[x]'),
]));

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

0 comments on commit 4f58fd4

Please sign in to comment.