diff --git a/Puc/v4p2/Vcs/GitLabApi.php b/Puc/v4p2/Vcs/GitLabApi.php index b8952d0..be3de09 100644 --- a/Puc/v4p2/Vcs/GitLabApi.php +++ b/Puc/v4p2/Vcs/GitLabApi.php @@ -50,8 +50,7 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @return Puc_v4p2_Vcs_Reference|null */ public function getLatestRelease() { - // GitLab doesn't use releases like GitHub, we should instead look through the tags for the latest one - // tagged as a release. + return $this->getLatestTag(); } /** @@ -71,24 +70,63 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : } $tag = $versionTags[0]; - return new Puc_v4p2_Vcs_Reference( array ( + return new Puc_v4p2_Vcs_Reference( array( 'name' => $tag->name, 'version' => ltrim( $tag->name, 'v' ), - 'downloadUrl' => '', + 'downloadUrl' => $this->buildArchiveDownloadUrl( $tag->name ), 'apiResponse' => $tag ) ); } + /** + * Get a branch by name. + * + * @param string $branchName + * @return null|Puc_v4p2_Vcs_Reference + */ public function getBranch( $branchName ) { - // todo + $branch = $this->api( '/:user/:repo/repository/branches/' . $branchName ); + if ( is_wp_error( $branch ) || empty( $branch ) ) { + return null; + } + + $reference = new Puc_v4p2_Vcs_Reference( array( + 'name' => $branch->name, + 'downloadUrl' => $this->buildArchiveDownloadUrl( $branch->name ), + 'apiResponse' => $branch, + ) ); + + if ( isset ( $branch->commit, $branch->commit->committed_date ) ) { + $reference->updated = $branch->commit->committed_date; + } + + return $reference; } + /** + * Get the latest commit that changed the specified file. + * + * @param string $filename + * @param string $ref Reference name (e.g. branch or tag). + * @return StdClass|null + */ public function getLatestCommit( $filename, $ref = 'master' ) { - // todo + throw new LogicException( 'The ' . __METHOD__ . ' method is not implemented and should not be used.' ); } + /** + * Get the timestamp of the latest commit that changed the specified branch or tag. + * + * @param string $ref Reference name (e.g. branch or tag). + * @return string|null + */ public function getLatestCommitTime( $ref ) { - // todo + $commits = $this->api( '/:user/:repo/repository/commits/', array( 'ref_name' => $ref ) ); + if ( is_wp_error( $commits ) || ! is_array( $commits ) || ! isset( $commits[0] ) ) { + return null; + } + + return $commits[0]->committed_date; } /** @@ -154,6 +192,13 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : return $url; } + /** + * Get the contents of a file from a specific branch or tag. + * + * @param string $path File name. + * @param string $ref + * @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error. + */ public function getRemoteFile( $path, $ref = 'master' ) { $response = $this->api( '/:user/:repo/repository/files/' . $path, array( 'ref' => $ref ) ); if ( is_wp_error( $response ) || ! isset( $response->content ) || $response->encoding !== 'base64' ) { @@ -163,18 +208,49 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : return base64_decode( $response->content ); } - public function getTag( $tagName ) { - // todo + /** + * Generate a URL to download a ZIP archive of the specified branch/tag/etc. + * + * @param string $ref + * @return string + */ + public function buildArchiveDownloadUrl( $ref = 'master' ) { + $url = sprintf( + 'https://%1$s/%2$s/%3$s/repository/%4$s/archive.zip', + $this->repositoryHost, + urlencode( $this->userName ), + urlencode( $this->repositoryName ), + urlencode( $ref ) ); + + if ( $this->accessToken ) { + $url = add_query_arg( 'private_token', $this->accessToken, $url ); + } + + return $url; } + /** + * Get a specific tag. + * + * @param string $tagName + * @return Puc_v4p2_Vcs_Reference|null + */ + public function getTag( $tagName ) { + throw new LogicException( 'The ' . __METHOD__ . ' method is not implemented and should not be used.' ); + } + + /** + * Figure out which reference (i.e tag or branch) contains the latest version. + * + * @param string $configBranch Start looking in this branch. + * @return null|Puc_v4p2_Vcs_Reference + */ public function chooseReference( $configBranch ) { $updateSource = null; + // GitLab doesn't handle releases the same as GitHub so just use the latest tag if ( $configBranch === 'master' ) { - // $updateSource = $this->getLatestRelease(); - // if ( $updateSource === null ) { $updateSource = $this->getLatestTag(); - // } } if ( empty( $updateSource ) ) { @@ -188,10 +264,6 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : parent::setAuthentication( $credentials ); $this->accessToken = is_string( $credentials ) ? $credentials : null; } - - private function getDownloadUrl( $file ) { - // todo - } } endif; \ No newline at end of file