@@ -25,9 +25,9 @@ final class GitlabApiClient
25
25
{
26
26
private const API_URL = 'https://%s/api/v4/ci/lint ' ;
27
27
28
- private string $ apiToken ;
28
+ private string $ gitlabToken ;
29
29
30
- private string $ configFile ;
30
+ private string $ gitlabFile ;
31
31
32
32
private string $ gitlabInstance ;
33
33
@@ -36,8 +36,8 @@ final class GitlabApiClient
36
36
*/
37
37
public function __construct (array $ config )
38
38
{
39
- $ this ->configFile = $ config ['gitlab_file ' ];
40
- $ this ->apiToken = $ config ['api_token ' ];
39
+ $ this ->gitlabToken = $ config ['api_token ' ];
40
+ $ this ->gitlabFile = $ config ['gitlab_file ' ];
41
41
$ this ->gitlabInstance = $ config ['gitlab_url ' ];
42
42
}
43
43
@@ -52,28 +52,19 @@ public function __construct(array $config)
52
52
*/
53
53
public function lint (): array
54
54
{
55
- $ httpClient = HttpClient::create ();
56
-
57
- $ yamlEncoder = new YamlEncoder ();
58
- $ jsonEncoder = new JsonEncoder ();
59
-
60
- $ fileContents = (string ) file_get_contents ($ this ->configFile );
61
-
62
- $ jsonData = $ jsonEncoder ->encode (
63
- $ yamlEncoder ->decode ($ fileContents , YamlEncoder::FORMAT ),
64
- JsonEncoder::FORMAT
65
- );
55
+ if (strlen ($ this ->gitlabToken ) === 0 ) {
56
+ throw GitlabLinterException::missingToken ();
57
+ }
66
58
67
- $ jsonData = str_replace ('\\' , '\\\\' , $ jsonData );
68
- $ jsonData = str_replace ('" ' , '\" ' , $ jsonData );
59
+ $ httpClient = HttpClient::create ();
69
60
70
61
$ url = sprintf (self ::API_URL , $ this ->gitlabInstance );
71
62
$ response = $ httpClient ->request ('POST ' , $ url , [
72
63
'headers ' => [
73
64
'Content-Type ' => 'application/json ' ,
74
- 'PRIVATE-TOKEN ' => $ this ->apiToken ,
65
+ 'PRIVATE-TOKEN ' => $ this ->gitlabToken ,
75
66
],
76
- 'body ' => sprintf ('{"content": "%s"} ' , $ jsonData ),
67
+ 'body ' => sprintf ('{"content": "%s"} ' , $ this -> yamlConfigEncodedAsJson () ),
77
68
]);
78
69
79
70
if ($ response ->getStatusCode () === 401 ) {
@@ -86,4 +77,21 @@ public function lint(): array
86
77
87
78
return $ response ->toArray ();
88
79
}
80
+
81
+ private function yamlConfigEncodedAsJson (): string
82
+ {
83
+ $ fileContents = (string ) file_get_contents ($ this ->gitlabFile );
84
+
85
+ $ yamlEncoder = new YamlEncoder ();
86
+ $ jsonEncoder = new JsonEncoder ();
87
+
88
+ $ jsonData = $ jsonEncoder ->encode (
89
+ $ yamlEncoder ->decode ($ fileContents , YamlEncoder::FORMAT ),
90
+ JsonEncoder::FORMAT
91
+ );
92
+
93
+ $ jsonData = str_replace ('\\' , '\\\\' , $ jsonData );
94
+
95
+ return str_replace ('" ' , '\" ' , $ jsonData );
96
+ }
89
97
}
0 commit comments