Commit: 44706d2e93c7d334710b9f1b202005fea6f9a770 Author: Peter Kokot <peterko...@gmail.com> Wed, 5 Dec 2018 17:19:08 +0100 Parents: 73062503a65bfa62d5f2f365ee99269225580bb9 Branches: master
Link: http://git.php.net/?p=web/bugs.git;a=commitdiff;h=44706d2e93c7d334710b9f1b202005fea6f9a770 Log: Refactor pull requests service class Changes: - Class moved to src under App namespace - Code refactored into repository class and GitHub client for managing pull requests - When new pull request is attached to a bug report the info comment is also added to bug activity as is practice with other activities already. Changed paths: D include/classes/bug_ghpulltracker.php A src/Repository/PullRequestRepository.php A src/Utils/GitHub.php M www/bug.php M www/gh-pull-add.php
diff --git a/include/classes/bug_ghpulltracker.php b/include/classes/bug_ghpulltracker.php deleted file mode 100644 index 6ed0540..0000000 --- a/include/classes/bug_ghpulltracker.php +++ /dev/null @@ -1,85 +0,0 @@ -<?php - -class Bug_Pulltracker -{ - private $dbh; - private $userAgent = 'bugs.php.net Pulltracker'; - - public function __construct() - { - $this->dbh = $GLOBALS['dbh']; - } - - private function getDataFromGithub($repo, $pull_id) - { - $ctxt = stream_context_create([ - 'http' => [ - 'ignore_errors' => '1', - 'user_agent' => $this->userAgent, - ] - ]); - $data = @json_decode(file_get_contents("https://api.github.com/repos/php/".urlencode($repo).'/pulls/'.((int)$pull_id), null, $ctxt)); - if (!is_object($data)) { - return false; - } - return $data; - } - - /** - * Attach a pull request to this bug - */ - public function attach($bugid, $repo, $pull_id, $developer) - { - $data = $this->getDataFromGithub($repo, $pull_id); - - if (!$data) { - throw new \Exception('Failed to retrieve pull request from GitHub'); - } - - $sql = 'INSERT INTO bugdb_pulls - (bugdb_id, github_repo, github_pull_id, github_title, github_html_url, developer) - VALUES (?, ?, ?, ?, ?, ?) - '; - - $arguments = [ - $bugid, - $repo, - $pull_id, - $data->title, - $data->html_url, - $developer, - ]; - - $this->dbh->prepare($sql)->execute($arguments); - - return $data; - } - - /** - * Remove a pull request from this bug - */ - public function detach($bugid, $repo, $pull_id) - { - $this->dbh->prepare('DELETE FROM bugdb_pulls - WHERE bugdb_id = ? and github_repo = ? and github_pull_id = ?')->execute( - [$bugid, $repo, $pull_id]); - } - - /** - * Retrieve a listing of all pull requests - * - * @param int $bugid - * @return array - */ - public function listPulls($bugid) - { - $query = ' - SELECT github_repo, github_pull_id, github_title, github_html_url, developer - FROM bugdb_pulls - WHERE bugdb_id = ? - ORDER BY github_repo, github_pull_id DESC - '; - - return $this->dbh->prepare($query)->execute([$bugid])->fetchAll(PDO::FETCH_ASSOC); - } -} diff --git a/src/Repository/PullRequestRepository.php b/src/Repository/PullRequestRepository.php new file mode 100644 index 0000000..64ab66a --- /dev/null +++ b/src/Repository/PullRequestRepository.php @@ -0,0 +1,39 @@ +<?php + +namespace App\Repository; + +/** + * Repository class for retrieving data from the bugdb_pulls database table. + */ +class PullRequestRepository +{ + /** + * Database handler. + */ + private $dbh; + + /** + * Class constructor. + */ + public function __construct($dbh) + { + $this->dbh = $dbh; + } + + /** + * Retrieve all pull requests by bug id. + * + * @param int $bugId + * @return array + */ + public function findAllByBugId(int $bugId) + { + $sql = 'SELECT github_repo, github_pull_id, github_title, github_html_url, developer + FROM bugdb_pulls + WHERE bugdb_id = ? + ORDER BY github_repo, github_pull_id DESC + '; + + return $this->dbh->prepare($sql)->execute([$bugId])->fetchAll(); + } +} diff --git a/src/Utils/GitHub.php b/src/Utils/GitHub.php new file mode 100644 index 0000000..c551959 --- /dev/null +++ b/src/Utils/GitHub.php @@ -0,0 +1,101 @@ +<?php + +namespace App\Utils; + +/** + * GitHub pull requests tracker client. + */ +class GitHub +{ + /** + * Database handler. + */ + private $dbh; + + /** + * API URL. + */ + private $url = 'https://api.github.com'; + + /** + * User agent string when establishing stream context with remote GitHub URL. + */ + private $userAgent = 'bugs.php.net Pulltracker'; + + /** + * Username or organization name on GitHub. + */ + private $organization = 'php'; + + /** + * Class constructor + */ + public function __construct($dbh) + { + $this->dbh = $dbh; + } + + /** + * Retrieve data from remote GitHub URL. + */ + private function getDataFromGithub(string $repo, int $pullId) + { + $context = stream_context_create([ + 'http' => [ + 'ignore_errors' => '1', + 'user_agent' => $this->userAgent, + ] + ]); + + $url = $this->url.'/repos/'.$this->organization.'/'.urlencode($repo).'/pulls/'.$pullId; + $data = @json_decode(file_get_contents($url, null, $context)); + + if (!is_object($data)) { + return false; + } + + return $data; + } + + /** + * Attach a pull request to bug. + */ + public function attach($bugId, $repo, $pullId, $developer) + { + $data = $this->getDataFromGithub($repo, (int)$pullId); + + if (!$data) { + throw new \Exception('Failed to retrieve pull request from GitHub'); + } + + $sql = 'INSERT INTO bugdb_pulls + (bugdb_id, github_repo, github_pull_id, github_title, github_html_url, developer) + VALUES (?, ?, ?, ?, ?, ?) + '; + + $arguments = [ + $bugId, + $repo, + $pullId, + $data->title, + $data->html_url, + $developer, + ]; + + $this->dbh->prepare($sql)->execute($arguments); + + return $data; + } + + /** + * Remove a pull request from given bug. + */ + public function detach(int $bugId, string $repo, int $pullId) + { + $sql = 'DELETE FROM bugdb_pulls + WHERE bugdb_id = ? AND github_repo = ? AND github_pull_id = ? + '; + + $this->dbh->prepare($sql)->execute([$bugId, $repo, $pullId]); + } +} diff --git a/www/bug.php b/www/bug.php index e2c6576..c676d13 100644 --- a/www/bug.php +++ b/www/bug.php @@ -2,6 +2,7 @@ /* User interface for viewing and editing bug details */ use App\Utils\Captcha; +use App\Repository\PullRequestRepository; // Obtain common includes require_once '../include/prepend.php'; @@ -1085,9 +1086,8 @@ OUTPUT; } echo "<p><a href='patch-add.php?bug_id={$bug_id}'>Add a Patch</a></p>"; - require_once "{$ROOT_DIR}/include/classes/bug_ghpulltracker.php"; - $pulltracker = new Bug_Pulltracker(); - $pulls = $pulltracker->listPulls($bug_id); + $pullRequestRepository = new PullRequestRepository($dbh); + $pulls = $pullRequestRepository->findAllByBugId($bug_id); echo "<h2>Pull Requests</h2>\n"; require "{$ROOT_DIR}/templates/listpulls.php"; diff --git a/www/gh-pull-add.php b/www/gh-pull-add.php index 93e147a..c86d4bd 100644 --- a/www/gh-pull-add.php +++ b/www/gh-pull-add.php @@ -1,6 +1,8 @@ <?php +use App\Repository\PullRequestRepository; use App\Utils\Captcha; +use App\Utils\GitHub; // Obtain common includes require_once '../include/prepend.php'; @@ -50,8 +52,8 @@ if (!$show_bug_info) { exit; } -require_once "{$ROOT_DIR}/include/classes/bug_ghpulltracker.php"; -$pullinfo = new Bug_Pulltracker; +$pullinfo = new GitHub($dbh); +$pullRequestRepository = new PullRequestRepository($dbh); if (isset($_POST['addpull'])) { $errors = []; @@ -84,7 +86,7 @@ if (isset($_POST['addpull'])) { } } catch (Exception $e) { - $pulls = $pullinfo->listPulls($bug_id); + $pulls = $pullRequestRepository->findAllByBugId($bug_id); include "{$ROOT_DIR}/templates/addghpull.php"; exit; } @@ -109,12 +111,11 @@ if (isset($_POST['addpull'])) { } if (count($errors)) { - $pulls = $pullinfo->listPulls($bug_id); + $pulls = $pullRequestRepository->findAllByBugId($bug_id); include "{$ROOT_DIR}/templates/addghpull.php"; exit; } -/* // Add a comment to the bug report. $text = <<<TXT The following pull request has been associated: @@ -127,15 +128,16 @@ TXT; $res = bugs_add_comment($bug_id, $auth_user->email, $auth_user->name, $text, 'patch'); // Send emails - mail_bug_updates($buginfo, $buginfo, $auth_user->email, $text, 4, $bug_id); - */ - $pulls = $pullinfo->listPulls($bug_id); + // TODO: enable also mailing + //mail_bug_updates($buginfo, $buginfo, $auth_user->email, $text, 4, $bug_id); + + $pulls = $pullRequestRepository->findAllByBugId($bug_id); $errors = []; include "{$ROOT_DIR}/templates/addghpull.php"; exit; } $email = isset($_GET['email']) ? $_GET['email'] : ''; -$pulls = $pullinfo->listPulls($bug_id); +$pulls = $pullRequestRepository->findAllByBugId($bug_id); include "{$ROOT_DIR}/templates/addghpull.php";
-- PHP Webmaster List Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php