-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathGit.php
More file actions
164 lines (133 loc) · 3.91 KB
/
Copy pathGit.php
File metadata and controls
164 lines (133 loc) · 3.91 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
<?php
namespace PHPCensor\Plugin;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Git plugin.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Git extends Plugin
{
protected $actions = [];
/**
* @return string
*/
public static function pluginName()
{
return 'git';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$this->actions = $options;
}
/**
* Run the Git plugin.
* @return bool
*/
public function execute()
{
// Check if there are any actions to be run for the branch we're running on:
if (!\array_key_exists($this->build->getBranch(), $this->actions)) {
return true;
}
$success = true;
foreach ($this->actions[$this->build->getBranch()] as $action => $options) {
if (!$this->runAction($action, $options)) {
$success = false;
break;
}
}
return $success;
}
/**
* Determine which action to run, and run it.
*
*
* @return bool
*/
protected function runAction($action, array $options = [])
{
switch ($action) {
case 'merge':
return $this->runMergeAction($options);
case 'tag':
return $this->runTagAction($options);
case 'pull':
return $this->runPullAction($options);
case 'push':
return $this->runPushAction($options);
}
return false;
}
/**
* Handle a merge action.
* @return bool
*/
protected function runMergeAction($options)
{
if (\array_key_exists('branch', $options)) {
$cmd = 'cd "%s" && git checkout %s && git merge "%s"';
$path = $this->builder->buildPath;
return $this->builder->executeCommand($cmd, $path, $options['branch'], $this->build->getBranch());
}
}
/**
* Handle a tag action.
* @return bool
*/
protected function runTagAction($options)
{
$tagName = \date('Ymd-His');
$message = \sprintf('Tag created by PHP Censor: %s', \date('Y-m-d H:i:s'));
if (\array_key_exists('name', $options)) {
$tagName = $this->builder->interpolate($options['name'], true);
}
if (\array_key_exists('message', $options)) {
$message = $this->builder->interpolate($options['message'], true);
}
$cmd = 'git tag %s -m "%s"';
return $this->builder->executeCommand($cmd, $tagName, $message);
}
/**
* Handle a pull action.
* @return bool
*/
protected function runPullAction($options)
{
$branch = $this->build->getBranch();
$remote = 'origin';
if (\array_key_exists('branch', $options)) {
$branch = $this->builder->interpolate($options['branch'], true);
}
if (\array_key_exists('remote', $options)) {
$remote = $this->builder->interpolate($options['remote'], true);
}
return $this->builder->executeCommand('git pull %s %s', $remote, $branch);
}
/**
* Handle a push action.
* @return bool
*/
protected function runPushAction($options)
{
$branch = $this->build->getBranch();
$remote = 'origin';
if (\array_key_exists('branch', $options)) {
$branch = $this->builder->interpolate($options['branch'], true);
}
if (\array_key_exists('remote', $options)) {
$remote = $this->builder->interpolate($options['remote'], true);
}
return $this->builder->executeCommand('git push %s %s', $remote, $branch);
}
}