Changeset 198234 in webkit


Ignore:
Timestamp:
Mar 15, 2016 3:42:15 PM (8 years ago)
Author:
rniwa@webkit.org
Message:

Extract the code to format commit logs into its own PHP file
https://bugs.webkit.org/show_bug.cgi?id=155514

Rubber-stamped by Chris Dumez.

Extracted CommitLogFetcher out of /api/commits so that it could be used in analysis-tasks.php
in the future to support associating cause/fix for each analysis task.

  • public/api/commits.php:
  • public/include/commit-log-fetcher.php: Added.

(CommitLogFetcher)
(CommitLogFetcher::construct): Added.
(CommitLogFetcher::repository_id_from_name): Added.
(CommitLogFetcher::fetch_between): Added.
(CommitLogFetcher::fetch_oldest): Added.
(CommitLogFetcher::fetch_latest): Added.
(CommitLogFetcher::fetch_last_reported): Added.
(CommitLogFetcher::fetch_revision): Added.
(CommitLogFetcher::commit_for_revision): Added.
(CommitLogFetcher::format_single_commit): Added.
(CommitLogFetcher::format_commit): Added.

Location:
trunk/Websites/perf.webkit.org
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Websites/perf.webkit.org/ChangeLog

    r197904 r198234  
     12016-03-15  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Extract the code to format commit logs into its own PHP file
     4        https://bugs.webkit.org/show_bug.cgi?id=155514
     5
     6        Rubber-stamped by Chris Dumez.
     7
     8        Extracted CommitLogFetcher out of /api/commits so that it could be used in analysis-tasks.php
     9        in the future to support associating cause/fix for each analysis task.
     10
     11        * public/api/commits.php:
     12        * public/include/commit-log-fetcher.php: Added.
     13        (CommitLogFetcher)
     14        (CommitLogFetcher::__construct): Added.
     15        (CommitLogFetcher::repository_id_from_name): Added.
     16        (CommitLogFetcher::fetch_between): Added.
     17        (CommitLogFetcher::fetch_oldest): Added.
     18        (CommitLogFetcher::fetch_latest): Added.
     19        (CommitLogFetcher::fetch_last_reported): Added.
     20        (CommitLogFetcher::fetch_revision): Added.
     21        (CommitLogFetcher::commit_for_revision): Added.
     22        (CommitLogFetcher::format_single_commit): Added.
     23        (CommitLogFetcher::format_commit): Added.
     24
    1252016-03-09  Ryosuke Niwa  <rniwa@webkit.org>
    226
  • trunk/Websites/perf.webkit.org/public/api/commits.php

    r194093 r198234  
    22
    33require_once('../include/json-header.php');
     4require_once('../include/commit-log-fetcher.php');
    45
    56function main($paths) {
     
    1112        exit_with_error('DatabaseConnectionFailure');
    1213
     14    $fetcher = new CommitLogFetcher($db);
     15
    1316    if (!is_numeric($paths[0])) {
    14         $repository_name = $paths[0];
    15         $repository_row = $db->select_first_row('repositories', 'repository', array('name' => $repository_name));
    16         if (!$repository_row)
    17             exit_with_error('RepositoryNotFound', array('repositoryName' => $repository_name));
    18         $repository_id = $repository_row['repository_id'];
     17        $repository_id = $fetcher->repository_id_from_name($paths[0]);
     18        if (!$repository_id)
     19            exit_with_error('RepositoryNotFound', array('repositoryName' => $paths[0]));
    1920    } else
    2021        $repository_id = intval($paths[0]);
    2122
    2223    $filter = array_get($paths, 1);
    23     $single_commit = NULL;
    2424    $commits = array();
    2525    if (!$filter) {
     
    2727        $from = array_get($_GET, 'from');
    2828        $to = array_get($_GET, 'to');
    29         $commits = fetch_commits_between($db, $repository_id, $from, $to, $keyword);
     29        $commits = $fetcher->fetch_between($repository_id, $from, $to, $keyword);
    3030    } else if ($filter == 'oldest') {
    31         $single_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id), 'time');
     31        $commits = $fetcher->fetch_oldest($repository_id);
    3232    } else if ($filter == 'latest') {
    33         $single_commit = $db->select_last_row('commits', 'commit', array('repository' => $repository_id), 'time');
     33        $commits = $fetcher->fetch_latest($repository_id);
    3434    } else if ($filter == 'last-reported') {
    35         $single_commit = $db->select_last_row('commits', 'commit', array('repository' => $repository_id, 'reported' => true), 'time');
     35        $commits = $fetcher->fetch_latest_reported($repository_id);
    3636    } else if (ctype_alnum($filter)) {
    37         $single_commit = commit_from_revision($db, $repository_id, $filter);
     37        $commits = $fetcher->fetch_revision($repository_id, $filter);
    3838    } else {
    3939        $matches = array();
    4040        if (!preg_match('/([A-Za-z0-9]+)[\:\-]([A-Za-z0-9]+)/', $filter, $matches))
    4141            exit_with_error('UnknownFilter', array('repositoryName' => $repository_name, 'filter' => $filter));
    42 
    43         $commits = fetch_commits_between($db, $repository_id, $matches[1], $matches[2]);
     42        $commits = $fetcher->fetch_between($repository_id, $matches[1], $matches[2]);
    4443    }
    4544
    46     if ($single_commit) {
    47         $committer = $db->select_first_row('committers', 'committer', array('id' => $single_commit['commit_committer']));
    48         exit_with_success(array('commits' => array(format_commit($single_commit, $committer))));
    49     }
     45    if (!is_array($commits))
     46        exit_with_error('FailedToFetchCommits', array('repository' => $repository_id, 'filter' => $filter));
    5047
    5148    exit_with_success(array('commits' => $commits));
    52 }
    53 
    54 function commit_from_revision($db, $repository_id, $revision) {
    55     $all_but_first = substr($revision, 1);
    56     if ($revision[0] == 'r' && ctype_digit($all_but_first))
    57         $revision = $all_but_first;
    58     $commit_info = array('repository' => $repository_id, 'revision' => $revision);
    59     $row = $db->select_last_row('commits', 'commit', $commit_info);
    60     if (!$row)
    61         exit_with_error('UnknownCommit', $commit_info);
    62     return $row;
    63 }
    64 
    65 function fetch_commits_between($db, $repository_id, $first, $second, $keyword = NULL) {
    66     $statements = 'SELECT commit_id as "id",
    67         commit_revision as "revision",
    68         commit_parent as "parent",
    69         commit_time as "time",
    70         committer_name as "authorName",
    71         committer_account as "authorEmail",
    72         commit_message as "message"
    73         FROM commits LEFT OUTER JOIN committers ON commit_committer = committer_id
    74         WHERE commit_repository = $1 AND commit_reported = true';
    75     $values = array($repository_id);
    76 
    77     if ($first && $second) {
    78         $first_commit = commit_from_revision($db, $repository_id, $first);
    79         $second_commit = commit_from_revision($db, $repository_id, $second);
    80         $first = $first_commit['commit_time'];
    81         $second = $second_commit['commit_time'];
    82         $column_name = 'commit_time';
    83         if (!$first || !$second) {
    84             $first = $first_commit['commit_order'];
    85             $second = $second_commit['commit_order'];
    86             $column_name = 'commit_order';
    87         }
    88 
    89         $in_order = $first < $second;
    90         array_push($values, $in_order ? $first : $second);
    91         $statements .= ' AND ' . $column_name . ' >= $' . count($values);
    92         array_push($values, $in_order ? $second : $first);
    93         $statements .= ' AND ' . $column_name . ' <= $' . count($values);
    94     }
    95 
    96     if ($keyword) {
    97         array_push($values, '%' . str_replace(array('\\', '_', '%'), array('\\\\', '\\_', '\\%'), $keyword) . '%');
    98         $keyword_index = '$' . count($values);
    99         array_push($values, ltrim($keyword, 'r'));
    100         $revision_index = '$' . count($values);
    101         $statements .= "
    102             AND ((committer_name LIKE $keyword_index OR committer_account LIKE $keyword_index) OR commit_revision = $revision_index)";
    103     }
    104 
    105     $commits = $db->query_and_fetch_all($statements . ' ORDER BY commit_time, commit_order', $values);
    106     if (!is_array($commits))
    107         exit_with_error('FailedToFetchCommits', array('repository' => $repository_id, 'first' => $first, 'second' => $second));
    108     foreach ($commits as &$commit)
    109         $commit['time'] = Database::to_js_time($commit['time']);
    110     return $commits;
    111 }
    112 
    113 function format_commit($commit_row, $committer_row) {
    114     return array(
    115         'id' => $commit_row['commit_id'],
    116         'revision' => $commit_row['commit_revision'],
    117         'parent' => $commit_row['commit_parent'],
    118         'time' => Database::to_js_time($commit_row['commit_time']),
    119         'order' => $commit_row['commit_order'],
    120         'authorName' => $committer_row ? $committer_row['committer_name'] : null,
    121         'authorEmail' => $committer_row ? $committer_row['committer_account'] : null,
    122         'message' => $commit_row['commit_message']
    123     );
    12449}
    12550
Note: See TracChangeset for help on using the changeset viewer.