Skip to content

Commit ce18db7

Browse files
authored
[Feature] Add GitLab CI job token (#57)
* add job token * fix phar building
1 parent 2a16950 commit ce18db7

File tree

21 files changed

+189
-74
lines changed

21 files changed

+189
-74
lines changed

.github/workflows/release.yml

+25-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ jobs:
99
steps:
1010
- uses: actions/checkout@master
1111

12+
- name: Get release
13+
id: get_release
14+
uses: bruceadams/[email protected]
15+
env:
16+
GITHUB_TOKEN: ${{ github.token }}
17+
1218
- name: set MR-Linter version
13-
run: echo "MR_LINTER_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
19+
run: echo "MR_LINTER_VERSION=${{ steps.get_release.outputs.tag_name }}" >> $GITHUB_ENV
1420

1521
- name: composer deps
1622
run: composer install --no-interaction --no-dev --prefer-dist --optimize-autoloader
@@ -26,11 +32,25 @@ jobs:
2632
with:
2733
args: gpg --command-fd 0 --pinentry-mode loopback -u [email protected] --batch --detach-sign --output bin/build/mr-linter.phar.asc bin/build/mr-linter.phar
2834

29-
- name: release phar
30-
uses: fnkr/github-action-ghr@v1
35+
- name: Upload phar
36+
uses: actions/upload-release-asset@v1
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
with:
40+
upload_url: ${{ steps.get_release.outputs.upload_url }}
41+
asset_path: ./bin/build/mr-linter.phar
42+
asset_name: mr-linter.phar
43+
asset_content_type: application/octet-stream
44+
45+
- name: Upload phar.asc
46+
uses: actions/upload-release-asset@v1
3147
env:
32-
GHR_PATH: bin/build
33-
GITHUB_TOKEN: ${{ secrets.MR_LINTER_GITHUB_HTTP_TOKEN }}
48+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49+
with:
50+
upload_url: ${{ steps.get_release.outputs.upload_url }}
51+
asset_path: ./bin/build/mr-linter.phar.asc
52+
asset_name: mr-linter.phar.asc
53+
asset_content_type: application/octet-stream
3454

3555
- name: build and publish Docker Image
3656
env:

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ This file contains changelogs.
66

77
-----------------------------------------------------------------
88

9+
## [v0.16.1 (2023-08-13)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.16.0..0.16.1)
10+
11+
## Added
12+
* Support GitLab CI job token
13+
14+
## Fixed
15+
* Fixed phar on "TwigTest not found"
16+
17+
[💾 Assets](https://github.com/ArtARTs36/php-merge-request-linter/releases/tag/0.16.1)
18+
19+
-----------------------------------------------------------------
20+
921
## [v0.16.0 (2023-08-06)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.15.3..0.16.0)
1022

1123
## Added

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ try:
1818
GITHUB_REF_NAME=${MR_ID}/merge \
1919
./bin/mr-linter lint --debug --metrics
2020

21+
# usage as `make try-phar MR_ID=1`
22+
try-phar: build-phar
23+
GITHUB_ACTIONS=1 \
24+
GITHUB_REPOSITORY=artarts36/php-merge-request-linter \
25+
GITHUB_GRAPHQL_URL=https://api.github.com/graphql \
26+
GITHUB_REF_NAME=${MR_ID}/merge \
27+
./bin/build/mr-linter.phar lint --debug --metrics
28+
2129
# usage as `make try MR_ID=1`
2230
try-docker: docker-build
2331
docker run \
@@ -161,3 +169,8 @@ deptrac-docker: docker-build
161169
artarts36/merge-request-linter "deptrac"
162170

163171
check-docker: lint-docker stat-analyse-docker test-docker deptrac-docker
172+
173+
build-phar:
174+
composer install --no-interaction --no-dev --prefer-dist --optimize-autoloader
175+
cd dev/build/ && composer install
176+
./dev/build/vendor/bin/box compile

box.json.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"check-requirements": false,
2+
"check-requirements": true,
33
"dump-autoload": true,
44
"compactors": [
55
"KevinGH\\Box\\Compactor\\Json",

dev/build/build.sh

-18
This file was deleted.

dev/build/composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"require": {
33
"php": "^8.0",
44
"ergebnis/composer-normalize": "^2.15.0",
5-
"humbug/box": "^3.8",
6-
"humbug/php-scoper": "dev-main#0a4e329 as 0.14.2"
5+
"humbug/box": "4.3.8"
76
},
87
"config": {
98
"optimize-autoloader": true,

docs/Builder/ConfigJsonSchema/Generator.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ public function generate(): JsonSchema
6565
'properties' => [
6666
'token' => [
6767
'type' => 'string',
68-
'description' => 'API Token',
68+
'description' => 'Access Token',
69+
],
70+
'job_token' => [
71+
'type' => 'string',
72+
'description' => 'Job Token',
6973
],
7074
],
7175
],

docs/getting-started.md

+29-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ Examples:
99

1010
Or generate **yaml** file with following command:
1111
```shell
12-
docker run -v "${PWD}:/app/:rw" --user 1000:1000 -it artarts36/merge-request-linter:0.10.0 install
12+
docker run -v "${PWD}:/app/:rw" --user 1000:1000 -it artarts36/merge-request-linter:0.16.1 install
1313
```
1414

1515
Or generate **json** file with following command:
1616
```shell
17-
docker run -v "${PWD}:/app/:rw" --user 1000:1000 -it artarts36/merge-request-linter:0.10.0 install --format=json
17+
docker run -v "${PWD}:/app/:rw" --user 1000:1000 -it artarts36/merge-request-linter:0.16.1 install --format=json
1818
```
1919

2020
When writing a config, look at [JSON Schema](config-schema.md).
@@ -60,14 +60,39 @@ build:
6060
3. Add new step into **.gitlab-ci.yml**
6161
```yaml
6262
mr-lint:
63-
image: artarts36/merge-request-linter:0.8.0
63+
image: artarts36/merge-request-linter:0.16.1
6464
stage: test
6565
only:
6666
- merge_requests
6767
script:
6868
- mr-linter lint
6969
```
7070

71+
You can also use `$CI_JOB_TOKEN` if you don't intend to use comments.
72+
73+
Configs with `$CI_JOB_TOKEN`:
74+
75+
* mr-linter.yaml:
76+
```yaml
77+
ci:
78+
gitlab_ci:
79+
credentials:
80+
job_token: 'env(MR_LINTER_GITHUB_HTTP_TOKEN)'
81+
```
82+
83+
* gitlab-ci.yaml
84+
```yaml
85+
mr-lint:
86+
image: artarts36/merge-request-linter:0.16.1
87+
stage: test
88+
only:
89+
- merge_requests
90+
variables:
91+
MR_LINTER_GITLAB_HTTP_TOKEN: $CI_JOB_TOKEN
92+
script:
93+
- mr-linter lint --debug
94+
```
95+
7196
## Usage with Bitbucket Pipelines
7297

7398
1. Create App Password on `https://bitbucket.org/account/settings/app-passwords/new` with permissions:
@@ -83,7 +108,7 @@ build:
83108
pull-requests:
84109
'**':
85110
- step:
86-
image: "artarts36/merge-request-linter:0.11.0"
111+
image: "artarts36/merge-request-linter:0.16.1"
87112
name: PR Review
88113
script:
89114
- mr-linter lint

docs/rules.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,4 @@ Currently is available that rules:
150150
<td>array of strings </td>
151151
</tr>
152152
</tbody>
153-
</t
153+
</table>

mr-linter-config-schema.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,11 @@
17651765
"properties": {
17661766
"token": {
17671767
"type": "string",
1768-
"description": "API Token"
1768+
"description": "Access Token"
1769+
},
1770+
"job_token": {
1771+
"type": "string",
1772+
"description": "Job Token"
17691773
}
17701774
}
17711775
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials;
4+
5+
/**
6+
* @codeCoverageIgnore
7+
*/
8+
class Header
9+
{
10+
public function __construct(
11+
public readonly string $name,
12+
public readonly string $value,
13+
) {
14+
}
15+
}

src/Infrastructure/Ci/Credentials/TokenAuthenticator.php renamed to src/Infrastructure/Ci/Credentials/HeaderAuthenticator.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,34 @@
55
use ArtARTs36\MergeRequestLinter\Domain\CI\Authenticator;
66
use Psr\Http\Message\RequestInterface;
77

8-
final class TokenAuthenticator implements Authenticator
8+
final class HeaderAuthenticator implements Authenticator
99
{
1010
public function __construct(
11-
private readonly string $header,
12-
private readonly string $token,
11+
private readonly Header $header,
1312
) {
1413
//
1514
}
1615

17-
public function authenticate(RequestInterface $request): RequestInterface
16+
public static function bearer(string $token): self
1817
{
19-
return $request->withHeader($this->header, $this->token);
18+
return new self(new Header('Authorization', 'Bearer ' . $token));
2019
}
2120

22-
public static function bearer(string $token): self
21+
public function authenticate(RequestInterface $request): RequestInterface
2322
{
24-
return new self('Authorization', 'Bearer ' . $token);
23+
return $request->withHeader($this->header->name, $this->header->value);
2524
}
2625

2726
/**
28-
* @return array<string, string>
27+
* @return array<string, array<string, string>>
2928
*/
3029
public function __debugInfo(): array
3130
{
3231
return [
33-
'header' => $this->header,
34-
'token' => '******',
32+
'header' => [
33+
'name' => $this->header->name,
34+
'value' => '******',
35+
],
3536
];
3637
}
3738
}

src/Infrastructure/Ci/System/Bitbucket/Credentials/BitbucketCredentialsMapper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\BasicBase64Authenticator;
77
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\CompositeAuthenticator;
88
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\HostAuthenticator;
9-
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\TokenAuthenticator;
9+
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\HeaderAuthenticator;
1010
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\CI\AuthenticatorMapper;
1111
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\Configuration\ConfigValueTransformer;
1212
use ArtARTs36\MergeRequestLinter\Infrastructure\Http\Exceptions\InvalidCredentialsException;
@@ -59,7 +59,7 @@ private function createTokenAuthenticator(array $credentials): ?Authenticator
5959
throw new InvalidCredentialsException('Given empty bitbucket token');
6060
}
6161

62-
return TokenAuthenticator::bearer($token);
62+
return HeaderAuthenticator::bearer($token);
6363
}
6464

6565
/**

src/Infrastructure/Ci/System/Github/Credentials/GithubActionsCredentialsMapper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ArtARTs36\MergeRequestLinter\Infrastructure\Ci\System\Github\Credentials;
44

55
use ArtARTs36\MergeRequestLinter\Domain\CI\Authenticator;
6-
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\TokenAuthenticator;
6+
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\HeaderAuthenticator;
77
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\CI\AuthenticatorMapper;
88
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\Configuration\ConfigValueTransformer;
99
use ArtARTs36\MergeRequestLinter\Infrastructure\Http\Exceptions\InvalidCredentialsException;
@@ -30,6 +30,6 @@ public function map(array $credentials): Authenticator
3030
));
3131
}
3232

33-
return TokenAuthenticator::bearer($this->value->tryTransform($credentials['token']));
33+
return HeaderAuthenticator::bearer($this->value->tryTransform($credentials['token']));
3434
}
3535
}

src/Infrastructure/Ci/System/Github/Env/GithubEnvironment.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public function getMergeRequestId(): ?int
4141

4242
if (! $id->isDigit()) {
4343
throw new InvalidEnvironmentVariableValueException(sprintf(
44-
'Var "%s" is invalid. Expected: {id}/merge',
44+
'Var "%s" is invalid. Expected: {id}/merge, given: "%s"',
4545
VarName::RefName->value,
46+
$ref,
4647
));
4748
}
4849

src/Infrastructure/Ci/System/Gitlab/Credentials/GitlabCredentialsMapper.php

+23-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace ArtARTs36\MergeRequestLinter\Infrastructure\Ci\System\Gitlab\Credentials;
44

55
use ArtARTs36\MergeRequestLinter\Domain\CI\Authenticator;
6-
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\TokenAuthenticator;
6+
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\Header;
7+
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Credentials\HeaderAuthenticator;
78
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\CI\AuthenticatorMapper;
89
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\Configuration\ConfigValueTransformer;
910
use ArtARTs36\MergeRequestLinter\Infrastructure\Http\Exceptions\InvalidCredentialsException;
@@ -18,12 +19,28 @@ public function __construct(
1819

1920
public function map(array $credentials): Authenticator
2021
{
21-
if (empty($credentials['token']) || ! is_string($credentials['token'])) {
22-
throw new InvalidCredentialsException(sprintf(
23-
'Gitlab CI supported only token',
24-
));
22+
$tokenFetchMap = [
23+
'token' => 'PRIVATE-TOKEN',
24+
'job_token' => 'JOB-TOKEN',
25+
];
26+
27+
$header = null;
28+
29+
foreach ($tokenFetchMap as $key => $headerName) {
30+
if (! empty($credentials[$key]) && is_string($credentials[$key])) {
31+
$header = new Header(
32+
$headerName,
33+
$this->valueTransformer->tryTransform($credentials[$key]),
34+
);
35+
}
36+
}
37+
38+
if ($header === null) {
39+
throw new InvalidCredentialsException(
40+
'Credentials for Gitlab CI not provided. Must be provided access token or job token',
41+
);
2542
}
2643

27-
return new TokenAuthenticator('PRIVATE-TOKEN', $this->valueTransformer->tryTransform($credentials['token']));
44+
return new HeaderAuthenticator($header);
2845
}
2946
}

src/Version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
final class Version
1010
{
11-
public const VERSION = '0.16.0';
11+
public const VERSION = '0.16.1';
1212

1313
private function __construct()
1414
{

0 commit comments

Comments
 (0)