Gitlab to Discord Webhook

10/21/2016 13:11 Devsome#1
Hallo elitepvpers,

kleines Snippet um die Discord webhooks für gitlab zu benutzen.
Einfach Irgendwo hochladen wo cUrl erlaubt ist und gitlab darauf verlinken.

PHP Code:
<?php
error_reporting
(E_ALL E_STRICT);
ini_set('display_errors''On');

/* Variables editing */
$GitlabToken    "";
$DiscordWebHook "https://ptb.discordapp.com/api/webhooks/";

if( 
$_SERVER['HTTP_X_GITLAB_TOKEN'] ) {
  if( 
$_SERVER['HTTP_X_GITLAB_TOKEN'] = $GitlabToken ) {

    
$json file_get_contents('php://input');
    
$action json_decode($jsontrue);

    
$payloadArr = array(
      
"username"  =>  "Gitlab",
      
"icon_url"  =>  "http://i.epvpimg.com/Rdh7c.jpg",
      
"text"      =>  $action['project']['name'],
      
"attachments" =>  array(
        array(
          
"author_icon" =>  $action['user_avatar'],
          
"author_name" =>  $action['commits'][0]['author']['name'],
          
"color"       =>  "#ffffff",
          
"fields"  => array(
            array(
              
"title" =>  $action['event_name'],
              
"value" =>  "[" substr($action['commits'][0]['id'], 07) . "](" $action['commits'][0]['url'] . ") ",
            ),
          ),
        ),
      ),
    );

    
$payload json_encode($payloadArr);

    
$ch curl_init();
    
$options = array(
      
CURLOPT_URL                   =>    $DiscordWebHook "/slack",
      
CURLOPT_POST                  =>    true,
      
CURLOPT_RETURNTRANSFER        =>    true,
      
CURLOPT_POSTFIELDS            =>    $payload,
      
CURLOPT_HTTPHEADER            =>    array('Content-type: application/json'),
    );
    
curl_setopt_array($ch$options);
    
$result curl_exec($ch);
    
$httpCodeReturned curl_getinfo($chCURLINFO_HTTP_CODE);
    
curl_close($ch);
    
$result_str json_decode($result);

    
/*
     * print_r($result_str);
     * print($payload);
     * */
    
print($httpCodeReturned);

  }
}
10/21/2016 18:16 mlukac89#2
Can u post on english too pls ?
10/21/2016 19:00 Devsome#3
Quote:
Originally Posted by mlukac89 View Post
Can u post on english too pls ?
Just upload it to any server and change the
PHP Code:
$GitlabToken    "";
$DiscordWebHook "https://ptb.discordapp.com/api/webhooks/"
and enable Webhooks in Gitlab [Only registered and activated users can see links. Click Here To Register...]
01/31/2019 04:08 +Yazzn#4
In case anyone sees this in <current_year>, GitLab has Discord integration out of the box since version 11.6 [Only registered and activated users can see links. Click Here To Register...]
01/29/2024 15:05 teslatx#5
Thank you