3
3
namespace ArtARTs36 \MergeRequestLinter \Infrastructure \Ci \System \Gitlab \Credentials ;
4
4
5
5
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 ;
7
8
use ArtARTs36 \MergeRequestLinter \Infrastructure \Contracts \CI \AuthenticatorMapper ;
8
9
use ArtARTs36 \MergeRequestLinter \Infrastructure \Contracts \Configuration \ConfigValueTransformer ;
9
10
use ArtARTs36 \MergeRequestLinter \Infrastructure \Http \Exceptions \InvalidCredentialsException ;
@@ -18,12 +19,28 @@ public function __construct(
18
19
19
20
public function map (array $ credentials ): Authenticator
20
21
{
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
+ );
25
42
}
26
43
27
- return new TokenAuthenticator ( ' PRIVATE-TOKEN ' , $ this -> valueTransformer -> tryTransform ( $ credentials [ ' token ' ]) );
44
+ return new HeaderAuthenticator ( $ header );
28
45
}
29
46
}
0 commit comments