13
13
14
14
use Composer \Command \BaseCommand ;
15
15
use Composer \Downloader \TransportException ;
16
- use Composer \Util \HttpDownloader ;
17
16
use Symfony \Component \Console \Input \InputArgument ;
18
17
use Symfony \Component \Console \Input \InputInterface ;
19
18
use Symfony \Component \Console \Input \InputOption ;
20
19
use Symfony \Component \Console \Output \OutputInterface ;
20
+ use Symfony \Flex \GithubApi ;
21
21
use Symfony \Flex \InformationOperation ;
22
22
use Symfony \Flex \Lock ;
23
23
use Symfony \Flex \Recipe ;
@@ -31,13 +31,13 @@ class RecipesCommand extends BaseCommand
31
31
private $ flex ;
32
32
33
33
private $ symfonyLock ;
34
- private $ downloader ;
34
+ private $ githubApi ;
35
35
36
36
public function __construct (/* cannot be type-hinted */ $ flex , Lock $ symfonyLock , $ downloader )
37
37
{
38
38
$ this ->flex = $ flex ;
39
39
$ this ->symfonyLock = $ symfonyLock ;
40
- $ this ->downloader = $ downloader ;
40
+ $ this ->githubApi = new GithubApi ( $ downloader) ;
41
41
42
42
parent ::__construct ();
43
43
}
@@ -136,7 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
136
136
'' ,
137
137
'Run: ' ,
138
138
' * <info>composer recipes vendor/package</info> to see details about a recipe. ' ,
139
- ' * <info>composer recipes:install vendor/package --force -v </info> to update that recipe. ' ,
139
+ ' * <info>composer recipes:update vendor/package</info> to update that recipe. ' ,
140
140
'' ,
141
141
]));
142
142
@@ -171,13 +171,15 @@ private function displayPackageInformation(Recipe $recipe)
171
171
$ commitDate = null ;
172
172
if (null !== $ lockRef && null !== $ lockRepo ) {
173
173
try {
174
- list ( $ gitSha , $ commitDate ) = $ this ->findRecipeCommitDataFromTreeRef (
174
+ $ recipeCommitData = $ this -> githubApi ->findRecipeCommitDataFromTreeRef (
175
175
$ recipe ->getName (),
176
176
$ lockRepo ,
177
177
$ lockBranch ?? '' ,
178
178
$ lockVersion ,
179
179
$ lockRef
180
180
);
181
+ $ gitSha = $ recipeCommitData ? $ recipeCommitData ['commit ' ] : null ;
182
+ $ commitDate = $ recipeCommitData ? $ recipeCommitData ['date ' ] : null ;
181
183
} catch (TransportException $ exception ) {
182
184
$ io ->writeError ('Error downloading exact git sha for installed recipe. ' );
183
185
}
@@ -232,7 +234,7 @@ private function displayPackageInformation(Recipe $recipe)
232
234
$ io ->write ([
233
235
'' ,
234
236
'Update this recipe by running: ' ,
235
- sprintf ('<info>composer recipes:install %s --force -v </info> ' , $ recipe ->getName ()),
237
+ sprintf ('<info>composer recipes:update %s</info> ' , $ recipe ->getName ()),
236
238
]);
237
239
}
238
240
}
@@ -324,63 +326,4 @@ private function writeTreeLine($line)
324
326
325
327
$ io ->write ($ line );
326
328
}
327
-
328
- /**
329
- * Attempts to find the original git sha when the recipe was installed.
330
- */
331
- private function findRecipeCommitDataFromTreeRef (string $ package , string $ repo , string $ branch , string $ version , string $ lockRef )
332
- {
333
- // only supports public repository placement
334
- if (0 !== strpos ($ repo , 'github.com ' )) {
335
- return [null , null ];
336
- }
337
-
338
- $ parts = explode ('/ ' , $ repo );
339
- if (3 !== \count ($ parts )) {
340
- return [null , null ];
341
- }
342
-
343
- $ recipePath = sprintf ('%s/%s ' , $ package , $ version );
344
- $ commitsData = $ this ->requestGitHubApi (sprintf (
345
- 'https://api.github.com/repos/%s/%s/commits?path=%s&sha=%s ' ,
346
- $ parts [1 ],
347
- $ parts [2 ],
348
- $ recipePath ,
349
- $ branch
350
- ));
351
-
352
- foreach ($ commitsData as $ commitData ) {
353
- // go back the commits one-by-one
354
- $ treeUrl = $ commitData ['commit ' ]['tree ' ]['url ' ].'?recursive=true ' ;
355
-
356
- // fetch the full tree, then look for the tree for the package path
357
- $ treeData = $ this ->requestGitHubApi ($ treeUrl );
358
- foreach ($ treeData ['tree ' ] as $ treeItem ) {
359
- if ($ treeItem ['path ' ] !== $ recipePath ) {
360
- continue ;
361
- }
362
-
363
- if ($ treeItem ['sha ' ] === $ lockRef ) {
364
- // shorten for brevity
365
- return [
366
- substr ($ commitData ['sha ' ], 0 , 7 ),
367
- $ commitData ['commit ' ]['committer ' ]['date ' ],
368
- ];
369
- }
370
- }
371
- }
372
-
373
- return [null , null ];
374
- }
375
-
376
- private function requestGitHubApi (string $ path )
377
- {
378
- if ($ this ->downloader instanceof HttpDownloader) {
379
- $ contents = $ this ->downloader ->get ($ path )->getBody ();
380
- } else {
381
- $ contents = $ this ->downloader ->getContents ('api.github.com ' , $ path , false );
382
- }
383
-
384
- return json_decode ($ contents , true );
385
- }
386
329
}
0 commit comments