1 | <?php namespace G2R; |
||
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( |
||
45 | |||
46 | /** |
||
47 | * Eventually attach a Logger |
||
48 | * |
||
49 | * @param LoggerInterface|null $logger |
||
50 | */ |
||
51 | 12 | public function __construct(LoggerInterface $logger = null) |
|
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 | 8 | ); |
|
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 | 4 | !$project['runOnFail'] |
|
73 | 8 | ) { |
|
74 | 2 | $this->warning( |
|
75 | 2 | "The project config for {$this->getHook()->getProjectName()} " . |
|
76 | "doesn't enable to run the job when the build failed" |
||
77 | 2 | ); |
|
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) |
|
100 | |||
101 | /** |
||
102 | * Load the projects config |
||
103 | * |
||
104 | * @param ProjectConfig $projectsConfig |
||
105 | */ |
||
106 | 8 | public function loadProjectsConfig(ProjectConfig $projectsConfig) |
|
114 | |||
115 | 10 | private function getProjectsConfig() |
|
123 | |||
124 | 6 | private function getJobRunner() |
|
132 | |||
133 | /** |
||
134 | * Get the hook |
||
135 | * |
||
136 | * @return HookResolver |
||
137 | */ |
||
138 | 8 | private function getHook() |
|
148 | |||
149 | /** |
||
150 | * Get the input (php://input by default) |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | private function getInput() |
||
166 | |||
167 | 10 | public function setHook(GitlabHook $hook) |
|
171 | |||
172 | public function setInput($input) |
||
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()) |
|
190 | } |
||
191 |