-
Notifications
You must be signed in to change notification settings - Fork 835
/
create_version_content_from_github_release.dart
68 lines (60 loc) · 1.99 KB
/
create_version_content_from_github_release.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// ignore_for_file: avoid_print
import 'dart:convert';
import 'dart:io' show exit;
import 'package:http/http.dart' as http;
import 'update_package_version.dart';
// A script is used in CI that will fetch release notes using GitHub API
// and create a file from it that will be used by another script
//
// This script should run from root project folder instead of scripts folder
// or others.
const _usage =
'Usage: ./script <github-repository> <release-tag> <github-authorization> (optional)';
Future<void> main(List<String> args) async {
print('📑 Fetch release notes from Github API');
if (args.isEmpty) {
print('Missing required arguments ($args). $_usage');
exit(1);
}
if (args.length > 2) {
print('Too many arguments ($args). $_usage');
exit(1);
}
if (args.length < 2) {
print('Missing arguments ($args). $_usage');
exit(1);
}
final githubRepository = args[0];
final releaseTag = args[1];
final githubAuthorization = args.elementAtOrNull(2);
final response = await http.get(
Uri.parse(
'https://api.github.com/repos/$githubRepository/releases/tags/$releaseTag',
),
headers: {
if (githubAuthorization != null) 'Authorization': githubAuthorization,
},
);
if (response.statusCode != 200) {
print('Response status code is ${response.statusCode} which is not 200');
print('Response body: ${response.body}');
exit(1);
}
final responseBody = response.body;
print('⚠️ Validate release notes response');
if (responseBody.trim().isEmpty) {
print('Release notes response is empty.');
exit(1);
}
print('Response body: $responseBody');
final githubReleaseNotes =
(jsonDecode(responseBody) as Map<String, Object?>)['body'] as String?;
if (githubReleaseNotes == null) {
print('Release notes is null.');
exit(1);
}
if (!await versionContentFile.parent.exists()) {
await versionContentFile.parent.create(recursive: true);
}
await versionContentFile.writeAsString(githubReleaseNotes);
}