This repository was archived by the owner on Apr 12, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathReleaseController.php
More file actions
198 lines (183 loc) · 8.57 KB
/
Copy pathReleaseController.php
File metadata and controls
198 lines (183 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Module\CoreManagerModule\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use vierbergenlars\SemVer\version;
use Zikula\Component\Wizard\FormHandlerInterface;
use Zikula\Component\Wizard\Wizard;
use Zikula\Component\Wizard\WizardCompleteInterface;
use Zikula\Core\Controller\AbstractController;
use Zikula\Module\CoreManagerModule\Settings;
use Zikula\ThemeModule\Engine\Annotation\Theme;
/**
* @Route("/admin")
*/
class ReleaseController extends AbstractController
{
/**
* @Route("/add-release/{stage}")
* @Theme("admin")
* @param Request $request
* @param $stage
* @return RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function wizard(Request $request, $stage = null)
{
if (!$this->hasPermission('ZikulaCoreManagerModule:addRelease:', '::', ACCESS_ADD)) {
throw new AccessDeniedException();
}
$wizard = new Wizard($this->container, realpath(__DIR__ . '/../Resources/config/release-stages.yml'));
$currentStage = $wizard->getCurrentStage($stage);
if ($currentStage instanceof WizardCompleteInterface) {
return $currentStage->getResponse($request);
}
if ($wizard->isHalted()) {
$this->addFlash('danger', $wizard->getWarning());
// @todo..
return $this->render('');
}
$templateParams = $currentStage->getTemplateParams();
if ($currentStage instanceof FormHandlerInterface) {
$form = $this->container->get('form.factory')->create($currentStage->getFormType());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$currentStage->handleFormResult($form);
$url = $this->container->get('router')->generate('zikulacoremanagermodule_release_wizard', array('stage' => $wizard->getNextStage()->getName()), true);
return new RedirectResponse($url);
}
$templateParams['form'] = $form->createView();
}
return $this->render($currentStage->getTemplateName(), $templateParams);
}
/**
* @Route("/add-release-ajax", methods = {"POST"}, options={"i18n"=false,"expose"=true})
*
* @param Request $request
* @return JsonResponse
*/
public function ajax(Request $request)
{
if (!$this->hasPermission('ZikulaCoreManagerModule:addRelease:', '::', ACCESS_ADD)) {
throw new AccessDeniedException();
}
set_time_limit(300);
ignore_user_abort(true);
$stage = $request->request->get('stage', false);
if ($stage === false) {
throw new \RuntimeException('No stage parameter received.');
}
$dataHelper = $this->container->get('zikula_core_manager_module.progress_data_storage_helper');
$data = $dataHelper->getData();
if (empty($data) || $data === 'null' || $data === 'false' || $data === 'Array') {
throw new \RuntimeException('Could not decode user data.');
}
$gitHubApiWrapper = $this->get('zikula_core_manager_module.github_api_wrapper');
$result = false;
switch ($stage) {
case 'create-qa-ticket':
// guess the milestone to use
$milestone = $gitHubApiWrapper->getMilestoneByCoreVersion(new version($data['version']));
// create title
$title = 'QA testing for release of ' . $data['version'];
// create issue without body
$return = $gitHubApiWrapper->createIssue(
$title,
'Further information follows in just a second my dear email reader. Checkout the issue already!.',
$milestone,
Settings::$QA_ISSUE_LABELS
);
if (!isset($return['number'])) {
break;
}
$issueNumber = $return['number'];
// prepare replacement array
$keys = array_map(function ($val) {
return '%' . strtoupper($val) . '%';
}, array_keys($data));
$values = array_values($data);
$replacement = array_combine($keys, $values);
$replacement['%QAISSUE%'] = $issueNumber;
// replace placeholders in issue body
$body = strtr(Settings::$QA_ISSUE_TEMPLATE, $replacement);
// update the issue
$return = $gitHubApiWrapper->updateIssue($issueNumber, null, $body);
if (isset($return['number'])) {
$data['github_qa_ticket_url'] = $return['html_url'];
$dataHelper->setData($data);
$result = true;
}
break;
case 'create-distribution-tag':
// disabled because the tag actually isn't created despite a successful response
//$commits = $gitHubApiWrapper->getLastNCommitsOfBranch('dist', 'master', 1);
//$lastCommit = array_shift($commits);
//$gitHubApiWrapper->createDistributionTag($data['tag'], $data['tag'], $lastCommit['sha']);
$result = true;
break;
case 'download-artifacts':
ini_set('memory_limit', '2G');
$data['artifactsArchivePath'] = $gitHubApiWrapper->downloadReleaseAssets($data['artifactsUrl']);
$dataHelper->setData($data);
$result = true;
break;
case 'create-core-release':
if (!isset($data['github_qa_ticket_url'])) {
$data['github_qa_ticket_url'] = '';
}
$description = str_replace('%QATICKETURL%', $data['github_qa_ticket_url'], $data['description']);
$return = $gitHubApiWrapper->createRelease('core', $data['title'], $description, $data['isPreRelease'], $data['tag'], $data['commit']);
if (isset($return['id'])) {
$data['github_core_release_id'] = $return['id'];
$dataHelper->setData($data);
$result = true;
}
break;
case 'create-distribution-release':
if (!isset($data['github_qa_ticket_url'])) {
$data['github_qa_ticket_url'] = '';
}
$description = str_replace('%QATICKETURL%', $data['github_qa_ticket_url'], $data['description']);
$commits = $gitHubApiWrapper->getLastNCommitsOfBranch('dist', 'master', 1);
$lastCommit = array_shift($commits);
$return = $gitHubApiWrapper->createRelease('dist', $data['title'], $description, $data['isPreRelease'], $data['tag'], $lastCommit['sha']);
if (isset($return['id'])) {
$data['github_distribution_release_id'] = $return['id'];
$dataHelper->setData($data);
$result = true;
}
break;
case 'copy-assets-to-core':
ini_set('memory_limit', '2G');
$result = $gitHubApiWrapper->createReleaseAssets('core', $data['github_core_release_id'], $data['artifactsArchivePath']);
break;
case 'copy-assets-to-distribution':
ini_set('memory_limit', '2G');
$result = $gitHubApiWrapper->createReleaseAssets('dist', $data['github_distribution_release_id'], $data['artifactsArchivePath']);
break;
case 'close-core-milestone':
// guess the milestone to close.
$milestone = $gitHubApiWrapper->getMilestoneByCoreVersion(new version($data['version']));
if (null !== $milestone) {
$return = $gitHubApiWrapper->closeMilestone($milestone);
$result = isset($return['number']);
} else {
$result = true;
}
break;
case 'finish':
break;
default:
throw new \RuntimeException('Invalid stage parameter received');
}
return new JsonResponse(['status' => $result]);
}
}