Ejemplo n.º 1
0
 /**
  * index
  */
 public function index()
 {
     if (!isset($_GET['owner']) or !isset($_GET['repo'])) {
         exit('Invalid Argument');
     }
     $owner = $_GET['owner'];
     $repo = $_GET['repo'];
     $client = new PRWatcher(GITHUB_ACCESS_TOKEN, $owner, $repo);
     try {
         $pr_arr = $client->getPullRequests();
     } catch (\Exception $e) {
         exit('Repository Access Denied');
     }
     $name_list = $GLOBALS['APP_DEFINE']['GITHUB_USER_NAME_LIST'];
     foreach ($pr_arr as $pr_number => $pr) {
         // コメントを取得する
         $comment_arr = $client->getComments($pr_number, PRWatcher::COMMENT_GET_PR);
         // 最後のコメントを取得する
         $last_comment = end($comment_arr);
         reset($comment_arr);
         // レビュー者を洗い出す
         $review_status = 0;
         $reviewer_arr = [];
         foreach ($comment_arr as $comment) {
             if (0 === $comment['review_status']) {
                 $reviewer_arr = [];
             } elseif ($review_status !== $comment['review_status']) {
                 $review_status = $comment['review_status'];
                 $reviewer_arr[] = PRWatcher::convertUserName($comment['content']['user']['login']);
             }
         }
         $reviewer_str = implode(', ', $reviewer_arr);
         printf('<div>(+%s) <a href="%s">%s</a> (%s)<br>(reviewer : %s)<div><br>', isset($last_comment['review_status']) ? $last_comment['review_status'] : 0, $pr['html_url'], $pr['title'], PRWatcher::convertUserName($pr['user']['login']), $reviewer_str);
     }
 }
Ejemplo n.º 2
0
 /**
  * PRのコメントをメール送信する
  */
 public function prWatcher()
 {
     try {
         // PRを取得する
         $pr_arr = $this->client->getPullRequests();
     } catch (\Exception $e) {
         exit("github接続に失敗しました、引数を確認してください\n");
     }
     // 保存済みPR、コメントidを取得する
     $saved_pr_number_arr = $this->client->getSavedPRNumber();
     $saved_comment_id_info = $this->client->getSavedCommentId();
     // マージ済みPRを検知し、メール本文を作成する
     $mail_body = '';
     $merged_pr_number_arr = array_diff($saved_pr_number_arr, array_keys($pr_arr));
     foreach ($saved_pr_number_arr as $pr_number) {
         if (false !== array_search($pr_number, $merged_pr_number_arr)) {
             $pr = $this->client->getOnePullRequest($pr_number);
             if ($pr['merged']) {
                 $state = '(beer) merged';
             } else {
                 $state = '(ninja) ' . $pr['state'] . ' (not merge)';
             }
             $mail_body .= sprintf("%s %s (%s)\n", $state, $pr['title'], PRWatcher::convertUserName($pr['user']['login']));
             $mail_body .= "--------------------------------------------------------------------------------\n";
         }
     }
     // コメントを取得し、メール本文を作成する
     $all_comment_arr = [];
     foreach ($pr_arr as $pr_number => $pr) {
         // コメントを取得する
         $comment_arr = $this->client->getComments($pr_number);
         $all_comment_arr = array_merge($all_comment_arr, $comment_arr);
         // メール本文を作成する
         foreach ($comment_arr as $comment) {
             $type = $comment['type'];
             $content = $comment['content'];
             if (!isset($saved_comment_id_info[$type]) or false === array_search($content['id'], $saved_comment_id_info[$type])) {
                 // 保存済みでないとき
                 $is_deco = ($comment['is_new'] or $comment['is_decorate']);
                 $mail_body .= $is_deco ? '(*) ' : '(mail) ';
                 $mail_body .= sprintf("(+%s) %s (%s)\n", $comment['review_status'], mb_strimwidth($pr['title'], 0, 80, '...'), PRWatcher::convertUserName($pr['user']['login']));
                 $mail_body .= $is_deco ? sprintf("%s\n", $pr['html_url']) : '';
                 $mail_body .= sprintf("%s (%s)\n", trim($content['body']), PRWatcher::convertUserName($content['user']['login']));
                 $mail_body .= "--------------------------------------------------------------------------------\n";
             }
         }
     }
     // 保存する
     // (メール送信したのにPEAR-MAILが例外を投げ、連投される事故があるため、先に保存)
     $this->client->savePRNumber($pr_arr);
     $this->client->saveCommentId($all_comment_arr);
     // メールを送信する
     if ('' !== $mail_body) {
         $this->client->sendMail($mail_body);
     }
 }