Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit ddefdf5

Browse files
author
AH-dark
committed
First Commit with Push module
1 parent cd48003 commit ddefdf5

File tree

6 files changed

+352
-0
lines changed

6 files changed

+352
-0
lines changed

GithubEvent/github.class.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Github;
4+
5+
use Github\Push\GithubPush;
6+
use Notification\Wxwork\GroupRobot\WxworkGroupRobot;
7+
8+
class GithubHandle
9+
{
10+
/**
11+
* Action 中文翻译
12+
*/
13+
const ActionWords = [
14+
"opened" => "发起",
15+
"closed" => "关闭",
16+
"reopened" => "重新发起",
17+
"edited" => "更新",
18+
"merge" => "合并",
19+
"created" => "创建",
20+
"requested" => "请求",
21+
"completed" => "完成",
22+
"synchronize" => "同步更新",
23+
"created" => "创建",
24+
"deleted" => "删除",
25+
"renamed" => "重命名",
26+
"added" => "加入",
27+
"removed" => "退出",
28+
"edited" => "修改",
29+
"reopened" => "重开",
30+
"published" => "发布",
31+
"unpublished" => "删除",
32+
"prereleased" => "预发布"
33+
];
34+
35+
/**
36+
* @var GithubEvent Github触发事件
37+
*/
38+
var $GithubEvent;
39+
40+
/**
41+
* @var Sender 触发事件的用户
42+
* @var Sender->login 用户昵称
43+
* @var Sender->id 用户ID
44+
* @var Sender->avatar_url 用户头像
45+
* @var Sender->html_url 用户页面
46+
* @var Sender->type 用户类型
47+
*/
48+
var $Sender;
49+
50+
/**
51+
* @var Body 触发器Body部分
52+
*/
53+
var $Body;
54+
55+
var $wxwork = new WxworkGroupRobot;
56+
57+
public function Handle(string $event)
58+
{
59+
$data = json_decode(urldecode($event));
60+
self::$Body = $data['body'];
61+
self::$GithubEvent = $data['headers']['X-GitHub-Event'];
62+
self::$wxwork->RobotID = $data['queryString']['id'];
63+
switch (self::$GithubEvent) {
64+
case "push":
65+
$github = new GithubPush;
66+
$github->PushHandle();
67+
break;
68+
}
69+
}
70+
}

GithubEvent/push.class.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Github\Push;
4+
5+
use Github\GithubHandle;
6+
use Notification\Notification;
7+
use Notification\Wxwork\GroupRobot\WxworkGroupRobot;
8+
9+
class GithubPush extends GithubHandle
10+
{
11+
/**
12+
* 提交的仓库分支
13+
* @var string $ref
14+
*/
15+
var $ref;
16+
17+
/**
18+
* 提交的仓库名 (组织/仓库)
19+
* @var string $repository_name
20+
*/
21+
var $repository_name;
22+
23+
/**
24+
* 是否为私有
25+
* @var bool $private
26+
*/
27+
var bool $private;
28+
29+
/**
30+
* 仓库页面地址
31+
* @var string $repository_html_url
32+
*/
33+
var string $repository_html_url;
34+
35+
/**
36+
* 提交信息
37+
* @var array commits[]
38+
*/
39+
var $commits;
40+
41+
/**
42+
* 在 parent::$Body 中读取并设置变量
43+
*/
44+
protected function SetData()
45+
{
46+
self::$ref = parent::$Body['ref'];
47+
self::$repository_name = parent::$Body['repository']['full_name'];
48+
self::$private = parent::$Body['repository']['private'];
49+
self::$repository_html_url = parent::$Body['repository']['html_url'];
50+
self::$commits = parent::$Body['commits'];
51+
}
52+
53+
public function PushHandle()
54+
{
55+
$this->SetData();
56+
57+
$message = '<font color="warning">**仓库' . self::$repository_name . '收到一次push提交**</font>\n';
58+
$message .= '\t分支: ' . self::$ref . '\n';
59+
$message .= '\t最新提交信息: \n';
60+
61+
foreach (self::$commits as $commit) {
62+
$message .= '\t\t[' . $commit['message'] . ' >' . $commit['timestamp'] . '](' . $commit['url'] . ')';
63+
foreach ($commit['added'] as $added) {
64+
$message .= '\t\t\t<font color="info">新增文件:' . $added . '</font>';
65+
}
66+
foreach ($commit['modified'] as $modified) {
67+
$message .= '\t\t\t<font color="comment">修改文件:' . $modified . '</font>';
68+
}
69+
foreach ($commit['removed'] as $removed) {
70+
$message .= '\t\t\t<font color="warning">移除文件:' . $removed . '</font>';
71+
}
72+
$message .= '\t\t\t提交者: [' . $commit['author']['name'] . '](mailto:' . $commit['author']['email'] . ')';
73+
}
74+
75+
if (self::$private == true)
76+
$message .= '> 该仓库为私有仓库';
77+
78+
$request = parent::$wxwork->MarkdownNotice($message);
79+
$code = $request['errcode'];
80+
if ($code == 0)
81+
return [
82+
"isBase64Encoded" => false,
83+
"statusCode" => 200,
84+
"headers" => [],
85+
"body" => "Success"
86+
];
87+
else {
88+
$errmsg = $request['errmsg'];
89+
return [
90+
"isBase64Encoded" => false,
91+
"statusCode" => $code,
92+
"headers" => [],
93+
"body" => $errmsg
94+
];
95+
}
96+
}
97+
}

Notification/notification.class.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Notification;
4+
5+
class Notification
6+
{
7+
}

