如何从GitHub更新bugzilla条目?

时间:2011-07-15 18:29:17

标签: perl git github bugzilla

我真的只希望能够在提交消息中使用错误号时添加提交链接。能够关闭错误等等将是一个加分,但真的超出了我的需要。

我们通常在提交消息上以 xxxx 的形式预先添加错误号。

我目前的计划是使用Bugzilla附带的email_in.pl脚本和github上的电子邮件提交后挂钩。电子邮件钩子发送有效负载,其中包含每个提交的详细信息我可以解析它将它重定向到email_in.pl脚本。这是最好的方法吗?还没有人这样做过吗?

任何帮助/提示/链接都将不胜感激。

1 个答案:

答案 0 :(得分:1)

由于我已经设置了email_in.pl,我决定编写一个小脚本来解析URL post-receive hook的有效负载并将其作为电子邮件发送给bugzilla。因此,post-receive挂钩会点击执行以下操作的URL:

<?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);
}
?>