Skip to content

Commit

Permalink
Merge pull request #5 from reallyli/add-wechat-bot-notification
Browse files Browse the repository at this point in the history
支持企业微信机器人的 webhook
  • Loading branch information
reallyli committed Jun 14, 2019
2 parents 9796711 + d27c210 commit 07c2ec6
Show file tree
Hide file tree
Showing 11 changed files with 1,085 additions and 978 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
"php": "^7.1.3",
"deployer/deployer": "^6.1",
"deployer/recipes": "^6.0",
"illuminate/support": "^5.5"
"illuminate/support": "5.5.*"
},
"require-dev": {
"mockery/mockery": "^1.0",
"codedungeon/phpunit-result-printer": "^0.19.13",
"phpunit/phpunit": "^7.0"
"orchestra/testbench": "^3.5",
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
1,891 changes: 987 additions & 904 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions deploy.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ options:
shared_dirs: null
log_file_name: laravel.log
log_lines: 200
notify_by: wechat_bot
app_repo_url: https://github.com
hosts: []
localhost: []
include: []
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer">
stopOnFailure="false">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">
Expand Down
2 changes: 1 addition & 1 deletion src/recipe/laravel-deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

require 'task/init.php';
require 'task/notify.php';
require 'task/notification.php';

require 'task/defaults.php';
require 'task/helpers.php';
Expand Down
72 changes: 72 additions & 0 deletions src/task/notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Deployer;

function sendHttpRequest($url, $formParams)
{
$formParams = json_encode($formParams);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $formParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
[
'Content-Type: application/json',
'Content-Length: '.strlen($formParams),
]
);

return curl_exec($ch);
}

function sendGroupMessage($subject)
{
$url = get('notify_channel_url');

if (! $url) {
throw new \InvalidArgumentException('[Laravel-Deployer]Notification is on but channel url is not set!');
}

$notifyBy = get('notify_by', 'webhook');

switch ($notifyBy) {
case 'wechat_bot':
$formParams = [
'msgtype' => 'news',
'news' => [
'articles' => [
[
'title' => get('user').' '.$subject,
'description' => ''.get('environment').' 环境更新 '.get('branch').' 分支 ',
'url' => get('app_repo_url', 'https://github.com'),
'picurl' => get('pic_url', 'https://picsum.photos/id/'.rand(1, 1000).'/800/600'),
],
],
],
];
break;
default:
$content = implode("\n", [
$subject,
'应用名称: '.get('application'),
'发布者: '.get('user'),
'分支名: '.get('branch'),
'环境: '.get('environment'),
]);
$formParams = ['text' => $content];
break;
}

return get('group_notify') ? sendHttpRequest($url, $formParams) : writeln($content);
}

task('success:notify', function () {
return sendGroupMessage('成功发布新版本!');
})->local();

task('failed:notify', function () {
return sendGroupMessage('发布新版本失败!');
})->local();
55 changes: 0 additions & 55 deletions src/task/notify.php

This file was deleted.

4 changes: 3 additions & 1 deletion tests/.build/deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

set('strategy', 'basic');
set('application', 'LaravelDeployer');
set('application', 'Example');
set('keep_releases', 6);
set('php_fpm_service', 'php7.2-fpm');
set('group_notify', false);
Expand All @@ -22,6 +22,8 @@
set('shared_dirs', null);
set('log_file_name', 'laravel.log');
set('log_lines', 200);
set('notify_by', 'wechat_bot');
set('app_repo_url', 'https://github.com');

/*
* Hosts and localhost
Expand Down
4 changes: 3 additions & 1 deletion tests/Features/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ hooks:
success: 'record:revision:log'
fail: { }
options:
application: LaravelDeployer
application: Example
keep_releases: 6
php_fpm_service: php7.2-fpm
group_notify: false
Expand All @@ -17,6 +17,8 @@ options:
shared_dirs: null
log_file_name: laravel.log
log_lines: 200
notify_by: wechat_bot
app_repo_url: 'https://github.com'
hosts: { }
localhost: { }
include: { }
Expand Down
18 changes: 10 additions & 8 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ public function __construct($name = null, array $data = [], $dataName = '')
parent::__construct($name, $data, $dataName);
}

/**
* sep up.
*/
public function setUp()
{
parent::setUp();
}

/**
* @param \Illuminate\Foundation\Application $app
*/
Expand All @@ -68,4 +60,14 @@ protected function getPackageProviders($app)
{
return ['Reallyli\LaravelDeployer\LaravelDeployerServiceProvider'];
}

public function setUp(): void
{
parent::setUp();
}

public function tearDown(): void
{
// parent::tearDown();
}
}
5 changes: 3 additions & 2 deletions tests/YamlParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public function testConfigFile()
public function testDeployFile()
{
$deployConfig = $this->deployYmlConfig;
array_set($deployConfig, 'options.application', 'LaravelDeployer');
$deployConfig['options']['application'] = 'Example';

$deployConfigToYml = Yaml::dump($deployConfig);

app(Filesystem::class)->put($this->getConfigFullPath(), $deployConfigToYml);

$this->assertTrue(str_contains($this->deployExecutableFileContent, 'LaravelDeployer'));
$this->assertTrue(strpos($this->deployExecutableFileContent, 'Example') !== false);
}
}

0 comments on commit 07c2ec6

Please sign in to comment.