Skip to content

Commit

Permalink
fix issue when github API results are paginated
Browse files Browse the repository at this point in the history
  • Loading branch information
ardera committed Aug 13, 2024
1 parent 5953bc4 commit b089db5
Show file tree
Hide file tree
Showing 4 changed files with 3,904 additions and 9 deletions.
3 changes: 1 addition & 2 deletions lib/src/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ class FlutterpiCache extends FlutterCache {
required ProcessManager processManager,
required ShutdownHooks hooks,
required MyGithub github,
io.HttpClient? httpClient,
gh.RepositorySlug? repo,
}) {
repo ??= gh.RepositorySlug('ardera', 'flutter-ci');
Expand Down Expand Up @@ -661,7 +660,6 @@ class FlutterpiCache extends FlutterCache {
gh.RepositorySlug? repo,
required String runId,
String? availableEngineVersion,
io.HttpClient? httpClient,
}) {
repo ??= gh.RepositorySlug('ardera', 'flutter-ci');

Expand Down Expand Up @@ -806,6 +804,7 @@ class FlutterpiCache extends FlutterCache {
cipdBaseUrl,
storageBaseUrl,
'https://github.com/ardera/flutter-pi',
'https://api.github.com/repos/ardera/flutter-ci/',
];

/// This has to be lazy because it requires FLUTTER_ROOT to be initialized.
Expand Down
31 changes: 24 additions & 7 deletions lib/src/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ abstract class MyGithub {
final artifacts = await getWorkflowRunArtifacts(
repo: repo,
runId: runId,
nameFilter: name,
);
return artifacts.singleOrNull;
return artifacts.where((a) => a.name == name).firstOrNull;
}

void authenticate(io.HttpClientRequest request);
Expand Down Expand Up @@ -128,12 +127,30 @@ class MyGithubImpl extends MyGithub {
}) async {
final path = workflowRunArtifactsUrlPath(repo, runId);

final response = await github.getJSON(path);

final results = <GithubArtifact>[];
for (final artifact in response['artifacts']) {
results.add(GithubArtifact.fromJson(artifact));
}

int? total;
var page = 1;
var fetched = 0;
do {
final response = await github.getJSON(
path,
params: {
if (nameFilter != null) 'name': nameFilter,
'page': page.toString(),
'per_page': '100',
},
);

total ??= response['total_count'] as int;

for (final artifact in response['artifacts']) {
results.add(GithubArtifact.fromJson(artifact));
}

fetched += (response['artifacts'] as Iterable).length;
page++;
} while (fetched < total);

return results;
}
Expand Down
64 changes: 64 additions & 0 deletions test/github_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:flutterpi_tool/src/github.dart';
import 'package:github/github.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:test/test.dart';

import 'github_test_api_output.dart';

void main() {
test('workflow artifacts querying', () async {
final client = MockClient((request) async {
final uri1 =
'https://api.github.com/repos/ardera/flutter-ci/actions/runs/10332084071/artifacts?page=1&per_page=100';

final uri2 =
'https://api.github.com/repos/ardera/flutter-ci/actions/runs/10332084071/artifacts?page=2&per_page=100';

expect(
request.url.toString(),
anyOf(equals(uri1), equals(uri2)),
);

if (request.url.queryParameters['page'] == '1') {
return Response(
githubWorkflowRunArtifactsPage1,
200,
headers: {
'Content-Type': 'application/json',
},
);
} else {
expect(request.url.queryParameters['page'], '2');
return Response(
githubWorkflowRunArtifactsPage2,
200,
headers: {
'Content-Type': 'application/json',
},
);
}
});

final github = MyGithub(
httpClient: client,
);

final artifact = await github.getWorkflowRunArtifact(
'universal',
repo: RepositorySlug('ardera', 'flutter-ci'),
runId: 10332084071,
);

expect(artifact, isNotNull);
artifact!;

expect(artifact.name, 'universal');

expect(artifact.archiveDownloadUrl, isNotNull);
expect(
artifact.archiveDownloadUrl.toString(),
'https://api.github.com/repos/ardera/flutter-ci/actions/artifacts/1797913057/zip',
);
});
}
Loading

0 comments on commit b089db5

Please sign in to comment.