forked from meolu/walle-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.php
196 lines (170 loc) · 4.25 KB
/
Task.php
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
<?php
namespace app\models;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
use app\components\GlobalHelper;
/**
* This is the model class for table "task".
*
* @property integer $id
* @property string $user_id
* @property integer $project_id
* @property integer $action
* @property integer $status
* @property string $title
* @property string $link_id
* @property string $ex_link_id
* @property string $commit_id
* @property integer $created_at
* @property integer $updated_at
* @property string $branch
* @property string $file_list
*/
class Task extends \yii\db\ActiveRecord
{
/**
* 普通上线任务
*/
const ACTION_ONLINE = 0;
/**
* 回滚任务
*/
const ACTION_ROLLBACK = 1;
/**
* 任务新提交
*/
const STATUS_SUBMIT = 0;
/**
* 任务通过
*/
const STATUS_PASS = 1;
/**
* 任务拒绝
*/
const STATUS_REFUSE = 2;
/**
* 任务上线完成
*/
const STATUS_DONE = 3;
/**
* 任务上线失败
*/
const STATUS_FAILED = 4;
/**
* 可回滚
*/
const ROLLBACK_TRUE = 1;
/**
* 不可回滚
*/
const ROLLBACK_FALSE = 0;
/**
* 上线模式: 全量发布
*/
const FILE_TRANSMISSION_MODE_FULL = 1;
/**
* 上线模式: 指定文件列表
*/
const FILE_TRANSMISSION_MODE_PART = 2;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'task';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'project_id', 'status', 'title'], 'required'],
[['user_id', 'project_id', 'action', 'status', 'file_transmission_mode'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
[['file_list'], 'string'],
[['title', 'link_id', 'ex_link_id', 'commit_id', 'branch'], 'string', 'max' => 100],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'project_id' => 'Project ID',
'action' => 'Action',
'status' => 'Status',
'title' => '上线单标题',
'link_id' => 'Link ID',
'ex_link_id' => 'Ex Link ID',
'commit_id' => 'Commit ID',
'created_at' => 'Created At',
];
}
/**
* 是否能进行部署
*
* @param $status
* @return bool
*/
public static function canDeploy($status) {
return in_array($status, [static::STATUS_PASS, static::STATUS_FAILED]);
}
/**
* width('user')
*
* @return \yii\db\ActiveQuery
*/
public function getUser() {
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
/**
* with('project')
*
* @return \yii\db\ActiveQuery
*/
public function getProject() {
return $this->hasOne(Project::className(), ['id' => 'project_id']);
}
/**
* 获取要发布的文件列表
*
* @return array|string
*/
public function getCommandFiles() {
if ($this->file_transmission_mode == static::FILE_TRANSMISSION_MODE_FULL) {
return '.';
} elseif ($this->file_transmission_mode == static::FILE_TRANSMISSION_MODE_PART && $this->file_list) {
$fileList = GlobalHelper::str2arr($this->file_list);
$commandFiles = join(' ', $fileList);
return trim($commandFiles);
} else {
throw new \InvalidArgumentException('file list empty');
}
}
/**
* 取得回滚的当前commit_id
* @return bool|string
*/
public function getRollbackCommitId()
{
return $this->ex_link_id ? static::find()->where(['link_id'=>$this->ex_link_id])->orderBy(['id'=>SORT_ASC])->select('commit_id')->scalar():'';
}
}