forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit.php
More file actions
149 lines (123 loc) · 3.61 KB
/
Copy pathGit.php
File metadata and controls
149 lines (123 loc) · 3.61 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
<?php
namespace PHPCensor\Plugin;
use PHPCensor\Plugin;
/**
* Git plugin.
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class Git extends Plugin
{
protected $actions = [];
/**
* @return string
*/
public static function pluginName()
{
return 'git';
}
/**
* 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.
* @param $action
* @param array $options
* @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.
* @param $options
* @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.
* @param $options
* @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']);
}
if (array_key_exists('message', $options)) {
$message = $this->builder->interpolate($options['message']);
}
$cmd = 'git tag %s -m "%s"';
return $this->builder->executeCommand($cmd, $tagName, $message);
}
/**
* Handle a pull action.
* @param $options
* @return bool
*/
protected function runPullAction($options)
{
$branch = $this->build->getBranch();
$remote = 'origin';
if (array_key_exists('branch', $options)) {
$branch = $this->builder->interpolate($options['branch']);
}
if (array_key_exists('remote', $options)) {
$remote = $this->builder->interpolate($options['remote']);
}
return $this->builder->executeCommand('git pull %s %s', $remote, $branch);
}
/**
* Handle a push action.
* @param $options
* @return bool
*/
protected function runPushAction($options)
{
$branch = $this->build->getBranch();
$remote = 'origin';
if (array_key_exists('branch', $options)) {
$branch = $this->builder->interpolate($options['branch']);
}
if (array_key_exists('remote', $options)) {
$remote = $this->builder->interpolate($options['remote']);
}
return $this->builder->executeCommand('git push %s %s', $remote, $branch);
}
}