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

Commit db722c6

Browse files
committed
Formatting and fix some bugs
1 parent 1e5cad0 commit db722c6

File tree

6 files changed

+120
-107
lines changed

6 files changed

+120
-107
lines changed

src/github/github.class.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace GitHub;
44

5-
use Exception;
6-
75
class github
86
{
97
/**
108
* @var array Data from body
119
*/
1210
protected $data = [];
11+
/**
12+
* @var string Event Name
13+
*/
14+
protected $event = "";
1315

1416
/**
1517
* @describe 设置Data
@@ -21,11 +23,6 @@ public function setData(array $data): array
2123
return ($this->data = $data);
2224
}
2325

24-
/**
25-
* @var string Event Name
26-
*/
27-
protected $event = "";
28-
2926
/**
3027
* @describe 设置Event
3128
* @param string $event
@@ -38,6 +35,6 @@ public function setEvent(string $event): string
3835

3936
protected function isSet(): bool
4037
{
41-
return $this->data!=[]&&$this->event!="";
38+
return $this->data != [] && $this->event != "";
4239
}
4340
}

src/github/pull-request.class.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77

88
class pull_request extends github
99
{
10-
function __construct($data = null) {
11-
$this->setEvent("push");
12-
if($data!=null) {
13-
$this->setData($data);
14-
}
15-
}
16-
1710
private const translateList = [
1811
"opened" => "打开",
1912
"ready_for_review" => "候审",
@@ -25,16 +18,11 @@ function __construct($data = null) {
2518
"unassigned" => "取消分配"
2619
];
2720

28-
private function _e($text,bool $echo = false)
21+
function __construct($data = null)
2922
{
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;
23+
$this->setEvent("push");
24+
if ($data != null) {
25+
$this->setData($data);
3826
}
3927
}
4028

@@ -45,14 +33,26 @@ private function _e($text,bool $echo = false)
4533
*/
4634
public function getMessage(): string
4735
{
48-
if(!$this->isSet()) { throw new Exception("未设置data或event"); }
36+
if (!$this->isSet()) {
37+
throw new Exception("未设置data或event");
38+
}
4939

5040
$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);
41+
$message->addTitle($message->getLink("#" . $this->data['number'], $this->data['pull_request']['url']) . " 有新的 " . $message->getColorText("Pull Request", "info") . " " .$this->_e($this->data['action']));
42+
$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']));
5643
return $message->message();
5744
}
45+
46+
private function _e($text, bool $echo = false)
47+
{
48+
$return = self::translateList[$text];
49+
if ($text == "closed" && $this->data['merged']) {
50+
$return = self::translateList[$text] . "并成功合入分支";
51+
}
52+
if ($echo) {
53+
return print $return;
54+
} else {
55+
return $return;
56+
}
57+
}
5858
}

src/github/push.class.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@
66

