Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

#1 2013-10-26 21:53:22

phelps
Member
Registered: 2013-10-26
Posts: 6

GitHub Hook

I wanted a way for people who don't follow github (or just don't know how) to see when I make changes, so I decided to create a little plugin for FluxBB to allow hooking my git commits, this was the result, I'm quite happy.

-4u9.jpg
5ig0.jpg
-hno.jpg

I've thought about making this a public plugin and would do so if enough people are interested in it.

Let me know what you think.

Last edited by phelps (2013-10-26 21:55:21)

Offline

#2 2013-10-26 22:00:10

kierownik
Member
From: Tilburg, Netherlands
Registered: 2008-05-10
Posts: 326
Website

Re: GitHub Hook

looks very nice


My GitHub Profile

Offline

#3 2013-10-27 02:32:01

Studio384
Developer
From: Belgium
Registered: 2012-04-11
Posts: 640
Website

Re: GitHub Hook

It's a sad thing that we do not have an emoticon with his mouth wide open and big eyes for impression...

'Cuz, you know, I'm pretty impresed. Great work.

Last edited by Studio384 (2013-10-27 02:32:38)


FluxBB Community Benelux - ModernBB 3.4
Profile Plus: A new FluxBB profile interface

Offline

#4 2013-11-04 13:50:06

Erkan
Member
Registered: 2012-07-23
Posts: 12
Website

Re: GitHub Hook

Handy! nice work smile

Offline

#5 2013-11-04 16:52:42

Tijnk
New member
From: The Netherlands
Registered: 2013-11-04
Posts: 4

Re: GitHub Hook

I'd be interested in this! smile Great work.

Offline

#6 2013-11-18 11:48:19

Tijnk
New member
From: The Netherlands
Registered: 2013-11-04
Posts: 4

Re: GitHub Hook

@phelps Would you mind sharing the code with us? smile

Offline

#7 2013-12-02 11:10:55

phelps
Member
Registered: 2013-10-26
Posts: 6

Re: GitHub Hook

I'm way to busy now to work on making this a plugin so I'm just going to share the code:

<?php

define('PUN_ROOT', dirname(__FILE__).'/');
require PUN_ROOT.'include/common.php';
require PUN_ROOT.'include/search_idx.php';
require PUN_ROOT.'include/parser.php';

