|
7 | 7 |
|
8 | 8 | namespace Required\Traduttore\WebhookHandler; |
9 | 9 |
|
| 10 | +use Required\Traduttore\Project; |
10 | 11 | use Required\Traduttore\ProjectLocator; |
11 | 12 | use Required\Traduttore\Repository; |
12 | 13 | use Required\Traduttore\Updater; |
@@ -117,24 +118,25 @@ public function callback(): \WP_Error|\WP_REST_Response { |
117 | 118 | * @var array{repository: array{ default_branch?: string, html_url: string, full_name: string, ssh_url: string, clone_url: string, private: bool }, ref: string } $params |
118 | 119 | */ |
119 | 120 |
|
120 | | - if ( ! isset( $params['repository']['default_branch'] ) ) { |
121 | | - return new \WP_Error( '400', 'Request incomplete', [ 'status' => 400 ] ); |
122 | | - } |
| 121 | + $default_branch = (string) ( $params['repository']['default_branch'] ?? '' ); |
| 122 | + $html_url = (string) $params['repository']['html_url']; |
| 123 | + $ref = (string) $params['ref']; |
123 | 124 |
|
124 | | - // We only care about the default branch but don't want to send an error still. |
125 | | - if ( 'refs/heads/' . $params['repository']['default_branch'] !== $params['ref'] ) { |
126 | | - return new WP_REST_Response( [ 'result' => 'Not the default branch' ] ); |
| 125 | + if ( '' === $default_branch |
| 126 | + | '' === $html_url |
| 127 | + | '' === $ref |
| 128 | + ) { |
| 129 | + return new \WP_Error( '400', 'Request incomplete', [ 'status' => 400 ] ); |
127 | 130 | } |
128 | 131 |
|
129 | | - $locator = new ProjectLocator( $params['repository']['html_url'] ); |
130 | | - $project = $locator->get_project(); |
| 132 | + $project = $this->resolve_project( $html_url, $default_branch, $ref ); |
131 | 133 |
|
132 | | - if ( ! $project ) { |
133 | | - return new \WP_Error( '404', 'Could not find project for this repository', [ 'status' => 404 ] ); |
| 134 | + if ( ! $project instanceof Project ) { |
| 135 | + return $project; |
134 | 136 | } |
135 | 137 |
|
136 | 138 | $project->set_repository_name( $params['repository']['full_name'] ); |
137 | | - $project->set_repository_url( $params['repository']['html_url'] ); |
| 139 | + $project->set_repository_url( $html_url ); |
138 | 140 | $project->set_repository_ssh_url( $params['repository']['ssh_url'] ); |
139 | 141 | $project->set_repository_https_url( $params['repository']['clone_url'] ); |
140 | 142 | $project->set_repository_visibility( false === $params['repository']['private'] ? 'public' : 'private' ); |
|
0 commit comments