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

Commit 1e5cad0

Browse files
committed
Add class: markdown.class.php and add method to answer Pull Request WebHooks.
1 parent 2e23ebc commit 1e5cad0

File tree

6 files changed

+317
-22
lines changed

6 files changed

+317
-22
lines changed

src/github/github.class.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace GitHub;
4+
5+
use Exception;
6+
7+
class github
8+
{
9+
/**
10+
* @var array Data from body
11+
*/
12+
protected $data = [];
13+
14+
/**
15+
* @describe 设置Data
16+
* @param array $data
17+
* @return array
18+
*/
19+
public function setData(array $data): array
20+
{
21+
return ($this->data = $data);
22+
}
23+
24+
/**
25+
* @var string Event Name
26+
*/
27+
protected $event = "";
28+
29+
/**
30+
* @describe 设置Event
31+
* @param string $event
32+
* @return string
33+
*/
34+
public function setEvent(string $event): string
35+
{
36+
return ($this->event = $event);
37+
}
38+
39+
protected function isSet(): bool
40+
{
41+
return $this->data!=[]&&$this->event!="";
42+
}
43+
}

src/github/pull-request.class.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace GitHub;
4+
5+
use Exception;
6+
use WeWork\markdown;
7+
8+
class pull_request extends github
9+
{
10+
function __construct($data = null) {
11+
$this->setEvent("push");
12+
if($data!=null) {
13+
$this->setData($data);
14+
}
15+
}
16+
17+
private const translateList = [
18+
"opened" => "打开",
19+
"ready_for_review" => "候审",
20+
"reopened" => "重新打开",
21+
"locked" => "锁定",
22+
"unlocked" => "解锁",
23+
"closed" => "关闭",
24+
"assigned" => "分配",
25+
"unassigned" => "取消分配"
26+
];
27+
28+
private function _e($text,bool $echo = false)
29+
{
30+
$return = self::translateList[$text];
31+
if($text == "closed" && $this->data['merged']) {
32+
$return = self::translateList[$text]."并成功合入分支";
33+
}
34+
if($echo) {
35+
return print $return;
36+
} else {
37+
return $return;
38+
}
39+
}
40+
41+
/**
42+
* @return string
43+
* @throws Exception
44+
* @noinspection HtmlDeprecatedTag
45+
*/
46+
public function getMessage(): string
47+
{
48+
if(!$this->isSet()) { throw new Exception("未设置data或event"); }
49+
50+
$message = new markdown();
51+
$message->addTitle($message->getLink("#".$this->data['number'],$this->data['pull_request']['url'])."有新的".$message->getColorText("Pull Request","info").$this->_e($this->data['action']));
52+
$message->addText("仓库: ".$message->getLink($this->data['pull_request']['head']['label'],$this->data['pull_request']['head']['repo']['html_url']."/tree/".$this->data['pull_request']['head']['ref'])." to ".$message->getLink($this->data['pull_request']['base']['label'],$this->data['pull_request']['base']['repo']['html_url']."/tree/".$this->data['pull_request']['base']['ref']));
53+
$patch = file_get_contents($this->data['pull_request']['patch_url']);
54+
$patch = explode("\n",$patch);
55+
$message->addQuote($patch);
56+
return $message->message();
57+
}
58+
}

src/github/push.class.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace GitHub;
4+
5+
use Exception;
6+
7+
class push extends github
8+
{
9+
function __construct($data = null) {
10+
$this->setEvent("push");
11+
if($data!=null) {
12+
$this->setData($data);
13+
}
14+
}
15+
16+
/**
17+
* @throws Exception
18+
* @return string
19+
* @noinspection HtmlDeprecatedTag
20+
*/
21+
public function getMessage(): string
22+
{
23+
if(!$this->isSet()) { throw new Exception("未设置data或event"); }
24+
$body = $this->data;
25+
$message = <<<EOF
26+
### 有新的<font color=\"warning\">Push</font>事件
27+
28+
仓库: [{$body['repository']['name']}]({$body['repository']['html_url']})
29+
30+
推送者: [{$body['pusher']['name']}({$body['pusher']['email']})]({$body['sender']['html_url']})
31+
32+
EOF;
33+
foreach ($body['commits'] as $commit) {
34+
$message .= <<<EOF
35+
36+
> Commit {$commit['id']}:
37+
>
38+
> <font color=\"comment\">{$commit['message']}</font>
39+
>
40+
> on {$commit['timestamp']}
41+
>
42+
> [查看详情]({$commit['url']})
43+
44+
EOF;
45+
}
46+
$message .= "\nSHA: ".$body['after'];
47+
return $message;
48+
}
49+
}

index.php renamed to src/index.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<?php
22

3+
use GitHub\pull_request;
4+
use GitHub\push;
35
use WeWork\GroupBot;
46

57
require "./wework.php";
8+
require "./markdown.class.php";
9+
require "./github/github.class.php";
10+
require "./github/push.class.php";
11+
require "./github/pull-request.class.php";
612

