|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class HookHandler |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $scripts; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Executor |
|
18
|
|
|
*/ |
|
19
|
|
|
private $executor; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
8 |
|
public function __construct(ContainerInterface $ci, Executor $executor) |
|
23
|
|
|
{ |
|
24
|
8 |
|
$this->executor = $executor; |
|
25
|
8 |
|
$this->scripts = (array) $ci->get('scripts'); |
|
26
|
8 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
1 |
|
public function handleDeploy(array $event, array $build) |
|
30
|
|
|
{ |
|
31
|
1 |
|
if ($build['status'] === 'created') { |
|
32
|
1 |
|
$projectName = $event['project']['path_with_namespace']; |
|
33
|
1 |
|
$buildId = $build['id']; |
|
34
|
1 |
|
$commitId = $event['commit']['id']; |
|
35
|
1 |
|
$name = $build['name']; |
|
36
|
1 |
|
if (isset($this->scripts[$projectName]['deploy'][$name])) { |
|
37
|
1 |
|
$this->executeCommand($this->scripts[$projectName]['deploy'][$name], [ |
|
38
|
1 |
|
'HOOK_PROJECT_PATH' => $projectName, |
|
39
|
1 |
|
'HOOK_ENV_NAME' => $name, |
|
40
|
1 |
|
'HOOK_BUILD_ID' => $buildId, |
|
41
|
|
|
'HOOK_BUILD_REF' => $commitId |
|
42
|
1 |
|
]); |
|
43
|
1 |
|
} |
|
44
|
1 |
|
} |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
1 |
|
public function handlePush(array $event) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$projectName = $event['project']['path_with_namespace']; |
|
51
|
1 |
|
$ref = $event['ref']; |
|
52
|
1 |
|
if (isset($this->scripts[$projectName]['push'][$ref])) { |
|
53
|
1 |
|
$this->executeCommand($this->scripts[$projectName]['push'][$ref], [ |
|
54
|
1 |
|
'HOOK_PROJECT_PATH' => $projectName, |
|
55
|
1 |
|
'HOOK_REF' => $ref, |
|
56
|
1 |
|
'HOOK_BRANCH'=> $this->extractBranchName($ref), |
|
57
|
1 |
|
'HOOK_BUILD_REF' => $event['after'] |
|
58
|
1 |
|
]); |
|
59
|
1 |
|
} |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
3 |
|
public function handleTag(array $event) |
|
64
|
|
|
{ |
|
65
|
3 |
|
$projectName = $event['project']['path_with_namespace']; |
|
66
|
3 |
|
if (isset($this->scripts[$projectName]['tag'])) { |
|
67
|
3 |
|
$this->executeCommand($this->scripts[$projectName]['tag'], [ |
|
68
|
3 |
|
'HOOK_PROJECT_PATH' => $projectName, |
|
69
|
3 |
|
'HOOK_REF' => $event['ref'], |
|
70
|
3 |
|
'HOOK_TAG'=> $this->extractTagName($event['ref']), |
|
71
|
3 |
|
'HOOK_BUILD_REF' => $event['after'] |
|
72
|
3 |
|
]); |
|
73
|
3 |
|
} |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $ref |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
1 |
|
private function extractBranchName($ref) |
|
82
|
|
|
{ |
|
83
|
1 |
|
return substr($ref, strlen('refs/heads/')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $ref |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
3 |
|
private function extractTagName($ref) |
|
92
|
|
|
{ |
|
93
|
3 |
|
return substr($ref, strlen('refs/tags/')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string|array $scriptPath |
|
99
|
|
|
* @param array $env |
|
100
|
|
|
*/ |
|
101
|
5 |
|
private function executeCommand($scriptPath, array $env) |
|
102
|
|
|
{ |
|
103
|
5 |
|
$this->executor->executeCommand($scriptPath, $env); |
|
104
|
5 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|