1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\WashingMachine\Commands; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Cz\Git\GitRepository; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Object to retrieve the command configuration based on environment variables and input. |
12
|
|
|
*/ |
13
|
|
|
class Config |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var InputInterface |
17
|
|
|
*/ |
18
|
|
|
private $input; |
19
|
|
|
|
20
|
|
|
public function __construct(InputInterface $input) |
21
|
|
|
{ |
22
|
|
|
$this->input = $input; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getCloverFilePath() : string |
26
|
|
|
{ |
27
|
|
|
return $this->input->getOption('clover'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getCrap4JFilePath() : string |
31
|
|
|
{ |
32
|
|
|
return $this->input->getOption('crap4j'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function getGitlabApiToken() : string |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$gitlabApiToken = $this->input->getOption('gitlab-api-token'); |
38
|
|
|
if ($gitlabApiToken === null) { |
39
|
|
|
$gitlabApiToken = getenv('GITLAB_API_TOKEN'); |
40
|
|
|
if ($gitlabApiToken === false) { |
41
|
|
|
throw new \RuntimeException('Could not find the Gitlab API token in the "GITLAB_API_TOKEN" environment variable. Either set this environment variable or pass the token via the --gitlab-api-token command line option.'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
return $gitlabApiToken; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getGitlabUrl() : string |
48
|
|
|
{ |
49
|
|
|
$gitlabUrl = $this->input->getOption('gitlab-url'); |
50
|
|
|
if ($gitlabUrl === null) { |
51
|
|
|
$ciProjectUrl = getenv('CI_REPOSITORY_URL'); |
52
|
|
|
if ($ciProjectUrl === false) { |
53
|
|
|
$ciProjectUrl = getenv('CI_BUILD_REPO'); |
54
|
|
|
if ($ciProjectUrl === false) { |
55
|
|
|
throw new \RuntimeException('Could not find the Gitlab URL in the "CI_REPOSITORY_URL" (Gitlab 9+) or "CI_BUILD_REPO" (Gitlab 8.x) environment variable (usually set by Gitlab CI). Either set this environment variable or pass the URL via the --gitlab-url command line option.'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$parsed_url = parse_url($ciProjectUrl); |
59
|
|
|
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; |
60
|
|
|
$host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
61
|
|
|
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; |
62
|
|
|
$gitlabUrl = $scheme.$host.$port; |
63
|
|
|
} |
64
|
|
|
return rtrim($gitlabUrl, '/'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getGitlabApiUrl() : string |
68
|
|
|
{ |
69
|
|
|
return $this->getGitlabUrl().'/api/v3/'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function getGitlabProjectName() : string |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$projectName = $this->input->getOption('gitlab-project-name'); |
75
|
|
|
if ($projectName === null) { |
76
|
|
|
$projectDir = getenv('CI_PROJECT_DIR'); |
77
|
|
|
if ($projectDir === false) { |
78
|
|
|
throw new \RuntimeException('Could not find the Gitlab project name in the "CI_PROJECT_DIR" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the project name via the --gitlab-project-name command line option.'); |
79
|
|
|
} |
80
|
|
|
$projectName = substr($projectDir, 8); |
81
|
|
|
} |
82
|
|
|
return $projectName; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function getCommitSha() : string |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$commitSha = $this->input->getOption('commit-sha'); |
88
|
|
|
|
89
|
|
|
if ($commitSha === null) { |
90
|
|
|
$commitSha = getenv('CI_COMMIT_SHA'); |
91
|
|
|
if ($commitSha === false) { |
92
|
|
|
$commitSha = getenv('CI_BUILD_REF'); |
93
|
|
|
if ($commitSha === false) { |
94
|
|
|
throw new \RuntimeException('Could not find the Gitlab build reference in the "CI_COMMIT_SHA" (Gitlab 9+) or "CI_BUILD_REF" (Gitlab 8.x) environment variable (usually set by Gitlab CI). Either set this environment variable or pass the build reference via the --commit-sha command line option.'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $commitSha; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function getJobStage() : string |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$commitSha = $this->input->getOption('job-stage'); |
105
|
|
|
|
106
|
|
|
if ($commitSha === null) { |
107
|
|
|
$commitSha = getenv('CI_JOB_STAGE'); |
108
|
|
|
if ($commitSha === false) { |
109
|
|
|
throw new \RuntimeException('Could not find the Gitlab job stage in the "CI_JOB_STAGE" (Gitlab 9+) environment variable (usually set by Gitlab CI). Either set this environment variable or pass the job stage via the --job_stage command line option.'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $commitSha; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function getGitlabBuildId() : int |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$buildId = $this->input->getOption('gitlab-job-id'); |
119
|
|
|
if ($buildId === null) { |
120
|
|
|
$buildId = getenv('CI_BUILD_ID'); |
121
|
|
|
if ($buildId === false) { |
122
|
|
|
$buildId = getenv('CI_JOB_ID'); |
123
|
|
|
if ($buildId === false) { |
124
|
|
|
throw new \RuntimeException('Could not find the Gitlab build id in the "CI_JOB_ID" (Gitlab 9+) or "CI_BUILD_ID" (Gitlab 8.x) environment variable (usually set by Gitlab CI). Either set this environment variable or pass the build id via the --gitlab-job-id command line option.'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return $buildId; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the current branch name (from Git) |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getCurrentBranchName() : string |
136
|
|
|
{ |
137
|
|
|
// Gitlab 8.x |
138
|
|
|
$branchName = getenv('CI_BUILD_REF_NAME'); |
139
|
|
|
if ($branchName !== false) { |
140
|
|
|
return $branchName; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Gitlab 9+ |
144
|
|
|
$branchName = getenv('CI_COMMIT_REF_NAME'); |
145
|
|
|
if ($branchName !== false) { |
146
|
|
|
return $branchName; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$repo = new GitRepository(getcwd()); |
150
|
|
|
return $repo->getCurrentBranchName(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getFiles() : array |
154
|
|
|
{ |
155
|
|
|
return $this->input->getOption('file'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function isOpenIssue() : bool |
159
|
|
|
{ |
160
|
|
|
return $this->input->getOption('open-issue'); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.