1
|
|
|
<?php namespace G2R; |
2
|
|
|
|
3
|
|
|
use G2R\Config\ProjectConfig; |
4
|
|
|
use G2R\Config\RundeckConfig; |
5
|
|
|
use G2R\Exception\Exception; |
6
|
|
|
use G2R\Gitlab\GitlabHook; |
7
|
|
|
use G2R\Gitlab\HookResolver; |
8
|
|
|
use G2R\Rundeck\JobRunner; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\LoggerTrait; |
11
|
|
|
|
12
|
|
|
class Adapter |
13
|
|
|
{ |
14
|
|
|
use LoggerTrait; |
15
|
|
|
|
16
|
|
|
protected $logger; |
17
|
|
|
|
18
|
|
|
protected $jobRunner; |
19
|
|
|
|
20
|
|
|
protected $projectsConfig; |
21
|
|
|
|
22
|
|
|
protected $hook; |
23
|
|
|
|
24
|
|
|
protected $input; |
25
|
|
|
|
26
|
|
|
public static function factory( |
27
|
|
|
$rundeckConfig, |
28
|
|
|
$projectConfig, |
29
|
|
|
$logger = null |
30
|
|
|
) { |
31
|
|
|
$adapter = new self($logger); |
32
|
|
|
|
33
|
|
|
// attach jobRunner |
34
|
|
|
$adapter->attachJobRunner( |
35
|
|
|
new JobRunner(new RundeckConfig($rundeckConfig)) |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
// load the projects config |
39
|
|
|
$adapter->loadProjectsConfig( |
40
|
|
|
new ProjectConfig($projectConfig) |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
return $adapter; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Eventually attach a Logger |
48
|
|
|
* |
49
|
|
|
* @param LoggerInterface|null $logger |
50
|
|
|
*/ |
51
|
12 |
|
public function __construct(LoggerInterface $logger = null) |
52
|
|
|
{ |
53
|
12 |
|
$this->logger = $logger; |
54
|
12 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Run the adapter |
58
|
|
|
* |
59
|
|
|
* @throws Exception |
60
|
|
|
*/ |
61
|
10 |
|
public function run() |
62
|
|
|
{ |
63
|
10 |
|
$project = $this->getProjectsConfig()->getProject( |
64
|
8 |
|
$this->getHook()->getProjectName(), |
65
|
8 |
|
$this->getHook()->getRef() |
66
|
4 |
|
); |
67
|
|
|
|
68
|
|
|
// before we run the job, we want to make sure the build passed or |
69
|
|
|
// the runOnFail is true |
70
|
|
|
|
71
|
8 |
|
if ($this->getHook()->getBuildStatus() != 'success' && |
72
|
6 |
|
!$project['runOnFail'] |
73
|
4 |
|
) { |
74
|
2 |
|
$this->warning( |
75
|
2 |
|
"The project config for {$this->getHook()->getProjectName()} " . |
76
|
1 |
|
"doesn't enable to run the job when the build failed" |
77
|
1 |
|
); |
78
|
|
|
|
79
|
2 |
|
return 0; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
$this->info("Running the job {$project['jobId']}"); |
83
|
|
|
|
84
|
6 |
|
return $this->getJobRunner()->run($project['jobId']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Load the rundeck job runner |
89
|
|
|
* |
90
|
|
|
* @param JobRunner $jobRunner |
91
|
|
|
*/ |
92
|
8 |
|
public function attachJobRunner(JobRunner $jobRunner) |
93
|
|
|
{ |
94
|
8 |
|
$this->info( |
95
|
8 |
|
"Job runner uses '{$jobRunner->getConfig()->getFilename()}' config" |
96
|
4 |
|
); |
97
|
|
|
|
98
|
8 |
|
$this->jobRunner = $jobRunner; |
99
|
8 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Load the projects config |
103
|
|
|
* |
104
|
|
|
* @param ProjectConfig $projectsConfig |
105
|
|
|
*/ |
106
|
8 |
|
public function loadProjectsConfig(ProjectConfig $projectsConfig) |
107
|
|
|
{ |
108
|
8 |
|
$this->info( |
109
|
8 |
|
"Loading projects config file '{$projectsConfig->getFilename()}'" |
110
|
4 |
|
); |
111
|
|
|
|
112
|
8 |
|
$this->projectsConfig = $projectsConfig; |
113
|
8 |
|
} |
114
|
|
|
|
115
|
10 |
|
private function getProjectsConfig() |
116
|
|
|
{ |
117
|
10 |
|
if (!isset($this->projectsConfig)) { |
118
|
2 |
|
throw new Exception('The projects config is missing'); |
119
|
|
|
} |
120
|
|
|
|
121
|
8 |
|
return $this->projectsConfig; |
122
|
|
|
} |
123
|
|
|
|
124
|
6 |
|
private function getJobRunner() |
125
|
|
|
{ |
126
|
6 |
|
if (!isset($this->jobRunner)) { |
127
|
2 |
|
throw new Exception('The Job runner is missing'); |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
return $this->jobRunner; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the hook |
135
|
|
|
* |
136
|
|
|
* @return HookResolver |
137
|
|
|
*/ |
138
|
8 |
|
private function getHook() |
139
|
|
|
{ |
140
|
8 |
|
if (!isset($this->hook)) { |
141
|
|
|
$this->hook = HookResolver::load($this->getInput()); |
142
|
|
|
|
143
|
|
|
$this->debug('Hook handler type: ' . get_class($this->hook)); |
144
|
|
|
} |
145
|
|
|
|
146
|
8 |
|
return $this->hook; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the input (php://input by default) |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
private function getInput() |
155
|
|
|
{ |
156
|
|
|
if (!isset($this->input)) { |
157
|
|
|
$this->debug('no input specified. Default php://input'); |
158
|
|
|
|
159
|
|
|
$this->input = file_get_contents('php://input'); |
160
|
|
|
|
161
|
|
|
$this->debug('input content: ' . $this->input); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->input; |
165
|
|
|
} |
166
|
|
|
|
167
|
10 |
|
public function setHook(GitlabHook $hook) |
168
|
|
|
{ |
169
|
10 |
|
$this->hook = $hook; |
170
|
10 |
|
} |
171
|
|
|
|
172
|
|
|
public function setInput($input) |
173
|
|
|
{ |
174
|
|
|
$this->input = $input; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Call the logger |
179
|
|
|
* |
180
|
|
|
* @param $level |
181
|
|
|
* @param $message |
182
|
|
|
* @param array $context |
183
|
|
|
*/ |
184
|
12 |
|
public function log($level, $message, array $context = array()) |
185
|
|
|
{ |
186
|
12 |
|
if ($this->logger) { |
187
|
12 |
|
$this->logger->log($level, $message, $context); |
188
|
6 |
|
} |
189
|
12 |
|
} |
190
|
|
|
} |
191
|
|
|
|