getdata.php 3.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php
require_once(dirname(__DIR__) . '/config.php');
require_once(__DIR__ . '/github-php-client-master/client/GitHubClient.php');

$repos = array(
  'civicrm/civicrm-core',
  'civicrm/civicrm-packages',
  'civicrm/civicrm-drupal',
  'civicrm/civicrm-wordpress',
  'civicrm/civicrm-joomla',
);

// Initialize database and prepared statements
$dbh = new PDO('mysql:dbname='.DBNAME.';host='.DBHOST, DBUSER, DBPASS);
$stm_c = $dbh->prepare("
  INSERT INTO github_commit (repository, hash, author_login, author_date, committer_login, committer_date, message)
  VALUES (:repository, :hash, :author_login, :author_date, :committer_login, :committer_date, :message);
");
$stm_u = $dbh->prepare("
  INSERT INTO github_user (id, login, name, company, email, location, avatar_url)
  VALUES (:id, :login, :name, :company, :email, :location, :avatar_url)
  ON DUPLICATE KEY UPDATE
     id=:id, login=:login, name=:name, company=:company, email=:email, location=:location, avatar_url=:avatar_url;
");

// Initialize GitHub
$client = new GitHubClient();
cividesk's avatar
cividesk committed
28
$client->setCredentials(GITHUB_USERNAME, GITHUB_PASSWORD);
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

// Loop over each repository
$users = array();
foreach($repos as $repo) {
  echo "Repository $repo ...";
  $new_commits = 0;

  // Get last commit date in database
  $result = $dbh->query("SELECT MAX(author_date) FROM github_commit WHERE repository='$repo';")->fetch();

  // Get commits from GitHub
  $parts = explode('/', $repo);
  $client->setPage(); // reinitialize the results pager
  $commits = $client->repos->commits->listCommitsOnRepository($parts[0], $parts[1], null, null, null, $result[0]);

  // Loop over each commit (paged)
  while (sizeof($commits)) {
    foreach($commits as $commit) {
cividesk's avatar
cividesk committed
47 48 49 50
      $params = array(
        ':repository' => $repo,
        ':hash' => $commit->getSha(),
        ':author_login' => ($commit->getAuthor() ? $commit->getAuthor()->getLogin() : ''),
51
        ':author_date' => str_replace(array('T','Z'), ' ', $commit->getCommit()->getAuthor()->getDate()),
cividesk's avatar
cividesk committed
52
        ':committer_login' => ($commit->getCommitter() ? $commit->getCommitter()->getLogin() : ''),
53 54
        ':committer_date' => str_replace(array('T','Z'), ' ', $commit->getCommit()->getCommitter()->getDate()),
        ':message' => $commit->getCommit()->getMessage(),
cividesk's avatar
cividesk committed
55 56 57 58 59 60 61
      );
      if ($stm_c->execute($params)) {
        $new_commits++;
      } else {
        $errorInfo = $stm_c->errorInfo();
        echo $errorInfo[2] . PHP_EOL;
      }
62 63 64 65 66 67 68 69
    }
    $commits = $client->getNextPage();
  }
  echo " $new_commits new commit(s)" . PHP_EOL;
}

// Now update all user records
echo "Updating users ...";
cividesk's avatar
cividesk committed
70
$updated = $skipped = 0;
71 72 73
$query = "SELECT DISTINCT author_login FROM github_commit WHERE author_login > ''";
foreach ($dbh->query($query) as $row) {
  $client->setPage(); // reinitialize the results pager
cividesk's avatar
cividesk committed
74 75 76 77 78 79
  try {
    $user = $client->users->getSingleUser($row[0]);
  } catch (Exception $e) {
    $skipped ++;
    continue;
  }
80 81
  /* @var $user GitHubFullUser */
  if ($user) {
cividesk's avatar
cividesk committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    $params = array(
      ':id' => $user->getId(),
      ':login' => $user->getLogin(),
      ':name' => $user->getName(),
      ':company' => $user->getCompany(),
      ':email' => $user->getEmail(),
      ':location' => $user->getLocation(),
      ':avatar_url' => $user->getAvatarUrl(),
    );
    if ($stm_u->execute($params)) {
      $updated ++;;
    } else {
      $errorInfo = $stm_u->errorInfo();
      echo $errorInfo[2] . PHP_EOL;
    }
97 98 99 100
  } else {
    echo "Unknown user - login: " . $row[0] . "\n";
  }
}
101 102 103 104 105 106 107 108 109 110
$query = "
  UPDATE github_user
     SET first_commit = (
           SELECT MIN(author_date)
             FROM github_commit
            WHERE author_login = login
           )
    WHERE first_commit IS NULL;
    ";
$dbh->query($query);
111
echo " $updated updates, $skipped in error." . PHP_EOL;