if ($_GET['key'] == "some_key" ) {

  $now = time();

  $payload = json_decode($_POST['payload']);

  $post = "";

  $post .= "[size=4][b]Github Commit Log[/b][/size][hr]";

  $count = count($payload->commits);

  if ($count <= 0) {

    $ref = explode('/', $payload->regs);
    $tag = end($ref);
    $download = 'https://github.com/ProbablyEngine/Probably/archive/'.$tag.'.zip';

    $post .= "[b]New Release:[/b] " . $tag . "\r\n";
    $post .= "[b]Download:[/b] [url=".$download."]Download Release ".$tag."[/url]\r\n";

    $subject = $payload->repository->name . " / New Release / " . $tag . "";

  }
  else {
    foreach ($payload->commits as $commit) {

      $exp = explode("\n\n", str_replace("\r", "", $commit->message), 2);
      $title = $exp[0];
      $message = $exp[1];

      $post .= "[b]Commit:[/b] [i][url=".$commit->url."]" . $title . "[/url][/i]\r\n";
      if(!empty($message)){
        $post .= "[b]Message:[/b] [i]".$message."[/i]\r\n";
      }
      $post .= "[b]Author:[/b] [i]" . $commit->author->name . " (". $commit->author->username .")[/i]\r\n";
      $post .= "[b]Date:[/b] [i]" .  date('M jS Y g:i a', strtotime($commit->timestamp)) . "[/i]\r\n";
      if (count($commit->added) > 0) {
        $post .= "[color=#669933][b]Files Added:[/b][/color]";
        $post .= "[list]\r\n";
        foreach ($commit->added as $file) {
           $post .= "[*]" . $file . "\r\n";
           $additions++;
        }
        $post .= "[/list]";
      }
      if (count($commit->removed) > 0) {
        $post .= "[color=#cc3333][b]Files Removed:[/b][/color]";
        $post .= "[list]\r\n";
        foreach ($commit->removed as $file) {
           $post .= "[*]" . $file . "\r\n";
           $deletions++;
        }
        $post .= "[/list]";
      }
      if (count($commit->modified) > 0) {
        $post .= "[color=#3366ff][b]Files Changed:[/b][/color]";
        $post .= "[list]";
        foreach ($commit->modified as $file) {
           $post .= "[*]" . $file . "\r\n";
           $changes++;
        }
        $post .= "[/list]";
      }
      if ($commit !== end($payload->commits)){
       $post .= "[hr]\r\n\r\n";
      }
    }
    $errors = [];
    $post = preparse_bbcode(addslashes($post), $errors);

    $exp = explode("\n\n", str_replace("\r", "", reset($payload->commits)->message), 2);
    $title = addslashes($exp[0]);
    $message = addslashes($exp[1]);
    $subject = $payload->repository->name . " / " . $title .  ($count>2?' / + ' . ($count-1) . ' Other Commits':'') . ($count==2?' / + 1 Other Commit':'');
  }

  $sqlA = "INSERT INTO ".$db->prefix."topics (poster, subject, posted, last_post, last_poster, sticky, forum_id) VALUES('GitHub Bot', '".$subject."', ".$now.", ".$now.", 'GitHub Bot', 0, 13)";

  $db->query($sqlA) or error('Unable to create topic', __FILE__, __LINE__, $db->error());

  $new_tid = $db->insert_id();

  $sqlB = "INSERT INTO ".$db->prefix."posts (poster, poster_id, poster_ip, message, hide_smilies, posted, topic_id) VALUES('GitHub Bot', '8', '', '".$post."', 1, ".$now.", ".$new_tid.")";

  $db->query($sqlB) or error('Unable to create post', __FILE__, __LINE__, $db->error());

  $new_pid = $db->insert_id();
  $db->query('UPDATE '.$db->prefix.'topics SET last_post_id='.$new_pid.', first_post_id='.$new_pid.' WHERE id='.$new_tid) or error('Unable to update topic', __FILE__, __LINE__, $db->error());

  update_search_index('post', $new_pid, $post, $subject);

  update_forum(13); // set your forum

  $db->query('UPDATE '.$db->prefix.'users SET num_posts=num_posts+1, last_post='.$now.' WHERE id=8') or error('Unable to update user', __FILE__, __LINE__, $db->error());

}

Place this in your pun root and set a github hook to point at it.

You'll have to edit some of the SQL manually to use the correct User ID.  I just registered a GitHub Bot user for it.

It's messy but it works tongue

Offline

#8 2013-12-02 12:15:12

Franz
Lead developer
From: Germany
Registered: 2008-05-13
Posts: 5,800
Website

Re: GitHub Hook

You should not use addslashes(). It's unsafe, use $db->escape() instead.

And for anybody using this: you should make sure to give the file some random, hard-to-guess name and only let GitHub know about it.


fluxbb.de | develoPHP

"As code is more often read than written it's really important to write clean code."

Offline

#9 2013-12-03 08:41:56

Tijnk
New member
From: The Netherlands
Registered: 2013-11-04
Posts: 4

Re: GitHub Hook

I'll probably take this on as a new project later this week. Will make the modifications you suggested Franz!
In addition I'll take another look at possible security issues and clean it up here and there.

Thanks for sharing the code Phelps!

Offline

#10 2014-04-02 07:54:49

cptmashek
Member
From: Tacoma, WA, USA
Registered: 2013-11-08
Posts: 11

Re: GitHub Hook

Any updates on this? I'm taking a hack at it, but as of now, it doesn't seem to be working.


"Artificial Intelligence beats real stupidity any day!"

Offline

Board footer

Powered by FluxBB 1.5.6