Adding releases to GitLab API with option for release asset download from either: source OR generic package releases (see https://gitlab.com/gitlab-org/release-cli/-/tree/master/docs/examples/release-assets-as-generic-package/)
This commit is contained in:
parent
2292442efc
commit
cb44a6ede1
|
|
@ -28,6 +28,16 @@ if ( !class_exists('Puc_v4p11_Vcs_GitLabApi', false) ):
|
|||
*/
|
||||
protected $accessToken;
|
||||
|
||||
/**
|
||||
* @var bool Whether to download release assets instead of the auto-generated source code archives.
|
||||
*/
|
||||
protected $releaseAssetsEnabled = false;
|
||||
|
||||
/**
|
||||
* @var bool Whether to download release asset package rather than release asset source.
|
||||
*/
|
||||
protected $releasePackageEnabled = false;
|
||||
|
||||
public function __construct($repositoryUrl, $accessToken = null, $subgroup = null) {
|
||||
//Parse the repository host to support custom hosts.
|
||||
$port = parse_url($repositoryUrl, PHP_URL_PORT);
|
||||
|
|
@ -94,7 +104,49 @@ if ( !class_exists('Puc_v4p11_Vcs_GitLabApi', false) ):
|
|||
* @return Puc_v4p11_Vcs_Reference|null
|
||||
*/
|
||||
public function getLatestRelease() {
|
||||
return $this->getLatestTag();
|
||||
$releases = $this->api('/:id/releases');
|
||||
if ( is_wp_error($releases) || empty($releases) || !is_array($releases) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$release = $releases[0];
|
||||
if ( is_wp_error($release) || !is_object($release) || !isset($release->tag_name) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$reference = new Puc_v4p11_Vcs_Reference(array(
|
||||
'name' => $release->tag_name,
|
||||
'version' => ltrim($release->tag_name, 'v'), //Remove the "v" prefix from "v1.2.3".
|
||||
'downloadUrl' => '',
|
||||
'updated' => $release->released_at,
|
||||
'apiResponse' => $release,
|
||||
));
|
||||
|
||||
if ( $this->isAuthenticationEnabled() ) {
|
||||
if ( $this->releasePackageEnabled && isset($release->assets, $release->assets->links) ) {
|
||||
/**
|
||||
* Use the first asset LINK file generated by a Gitlab Release Pipeline
|
||||
*
|
||||
* @link https://gist.github.com/timwiel/9dfd3526c768efad4973254085e065ce
|
||||
*/
|
||||
$download_url = $release->assets->links[0]->url;
|
||||
if ( ! empty( $this->accessToken ) ) {
|
||||
$download_url = add_query_arg('private_token', $this->accessToken, $download_url);
|
||||
}
|
||||
$reference->downloadUrl = $download_url;
|
||||
} elseif ( $this->releaseAssetsEnabled && isset($release->assets) ) {
|
||||
/**
|
||||
* Use the first asset SOURCE file from a Gitlab Release which should be a zip file
|
||||
*/
|
||||
$download_url = $release->assets->sources[0]->url;
|
||||
if ( ! empty( $this->accessToken ) ) {
|
||||
$download_url = add_query_arg('private_token', $this->accessToken, $download_url);
|
||||
}
|
||||
$reference->downloadUrl = $download_url;
|
||||
}
|
||||
}
|
||||
|
||||
return $reference;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -290,7 +342,12 @@ if ( !class_exists('Puc_v4p11_Vcs_GitLabApi', false) ):
|
|||
|
||||
// GitLab doesn't handle releases the same as GitHub so just use the latest tag
|
||||
if ( $configBranch === 'master' ) {
|
||||
$updateSource = $this->getLatestTag();
|
||||
//Use the latest release.
|
||||
$updateSource = $this->getLatestRelease();
|
||||
if ( $updateSource === null ) {
|
||||
//Failing that, use the tag with the highest version number.
|
||||
$updateSource = $this->getLatestTag();
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty($updateSource) ) {
|
||||
|
|
@ -304,6 +361,17 @@ if ( !class_exists('Puc_v4p11_Vcs_GitLabApi', false) ):
|
|||
parent::setAuthentication($credentials);
|
||||
$this->accessToken = is_string($credentials) ? $credentials : null;
|
||||
}
|
||||
|
||||
public function enableReleaseAssets() {
|
||||
$this->releaseAssetsEnabled = true;
|
||||
$this->releasePackageEnabled = false;
|
||||
}
|
||||
|
||||
public function enableReleasePackage() {
|
||||
$this->releaseAssetsEnabled = false;
|
||||
$this->releasePackageEnabled = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
endif;
|
||||
|
|
|
|||
Loading…
Reference in New Issue