Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
afragen committed Dec 1, 2018
2 parents d82520e + 5beb1ed commit 6b8b115
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 186 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#### [unreleased]

#### 8.5.1 / 2018-11-30
* refactor release asset API calls to `trait API_Common`
* updated GitLab API v4 endpoints, thanks for all the notice GitLab 😩

#### 8.5.0 / 2018-11-26
* silence rename PHP warning during plugin update
* specify branch for changelog
Expand Down
2 changes: 1 addition & 1 deletion github-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Plugin Name: GitHub Updater
* Plugin URI: https://github.com/afragen/github-updater
* Description: A plugin to automatically update GitHub, Bitbucket, GitLab, or Gitea hosted plugins, themes, and language packs. It also allows for remote installation of plugins or themes into WordPress.
* Version: 8.5.0
* Version: 8.5.1
* Author: Andy Fragen
* License: GNU General Public License v2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down
55 changes: 11 additions & 44 deletions src/GitHub_Updater/API/Bitbucket_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ public function get_remote_branches() {
return $this->get_remote_api_branches( 'bitbucket', '/2.0/repositories/:owner/:repo/refs/branches' );
}

/**
* Return the Bitbucket release asset URL.
*
* @return string
*/
public function get_release_asset() {
return $this->get_api_release_asset( 'bitbucket', '/2.0/repositories/:owner/:repo/downloads' );
}

/**
* Construct $this->type->download_link using Bitbucket API
*
Expand All @@ -163,6 +172,7 @@ public function construct_download_link( $branch_switch = false ) {
$download_link_base = $this->get_api_url( '/:owner/:repo/get/', true );
$endpoint = '';

// Release asset.
if ( $this->type->release_asset && '0.0.0' !== $this->type->newest_tag ) {
$release_asset = $this->get_release_asset();
return $this->get_release_asset_redirect( $release_asset, true );
Expand Down Expand Up @@ -200,41 +210,6 @@ public function construct_download_link( $branch_switch = false ) {
return $download_link_base . $endpoint;
}

/**
* Return the Bitbucket release asset URL.
*
* @return string|bool $download_link
*/
public function get_release_asset() {
self::$method = 'release_asset';
$response = isset( $this->response['release_asset'] ) ? $this->response['release_asset'] : false;

if ( $response && $this->exit_no_update( $response ) ) {
return false;
}

if ( ! $response ) {
$response = $this->api( '/2.0/repositories/:owner/:repo/downloads' );
$download_base = $this->get_api_url( '/2.0/repositories/:owner/:repo/downloads', true );
$response = isset( $response->values[0] ) && ! is_wp_error( $response ) ? $download_base . '/' . $response->values[0]->name : $response;

if ( ! $response && ! is_wp_error( $response ) ) {
$response = new \stdClass();
$response->message = 'No release asset found';
}
}

if ( $response && ! isset( $this->response['release_asset'] ) ) {
$this->set_repo_cache( 'release_asset', $response );
}

if ( $this->validate_response( $response ) ) {
return false;
}

return $response;
}

/**
* Added due to interface contract, not used for Bitbucket.
*
Expand Down Expand Up @@ -532,15 +507,7 @@ public function remote_install( $headers, $install ) {
}

if ( $bitbucket_org ) {
$install['download_link'] = implode(
'/',
[
$base,
$install['github_updater_repo'],
'get',
$install['github_updater_branch'] . '.zip',
]
);
$install['download_link'] = "{$base}/{$install['github_updater_repo']}/get/{$install['github_updater_branch']}.zip";
if ( isset( $install['is_private'] ) ) {
$install['options'][ $install['repo'] ] = 1;
}
Expand Down
62 changes: 12 additions & 50 deletions src/GitHub_Updater/API/GitHub_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public function get_remote_branches() {
return $this->get_remote_api_branches( 'github', '/repos/:owner/:repo/branches' );
}

/**
* Return the GitHub release asset URL.
*
* @return string|bool
*/
public function get_release_asset() {
return $this->get_api_release_asset( 'github', '/repos/:owner/:repo/releases/latest' );
}

