Skip to content
Browse files

Polished up the closed request notifications. Rejigged the template f…

…ile system.
  • Loading branch information...
1 parent d5a8efb commit 08e5942eb90ef1f4f1c290a5f959d8bd35f0e3ca @samwho committed Apr 23, 2011
View
44 classes/class.PullRequestCrawl.php
@@ -41,11 +41,20 @@ public function __construct() {
$this->new_requests = $this->fetcher->getRequestsSinceLastCrawl();
}
+ /**
+ * Runs the crawl process. This is essentially the "main" method of this program. It is the entry point.
+ */
public function run() {
$this->checkForNewPulls();
- $this->checkPullsClosed();
+
+ if ($this->config['alert_on_close'] == true) {
+ $this->checkPullsClosed();
+ }
}
+ /**
+ * Checks for new pull requests and sends emails if it finds any.
+ */
private function checkForNewPulls() {
if (count($this->new_requests) > 0) {
@@ -59,29 +68,24 @@ private function checkForNewPulls() {
$formatted_pull_requests = '';
foreach ($this->new_requests as $request) {
- $formatted_pull_requests .= TemplateParser::parse(
- $this->config['templates_dir'] . '/pull_request_group.tpl', $request);
+ $formatted_pull_requests .= TemplateParser::parse('_request.tpl', $request);
}
$group_email_placeholders =
array('pull_requests' => $formatted_pull_requests);
- $content = TemplateParser::parse(
- $this->config['templates_dir'] . '/group_request_email.tpl', $request,
+ $content = TemplateParser::parse('group_request.tpl', $request,
$group_email_placeholders);
- $subject = TemplateParser::parse(
- $this->config['templates_dir'] . '/pull_request_group_subject_line.tpl', $request);
+ $subject = TemplateParser::parse('group_request_subject.tpl', $request);
Email::send($content, $subject);
} else {
//Send requests in multiple emails
foreach ($this->new_requests as $request) {
- $content = TemplateParser::parse(
- $this->config['templates_dir'] . '/pull_request_single.tpl', $request);
- $subject = TemplateParser::parse(
- $this->config['templates_dir'] . '/pull_request_single_subject_line.tpl', $request);
+ $content = TemplateParser::parse('single_request.tpl', $request);
+ $subject = TemplateParser::parse('single_request_subject.tpl', $request);
Email::send($content, $subject);
}
}
@@ -98,8 +102,7 @@ private function checkPullsClosed() {
foreach ($this->old_requests as $number=>$request) {
$request = $this->fetcher->updatePullRequestInfo($number);
if ($request->state == 'closed') {
- $formatted_closed_requests .= TemplateParser::parse(
- $this->config['templates_dir'] . '/pull_request_closed_single.tpl', $request);
+ $formatted_closed_requests .= TemplateParser::parse('_closed.tpl', $request);
unlink($this->config['data_dir'] . '/' .$request->number. '.json');
}
@@ -114,21 +117,18 @@ private function checkPullsClosed() {
'closed_requests' => $formatted_closed_requests
);
- $content = TemplateParser::parse(
- $this->config['templates_dir'] . '/group_closed_email.tpl', $request,
+ $content = TemplateParser::parse('group_closed.tpl', $request,
$group_email_placeholders);
- $subject = 'Re: ' . TemplateParser::parse(
- $this->config['templates_dir'] . '/pull_request_group_subject_line.tpl', $request);
+ $subject = TemplateParser::parse('group_closed_subject.tpl', $request);
Email::send($content, $subject);
} else {
- foreach ($this->old_requests as $request) {
+ foreach ($this->old_requests as $number=>$request) {
$request = $this->fetcher->updatePullRequestInfo($number);
+
if ($request->state == 'closed') {
- $content = TemplateParser::parse(
- $this->config['templates_dir'] . '/pull_request_closed.tpl', $request);
- $subject = 'Re: ' . TemplateParser::parse(
- $this->config['templates_dir'] . '/pull_request_single_subject_line.tpl', $request);
+ $content = TemplateParser::parse('single_closed.tpl', $request);
+ $subject = TemplateParser::parse('single_closed_subject.tpl', $request);
Email::send($content, $subject);
unlink($this->config['data_dir'] . '/' .$request->number. '.json');
View
11 classes/class.PullRequestFetcher.php
@@ -139,7 +139,11 @@ public function saveLatestPull() {
* @return String $number
*/
public function getLatestPull() {
- return intval(file_get_contents($this->config['data_dir'] . '/' . $this->last_pull_file_name));
+ if (file_exists($this->config['data_dir'] . '/' . $this->last_pull_file_name)) {
+ return intval(file_get_contents($this->config['data_dir'] . '/' . $this->last_pull_file_name));
+ } else {
+ return -1;
+ }
}
/**
@@ -157,7 +161,7 @@ public function deleteLatestPull() {
* @return Array $array
*/
public function getRequestsSinceLastCrawl() {
- $latest_pull = $this->getLatestPull() ? $this->getLatestPull() : -1;
+ $latest_pull = $this->getLatestPull();
$return_array = array();
foreach ($this->requests as $request) {
@@ -178,7 +182,8 @@ public function getOldPullRequests() {
$return = array();
$dir = array_filter(scandir($this->config['data_dir']) , __CLASS__.'::filterPullRequestFiles');
foreach ($dir as $file) {
- $return[str_replace('.json', '', $file)] = file_get_contents($this->config['data_dir'] . '/' . $file);
+ $return[str_replace('.json', '', $file)] = json_decode(file_get_contents($this->config['data_dir'] . '/' .
+ $file));
}
return $return;
}
View
8 classes/class.TemplateParser.php
@@ -46,8 +46,9 @@ private static function getPlaceholders($request) {
'body' => $request->body,
'link' => $request->html_url,
'user_real_name' => isset($request->user->name) ? $request->user->name : $request->user->login,
- 'date_today' => strftime('%c'),
- 'state' => $request->state
+ 'date_today' => strftime('%D'),
+ 'state' => $request->state,
+ 'number' => $request->number
);
}
@@ -60,10 +61,11 @@ private static function getPlaceholders($request) {
* @param Array $placeholders
*/
public static function parse($template, $request, $placeholders = null) {
+ $config = Config::getInstance();
$placeholders = $placeholders ? array_merge($placeholders, self::getPlaceholders($request)) :
self::getPlaceholders($request);
- $template = file_get_contents($template);
+ $template = file_get_contents($config['templates_dir'] . '/' . $template);
foreach ($placeholders as $search=>$replace) {
$template = str_replace(
self::$open_delimiter.$search.self::$close_delimiter,
View
3 config.sample.inc.php
@@ -29,6 +29,9 @@
// Set this to true if you want to group multiple pull requests into one email.
$PULL_REQUEST_BOT['group_requests'] = true;
+// Whether or not to receieve alerts when a pull request is closed.
+$PULL_REQUEST_BOT['alert_on_close'] = true;
+
/*
* Details for the repository you want to fetch pull requests from.
*/
View
1 templates/_closed.tpl
@@ -0,0 +1 @@
+<a href="[+link+]">[+title+]</a> by [+user_real_name+]<br />
View
1 templates/_request.tpl
@@ -0,0 +1 @@
+<a href="[+link+]">[+title+]</a> by [+user_real_name+]<br />
View
14 templates/group_closed.tpl
@@ -0,0 +1,14 @@
+<html>
+ <head>
+ </head>
+ <body>
+ Hi ThinkUp developers,
+ <br /><br />
+ The following pull requests have been closed:
+ <br /><br />
+ [+closed_requests+]
+ <br />
+ Best,
+ Gina
+</body>
+</html>
View
11 templates/group_closed_email.tpl
@@ -1,11 +0,0 @@
-<html>
- <head>
- </head>
- <body>
- Hey all,
- <br /><br />
- The following pull requests have been closed:
- <br /><br />
- [+closed_requests+]
- </body>
-</html>
View
1 templates/group_closed_subject.tpl
@@ -0,0 +1 @@
+Pull Requests Closed [+date_today+]
View
14 templates/group_request.tpl
@@ -0,0 +1,14 @@
+<html>
+ <head>
+ </head>
+ <body>
+ Hi ThinkUp developers,
+
+ The following new pull requests have been opened:
+
+ [+pull_requests+]
+
+ Best,
+ Gina
+ </body>
+</html>
View
14 templates/group_request_email.tpl
@@ -1,14 +0,0 @@
-<html>
- <head>
- </head>
- <body>
- <h2>New pull requests!</h2>
- <br />
- [+pull_requests+]
- <br /><br />
- Please help ThinkUp by reviewing pull requests and offering code review! :)
- <br />
- Thanks,<br />
- ThinkUp
- </body>
-</html>
View
1 templates/group_request_subject.tpl
@@ -0,0 +1 @@
+New Pull Requests [+date_today+]
View
1 templates/pull_request_closed_group.tpl
@@ -1 +0,0 @@
-<a href="[+link+]" target="_blank">[+title+]</a> - by [+user_real_name+].
View
6 templates/pull_request_closed_single.tpl
@@ -1,6 +0,0 @@
-This pull request has been closed.
-
-Title: [+title+]
-Description: [+body+]
-
-Link: [+link+]
View
1 templates/pull_request_group.tpl
@@ -1 +0,0 @@
-<a href="[+link+]" target="_blank">[+title+]</a> - by [+user_real_name+]
View
1 templates/pull_request_group_subject_line.tpl
@@ -1 +0,0 @@
-Code Review Request [+date_today+]
View
17 templates/pull_request_single.tpl
@@ -1,17 +0,0 @@
-Hi ThinkUp developers,
-
-[+user_login+] has issued a new pull request.
-
-Title: [+title+]
-Description: [+body+]
-
-Link: [+link+]
-
-Please review the patch and leave any comments/feedback you have inline on GitHub, or reply to this email.
-
-Thanks for helping us code review ThinkUp.
-
-Best,
-Gina
-
-[This is an automated email to the ThinkUp Developers mailing list. Any time you issue a pull request on GitHub, this mailing list will be notified automatically.]
View
12 templates/single_closed.tpl
@@ -0,0 +1,12 @@
+<html>
+ <head>
+ </head>
+ <body>
+ Hi ThinkUp developers,
+
+ Pull request <a href="[+link+]">[+title+]</a> has been closed.
+
+ Best,
+ Gina
+ </body>
+</html>
View
1 templates/single_closed_subject.tpl
@@ -0,0 +1 @@
+Pull Request Closed: "[+title+]" by [+user_login+]
View
23 templates/single_request.tpl
@@ -0,0 +1,23 @@
+<html>
+ <head>
+ </head>
+ <body>
+ Hi ThinkUp developers,
+
+ [+user_login+] has issued a new pull request.
+
+ Title: [+title+]
+ Description: [+body+]
+
+ Link: [+link+]
+
+ Please review the patch and leave any comments/feedback you have inline on GitHub, or reply to this email.
+
+ Thanks for helping us code review ThinkUp.
+
+ Best,
+ Gina
+
+ [This is an automated email to the ThinkUp Developers mailing list. Any time you issue a pull request on GitHub, this mailing list will be notified automatically.]
+ </body>
+</html>
View
0 ...ates/pull_request_single_subject_line.tpl → templates/single_request_subject.tpl
File renamed without changes.
View
40 templates/template_readme.txt
@@ -1,40 +0,0 @@
-The templating in this request bot is a very simple placeholder replacement
-system. Nothing fancy, nothing like Smarty templates, just placeholder
-replacement.
-
-It is, however, pretty customisable. By default, placeholders are delimited
-[+like this+]. So the token there is the "like this" and the "delimiters" are
-the square brackets and the plus symbols.
-
-All template parsing happens in classes/class.TemplateParser.php. In this class
-you can change the delimiters and you can change the tokens (if you want to).
-
-The currently available tokens are:
-
-'title' - pull request title
-'user_real_name' - the full, real name of the user who issued the pull.
-'user_login' - the login name of the user who issued the pull.
-'gravatar_id' - the id of the user's gravatar.
-'gravatar' - an img html tag containing the gravatar url and alt text.
-'created_at' - the date and time that the pull was created.
-'body' - the contents of the pull request body.
-'link' - a link to the pull request. Not in an <a> tag, just a URL.
-
-These placeholders persist through all .tpl files apart from
-'group_request_email.tpl' that only has one placeholder and that is the
-"pull_request" token that contains all pull requests after being parsed through
-the 'pull_request_group.tpl' template.
-
-Current .tpl files available:
-
-'pull_request_single.tpl' - this is used when you are sending an individual
-email for each pull request the crawler finds. In the config file, if you
-set the 'group_requests' to false, this is the .tpl file that will be used.
-
-'pull_request_group.tpl' - If you set the 'group_requests' value to true in the
-config file, this .tpl is used FOR EACH PULL REQUEST. It is for a SINGLE PULL
-REQUEST. All pull requests will be formatted like this. Look at
-'group_request_email.tpl' for the formatting of the entire email.
-
-'pull_request_single_subject_line.tpl' - This is the email subject line for
-single pull request emails.

0 comments on commit 08e5942

Please sign in to comment.
Something went wrong with that request. Please try again.