Skip to content

Commit a01b046

Browse files
authored
Merge pull request #7 from moufmouf/gitlab9
Adding support for Gitlab 9.
2 parents 42f9130 + 1dd8e05 commit a01b046

File tree

5 files changed

+79
-39
lines changed

5 files changed

+79
-39
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
[![Total Downloads](https://poser.pugx.org/thecodingmachine/washingmachine/downloads)](https://packagist.org/packages/thecodingmachine/washingmachine)
33
[![Latest Unstable Version](https://poser.pugx.org/thecodingmachine/washingmachine/v/unstable)](https://packagist.org/packages/thecodingmachine/washingmachine)
44
[![License](https://poser.pugx.org/thecodingmachine/washingmachine/license)](https://packagist.org/packages/thecodingmachine/washingmachine)
5-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/washingmachine/badges/quality-score.png?b=1.0)](https://scrutinizer-ci.com/g/thecodingmachine/washingmachine/?branch=1.0)
6-
[![Build Status](https://travis-ci.org/thecodingmachine/washingmachine.svg?branch=1.0)](https://travis-ci.org/thecodingmachine/washingmachine)
7-
[![Coverage Status](https://coveralls.io/repos/thecodingmachine/washingmachine/badge.svg?branch=1.0&service=github)](https://coveralls.io/github/thecodingmachine/washingmachine?branch=1.0)
5+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/washingmachine/badges/quality-score.png?b=1.2)](https://scrutinizer-ci.com/g/thecodingmachine/washingmachine/?branch=1.2)
6+
[![Build Status](https://travis-ci.org/thecodingmachine/washingmachine.svg?branch=1.2)](https://travis-ci.org/thecodingmachine/washingmachine)
7+
[![Coverage Status](https://coveralls.io/repos/thecodingmachine/washingmachine/badge.svg?branch=1.2&service=github)](https://coveralls.io/github/thecodingmachine/washingmachine?branch=1.2)
88

99

1010
# Washing machine
1111

12-
**Work in progress**
13-
1412
The *washing machine* is a tool that helps you writing cleaner code by integrating PHPUnit with Gitlab CI.
1513

1614
As a result, when you perform a merge request in Gitlab, the washing machine will add meaningful information about your code quality.

src/Commands/Config.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ public function getGitlabUrl() : string
4848
{
4949
$gitlabUrl = $this->input->getOption('gitlab-url');
5050
if ($gitlabUrl === null) {
51-
$ciProjectUrl = getenv('CI_BUILD_REPO');
51+
$ciProjectUrl = getenv('CI_REPOSITORY_URL');
5252
if ($ciProjectUrl === false) {
53-
throw new \RuntimeException('Could not find the Gitlab URL in the "CI_BUILD_REPO" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the URL via the --gitlab-url command line option.');
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+
}
5457
}
5558
$parsed_url = parse_url($ciProjectUrl);
5659
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
@@ -79,25 +82,33 @@ public function getGitlabProjectName() : string
7982
return $projectName;
8083
}
8184

82-
public function getGitlabBuildRef() : string
85+
public function getCommitSha() : string
8386
{
84-
$buildRef = $this->input->getOption('gitlab-build-ref');
85-
if ($buildRef === null) {
86-
$buildRef = getenv('CI_BUILD_REF');
87-
if ($buildRef === false) {
88-
throw new \RuntimeException('Could not find the Gitlab build reference in the "CI_BUILD_REF" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the build reference via the --gitlab-build-ref command line option.');
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+
}
8996
}
9097
}
91-
return $buildRef;
98+
99+
return $commitSha;
92100
}
93101

94102
public function getGitlabBuildId() : int
95103
{
96-
$buildId = $this->input->getOption('gitlab-build-id');
104+
$buildId = $this->input->getOption('gitlab-job-id');
97105
if ($buildId === null) {
98106
$buildId = getenv('CI_BUILD_ID');
99107
if ($buildId === false) {
100-
throw new \RuntimeException('Could not find the Gitlab build id in the "CI_BUILD_ID" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the build id via the --gitlab-build-id command line option.');
108+
$buildId = getenv('CI_JOB_ID');
109+
if ($buildId === false) {
110+
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.');
111+
}
101112
}
102113
}
103114
return $buildId;
@@ -109,11 +120,18 @@ public function getGitlabBuildId() : int
109120
*/
110121
public function getCurrentBranchName() : string
111122
{
123+
// Gitlab 8.x
112124
$branchName = getenv('CI_BUILD_REF_NAME');
113125
if ($branchName !== false) {
114126
return $branchName;
115127
}
116128

129+
// Gitlab 9+
130+
$branchName = getenv('CI_COMMIT_REF_NAME');
131+
if ($branchName !== false) {
132+
return $branchName;
133+
}
134+
117135
$repo = new GitRepository(getcwd());
118136
return $repo->getCurrentBranchName();
119137
}

src/Commands/RunCommand.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ protected function configure()
5858
InputOption::VALUE_REQUIRED,
5959
'The Gitlab project name (in the form "group/name"). If not specified, it is deduced from the CI_PROJECT_DIR environment variable.',
6060
null)
61-
->addOption('gitlab-build-ref',
61+
->addOption('commit-sha',
6262
'r',
6363
InputOption::VALUE_REQUIRED,
64-
'The Gitlab CI build reference. If not specified, it is deduced from the CI_BUILD_REF environment variable.',
64+
'The commit SHA. If not specified, it is deduced from the CI_COMMIT_SHA environment variable.',
6565
null)
66-
->addOption('gitlab-build-id',
66+
->addOption('gitlab-job-id',
6767
'b',
6868
InputOption::VALUE_REQUIRED,
6969
'The Gitlab CI build id. If not specified, it is deduced from the CI_BUILD_ID environment variable.',
@@ -132,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
132132

133133
$projectName = $config->getGitlabProjectName();
134134

135-
$buildRef = $config->getGitlabBuildRef();
135+
$commitSha = $config->getCommitSha();
136136

137137
$currentBranchName = $config->getCurrentBranchName();
138138

@@ -143,7 +143,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
143143

144144
$sendCommentService = new SendCommentService($client, $diffService);
145145

146-
// From CI_BUILD_REF, we can get the commit ( -> project -> build -> commit )
146+
// From CI_COMMIT_SHA, we can get the commit ( -> project -> build -> commit )
147147
// From the merge_requests API, we can get the list of commits for a single merge request
148148
// Hence, we can find the merge_request matching a build!
149149

@@ -152,11 +152,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
152152
$inMergeRequest = false;
153153

154154
try {
155-
$mergeRequest = $buildService->findMergeRequestByBuildRef($projectName, $buildRef);
155+
$mergeRequest = $buildService->findMergeRequestByCommitSha($projectName, $commitSha);
156156

157157
$repo = new GitRepository(getcwd());
158158
$targetCommit = $repo->getLatestCommitForBranch('origin/'.$mergeRequest['target_branch']);
159-
$lastCommonCommit = $repo->getMergeBase($targetCommit, $buildRef);
159+
$lastCommonCommit = $repo->getMergeBase($targetCommit, $commitSha);
160160

161161

162162
list($previousCodeCoverageProvider, $previousMethodsProvider) = $this->getMeasuresFromCommit($buildService, $mergeRequest['target_project_id'], $lastCommonCommit, $cloverFilePath, $crap4JFilePath);
@@ -169,7 +169,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
169169
$output->writeln('Could not find clover file for code coverage analysis.');
170170
}
171171
if ($methodsProvider !== null) {
172-
$message->addDifferencesHtml($methodsProvider, $previousMethodsProvider, $diffService, $buildRef, $gitlabUrl, $projectName);
172+
$message->addDifferencesHtml($methodsProvider, $previousMethodsProvider, $diffService, $commitSha, $gitlabUrl, $projectName);
173173
} else {
174174
$output->writeln('Could not find clover file nor crap4j file for CRAP score analysis.');
175175
}
@@ -188,7 +188,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
188188
$targetProjectId = $mergeRequest['target_project_id'] ?? $projectName;
189189
list($lastCommitCoverage, $lastCommitMethodsProvider) = $this->getMeasuresFromBranch($buildService, $targetProjectId, $currentBranchName, $cloverFilePath, $crap4JFilePath);
190190

191-
$sendCommentService->sendDifferencesCommentsInCommit($methodsProvider, $lastCommitMethodsProvider, $projectName, $buildRef, $gitlabUrl);
191+
$sendCommentService->sendDifferencesCommentsInCommit($methodsProvider, $lastCommitMethodsProvider, $projectName, $commitSha, $gitlabUrl);
192192

193193

194194
if ($config->isOpenIssue() && !$inMergeRequest) {
@@ -201,7 +201,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
201201
}
202202

203203
if ($methodsProvider !== null) {
204-
$message->addDifferencesHtml($methodsProvider, $lastCommitMethodsProvider, $diffService, $buildRef, $gitlabUrl, $projectName);
204+
$message->addDifferencesHtml($methodsProvider, $lastCommitMethodsProvider, $diffService, $commitSha, $gitlabUrl, $projectName);
205205
} else {
206206
$output->writeln('Could not find clover file nor crap4j file for CRAP score analysis.');
207207
}
@@ -214,7 +214,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
214214
'description' => (string) $message
215215
];
216216

217-
$userId = $this->getCommiterId($client, $project, $buildRef);
217+
$userId = $this->getCommiterId($client, $project, $commitSha);
218218
if ($userId !== null) {
219219
$options['assignee_id'] = $userId;
220220
}

src/Gitlab/BuildService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function getCommitId(string $projectName, string $buildRef) : string
3434

3535
/**
3636
* @param string $projectName
37-
* @param string $buildRef
37+
* @param string $commitSha
3838
* @return array The merge request object
3939
* @throws MergeRequestNotFoundException
4040
*/
41-
public function findMergeRequestByBuildRef(string $projectName, string $buildRef) : array
41+
public function findMergeRequestByCommitSha(string $projectName, string $commitSha) : array
4242
{
4343
// Find in the last 50 merge requests (since our build was triggered recently, it should definitely be there)
4444
$mergeRequests = $this->client->merge_requests->all($projectName, 1, 50, 'updated_at', 'desc');
@@ -48,12 +48,12 @@ public function findMergeRequestByBuildRef(string $projectName, string $buildRef
4848
// Let's only return this PR if the returned commit is the FIRST one (otherwise, the commit ID is on an outdated version of the PR)
4949

5050
// Note: strangely, the "id" column of the commit is the build ref and not the commit id... weird!
51-
if ($commits[0]['id'] === $buildRef) {
51+
if ($commits[0]['id'] === $commitSha) {
5252
return $mergeRequest;
5353
}
5454
}
5555

56-
throw new MergeRequestNotFoundException('Could not find a PR (in the 50 last PRs) whose last commit/buildRef ID is '.$buildRef);
56+
throw new MergeRequestNotFoundException('Could not find a PR (in the 50 last PRs) whose last commit/buildRef ID is '.$commitSha);
5757
}
5858

5959
public function getLatestCommitIdFromBranch(string $projectName, string $branchName) : string

tests/Commands/ConfigTest.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ private function getInputDefinition()
3434
InputOption::VALUE_REQUIRED,
3535
'The Gitlab project name (in the form "group/name"). If not specified, it is deduced from the CI_PROJECT_DIR environment variable.',
3636
null),
37-
new InputOption('gitlab-build-ref',
37+
new InputOption('commit-sha',
3838
'r',
3939
InputOption::VALUE_REQUIRED,
40-
'The Gitlab CI build reference. If not specified, it is deduced from the CI_BUILD_REF environment variable.',
40+
'The commit SHA. If not specified, it is deduced from the CI_COMMIT_SHA environment variable.',
4141
null),
42-
new InputOption('gitlab-build-id',
42+
new InputOption('gitlab-job-id',
4343
'b',
4444
InputOption::VALUE_REQUIRED,
4545
'The Gitlab CI build id. If not specified, it is deduced from the CI_BUILD_ID environment variable.',
@@ -155,25 +155,37 @@ public function testGitlabBuildRefFromEnv()
155155
putenv('CI_BUILD_REF=DEADBEEFDEADBEEF');
156156
$input = new ArrayInput([], $this->getInputDefinition());
157157
$config = new Config($input);
158-
$this->assertSame('DEADBEEFDEADBEEF', $config->getGitlabBuildRef());
158+
$this->assertSame('DEADBEEFDEADBEEF', $config->getCommitSha());
159+
}
160+
161+
public function testGitlab9BuildRefFromEnv()
162+
{
163+
putenv('CI_BUILD_REF');
164+
putenv('CI_COMMIT_SHA=DEADBEEFDEADBEEF');
165+
$input = new ArrayInput([], $this->getInputDefinition());
166+
$config = new Config($input);
167+
$this->assertSame('DEADBEEFDEADBEEF', $config->getCommitSha());
159168
}
160169

161170
public function testGitlabBuildRefFromParam()
162171
{
163-
$input = new ArrayInput(array('--gitlab-build-ref' => 'DEADBEEFDEADBEEF2'), $this->getInputDefinition());
172+
putenv('CI_BUILD_REF');
173+
putenv('CI_COMMIT_SHA');
174+
$input = new ArrayInput(array('--commit-sha' => 'DEADBEEFDEADBEEF2'), $this->getInputDefinition());
164175

165176
$config = new Config($input);
166-
$this->assertSame('DEADBEEFDEADBEEF2', $config->getGitlabBuildRef());
177+
$this->assertSame('DEADBEEFDEADBEEF2', $config->getCommitSha());
167178
}
168179

169180
public function testNoGitlabBuildRef()
170181
{
171182
putenv('CI_BUILD_REF');
183+
putenv('CI_COMMIT_SHA');
172184
$input = new ArrayInput([], $this->getInputDefinition());
173185

174186
$config = new Config($input);
175187
$this->expectException(\RuntimeException::class);
176-
$config->getGitlabBuildRef();
188+
$config->getCommitSha();
177189
}
178190

179191
public function testGitlabBuildIdFromEnv()
@@ -184,9 +196,21 @@ public function testGitlabBuildIdFromEnv()
184196
$this->assertSame(42, $config->getGitlabBuildId());
185197
}
186198

199+
public function testGitlabJobIdFromEnv()
200+
{
201+
putenv('CI_BUILD_ID');
202+
putenv('CI_JOB_ID=42');
203+
$input = new ArrayInput([], $this->getInputDefinition());
204+
$config = new Config($input);
205+
$this->assertSame(42, $config->getGitlabBuildId());
206+
}
207+
187208
public function testGitlabBuildIdFromParam()
188209
{
189-
$input = new ArrayInput(array('--gitlab-build-id' => '42'), $this->getInputDefinition());
210+
putenv('CI_BUILD_ID');
211+
putenv('CI_JOB_ID');
212+
213+
$input = new ArrayInput(array('--gitlab-job-id' => '42'), $this->getInputDefinition());
190214

191215
$config = new Config($input);
192216
$this->assertSame(42, $config->getGitlabBuildId());

0 commit comments

Comments
 (0)