PHP code example of zyan / gitlab-hooks
1. Go to this page and download the library: Download zyan/gitlab-hooks library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zyan / gitlab-hooks example snippets
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Zyan\GitLabHooks;
class Test extends Controller
{
public function gitlab()
{
$gitlab = new GitlabHooks();
//快速发送给企业微信群机器人
$res = $gitlab->sendToWeWork('您的企业微信群机器人key');
//快速发送给钉钉群机器人
//$res = $gitlab->sendToDingTalk('您的钉钉群机器人access_token');
//快速发送给飞书群机器人
//$res = $gitlab->sendToFeiShu('您的飞书群机器人key');
return response()->json($res->getBody()->getContents());
}
}
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Zyan\GitLabHooks;
class Test extends Controller
{
public function gitlab()
{
//定制多个仓库发送不同的群
$config =[
//把仓库1跟仓库2发送到群1
[
'project' => ['仓库1','仓库2'],
'key' => '群1_key',
'is_at_all' => false, //是否@全体成员 可选
'at_mobiles' => [], //需要@成员的手机号 可选
'at_userids' => [], //需要@成员的userid 可选
],
//把仓库2跟仓库3发送到群2
[
'project' => ['仓库3','仓库4'],
'key' => '群2_key',
'is_at_all' => false, //是否@全体成员 可选
'at_mobiles' => [], //需要@成员的手机号 可选
'at_userids' => [], //需要@成员的userid 可选
],
//注意: 飞书群机器人不支持@成员
//...
];
$gitlab = new GitlabHooks($config);
//发送到企业微信群机器人
$res = $gitlab->app('wework')->send();
//发送到钉钉群机器人
//$res = $gitlab->app('dingtalk')->send();
//发送到飞书群机器人
//$res = $gitlab->app('feishu')->send();
//如果同时发送给多个群,则返顺多个送发结果
return response()->json($res);
}
}
$gitlab = new GitlabHooks($config);
$pushObjectKinds = [
'push',
'merge_request',
'tag_push'
];
$gitlab->setPushObjectKinds($pushObjectKinds);
[
'push',
'merge_request',
'tag_push'
];
namespace Xxxx\Provider;
use Zyan\Client\HttpClient;
use Zyan\Contract\ProviderInterface;
//这里需要继承 HttpClient 与 ProviderInterface 接口
class XxxBot extends HttpClient implements ProviderInterface
{
/**
* 推送到某某机器人.
*
* @param string $key
* @param string $text
* @param bool $isAtAll
* @param array $atMobiles
* @param array $atUserIds
*
* @return string
*
* @author 读心印 <[email protected] >
*/
public function send(string $key, string $text, bool $isAtAll = false, array $atMobiles = [], array $atUserIds = []): \Psr\Http\Message\ResponseInterface
{
$data = [
"msgtype" => "markdown",
"markdown" => [
"title" => "gitLab通知",
"text" => $text,
],
"at" => [
"atMobiles" => $atMobiles,
"atUserIds" => $atUserIds,
"isAtAll" => $isAtAll
]
];
$url = 'https://api.xxxx.com/robot/send?access_token='.$key;
return $this->postJson($url, $data);
}
}
use Zyan\GitLabHooks;
use Zyan\Template\Markdown; //发送模板
use Xxx\XxxBot; //你自己写的类
//自定义提供者 仅限进阶模式,请配置您的发送信息
$config = [
[
'project' => ['仓库1','仓库2'],
'key' => '群1_key',
'is_at_all' => false, //是否@全体成员 可选
'at_mobiles' => [], //需要@成员的手机号 可选
'at_userids' => [], //需要@成员的userid 可选
]
];
$gitlab = new GitlabHooks($config);
$gitlab->setPorovider([
'xxxbot' => XxxBot::class
]);
$res = $gitlab->app('xxxbot')->send();
namespace Zyan\Template;
use Zyan\Body;
use Zyan\Contract\TemplateInterface;
//这里需要继承 Body 与 TemplateInterface 接口
class Text extends Body implements TemplateInterface
{
public function getMessage(): string
{
$objectKind = $this->getObjectKind();
switch ($objectKind) {
case 'push':
$branch = $this->getBranch();
break;
case 'merge_request':
$branch = $this->getSourceBranch() .'→'. $this->getTargetBranch();
break;
default:
$branch = '';
break;
}
$content = '# 代码推送通知 ';
$content .= "\n--------------------------------------";
$content .= "\n> 开发 : " . $this->getUserName() . " ";
$content .= "\n> 项目 : " . $this->getProjectName() . " ";
$content .= "\n> 分支 : " . $branch . " ";
$content .= "\n> 事件 : " . $objectKind . " ";
$state = $this->getState();
if (!empty($state)) {
$content .= "\n> 状态 : " . $state . " ";
}
if (!empty($message)) {
$content .= "\n";
$content .= "\n " . $message ;
}
return $content;
}
}
use Zyan\GitLabHooks;
use Zyan\Template\Text; //发送模板
//自定义提供者 仅限进阶模式,请配置您的发送信息
$config = [
[
'project' => ['仓库1','仓库2'],
'key' => '群1_key',
'is_at_all' => false, //是否@全体成员 可选
'at_mobiles' => [], //需要@成员的手机号 可选
'at_userids' => [], //需要@成员的userid 可选
]
];
$gitlab = new GitlabHooks($config);
$gitlab->setTemplate([
'xxxbot' => Text::class,
//如果需要对其他机器人使用这个模板,请填写多个
'bot2' => Text::class
//...
]);
$res = $gitlab->app('xxxbot')->send();