diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 436dfc73..421630f5 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -74,28 +74,11 @@ jobs: id: release_tag run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT - - name: 📑 Fetch release notes from Github API - id: fetch-release-notes-request - uses: fjogeleit/http-request-action@v1 - with: - url: https://api.github.com/repos/${{ github.repository }}/releases/tags/${{ steps.release_tag.outputs.tag }} - method: 'GET' - customHeaders: '{"Authorization": "${{ secrets.GITHUB_TOKEN }}"}' - preventFailureOnNoResponse: 'false' - - - name: ⚠️ Validate release notes response - run: | - releaseNotesResponse="${{ fromJson(steps.fetch-release-notes-request.outputs.response) }}" - if [[ -z "$releaseNotesResponse" || "$releaseNotesResponse" == "null" ]]; then - echo "Error: Release notes response is empty." - exit 1 - fi - - - name: 🖌️ Create required version content file to use in the next step - run: echo "${{ fromJson(steps.fetch-release-notes-request.outputs.response).body }}" > ./build/versionContent.md + - name: 📑 Fetch release notes from Github API and create a required file by the next step + run: dart ./scripts/create_version_content_from_github_release.dart "${{ github.repository }}" "${{ steps.release_tag.outputs.tag }}" "${{ secrets.GITHUB_TOKEN }}" - name: 📝 Update version and CHANGELOG for all the packages - run: dart ./scripts/update_package_version.dart ${{ steps.extract_version.outputs.VERSION }} '${{ fromJson(steps.fetch-release-notes-request.outputs.response).body }}' + run: dart ./scripts/update_package_version.dart ${{ steps.extract_version.outputs.VERSION }} - name: 💾 Commit updated version and CHANGELOG id: auto-commit-action diff --git a/pubspec.yaml b/pubspec.yaml index d4bfc5c9..089858b6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -70,9 +70,10 @@ dev_dependencies: flutter_quill_test: ^9.3.4 test: ^1.24.3 - # Both yaml_edit and yaml are only used for the scripts + # For scripts only yaml_edit: ^2.2.1 yaml: ^3.1.2 + http: ^1.2.1 flutter: uses-material-design: true diff --git a/scripts/create_version_content_from_github_release.dart b/scripts/create_version_content_from_github_release.dart new file mode 100644 index 00000000..59ca0c97 --- /dev/null +++ b/scripts/create_version_content_from_github_release.dart @@ -0,0 +1,65 @@ +// 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 (optional)'; + +Future main(List 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)['body'] as String?; + if (githubReleaseNotes == null) { + print('Release notes is null.'); + exit(1); + } + await versionContentFile.writeAsString(githubReleaseNotes); +} diff --git a/scripts/update_package_version.dart b/scripts/update_package_version.dart index 61c33ef1..8ee1c9e8 100644 --- a/scripts/update_package_version.dart +++ b/scripts/update_package_version.dart @@ -20,6 +20,7 @@ final _packages = [ const _usage = 'Usage: ./script '; const _versionContentFileName = 'versionContent.md'; +final versionContentFile = File(path.join('build', _versionContentFileName)); /// A script that should run in the root folder and not inside any other folder /// it has one task, which update the version for `pubspec.yaml` and @@ -50,7 +51,6 @@ Future main(List args) async { print('The version is empty ($args). $_usage'); exit(1); } - final versionContentFile = File(path.join('build', _versionContentFileName)); if (!(await versionContentFile.exists())) { print( 'The file "$_versionContentFileName" in ${versionContentFile.path} does not exist.',