77
class push extends github
88
{
9-
function __construct($data = null) {
9+
function __construct($data = null)
10+
{
1011
$this->setEvent("push");
11-
if($data!=null) {
12+
if ($data != null) {
1213
$this->setData($data);
1314
}
1415
}
1516

1617
/**
17-
* @throws Exception
1818
* @return string
1919
* @noinspection HtmlDeprecatedTag
20+
* @throws Exception
2021
*/
2122
public function getMessage(): string
2223
{
23-
if(!$this->isSet()) { throw new Exception("未设置data或event"); }
24+
if (!$this->isSet()) {
25+
throw new Exception("未设置data或event");
26+
}
2427
$body = $this->data;
2528
$message = <<<EOF
2629
### 有新的<font color=\"warning\">Push</font>事件
@@ -43,7 +46,7 @@ public function getMessage(): string
4346
4447
EOF;
4548
}
46-
$message .= "\nSHA: ".$body['after'];
49+
$message .= "\nSHA: " . $body['after'];
4750
return $message;
4851
}
4952
}

src/index.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
function object_to_array(object $obj): array
1414
{
15-
$_arr= get_object_vars($obj);
15+
$_arr = get_object_vars($obj);
1616
$arr = null;
17-
foreach($_arr as $key=>$val){
18-
$val=(is_array($val))||is_object($val)?object_to_array($val):$val;
19-
$arr[$key]=$val;
17+
foreach ($_arr as $key => $val) {
18+
$val = (is_array($val)) || is_object($val) ? object_to_array($val) : $val;
19+
$arr[$key] = $val;
2020
}
2121
return $arr;
2222
}
@@ -36,36 +36,38 @@ function main_handler($event, $context)
3636
$githubEvent = $headers['x-github-event'];
3737
$wechat = new GroupBot();
3838
$wechat->setSendKey($event->queryString->sendkey);
39-
$body = json_decode($event->body,1);
39+
$body = json_decode($event->body, 1);
4040

41-
switch($githubEvent) {
41+
switch ($githubEvent) {
4242
case "ping":
43-
$res = $wechat->sendMessage("收到Ping请求,字符串为".$body['zen'].",hookId为".$body['hook_id']."");
43+
$res = $wechat->sendMessage("收到Ping请求,字符串为" . $body['zen'] . ",hookId为" . $body['hook_id'] . "");
4444
break;
4545
case "push":
4646
$github = new push($body);
4747
try {
48-
$res = $wechat->sendMarkdownMessage($github->getMessage());
48+
$message = $github->getMessage();
49+
$res = $wechat->sendMarkdownMessage($message);
4950
} catch (Exception $e) {
50-
$wechat->sendMessage($e->getCode() ."error: ".$e->getMessage());
51+
$wechat->sendMessage($e->getCode() . "error: " . $e->getMessage());
5152
}
5253
break;
5354
case "pull_request":
5455
$github = new pull_request($body);
5556
try {
56-
$res = $wechat->sendMarkdownMessage($github->getMessage());
57+
$message = $github->getMessage();
58+
$res = $wechat->sendMarkdownMessage($message);
5759
} catch (Exception $e) {
58-
$wechat->sendMessage($e->getCode() ."error: ".$e->getMessage());
60+
$wechat->sendMessage($e->getCode() . "error: " . $e->getMessage());
5961
}
6062
break;
6163
}
6264

6365
return [
6466
"isBase64Encoded" => false,
65-
"statusCode" => isset($res['error'])?500:200,
67+
"statusCode" => isset($res['error']) ? 500 : 200,
6668
"headers" => '{"Content-Type":"application/json"}',
6769
"body" => [
68-
"code" => isset($res['error'])?500:200,
70+
"code" => isset($res['error']) ? 500 : 200,
6971
"message" => $res['error'] ?? "OK"
7072
]
7173
];

src/markdown.class.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace WeWork;
34

45
/**
@@ -9,24 +10,34 @@
910
*/
1011
class markdown
1112
{
12-
protected $message = "";
13+
protected $message = "";
1314

1415
/**
1516
* @param string $title
1617
* @param string $text
1718
*/
18-
function __construct(string $title="", string $text="") {
19-
$this->addTitle($title);
20-
$this->addMessage($text);
19+
function __construct(string $title = "", string $text = "")
20+
{
21+
if ($title != "") {
22+
$this->addTitle($title);
23+
}
24+
if ($text != "") {
25+
$this->addMessage($text);
26+
}
2127
}
2228

2329
/**
24-
* @describe 获取全部消息
25-
* @return string
30+
* @describe 添加一段标题
31+
* @param string $title 标题内容
32+
* @param int $level '#'数量
33+
* @return bool
2634
*/
27-
public function message(): string
35+
public function addTitle(string $title, int $level = 3): bool
2836
{
29-
return $this->message;
37+
if ($level > 6) $level = 6;
38+
if ($level < 1) $level = 1;
39+
40+
return $this->addMessage(str_repeat("#", $level) . " " . $title);
3041
}
3142

3243
/**
@@ -36,22 +47,17 @@ public function message(): string
3647
*/
3748
protected function addMessage(string $message): bool
3849
{
39-
$this->message .= $message."\n\n";
50+
$this->message .= $message . "\n\n";
4051
return true;
4152
}
4253

4354
/**
44-
* @describe 添加一段标题
45-
* @param string $title 标题内容
46-
* @param int $level '#'数量
47-
* @return bool
55+
* @describe 获取全部消息
56+
* @return string
4857
*/
49-
public function addTitle(string $title,int $level = 3): bool
58+
public function message(): string
5059
{
51-
if($level>6) $level=6;
52-
if($level<1) $level=1;
53-
54-
return $this->addMessage(str_repeat("#", $level)." ".$title);
60+
return $this->message;
5561
}
5662

5763
/**
@@ -71,18 +77,18 @@ public function addText(string $text): bool
7177
*/
7278
public function addList($list): bool
7379
{
74-
if(gettype($list)=="string") {
80+
if (gettype($list) == "string") {
7581
return $this->addMessage($list);
76-
} else if (gettype($list)=="array") {
82+
} else if (gettype($list) == "array") {
7783
$return = "";
7884
foreach ($list as $k => $v) {
79-
if(is_int($k)) {
85+
if (is_int($k)) {
8086
$return .= "- $v\n";
8187
} else {
8288
$return .= "- $k: $v\n";
8389
}
8490
}
85-
return $this->addMessage(substr($return,0,-1));
91+
return $this->addMessage(substr($return, 0, -1));
8692
} else {
8793
return false;
8894
}
@@ -95,18 +101,18 @@ public function addList($list): bool
95101
*/
96102
public function addQuote($list): bool
97103
{
98-
if(gettype($list)=="string") {
104+
if (gettype($list) == "string") {
99105
return $this->addMessage($list);
100-
} else if (gettype($list)=="array") {
106+
} else if (gettype($list) == "array") {
101107
$return = "";
102108
foreach ($list as $k => $v) {
103-
if(is_int($k)) {
109+
if (is_int($k)) {
104110
$return .= "> $v\n";
105111
} else {
106112
$return .= "> $k: $v\n";
107113
}
108114
}
109-
return $this->addMessage(substr($return,0,-1));
115+
return $this->addMessage(substr($return, 0, -1));
110116
} else {
111117
return false;
112118
}
@@ -139,10 +145,15 @@ public function getCode(string $code): string
139145
* @param string $text
140146
* @param string $color Expect "info","comment","warning"
141147
* @return string 企业微信文字加颜色
148+
* @noinspection HtmlDeprecatedTag
149+
* @noinspection HtmlDeprecatedAttribute
142150
*/
143-
public function getColorText(string $text,string $color): string
151+
public function getColorText(string $text, string $color): string
144152
{
145-
if($color==="info"||$color==="comment"||$color==="warning") { return "<font color=\"$color\">$text</font>"; }
146-
else { return $text; }
153+
if ($color === "info" || $color === "comment" || $color === "warning") {
154+
return "<font color=\"$color\">$text</font>";
155+
} else {
156+
return $text;
157+
}
147158
}
148159
}

0 commit comments

Comments
 (0)