@@ -768,7 +768,7 @@ function devshop_github_form_devshop_project_create_step_environments_alter(&$fo
768768 // Load the PRs.
769769 try {
770770 $prs = devshop_github_client()->pullRequests()->all($project->github_owner, $project->github_repo);
771-
771+ $settings_link = '';
772772 $environment_forms = array();
773773 if (!empty($project->settings->github['pull_request_environments'])) {
774774
@@ -1569,131 +1569,132 @@ function devshop_github_webhook($project_node) {
15691569 $environment = $project->environments[$environment_name];
15701570
15711571 // Environment might have been deleted.
1572- if (empty($environment)) {
1572+ if (! empty($environment)) {
15731573 $message .= "Environment $environment_name not found in project $project->name.";
1574- continue;
15751574 }
1575+ else {
15761576
1577- $message .= "About to set deployment status for repo $owner/$repo using environment $environment_name \n";
1578- $message .= "Environment: " . print_r($environment, 1);
1579-
1580- try {
1581- $token = variable_get('devshop_github_token', '');
1582- $client = new \Github\Client();
1583- $client->authenticate($token, \Github\AuthMethod::ACCESS_TOKEN);
1584-
1585- $sha = $data->pull_request->head->sha;
1586-
1587- $params = new stdClass();
1588- $params->ref = $sha;
1589- $params->environment = $environment->url;
1590- $params->required_contexts = array();
1591- $post_url = "/repos/$owner/$repo/deployments";
1592- $deployment = json_decode($client->getHttpClient()->post($post_url, json_encode($params))->getBody(TRUE));
1577+ $message .= "About to set deployment status for repo $owner/$repo using environment $environment_name \n";
1578+ $message .= "Environment: " . print_r($environment, 1);
15931579
1594- // Save deployment to pull request data for later access.
1595- $data->pull_request->deployment = $deployment;
1580+ try {
1581+ $token = variable_get('devshop_github_token', '');
1582+ $client = new \Github\Client();
1583+ $client->authenticate($token, \Github\AuthMethod::ACCESS_TOKEN);
15961584
1597- $message .= " Deployment Created! \n" ;
1585+ $sha = $data->pull_request->head->sha ;
15981586
1599- // Create deployment status
1600- $deployment_id = $deployment->id;
1587+ $params = new stdClass();
1588+ $params->ref = $sha;
1589+ $params->environment = $environment->url;
1590+ $params->required_contexts = array();
1591+ $post_url = "/repos/$owner/$repo/deployments";
1592+ $deployment = json_decode($client->getHttpClient()->post($post_url, json_encode($params))->getBody(TRUE));
16011593
1602- $params = new stdClass();
1603- $params->state = 'pending';
1604- $params->target_url = $environment->url;
1605- $params->description = t('Code is being deployed. Please stand by.');
1594+ // Save deployment to pull request data for later access.
1595+ $data->pull_request->deployment = $deployment;
16061596
1607- $post_url = "/repos/$owner/$repo/deployments/{$deployment_id}/statuses";
1608- $message .= "Attempting to create deployment status: $post_url \n";
1597+ $message .= " Deployment Created! \n";
16091598
1610- $deployment_status = $client->getHttpClient()->post($post_url, json_encode($params));
1599+ // Create deployment status
1600+ $deployment_id = $deployment->id;
16111601
1612- $message .= " Deployment Status Created! \n";
1602+ $params = new stdClass();
1603+ $params->state = 'pending';
1604+ $params->target_url = $environment->url;
1605+ $params->description = t('Code is being deployed. Please stand by.');
16131606
1614- // Set a commit status for this REF for devshop/deploy context
1615- $sha = $data->pull_request->head->sha ;
1607+ $post_url = "/repos/$owner/$repo/deployments/{$deployment_id}/statuses";
1608+ $message .= "Attempting to create deployment status: $post_url \n" ;
16161609
1617- $params = new stdClass();
1618- $params->state = 'pending';
1619- $params->target_url = url("node/$project->nid", array('absolute' => TRUE));
1620- $params->description = t('DevShop: Deploy');
1621- $params->context = "devshop/{$project->name}/deploy";
1610+ $deployment_status = $client->getHttpClient()->post($post_url, json_encode($params));
16221611
1623- // Post status to github
1624- $status = $client->getHttpClient()->post("/repos/$owner/$repo/statuses/$sha", json_encode($params));
1612+ $message .= " Deployment Status Created! \n";
16251613
1626- $message .= " Commit Status Created! \n";
1614+ // Set a commit status for this REF for devshop/deploy context
1615+ $sha = $data->pull_request->head->sha;
16271616
1628- // If environment is configured to run tests, add another status.
1629- if (!empty($environment->settings->deploy['test'])) {
16301617 $params = new stdClass();
16311618 $params->state = 'pending';
1632-
1633- // @TODO: Add the link to the last "test" task here instead of the project.
16341619 $params->target_url = url("node/$project->nid", array('absolute' => TRUE));
1635- $params->description = t('DevShop: Run Tests ');
1636- $params->context = "devshop/{$project->name}/tests ";
1620+ $params->description = t('DevShop: Deploy ');
1621+ $params->context = "devshop/{$project->name}/deploy ";
16371622
16381623 // Post status to github
16391624 $status = $client->getHttpClient()->post("/repos/$owner/$repo/statuses/$sha", json_encode($params));
16401625
1641- $message .= " Commit Status Created for test runs! \n";
1626+ $message .= " Commit Status Created! \n";
1627+
1628+ // If environment is configured to run tests, add another status.
1629+ if (!empty($environment->settings->deploy['test'])) {
1630+ $params = new stdClass();
1631+ $params->state = 'pending';
1632+
1633+ // @TODO: Add the link to the last "test" task here instead of the project.
1634+ $params->target_url = url("node/$project->nid", array('absolute' => TRUE));
1635+ $params->description = t('DevShop: Run Tests');
1636+ $params->context = "devshop/{$project->name}/tests";
1637+
1638+ // Post status to github
1639+ $status = $client->getHttpClient()->post("/repos/$owner/$repo/statuses/$sha", json_encode($params));
1640+
1641+ $message .= " Commit Status Created for test runs! \n";
1642+ }
1643+
1644+ } catch (Github\Exception\RuntimeException $e) {
1645+ $log = 'GitHub API Error during PR Syncronize: ' . $e->getMessage() . $e->getCode();
1646+ watchdog('devshop_github', $log);
1647+
1648+ if ($e->getCode() == '409') {
1649+ $log .= "\n Out of date! alert the developer!";
1650+
1651+ // Send a failed commit status to alert to developer
1652+ $params = new stdClass();
1653+ $params->state = 'failure';
1654+ $params->target_url = $project->git_repo_url;
1655+ $params->description = t('Branch is out of date! Merge from default branch.');
1656+ $params->context = "devshop/{$project->name}/merge";
1657+
1658+ // Post status to github
1659+ $status = $client->getHttpClient()->post("/repos/$owner/$repo/statuses/$sha", json_encode($params));
1660+ }
1661+ else {
1662+ http_response_code(500);
1663+ }
1664+ $message .= $log . "\n";
16421665 }
16431666
1644- } catch (Github\Exception\RuntimeException $e) {
1645- $log = 'GitHub API Error during PR Syncronize: ' . $e->getMessage() . $e->getCode();
1646- watchdog('devshop_github', $log);
16471667
1648- if ($e->getCode() == '409') {
1649- $log .= "\n Out of date! alert the developer!";
1668+ // Update the PR record
1669+ $info = new stdClass();
1670+ $info->site_nid = $environment->site;
1671+ $info->id = $data->pull_request->id;
1672+ $info->number = $data->pull_request->number;
1673+ $info->pull_request_object = serialize($data->pull_request);
16501674
1651- // Send a failed commit status to alert to developer
1652- $params = new stdClass();
1653- $params->state = 'failure';
1654- $params->target_url = $project->git_repo_url;
1655- $params->description = t('Branch is out of date! Merge from default branch.');
1656- $params->context = "devshop/{$project->name}/merge" ;
1675+ // Save environment record.
1676+ $results = db_select('hosting_devshop_github_pull_requests', 'pr')
1677+ ->condition('id', $data->pull_request->id)
1678+ ->fields('pr', array('id'))
1679+ ->execute()
1680+ ->fetchAssoc() ;
16571681
1658- // Post status to github
1659- $status = $client->getHttpClient()->post("/repos/$owner/$repo/statuses/$sha", json_encode($params));
1682+ // If there are results, save $update param for drupal_write_record().
1683+ if (empty($results)) {
1684+ $update = array();
16601685 }
16611686 else {
1662- http_response_code(500 );
1687+ $update = array('id' );
16631688 }
1664- $message .= $log . "\n";
1665- }
1666-
1667-
1668- // Update the PR record
1669- $info = new stdClass();
1670- $info->site_nid = $environment->site;
1671- $info->id = $data->pull_request->id;
1672- $info->number = $data->pull_request->number;
1673- $info->pull_request_object = serialize($data->pull_request);
1674-
1675- // Save environment record.
1676- $results = db_select('hosting_devshop_github_pull_requests', 'pr')
1677- ->condition('id', $data->pull_request->id)
1678- ->fields('pr', array('id'))
1679- ->execute()
1680- ->fetchAssoc();
1681-
1682- // If there are results, save $update param for drupal_write_record().
1683- if (empty($results)) {
1684- $update = array();
1685- }
1686- else {
1687- $update = array('id');
1688- }
16891689
1690- // Save environment record.
1691- try {
1692- drupal_write_record('hosting_devshop_github_pull_requests', $info, $update);
1693- $message .= ' ' . t('Pull Request info saved to DevShop.');
1694- }
1695- catch (\PDOException $e) {
1696- $message .= 'Saving PR record failed: ' . $e->getMessage();
1690+ // Save environment record.
1691+ try {
1692+ drupal_write_record('hosting_devshop_github_pull_requests', $info, $update);
1693+ $message .= ' ' . t('Pull Request info saved to DevShop.');
1694+ }
1695+ catch (\PDOException $e) {
1696+ $message .= 'Saving PR record failed: ' . $e->getMessage();
1697+ }
16971698 }
16981699 }
16991700 // When PR is closed, delete environment.
0 commit comments