Skip to content

Commit 3d4b355

Browse files
authored
ci: Update token (#2)
* ci: Update token * ci: permission * ci: Update permission * ci: update action input * ci: test code * ci: update if for test * ci: Update ci * ci: update pull request type * ci: update type draft * ci: Update ref for comment * fix: Update sha * fix: change sha * docs: Update changelog * test: update test case * chore: clean up
1 parent 9a059ba commit 3d4b355

File tree

6 files changed

+76
-17
lines changed

6 files changed

+76
-17
lines changed

.github/workflows/test_code.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
name: "Test dart code"
22

33
on:
4-
push:
54
pull_request:
6-
5+
types:
6+
- ready_for_review
7+
- converted_to_draft
8+
- reopened
9+
- opened
10+
- unlocked
11+
- synchronize
712
jobs:
813
test:
14+
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false && github.event.pull_request.merged == false
915
runs-on: ubuntu-latest
1016
steps:
1117
- uses: actions/checkout@v3
@@ -26,7 +32,11 @@ jobs:
2632
run: dart run bin/main.dart
2733
shell: bash
2834
env:
29-
PERSON_TOKEN: ${{ secrets.PERSON_TOKEN }}
35+
WORKFLOW_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3036
- name: Run dart test
3137
run: dart test
32-
shell: bash
38+
shell: bash
39+
permissions:
40+
contents: write
41+
issues: write
42+
pull-requests: write

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
- support check pull requets
5+
- Support check pull requests changelog
66

77
## 0.0.1
88

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ runs:
3232
run: dart run "${{ github.action_path }}/main.dart"
3333
shell: bash
3434
env:
35-
PERSON_TOKEN: ${{ inputs.github-token }}
35+
WORKFLOW_TOKEN: ${{ inputs.github-token }}

bin/main.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:github_action_core/github_action_core.dart';
88

99
void injectGithub() {
1010
// Get the token from env
11-
final token = Platform.environment['PERSON_TOKEN'];
11+
final token = Platform.environment['WORKFLOW_TOKEN'];
1212
if (token == null) {
1313
setFailed('The input github-token is required');
1414
}
@@ -17,9 +17,8 @@ void injectGithub() {
1717

1818
Future<void> main(List<String> arguments) async {
1919
// 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) {
20+
final number = context.payload.pullRequest?.number;
21+
if (number == null) {
2322
print('This is not a pull request, skipping');
2423
return;
2524
}
@@ -34,11 +33,24 @@ Future<void> main(List<String> arguments) async {
3433

3534
final owner = repository.owner.login;
3635
final repo = repository.name;
37-
final latestCommitIdShort = context.payload['after'];
36+
37+
final pr = await getPullRequest(
38+
owner: owner,
39+
repo: repo,
40+
number: number,
41+
);
42+
43+
final headSha = pr.head?.sha;
44+
45+
if (headSha == null) {
46+
info('Cannot get head ref, skipping');
47+
return;
48+
}
49+
3850
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).
51+
'''The PR applies invalid `CHANGELOG.md` (latest check $headSha ). Please correct it according to the [Wiki](https://github.com/cfug/dio/wiki/Releasing-a-new-version-of-packages#before-start).
4052
41-
> PR 更改了 `CHANGELOG.md`(最新检查的提交 $latestCommitIdShort)但内容不符合格式。请参考 [Wiki](https://github.com/cfug/dio/wiki/Releasing-a-new-version-of-packages#before-start) 修改。
53+
> PR 更改了 `CHANGELOG.md`(最新检查的提交 $headSha)但内容不符合格式。请参考 [Wiki](https://github.com/cfug/dio/wiki/Releasing-a-new-version-of-packages#before-start) 修改。
4254
4355
''';
4456

lib/github.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,44 @@ class ChangeFileContent {
3131
const ChangeFileContent(this.before, this.after);
3232
}
3333

34+
Future<PullRequest> getPullRequest({
35+
required String owner,
36+
required String repo,
37+
required int number,
38+
}) {
39+
return github.pullRequests.get(RepositorySlug(owner, repo), number);
40+
}
41+
42+
Future<GitCommit?> getPullRequestHeadCommit({
43+
required String owner,
44+
required String repo,
45+
required int prNumber,
46+
}) async {
47+
final slug = RepositorySlug(owner, repo);
48+
final pr = await github.pullRequests.get(slug, prNumber);
49+
final sha = pr.head?.sha;
50+
51+
if (sha == null) {
52+
return null;
53+
}
54+
55+
final commit = await github.git.getCommit(slug, sha);
56+
57+
return commit;
58+
}
59+
3460
Future<ChangeFileContent> getChangeFileContentWithPullRequest({
3561
required String owner,
3662
required String repo,
3763
required int number,
3864
required String path,
3965
}) async {
4066
final slug = RepositorySlug(owner, repo);
41-
final pr = await github.pullRequests.get(slug, number);
67+
final pr = await getPullRequest(
68+
owner: owner,
69+
number: number,
70+
repo: repo,
71+
);
4272

4373
final head = pr.head?.sha;
4474
final base = pr.base?.sha;

test/dart_action_template_test.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:io';
2-
31
import 'package:dio_pull_request_checker/dio_pull_request_checker.dart';
42
import 'package:dio_pull_request_checker/github.dart';
53
import 'package:github/github.dart';
@@ -142,7 +140,16 @@ support check pull requets
142140
path: path,
143141
);
144142

145-
final currentChangeContent = File(path).readAsStringSync();
143+
final currentChangeContent = '''# CHANGELOG
144+
145+
## Unreleased
146+
147+
- support check pull requets
148+
149+
## 0.0.1
150+
151+
Init project
152+
''';
146153

147154
expect(content.after, currentChangeContent);
148155
});

0 commit comments

Comments
 (0)