forked from meolu/walle-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskController.php
More file actions
206 lines (185 loc) · 7.07 KB
/
Copy pathTaskController.php
File metadata and controls
206 lines (185 loc) · 7.07 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
199
200
201
202
203
204
205
206
<?php
namespace app\controllers;
use yii;
use yii\data\Pagination;
use app\components\Controller;
use app\models\Task;
use app\models\Project;
use app\models\User;
use app\models\Group;
use app\components\Repo;
class TaskController extends Controller {
protected $task;
public function actionIndex($page = 1, $size = 10) {
$size = $this->getParam('per-page') ?: $size;
$list = Task::find()
->with('user')
->with('project')
->where(['user_id' => $this->uid]);
// 有审核权限的任务
$auditProjects = Group::getAuditProjectIds($this->uid);
if ($auditProjects) {
$list->orWhere(['project_id' => $auditProjects]);
}
$kw = \Yii::$app->request->post('kw');
if ($kw) {
$list->andWhere(['or', "commit_id like '%" . $kw . "%'", "title like '%" . $kw . "%'"]);
}
$tasks = $list->orderBy('id desc');
$list = $tasks->offset(($page - 1) * $size)->limit(10)
->asArray()->all();
$pages = new Pagination(['totalCount' => $tasks->count(), 'pageSize' => 10]);
return $this->render('list', [
'list' => $list,
'pages' => $pages,
'audit' => $auditProjects,
]);
}
/**
* 提交任务
*
* @param $projectId
* @return string
*/
public function actionSubmit($projectId = null) {
$task = new Task();
if ($projectId) {
// svn下无trunk
$nonTrunk = false;
$conf = Project::find()
->where(['id' => $projectId, 'status' => Project::STATUS_VALID])
->one();
$conf = Project::getConf($projectId);
// 第一次可能会因为更新而耗时,但一般不会,第一次初始化会是在检测里
if ($conf->repo_type == Project::REPO_SVN && !file_exists(Project::getDeployFromDir())) {
$version = Repo::getRevision($conf);
$version->updateRepo();
}
// 为了简化svn无trunk, branches时,不需要做查看分支,直接就是主干
$svnTrunk = sprintf('%s/trunk', Project::getDeployFromDir());
// svn下无trunk目录
if (!file_exists($svnTrunk)) {
$nonTrunk = true;
}
}
if (\Yii::$app->request->getIsPost()) {
if (!$conf) throw new \Exception(yii::t('task', 'unknown project'));
$group = Group::find()
->where(['user_id' => $this->uid, 'project_id' => $projectId])
->count();
if (!$group) throw new \Exception(yii::t('task', 'you are not the member of project'));
if ($task->load(\Yii::$app->request->post())) {
// 是否需要审核
$status = $conf->audit == Project::AUDIT_YES ? Task::STATUS_SUBMIT : Task::STATUS_PASS;
$task->user_id = $this->uid;
$task->project_id = $projectId;
$task->status = $status;
if ($task->save()) {
return $this->redirect('/task/');
}
}
}
if ($projectId) {
$tpl = $conf->repo_type == Project::REPO_GIT ? 'submit-git' : 'submit-svn';
return $this->render($tpl, [
'task' => $task,
'conf' => $conf,
'nonTrunk' => $nonTrunk,
]);
}
// 成员所属项目
$projects = Project::find()
->leftJoin(Group::tableName(), '`group`.project_id=project.id')
->where(['project.status' => Project::STATUS_VALID, '`group`.user_id' => $this->uid])
->asArray()->all();
return $this->render('select-project', [
'projects' => $projects,
]);
}
/**
* 任务删除
*
* @return string
* @throws \Exception
*/
public function actionDelete($taskId) {
$task = Task::findOne($taskId);
if (!$task) {
throw new \Exception(yii::t('task', 'unknown deployment bill'));
}
if ($task->user_id != $this->uid) {
throw new \Exception(yii::t('w', 'you are not master of project'));
}
if ($task->status == Task::STATUS_DONE) {
throw new \Exception(yii::t('task', 'can\'t delele the job which is done'));
}
if (!$task->delete()) throw new \Exception(yii::t('w', 'delete failed'));
$this->renderJson([]);
}
/**
* 生成回滚任务
*
* @return string
* @throws \Exception
*/
public function actionRollback($taskId) {
$this->task = Task::findOne($taskId);
if (!$this->task) {
throw new \Exception(yii::t('task', 'unknown deployment bill'));
}
if ($this->task->user_id != $this->uid) {
throw new \Exception(yii::t('w', 'you are not master of project'));
}
if ($this->task->ex_link_id == $this->task->link_id) {
throw new \Exception(yii::t('task', 'no rollback twice'));
}
$conf = Project::find()
->where(['id' => $this->task->project_id, 'status' => Project::STATUS_VALID])
->one();
if (!$conf) {
throw new \Exception(yii::t('task', 'can\'t rollback the closed project\'s job'));
}
// 是否需要审核
$status = $conf->audit == Project::AUDIT_YES ? Task::STATUS_SUBMIT : Task::STATUS_PASS;
$rollbackTask = new Task();
$rollbackTask->attributes = [
'user_id' => $this->uid,
'project_id' => $this->task->project_id,
'status' => $status,
'action' => Task::ACTION_ROLLBACK,
'link_id' => $this->task->ex_link_id,
'ex_link_id' => $this->task->ex_link_id,
'title' => $this->task->title . ' - ' . yii::t('task', 'rollback'),
'commit_id' => $this->task->commit_id,
];
if ($rollbackTask->save()) {
$url = $conf->audit == Project::AUDIT_YES
? '/task/'
: '/walle/deploy?taskId=' . $rollbackTask->id;
$this->renderJson([
'url' => $url,
]);
} else {
$this->renderJson([], -1, yii::t('task', 'create a rollback job failed'));
}
}
/**
* 任务审核
*
* @param $id
* @param $operation
*/
public function actionTaskOperation($id, $operation) {
$task = Task::findOne($id);
if (!$task) {
static::renderJson([], -1, yii::t('task', 'unknown deployment bill'));
}
// 是否为该项目的审核管理员(超级管理员可以不用审核,如果想审核就得设置为审核管理员,要不只能维护配置)
if (!Group::isAuditAdmin($this->uid, $task->project_id)) {
throw new \Exception(yii::t('w', 'you are not master of project'));
}
$task->status = $operation ? Task::STATUS_PASS : Task::STATUS_REFUSE;
$task->save();
static::renderJson(['status' => \Yii::t('w', 'task_status_' . $task->status)]);
}
}