Notification/wxwork_robot.class.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Notification\Wxwork\GroupRobot;
4+
5+
use Notification\Notification;
6+
7+
use function Tool\Img2Base64;
8+
use function Tool\Img2MD5;
9+
10+
class WxworkGroupRobot extends Notification
11+
{
12+
/**
13+
* @var string BaseURL 企业微信群机器人通知地址
14+
* @link https://work.weixin.qq.com/api/doc/90000/90136/91770
15+
* @api
16+
*/
17+
const BaseURL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send';
18+
19+
/**
20+
* @var string RobotID 企业微信机器人ID
21+
* @link https://work.weixin.qq.com/api/doc/90000/90136/91770
22+
*/
23+
var $RobotID;
24+
25+
/**
26+
* 向企业微信发送Post请求
27+
* @param array $data 数组数据
28+
* @return array 经json_decode的数组数据
29+
*/
30+
private function SendPost(array $data)
31+
{
32+
$data = json_encode($data);
33+
$headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
34+
$ch = curl_init(self::BaseURL . '?key=' . self::$RobotID);
35+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
36+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
37+
curl_setopt($ch, CURLOPT_POST, 1);
38+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
39+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
40+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
41+
$output = curl_exec($ch);
42+
curl_close($ch);
43+
return json_decode($output, true);
44+
}
45+
46+
/**
47+
* 发送文字消息
48+
* @link https://work.weixin.qq.com/api/doc/90000/90136/91770#%E6%96%87%E6%9C%AC%E7%B1%BB%E5%9E%8B
49+
* @param string $content 文字消息内容
50+
* @param array $mentioned_list userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人
51+
* @param array $mentioned_mobile_list 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
52+
*/
53+
public function TextNotice(string $content, array $mentioned_list = null, array $mentioned_mobile_list = null)
54+
{
55+
$data = [
56+
"msgtype" => "text",
57+
"text" => [
58+
"content" => $content,
59+
]
60+
];
61+
if ($mentioned_list != null)
62+
$data['text']['mentioned_list'] = $mentioned_list;
63+
if ($mentioned_mobile_list != null)
64+
$data['text']['mentioned_mobile_list'] = $mentioned_mobile_list;
65+
66+
return $this->SendPost($data);
67+
}
68+
69+
/**
70+
* 发送Markdown消息
71+
* @link https://work.weixin.qq.com/api/doc/90000/90136/91770#markdown%E7%B1%BB%E5%9E%8B
72+
* @param string $content 文字消息内容
73+
*/
74+
public function MarkdownNotice(string $content)
75+
{
76+
$data = [
77+
"msgtype" => "markdown",
78+
"markdown" => [
79+
"content" => $content,
80+
]
81+
];
82+
83+
return $this->SendPost($data);
84+
}
85+
86+
/**
87+
* 发送图片消息
88+
* @link https://work.weixin.qq.com/api/doc/90000/90136/91770#%E5%9B%BE%E7%89%87%E7%B1%BB%E5%9E%8B
89+
* @param string $imgurl 图片URL地址
90+
*/
91+
public function ImageNotice(string $imgurl)
92+
{
93+
$data = [
94+
"msgtype" => "image",
95+
"image" => [
96+
"base64" => Img2Base64($imgurl),
97+
"md5" => Img2MD5($imgurl)
98+
]
99+
];
100+
101+
return $this->SendPost($data);
102+
}
103+
}

function.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Tool;
4+
5+
//Unicode加密
6+
function UnicodeEncode($str)
7+
{
8+
preg_match_all('/./u', $str, $matches);
9+
10+
$unicodeStr = "";
11+
foreach ($matches[0] as $m) {
12+
$unicodeStr .= "&#" . base_convert(bin2hex(iconv('UTF-8', "UCS-4", $m)), 16, 10);
13+
}
14+
return $unicodeStr;
15+
}
16+
17+
//Unicode解密
18+
function UnicodeDecode($unicode_str)
19+
{
20+
$json = '{"str":"' . $unicode_str . '"}';
21+
$arr = json_decode($json, true);
22+
if (empty($arr)) return '';
23+
return $arr['str'];
24+
}
25+
26+
//图片->base64 转换
27+
function Img2Base64($url)
28+
{
29+
$imgInfo = getimagesize($url);
30+
return 'data:' . $imgInfo['mime'] . ';base64,' . base64_encode(file_get_contents($url));
31+
}
32+
33+
//图片->MD5 转换
34+
function Img2MD5($url)
35+
{
36+
return md5_file($url);
37+
}

index.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Main;
4+
5+
use Github\GithubHandle;
6+
7+
function main_handler($event, $context)
8+
{
9+
$handle = json_decode(urldecode($event));
10+
$_Event = $handle['headers']['X-GitHub-Event'];
11+
12+
// 非Github请求
13+
if ($_Event == null) {
14+
return json_encode([
15+
"isBase64Encoded" => false,
16+
"statusCode" => 500,
17+
"headers" => [
18+
"Content-Type" => "text/html"
19+
],
20+
"body" => "<html><body><p>Not a Github Webhook handle.</p></body></html>"
21+
]);
22+
}
23+
24+
// 验证UA
25+
if (strpos($handle['headers']['GitHub-Hookshot'], 'GitHub-Hookshot') === false) {
26+
return json_encode([
27+
"isBase64Encoded" => false,
28+
"statusCode" => 500,
29+
"headers" => [
30+
"Content-Type" => "text/html"
31+
],
32+
"body" => "<html><body><p>Error User Agent</p></body></html>"
33+
]);
34+
}
35+
36+
$Github = new GithubHandle;
37+
$Github->Handle($event);
38+
}

0 commit comments

Comments
 (0)