Search code examples
perlgitgithubbugzilla

How can I update bugzilla entries from GitHub?


I really only want to be able to add a link to the commit whenever a bug number is used in the commit message. Being able to close bugs and so on would be a plus, but really beyond my needs.

We generally prepend the bug number in the form of xxxx on the commit message.

My current plan was to use the email_in.pl script that ships with Bugzilla and an email post-commit hook on github. The email hook sends a payload with the details of each commit. I could parse that re-direct it to the email_in.pl script. Is this the best approach? Has nobody done this yet?

Any help/tips/links would be appreciated.


Solution

  • Since I had email_in.pl already set up, I decided to write a little script that would parse the URL post-receive hook's payload and send it in as an email to bugzilla. So a post-receive hook will hit a URL that does the following:

    <?php
    $payload = json_decode($_REQUEST['payload']);
    
    if ($payload) {
        // assumes you want to process all commits (we only commit to the master branch)
        // but you may want to add some other conditionals above.
        $commits = $payload->commits;
    
        // there may be many commits per payload
        foreach ($commits as $commit) {
           $message = $commit->message;
           preg_match('/^(\*(\d+)\*)(.*)/i', $message, $matches);
           // The commit message must match the above regex
           // i.e. *1234* commit message
           if ( !(is_array($matches) && count($matches) == 4) )
              continue;
    
            $bugNumber = $matches[2];
            $comment = trim($matches[3]);
            $url = $commit->url;
    
            // get the author info
            $author = $commit->author;
            $authorName = $author->name;
            // assumes github email address exists in bugzilla.
            $authorEmail = $author->email;
    
            // construct the email
            $subject = "[Bug $bugNumber]";
            $body = "$comment\n$url";
            $header = "From: $authorName <$authorEmail>";
            // $bugzillaEmail = 'your@bugzilla.email
            mail($bugzillaEmail, $subject, $body, $header);
    }
    ?>