Hi Yannick,

This is great. Could you add an excerpt of the modified code? I know I'm
probably the only guy doing this (but I don't consider I *should* be the
only one - this is just basic peer review), but I usually review the
type of code change, and clicking on a link doesn't allow for an
"unplugged" review (and highly depends on the availability of bitbucket
and an internet connection at that point). Funnily enough, I spend most
of my unplugged working time to review code from others to ensure high
quality of the commits.
I've attached the entire script. It's the simplest thing I've written in years, but it works like a charm. It's basically just parsing a JSON object and formatting the result into a message. Bitbucket already has 2 built-in e-mail notifications options, one of which contains complete diffs of the changesets, so that might be of particular interest to you. The number of services supported by Bitbucket is quite amazing if you ask me.

Trust me, you're not the only one reviewing code. One of the reasons I need these mails is to stay up to date and to try and go through things which seem interesting (and that can be in a good as well as a bad way). It's always online for me though, hence the non-inclusion of the complete diffs right now. (and in the past as far as C2 was concerned) Considering the way Bitbucket works and the excellent API access they provide it's not even that farfetched to actually included them as well.

I can create that one for you and highly agree (for the unplugged
reasons mentioned above). How should it be named? dev2-commits?
dev2-commits would be great :)

Best regards,
Hans

On 19/03/2011 16:33, Yannick Warnier wrote:
El sáb, 19-03-2011 a las 13:43 +0100, Hans De Bisschop escribió:
Hi all,

Just spent some time writing a new script that enables us to get
notified by mail of new changesets on any of the Chamilo 2
repositories. The format of said mails can be seen below. Compared to
the "old" script format on Google Code I've added direct links to
comparisons for modified files.
This is great. Could you add an excerpt of the modified code? I know I'm
probably the only guy doing this (but I don't consider I *should* be the
only one - this is just basic peer review), but I usually review the
type of code change, and clicking on a link doesn't allow for an
"unplugged" review (and highly depends on the availability of bitbucket
and an internet connection at that point). Funnily enough, I spend most
of my unplugged working time to review code from others to ensure high
quality of the commits.

I'll be adding the post-hook to all repositories over the next few
days. Might it be a good idea to have a seperate mailing list (the
irony is not lost upon me) for all these messages to arrive on? Would
prefer that they don't clutter up the regular list.
I can create that one for you and highly agree (for the unplugged
reasons mentioned above). How should it be named? dev2-commits?


Best regards,
Hans

-------- Original Message --------
                           Subject:
C2M
chamilo-ext-repo-matterhorn-dev:
[Scaramanga] Testing the new
push-hook script.
                              Date:
Sat, 19 Mar 2011 13:41:18 +0100
                              From:
Chamilo 2 AT Bitbucket
<[email protected]>
                          Reply-To:
<[email protected]>
                                To:
Chamilo 2 Notifications
<[email protected]>


Repository: chamilo-ext-repo-matterhorn-dev
Revision: 64
Node: 9cde15af93df
Link: 
https://bitbucket.org/chamilo/chamilo-ext-repo-matterhorn-dev/changeset/9cde15af93df

Author: Scaramanga
Date: 2011-03-19 13:39:55
Pushed by: ScaraMcDuck

Log Message:
------------
Testing the new push-hook script.

Added Paths:
------------
php/temp/test.xml

Modified Paths:
---------------
php/settings/settings_matterhorn.xml 
(https://bitbucket.org/chamilo/chamilo-ext-repo-matterhorn-dev/diff/php/settings/settings_matterhorn.xml?diff2=9cde15af93df&diff1=99c990e56d6b)


_______________________________________________
Dev mailing list
[email protected]
http://lists.chamilo.org/listinfo/dev

_______________________________________________
Dev mailing list
[email protected]
http://lists.chamilo.org/listinfo/dev
<?php
error_reporting(0);
/*
 * Bitbucket post-commit hook
 */
require_once dirname(__FILE__) . '/phpmailer/class.phpmailer.php';

function send_mail($subject, $body)
{
    $mail = new PHPMailer();
    $mail->isHtml(false);
    $mail->CharSet = 'utf-8';
    $mail->Mailer = 'smtp';
    $mail->Host = 'localhost';
    $mail->Port = 25;
    
    $mail->Priority = 3; // 5=low, 1=high
    $mail->AddCustomHeader('Errors-To: [email protected]');
    $mail->SMTPKeepAlive = true;
    
    $mail->From = '[email protected]';
    $mail->Sender = '[email protected]';
    $mail->FromName = 'Chamilo 2 AT Bitbucket';
    $mail->AddCustomHeader('Return-Path: <[email protected]>');
    
    $mail->Subject = $subject;
    $mail->Body = $body;
    
    $mail->AddAddress('[email protected]', 'Chamilo 2 Notifications');
    
    if (! $mail->Send())
    {
        return false;
    }
    $mail->ClearAddresses();
}

/*
 * Check whether the request originates from Bitbucket
 */
// Decoding the JSON payload to a regular PHP object
$payload = json_decode(stripcslashes($_POST['payload']));

/*
 * First, let's split things up into some smaller parts
 */

// The actual repository that was being pushed to
$repository = $payload->repository;

// The commit or commits being pushed
$commits = $payload->commits;

// The user that executed the push
$user = $payload->user;

/*
 * Loop over all the commits
 */
foreach ($commits as $commit)
{
    $parents = $commit->parents;
    
    /*
     * One or more files might have been affected by the commit,
     * process them and group by manipulation type
     */
    
    $file_changes = array();
    
    foreach ($commit->files as $file)
    {
        if (! key_exists($file->type, $file_changes))
        {
            $file_changes[$file->type] = array();
        }
        
        $file_changes[$file->type][] = $file->file;
    }
    
    foreach ($file_changes as $type => $file_change)
    {
        sort($file_changes[$type]);
    }
    
    /*
     * Compile the subject
     */
    $subject = array();
    $subject[] = 'C2M';
    $subject[] = $repository->slug . ':';
    $subject[] = '[' . $commit->author . ']';
    $subject[] = substr($commit->message, 0, 40);
    
    $subject = implode(' ', $subject);
    
    /*
     * Compile the message
     */
    
    $message = array();
    
    // General
    $message[] = 'Repository: ' . $repository->slug;
    $message[] = 'Revision: ' . $commit->revision;
    $message[] = 'Node: ' . $commit->node;
    $message[] = 'Link: ' . 'https://bitbucket.org' . $repository->absolute_url 
. 'changeset/' . $commit->node;
    $message[] = '';
    $message[] = 'Author: ' . $commit->author;
    $message[] = 'Date: ' . $commit->timestamp;
    $message[] = 'Pushed by: ' . $user;
    $message[] = '';
    $message[] = 'Log Message:';
    $message[] = '------------';
    $message[] = $commit->message;
    $message[] = '';
    
    // Paths
    foreach ($file_changes as $type => $file_change)
    {
        $title = ucfirst($type) . ' Paths:';
        $message[] = $title;
        $message[] = str_repeat('-', strlen($title));
        
        foreach ($file_change as $file)
        {
            if ($type == 'modified')
            {
                $message[] = $file . ' (https://bitbucket.org' . 
$repository->absolute_url . 'diff/' . $file . '?diff2=' . $commit->node . 
'&diff1=' . $parents[0] . ')';
            }
            else
            {
                $message[] = $file;
            }
        }
        
        $message[] = '';
    }
    
    $message = implode("\n", $message);
    
    send_mail($subject, $message);
}
?>
_______________________________________________
Dev mailing list
[email protected]
http://lists.chamilo.org/listinfo/dev

Reply via email to