|
1 | 1 | import 'dart:io'; |
2 | 2 |
|
| 3 | +import 'package:dio_pull_request_checker/dio_pull_request_checker.dart'; |
| 4 | +import 'package:dio_pull_request_checker/github.dart'; |
| 5 | +import 'package:github/github.dart'; |
3 | 6 | import 'package:github_action_context/github_action_context.dart'; |
4 | 7 | import 'package:github_action_core/github_action_core.dart'; |
5 | 8 |
|
6 | | -void main(List<String> arguments) { |
7 | | - startGroup('context.fields'); |
8 | | - |
9 | | - info('context.eventName: ${context.eventName}'); |
10 | | - info('context.sha: ${context.sha}'); |
11 | | - info('context.ref: ${context.ref}'); |
12 | | - info('context.workflow: ${context.workflow}'); |
13 | | - info('context.action: ${context.action}'); |
14 | | - info('context.actor: ${context.actor}'); |
15 | | - info('context.job: ${context.job}'); |
16 | | - info('context.runNumber: ${context.runNumber}'); |
17 | | - info('context.runId: ${context.runId}'); |
18 | | - info('context.apiUrl: ${context.apiUrl}'); |
19 | | - info('context.serverUrl: ${context.serverUrl}'); |
20 | | - info('context.graphqlUrl: ${context.graphqlUrl}'); |
21 | | - groupEnd(); |
22 | | - |
23 | | - startGroup('env'); |
24 | | - print('Current working directory: ${Directory.current.absolute.path}'); |
25 | | - groupEnd(); |
26 | | - |
27 | | - startGroup('context.payload'); |
28 | | - info(context.payload.toString()); |
29 | | - groupEnd(); |
| 9 | +void injectGithub() { |
| 10 | + // Get the token from env |
| 11 | + final token = Platform.environment['PERSON_TOKEN']; |
| 12 | + if (token == null) { |
| 13 | + setFailed('The input github-token is required'); |
| 14 | + } |
| 15 | + github = GitHub(auth: Authentication.withToken(token)); |
| 16 | +} |
| 17 | + |
| 18 | +Future<void> main(List<String> arguments) async { |
| 19 | + // 1. Check if the current workflow run is a pull request |
| 20 | + final pr = context.payload.pullRequest; |
| 21 | + final number = pr?.number; |
| 22 | + if (pr == null || number == null) { |
| 23 | + print('This is not a pull request, skipping'); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + final repository = context.payload.repository; |
| 28 | + if (repository == null) { |
| 29 | + print('Cannot get repository information, skipping'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + injectGithub(); |
| 34 | + |
| 35 | + final owner = repository.owner.login; |
| 36 | + final repo = repository.name; |
| 37 | + final latestCommitIdShort = context.payload['after']; |
| 38 | + final comment = |
| 39 | + '''The PR applies invalid `CHANGELOG.md` (latest check @$latestCommitIdShort). Please correct it according to the [Wiki](https://github.com/cfug/dio/wiki/Releasing-a-new-version-of-packages#before-start). |
| 40 | +
|
| 41 | +> PR 更改了 `CHANGELOG.md`(最新检查的提交 $latestCommitIdShort)但内容不符合格式。请参考 [Wiki](https://github.com/cfug/dio/wiki/Releasing-a-new-version-of-packages#before-start) 修改。 |
| 42 | +
|
| 43 | +'''; |
| 44 | + |
| 45 | + // 2. Get all changed files |
| 46 | + final files = await getPullRequestFiles( |
| 47 | + owner: owner, |
| 48 | + repo: repo, |
| 49 | + number: number, |
| 50 | + ); |
| 51 | + |
| 52 | + // 3. Check if the changed files contain changelog |
| 53 | + final changelogFiles = files.where((file) { |
| 54 | + final filename = file.filename; // The file name is path |
| 55 | + return filename != null && filename.contains('CHANGELOG.md'); |
| 56 | + }).toList(); |
| 57 | + |
| 58 | + if (changelogFiles.isEmpty) { |
| 59 | + // 4. If not, fail the workflow, before that, we add comments to the pull request |
| 60 | + // Get the latest commit id |
| 61 | + notice('No CHANGELOG.md found in the pull request, please add one.'); |
| 62 | + await sendComment(owner: owner, repo: repo, number: number, body: comment); |
| 63 | + setFailed('No CHANGELOG.md found in the pull request, please add one.'); |
| 64 | + } |
| 65 | + |
| 66 | + // 5. Have changelog, check if the content is valid |
| 67 | + for (final file in changelogFiles) { |
| 68 | + final content = await getChangeFileContentWithPullRequest( |
| 69 | + owner: owner, |
| 70 | + repo: repo, |
| 71 | + number: number, |
| 72 | + path: file.filename!, |
| 73 | + ); |
| 74 | + |
| 75 | + final before = content.before; |
| 76 | + final after = content.after; |
| 77 | + |
| 78 | + final checkMsg = checkChangeLog(before, after); |
| 79 | + if (checkMsg != null) { |
| 80 | + // 6. If not, fail the workflow, before that, we add comments to the pull request |
| 81 | + // Get the latest commit id |
| 82 | + notice(checkMsg); |
| 83 | + await sendComment( |
| 84 | + owner: owner, |
| 85 | + repo: repo, |
| 86 | + number: number, |
| 87 | + body: comment, |
| 88 | + ); |
| 89 | + setFailed(checkMsg); |
| 90 | + } |
| 91 | + } |
30 | 92 | } |
0 commit comments