Skip to content

Commit

Permalink
Merge pull request #435 from limikael/restful-commitish-endpoints
Browse files Browse the repository at this point in the history
Restful commitish endpoints
  • Loading branch information
afragen authored Aug 26, 2016
2 parents 9a51434 + c393cdc commit 2b24af9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ And our plugin is updated! The messages displayed are those that otherwise would

When using the RESTful endpoints for updating themes or plugins, you need to specify at least the `key` attribute, as well as one of the attributes `plugin`, `theme`, or `updates`. All other attributes are optional.

If the incoming request looks like it is push event from a [GitHub Webhook](https://developer.github.com/v3/activity/events/types/#pushevent), and if the pushed branch matches what is specified in the `tag` attribute, then the update will be made according to the latest push as specified in the event. This is done in order to avoid race conditions where the zip file for the branch might not have been created yet on GitHub.
The RESTful api is useful, among other things, for automatically updating themes and plugins on events sent as Webhooks from GitHub and the other services supported by this plugin. Some special functionality has been implemented to support this in order avoid race conditions, i.e. to make sure that the updated version is really the version that was just pushed to the repository. Specifically, what this functionality does is to see if the incoming request looks like it comes from a
[GitHub Webhook](https://developer.github.com/v3/activity/events/types/#pushevent) or a [Bitbucket Webhook](https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html#EventPayloads-Push). If this is the case, and if the branch that was pushed to matches the branch specified in the `tag` attribute, then the update will be made according to the latest push as specified in the event. Note that this is currently implemented for GitHub and Bitbucket only.

## Extended Naming

Expand Down
51 changes: 51 additions & 0 deletions src/GitHub_Updater/Rest_Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,52 @@ private function get_github_webhook_data() {
return $res;
}

/**
* See if a tag came in through a bitbucket webhook. It returns data
* on the same format as get_github_webhook_data.
*
* We assume here that changes contains one single entry, not sure if
* this is a safe assumption:
*
* http://stackoverflow.com/questions/39165255/how-do-i-get-the-latest-hash-from-a-bitbucket-push-payload-why-is-changes-an
*/
private function get_bitbucket_webhook_data() {
$request_body = file_get_contents('php://input');
$request_data = json_decode($request_body, TRUE);

if (!$request_data) {
return NULL;
}

if (!isset($request_data["push"]) ||
!isset($request_data["push"]["changes"])) {
return NULL;
}

$changes = $request_data["push"]["changes"];

if (!$changes || !sizeof($changes)) {
return NULL;
}

// Just use the first entry, assume that it is the right one.
$change = $changes[0];
$new = $change["new"];

// What else could this be? For now, just expect branch.
if ($new["type"] != "branch") {
return NULL;
}

$hash = $new["target"]["hash"];
$branch = $new["name"];

return array(
"branch" => $branch,
"hash" => $hash
);
}

/**
* Process request.
* Relies on data in $_REQUEST, prints out json and exits.
Expand Down Expand Up @@ -257,6 +303,11 @@ public function process_request() {
$tag = $hook_data["hash"];
}

$hook_data = $this->get_bitbucket_webhook_data();
if ($hook_data && $tag == $hook_data["branch"]) {
$tag = $hook_data["hash"];
}

if ( isset( $_REQUEST['plugin'] ) ) {
$this->update_plugin( $_REQUEST['plugin'], $tag );
} elseif ( isset( $_REQUEST['theme'] ) ) {
Expand Down

0 comments on commit 2b24af9

Please sign in to comment.