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