Skip to content

Commit 736b5a9

Browse files
FenikkusuDan Cryer
authored and
Dan Cryer
committed
GitHub private pull request (dancryer#1323)
* Updating Remote Build To Have 'runBySsh' Method - Separates Out SSH Logic So Child Classes Can Easily Make SSH Based Commands * Updating GitHub Pull Request To Utilize SSH Call * Adding Additional Logic To Maintain Original http Methods Correctly.
1 parent 3400209 commit 736b5a9

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

PHPCI/Model/Build/GithubBuild.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ protected function postCloneSetup(Builder $builder, $cloneTo)
174174
$remoteBranch = $this->getExtra('remote_branch');
175175

176176
$cmd = 'cd "%s" && git checkout -b phpci/' . $this->getId() . ' %s && git pull -q --no-edit %s %s';
177-
$success = $builder->executeCommand($cmd, $cloneTo, $this->getBranch(), $remoteUrl, $remoteBranch);
177+
if ($this->canRunSsh()) {
178+
$success = $this->runBySsh($builder, $cloneTo, $cmd, [$cloneTo, $this->getBranch(), $remoteUrl, $remoteBranch]);
179+
} else {
180+
$success = $builder->executeCommand($cmd, $cloneTo, $this->getBranch(), $remoteUrl, $remoteBranch);
181+
}
178182
}
179183
} catch (\Exception $ex) {
180184
$success = false;

PHPCI/Model/Build/RemoteGitBuild.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ protected function getCloneUrl()
3333
*/
3434
public function createWorkingCopy(Builder $builder, $buildPath)
3535
{
36-
$key = trim($this->getProject()->getSshPrivateKey());
37-
38-
if (!empty($key)) {
36+
if ($this->canRunSsh()) {
3937
$success = $this->cloneBySsh($builder, $buildPath);
4038
} else {
4139
$success = $this->cloneByHttp($builder, $buildPath);
@@ -77,32 +75,42 @@ protected function cloneByHttp(Builder $builder, $cloneTo)
7775
*/
7876
protected function cloneBySsh(Builder $builder, $cloneTo)
7977
{
80-
$keyFile = $this->writeSshKey($cloneTo);
81-
82-
if (!IS_WIN) {
83-
$gitSshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
84-
}
85-
8678
// Do the git clone:
87-
$cmd = 'git clone --recursive ';
88-
79+
$cmd = 'git clone --recursive ';
8980
$depth = $builder->getConfig('clone_depth');
90-
9181
if (!is_null($depth)) {
9282
$cmd .= ' --depth ' . intval($depth) . ' ';
9383
}
9484

9585
$cmd .= ' -b %s %s "%s"';
9686

87+
$success = $this->runBySsh($builder, $cloneTo, $cmd, [$this->getBranch(), $this->getCloneUrl(), $cloneTo]);
88+
89+
if ($success) {
90+
$success = $this->postCloneSetup($builder, $cloneTo);
91+
}
92+
93+
return $success;
94+
}
95+
96+
protected function canRunSsh() {
97+
$key = trim($this->getProject()->getSshPrivateKey());
98+
return !empty($key);
99+
}
100+
101+
protected function runBySsh(Builder $builder, $cloneTo, $runCommand, $runArguments) {
102+
$keyFile = $this->writeSshKey($cloneTo);
103+
104+
$cmd = $runCommand;
105+
97106
if (!IS_WIN) {
107+
$gitSshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
98108
$cmd = 'export GIT_SSH="'.$gitSshWrapper.'" && ' . $cmd;
99109
}
100110

101-
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
111+
array_unshift($runArguments, $cmd);
102112

103-
if ($success) {
104-
$success = $this->postCloneSetup($builder, $cloneTo);
105-
}
113+
$success = call_user_func_array([$builder, 'executeCommand'], $runArguments);
106114

107115
// Remove the key file and git wrapper:
108116
unlink($keyFile);

0 commit comments

Comments
 (0)