713
function object_to_array(object $obj): array
814
{
@@ -37,29 +43,20 @@ function main_handler($event, $context)
3743
$res = $wechat->sendMessage("收到Ping请求,字符串为".$body['zen'].",hookId为".$body['hook_id']."");
3844
break;
3945
case "push":
40-
$message = <<<EOF
41-
### 有新的Push事件
42-
43-
仓库: [{$body['repository']['name']}]({$body['repository']['html_url']})
44-
45-
推送者: [{$body['pusher']['name']}({$body['pusher']['email']})]({$body['sender']['html_url']})
46-
47-
EOF;
48-
foreach ($body['commits'] as $commit) {
49-
$message .= <<<EOF
50-
51-
> Commit {$commit['id']}:
52-
>
53-
> <font color=\"comment\">{$commit['message']}</font>
54-
>
55-
> on {$commit['timestamp']}
56-
>
57-
> [查看详情]({$commit['url']})
58-
59-
EOF;
46+
$github = new push($body);
47+
try {
48+
$res = $wechat->sendMarkdownMessage($github->getMessage());
49+
} catch (Exception $e) {
50+
$wechat->sendMessage($e->getCode() ."error: ".$e->getMessage());
51+
}
52+
break;
53+
case "pull_request":
54+
$github = new pull_request($body);
55+
try {
56+
$res = $wechat->sendMarkdownMessage($github->getMessage());
57+
} catch (Exception $e) {
58+
$wechat->sendMessage($e->getCode() ."error: ".$e->getMessage());
6059
}
61-
$message .= "\nSHA: ".$body['after'];
62-
$res = $wechat->sendMarkdownMessage($message);
6360
break;
6461
}
6562

src/markdown.class.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
namespace WeWork;
3+
4+
/**
5+
* @describe 企业微信Markdown类型消息
6+
* @see https://work.weixin.qq.com/api/doc/90000/90136/91770#h3-markdown-
7+
* @copyright AHDark
8+
* @since 7.2
9+
*/
10+
class markdown
11+
{
12+
protected $message = "";
13+
14+
/**
15+
* @param string $title
16+
* @param string $text
17+
*/
18+
function __construct(string $title="", string $text="") {
19+
$this->addTitle($title);
20+
$this->addMessage($text);
21+
}
22+
23+
/**
24+
* @describe 获取全部消息
25+
* @return string
26+
*/
27+
public function message(): string
28+
{
29+
return $this->message;
30+
}
31+
32+
/**
33+
* @describe 添加一段消息
34+
* @param string $message
35+
* @return bool
36+
*/
37+
protected function addMessage(string $message): bool
38+
{
39+
$this->message .= $message."\n\n";
40+
return true;
41+
}
42+
43+
/**
44+
* @describe 添加一段标题
45+
* @param string $title 标题内容
46+
* @param int $level '#'数量
47+
* @return bool
48+
*/
49+
public function addTitle(string $title,int $level = 3): bool
50+
{
51+
if($level>6) $level=6;
52+
if($level<1) $level=1;
53+
54+
return $this->addMessage(str_repeat("#", $level)." ".$title);
55+
}
56+
57+
/**
58+
* @describe 添加一段文字
59+
* @param string $text 文字内容
60+
* @return bool
61+
*/
62+
public function addText(string $text): bool
63+
{
64+
return $this->addMessage($text);
65+
}
66+
67+
/**
68+
* @describe 添加一段列表
69+
* @param string|array $list
70+
* @return bool
71+
*/
72+
public function addList($list): bool
73+
{
74+
if(gettype($list)=="string") {
75+
return $this->addMessage($list);
76+
} else if (gettype($list)=="array") {
77+
$return = "";
78+
foreach ($list as $k => $v) {
79+
if(is_int($k)) {
80+
$return .= "- $v\n";
81+
} else {
82+
$return .= "- $k: $v\n";
83+
}
84+
}
85+
return $this->addMessage(substr($return,0,-1));
86+
} else {
87+
return false;
88+
}
89+
}
90+
91+
/**
92+
* @describe 添加一段引用
93+
* @param string|array $list
94+
* @return bool
95+
*/
96+
public function addQuote($list): bool
97+
{
98+
if(gettype($list)=="string") {
99+
return $this->addMessage($list);
100+
} else if (gettype($list)=="array") {
101+
$return = "";
102+
foreach ($list as $k => $v) {
103+
if(is_int($k)) {
104+
$return .= "> $v\n";
105+
} else {
106+
$return .= "> $k: $v\n";
107+
}
108+
}
109+
return $this->addMessage(substr($return,0,-1));
110+
} else {
111+
return false;
112+
}
113+
}
114+
115+
/**
116+
* @describe 返回链接格式
117+
* @param string $text 文字
118+
* @param string $url 链接
119+
* @return string Markdown格式超链接
120+
*/
121+
public function getLink(string $text, string $url): string
122+
{
123+
return "[$text]($url)";
124+
}
125+
126+
/**
127+
* @describe 返回代码块
128+
* @param string $code
129+
* @return string Markdown格式代码块
130+
*/
131+
public function getCode(string $code): string
132+
{
133+
return "`$code`";
134+
}
135+
136+
137+
/**
138+
* @describe 返回特殊文字格式
139+
* @param string $text
140+
* @param string $color Expect "info","comment","warning"
141+
* @return string 企业微信文字加颜色
142+
*/
143+
public function getColorText(string $text,string $color): string
144+
{
145+
if($color==="info"||$color==="comment"||$color==="warning") { return "<font color=\"$color\">$text</font>"; }
146+
else { return $text; }
147+
}
148+
}
File renamed without changes.

0 commit comments

Comments
 (0)