Skip to content

Commit 08f53fb

Browse files
schlesseraclaude
andcommitted
Test the GitHub API layer against recorded responses
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1e62739 commit 08f53fb

17 files changed

Lines changed: 3693 additions & 6 deletions

‎.maintenance/src/GitHub.php‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ class GitHub {
99
const API_ROOT = 'https://api.github.com/';
1010

1111
/**
12-
* Minimum number of seconds between two write requests, as GitHub asks
13-
* of integrations to stay clear of its secondary rate limits.
12+
* Default minimum number of seconds between two write requests, as GitHub
13+
* asks of integrations to stay clear of its secondary rate limits.
1414
*/
15-
const WRITE_INTERVAL = 1;
15+
const DEFAULT_WRITE_INTERVAL = 1.0;
16+
17+
/**
18+
* Minimum number of seconds between two write requests, or null until it
19+
* is resolved from the WP_CLI_GITHUB_WRITE_INTERVAL environment variable.
20+
*
21+
* @var float|null
22+
*/
23+
private static $write_interval;
1624

1725
/**
1826
* Credentials in use, resolved on first use.
@@ -589,10 +597,38 @@ private static function get_error_hint( $code, $source ) {
589597
}
590598

591599
/**
592-
* Waits until enough time has passed since the last write request.
600+
* Gets the minimum number of seconds between two write requests.
601+
*
602+
* Set the WP_CLI_GITHUB_WRITE_INTERVAL environment variable to space the
603+
* writes out further when GitHub still applies secondary rate limits.
604+
*
605+
* @return float
606+
*/
607+
public static function get_write_interval() {
608+
if ( null === self::$write_interval ) {
609+
$interval = getenv( 'WP_CLI_GITHUB_WRITE_INTERVAL' );
610+
self::$write_interval = is_numeric( $interval ) && $interval >= 0 ? (float) $interval : self::DEFAULT_WRITE_INTERVAL;
611+
}
612+
613+
return self::$write_interval;
614+
}
615+
616+
/**
617+
* Sets the minimum number of seconds between two write requests, such as
618+
* 0 for tests against a fake API.
619+
*
620+
* @param float $seconds
621+
*/
622+
public static function set_write_interval( $seconds ) {
623+
self::$write_interval = max( 0.0, (float) $seconds );
624+
}
625+
626+
/**
627+
* Waits until the write interval has passed since the last write request.
628+
* When it already has, the write goes out right away.
593629
*/
594630
private static function pace_writes() {
595-
$wait = self::$last_write + self::WRITE_INTERVAL - microtime( true );
631+
$wait = self::$last_write + self::get_write_interval() - microtime( true );
596632

597633
if ( $wait > 0 ) {
598634
usleep( (int) ( $wait * 1000000 ) );

‎.typos.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extend-exclude = [
1515
"process/*",
1616
"restful/*",
1717
"spyc/*",
18+
"tests/fixtures/*",
1819
"vendor/*",
1920
"wordpress-core/*",
2021
"wp-cli/*",

‎composer.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@
312312
},
313313
"autoload-dev": {
314314
"psr-4": {
315-
"WP_CLI\\Maintenance\\": ".maintenance/src/"
315+
"WP_CLI\\Maintenance\\": ".maintenance/src/",
316+
"WP_CLI\\Maintenance\\Tests\\": "tests/includes/"
316317
},
317318
"files": [
318319
".maintenance/bootstrap.php"
@@ -338,6 +339,7 @@
338339
"phpstan": "run-phpstan-tests",
339340
"phpunit": "run-php-unit-tests",
340341
"prepare-tests": "install-package-tests",
342+
"update-github-fixtures": "php tests/fixtures/github/update.php",
341343
"test": [
342344
"@lint",
343345
"@phpcs",

‎tests/GitHubApiTest.php‎

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
use WP_CLI\ExitException;
4+
use WP_CLI\Maintenance\GitHub;
5+
use WP_CLI\Maintenance\Tests\GitHub_TestCase;
6+
7+
class GitHubApiTest extends GitHub_TestCase {
8+
9+
public function test_lists_follow_the_pagination_links(): void {
10+
$this->respond(
11+
'GET',
12+
'repos/wp-cli/block-command/tags?per_page=100',
13+
$this->fixture( 'tags-page-1' ),
14+
200,
15+
[ 'Link' => '<https://api.github.com/repositories/1/tags?per_page=100&page=2>; rel="next", <https://api.github.com/repositories/1/tags?per_page=100&page=2>; rel="last"' ]
16+
);
17+
$this->respond( 'GET', 'repositories/1/tags?page=2', $this->fixture( 'tags-page-2' ) );
18+
19+
$tags = array_column( GitHub::get_tags( 'wp-cli/block-command' ), 'name' );
20+
21+
$this->assertSame(
22+
array_merge(
23+
array_column( $this->fixture( 'tags-page-1' ), 'name' ),
24+
array_column( $this->fixture( 'tags-page-2' ), 'name' )
25+
),
26+
$tags
27+
);
28+
$this->assertCount( 2, $this->requests );
29+
}
30+
31+
public function test_a_missing_release_is_null(): void {
32+
$this->respond( 'GET', 'repos/wp-cli/block-command/releases/tags/v0.0.0', $this->fixture( 'release-absent' ), 404 );
33+
34+
$this->assertNull( GitHub::get_release_by_tag( 'wp-cli/block-command', 'v0.0.0' ) );
35+
}
36+
37+
public function test_a_failed_request_stops_with_an_error_code(): void {
38+
$this->respond(
39+
'GET',
40+
'repos/wp-cli/block-command',
41+
[ 'message' => 'API rate limit exceeded' ],
42+
403,
43+
[ 'X-RateLimit-Remaining' => '0' ]
44+
);
45+
46+
try {
47+
GitHub::get_default_branch( 'wp-cli/block-command' );
48+
$this->fail( 'The request should have failed.' );
49+
} catch ( ExitException $exception ) {
50+
$this->assertSame( 1, $exception->getCode() );
51+
}
52+
53+
$this->assertStringContainsString( 'GitHub API request failed (rate_limited)', $this->logger->stderr );
54+
}
55+
56+
public function test_writes_are_sent_as_json(): void {
57+
$this->respond( 'PATCH', 'repos/wp-cli/block-command/milestones/4', $this->fixture( 'milestone' ) );
58+
59+
GitHub::close_milestone( 'wp-cli/block-command', 4 );
60+
61+
$this->assertSame( [ 'PATCH https://api.github.com/repos/wp-cli/block-command/milestones/4' ], $this->requests );
62+
$this->assertEquals( (object) [ 'state' => 'closed' ], $this->sent_data[0] );
63+
}
64+
65+
public function test_writes_need_credentials(): void {
66+
$auth = new ReflectionProperty( GitHub::class, 'auth' );
67+
if ( PHP_VERSION_ID < 80100 ) {
68+
$auth->setAccessible( true );
69+
}
70+
$auth->setValue(
71+
null,
72+
[
73+
'token' => null,
74+
'source' => null,
75+
]
76+
);
77+
78+
try {
79+
GitHub::close_milestone( 'wp-cli/block-command', 4 );
80+
$this->fail( 'The write should have been refused.' );
81+
} catch ( ExitException $exception ) {
82+
$this->assertSame( [], $this->requests );
83+
}
84+
85+
$this->assertStringContainsString( 'No GitHub credentials found', $this->logger->stderr );
86+
}
87+
88+
public function test_writes_wait_only_for_what_is_left_of_the_interval(): void {
89+
$this->respond( 'PATCH', 'repos/wp-cli/block-command/milestones/4', $this->fixture( 'milestone' ) );
90+
GitHub::set_write_interval( 0.2 );
91+
92+
$start = microtime( true );
93+
GitHub::close_milestone( 'wp-cli/block-command', 4 );
94+
GitHub::close_milestone( 'wp-cli/block-command', 4 );
95+
$paced = microtime( true ) - $start;
96+
97+
usleep( 250000 );
98+
99+
$start = microtime( true );
100+
GitHub::close_milestone( 'wp-cli/block-command', 4 );
101+
$unpaced = microtime( true ) - $start;
102+
103+
$this->assertGreaterThanOrEqual( 0.19, $paced, 'The second write should wait for the interval.' );
104+
$this->assertLessThan( 0.1, $unpaced, 'A write after the interval has passed should not wait.' );
105+
}
106+
107+
public function test_merged_pull_requests_since_a_tag(): void {
108+
$comparison = $this->fixture( 'comparison' );
109+
$tagged_at = $comparison->base_commit->commit->committer->date;
110+
$in_range = $comparison->commits[0]->sha;
111+
112+
$this->respond( 'GET', 'repos/wp-cli/block-command/compare/v1.0.1...main', $comparison );
113+
$this->respond(
114+
'GET',
115+
'repos/wp-cli/block-command/pulls?state=closed&base=main',
116+
[
117+
$this->pull_request( 0, 30, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', $in_range ),
118+
// Merged into another branch, so its merge commit is not on main.
119+
$this->pull_request( 1, 29, '2026-08-01T00:00:00Z', '2026-08-01T00:00:00Z', str_repeat( 'f', 40 ) ),
120+
// Closed without merging.
121+
$this->pull_request( 2, 28, '2026-07-01T00:00:00Z', null, null ),
122+
// Last updated before the tag, which ends the listing.
123+
$this->pull_request( 0, 27, '2026-01-01T00:00:00Z', '2026-01-01T00:00:00Z', $in_range ),
124+
],
125+
200,
126+
[ 'Link' => '<https://api.github.com/repositories/1/pulls?page=2>; rel="next"' ]
127+
);
128+
129+
$pull_requests = GitHub::get_pull_requests_merged_since( 'wp-cli/block-command', 'v1.0.1', 'main' );
130+
131+
$this->assertGreaterThan( '2026-01-01T00:00:00Z', $tagged_at );
132+
$this->assertSame( [ 30 ], array_column( $pull_requests, 'number' ) );
133+
// The second page is never requested.
134+
$this->assertCount( 2, $this->requests );
135+
}
136+
137+
public function test_merged_pull_requests_fall_back_to_the_merge_date_past_250_commits(): void {
138+
$comparison = $this->fixture( 'comparison' );
139+
$comparison->total_commits = 300;
140+
141+
$this->respond( 'GET', 'repos/wp-cli/block-command/compare/v1.0.1...main', $comparison );
142+
$this->respond(
143+
'GET',
144+
'repos/wp-cli/block-command/pulls?state=closed&base=main',
145+
[ $this->pull_request( 0, 30, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', str_repeat( 'f', 40 ) ) ]
146+
);
147+
148+
$pull_requests = GitHub::get_pull_requests_merged_since( 'wp-cli/block-command', 'v1.0.1', 'main' );
149+
150+
$this->assertSame( [ 30 ], array_column( $pull_requests, 'number' ) );
151+
}
152+
153+
private function pull_request( $index, $number, $updated_at, $merged_at, $merge_commit_sha ) {
154+
return $this->fixture_object(
155+
'pull-requests',
156+
$index,
157+
[
158+
'number' => $number,
159+
'updated_at' => $updated_at,
160+
'merged_at' => $merged_at,
161+
'merge_commit_sha' => $merge_commit_sha,
162+
]
163+
);
164+
}
165+
}

0 commit comments

Comments
 (0)