/**
* Construct $this->type->download_link using Repository Contents API.
*
Expand All @@ -121,9 +130,7 @@ public function construct_download_link( $branch_switch = false ) {
$download_link_base = $this->get_api_url( '/repos/:owner/:repo/zipball/', true );
$endpoint = '';

/*
* If release asset.
*/
// Release asset.
if ( $this->type->release_asset && '0.0.0' !== $this->type->newest_tag ) {
$release_asset = $this->get_release_asset();
return $release_asset;
Expand All @@ -139,9 +146,7 @@ public function construct_download_link( $branch_switch = false ) {
$endpoint .= $this->type->newest_tag;
}

/*
* Create endpoint for branch switching.
*/
// Create endpoint for branch switching.
if ( $branch_switch ) {
$endpoint = $branch_switch;
}
Expand Down Expand Up @@ -318,40 +323,6 @@ protected function parse_tags( $response, $repo_type ) {
return [ $tags, $rollback ];
}

/**
* Return the GitHub release asset URL.
*
* @return string|bool
*/
public function get_release_asset() {
self::$method = 'release_asset';
$response = isset( $this->response['release_asset'] ) ? $this->response['release_asset'] : false;

if ( $response && $this->exit_no_update( $response ) ) {
return false;
}

if ( ! $response ) {
$response = $this->api( '/repos/:owner/:repo/releases/latest' );
$response = isset( $response->assets[0] ) && ! is_wp_error( $response ) ? $response->assets[0]->browser_download_url : $response;

if ( ! $response && ! is_wp_error( $response ) ) {
$response = new \stdClass();
$response->message = 'No release asset found';
}
}

if ( $response && ! isset( $this->response['release_asset'] ) ) {
$this->set_repo_cache( 'release_asset', $response );
}

if ( $this->validate_response( $response ) ) {
return false;
}

return $response;
}

/**
* Add settings for GitHub Personal Access Token.
*
Expand Down Expand Up @@ -497,16 +468,7 @@ public function remote_install( $headers, $install ) {
$github_com = false;
}

$install['download_link'] = implode(
'/',
[
$base,
'repos',
$install['github_updater_repo'],
'zipball',
$install['github_updater_branch'],
]
);
$install['download_link'] = "{$base}/repos/{$install['github_updater_repo']}/zipball/{$install['github_updater_branch']}";

// If asset is entered install it.
if ( false !== stripos( $headers['uri'], 'releases/download' ) ) {
Expand Down
101 changes: 34 additions & 67 deletions src/GitHub_Updater/API/GitLab_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,70 +168,48 @@ public function get_remote_branches() {
}

/**
* Construct $this->type->download_link using GitLab API.
* Get GitLab release asset download link.
*
* @return string|bool
*/
public function get_release_asset() {
return $this->get_api_release_asset( 'gitlab', "/projects/{$this->response['project_id']}/jobs/artifacts/{$this->type->newest_tag}/download" );
}

/**
* Construct $this->type->download_link using GitLab API v4.
*
* @param boolean $branch_switch for direct branch changing.
*
* @return string $endpoint
*/
public function construct_download_link( $branch_switch = false ) {
$download_link_base = $this->get_api_url( '/:owner/:repo/repository/archive.zip', true );
$endpoint = '';
self::$method = 'download_link';
$download_link_base = $this->get_api_url( "/projects/{$this->get_gitlab_id()}/repository/archive.zip" );
$download_link_base = remove_query_arg( 'private_token', $download_link_base );

/*
* If release asset.
*/
if ( $this->type->ci_job && '0.0.0' !== $this->type->newest_tag ) {
$release_asset = $this->get_release_asset();
$release_asset = $this->get_release_asset_redirect( $release_asset );
return $this->add_access_token_endpoint( $this, $release_asset );
}
$endpoint = '';
$endpoint = add_query_arg( 'sha', $this->type->branch, $endpoint );

/*
* If a branch has been given, use branch.
* If branch is master (default) and tags are used, use newest tag.
*/
// If branch is master (default) and tags are used, use newest tag.
if ( 'master' === $this->type->branch && ! empty( $this->type->tags ) ) {
$endpoint = remove_query_arg( 'ref', $endpoint );
$endpoint = add_query_arg( 'ref', $this->type->newest_tag, $endpoint );
} elseif ( ! empty( $this->type->branch ) ) {
$endpoint = remove_query_arg( 'ref', $endpoint );
$endpoint = add_query_arg( 'ref', $this->type->branch, $endpoint );
$endpoint = add_query_arg( 'sha', $this->type->newest_tag, $endpoint );
}

/*
* Create endpoint for branch switching.
*/
// Create endpoint for branch switching.
if ( $branch_switch ) {
$endpoint = remove_query_arg( 'ref', $endpoint );
$endpoint = add_query_arg( 'ref', $branch_switch, $endpoint );
$endpoint = add_query_arg( 'sha', $branch_switch, $endpoint );
}

$endpoint = $this->add_access_token_endpoint( $this, $endpoint );

return $download_link_base . $endpoint;
}

/**
* Get GitLab release asset download link.
*
* @return string $download_link
*/
public function get_release_asset() {
self::$method = 'release_asset';
$response = isset( $this->response['release_asset'] ) ? $this->response['release_asset'] : false;

if ( $response && $this->exit_no_update( $response ) ) {
return false;
// Release asset.
if ( $this->type->ci_job && '0.0.0' !== $this->type->newest_tag ) {
$release_asset = $this->get_release_asset();
return $release_asset;
}

if ( ! $response ) {
$response = $this->get_api_url( "/:owner/:repo/-/jobs/artifacts/{$this->type->newest_tag}/download", true );
$response = add_query_arg( 'job', $this->type->ci_job, $response );
$this->set_repo_cache( 'release_asset', $response );
}
$endpoint = $this->add_access_token_endpoint( $this, $endpoint );

return $response;
return $download_link_base . $endpoint;
}

/**
Expand All @@ -250,7 +228,7 @@ public function add_endpoints( $git, $endpoint ) {
case 'meta':
case 'tags':
case 'branches':
case 'release_asset':
case 'download_link':
break;
case 'file':
case 'changes':
Expand All @@ -260,6 +238,8 @@ public function add_endpoints( $git, $endpoint ) {
case 'translation':
$endpoint = add_query_arg( 'ref', 'master', $endpoint );
break;
case 'release_asset':
$endpoint = add_query_arg( 'job', $git->type->ci_job, $endpoint );
default:
break;
}
Expand Down Expand Up @@ -399,16 +379,9 @@ protected function parse_tags( $response, $repo_type ) {
$rollback = [];

foreach ( (array) $response as $tag ) {
$download_link = implode(
'/',
[
$repo_type['base_download'],
$this->type->owner,
$this->type->slug,
'repository/archive.zip',
]
);
$download_link = add_query_arg( 'ref', $tag, $download_link );
$download_link = "/projects/{$this->get_gitlab_id()}/repository/archive.zip";
$download_link = $this->get_api_url( $download_link );
$download_link = add_query_arg( 'sha', $tag, $download_link );
$tags[] = $tag;
$rollback[ $tag ] = $download_link;
}
Expand Down Expand Up @@ -597,15 +570,9 @@ public function remote_install( $headers, $install ) {
$gitlab_com = false;
}

$install['download_link'] = implode(
'/',
[
$base,
$install['github_updater_repo'],
'repository/archive.zip',
]
);
$install['download_link'] = add_query_arg( 'ref', $install['github_updater_branch'], $install['download_link'] );
$id = urlencode( $install['github_updater_repo'] );
$install['download_link'] = "{$base}/api/v4/projects/{$id}/repository/archive.zip";
$install['download_link'] = add_query_arg( 'sha', $install['github_updater_branch'], $install['download_link'] );

/*
* Add/Save access token if present.
Expand Down
Loading

0 comments on commit 6b8b115

Please sign in to comment.