|
| 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