Skip to content

Commit

Permalink
ci: Update token (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
CaiJingLong authored Jun 13, 2023
1 parent 9a059ba commit 3d4b355
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 17 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/test_code.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
name: "Test dart code"

on:
push:
pull_request:

types:
- ready_for_review
- converted_to_draft
- reopened
- opened
- unlocked
- synchronize
jobs:
test:
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false && github.event.pull_request.merged == false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -26,7 +32,11 @@ jobs:
run: dart run bin/main.dart
shell: bash
env:
PERSON_TOKEN: ${{ secrets.PERSON_TOKEN }}
WORKFLOW_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Run dart test
run: dart test
shell: bash
shell: bash
permissions:
contents: write
issues: write
pull-requests: write
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- support check pull requets
- Support check pull requests changelog

## 0.0.1

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ runs:
run: dart run "${{ github.action_path }}/main.dart"
shell: bash
env:
PERSON_TOKEN: ${{ inputs.github-token }}
WORKFLOW_TOKEN: ${{ inputs.github-token }}
26 changes: 19 additions & 7 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:github_action_core/github_action_core.dart';

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

Future<void> main(List<String> arguments) async {
// 1. Check if the current workflow run is a pull request
final pr = context.payload.pullRequest;
final number = pr?.number;
if (pr == null || number == null) {
final number = context.payload.pullRequest?.number;
if (number == null) {
print('This is not a pull request, skipping');
return;
}
Expand All @@ -34,11 +33,24 @@ Future<void> main(List<String> arguments) async {

final owner = repository.owner.login;
final repo = repository.name;
final latestCommitIdShort = context.payload['after'];

final pr = await getPullRequest(
owner: owner,
repo: repo,
number: number,
);

final headSha = pr.head?.sha;

if (headSha == null) {
info('Cannot get head ref, skipping');
return;
}

final comment =
'''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).
'''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).
> PR 更改了 `CHANGELOG.md`(最新检查的提交 $latestCommitIdShort)但内容不符合格式。请参考 [Wiki](https://github.com/cfug/dio/wiki/Releasing-a-new-version-of-packages#before-start) 修改。
> PR 更改了 `CHANGELOG.md`(最新检查的提交 $headSha)但内容不符合格式。请参考 [Wiki](https://github.com/cfug/dio/wiki/Releasing-a-new-version-of-packages#before-start) 修改。
''';

Expand Down
32 changes: 31 additions & 1 deletion lib/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,44 @@ class ChangeFileContent {
const ChangeFileContent(this.before, this.after);
}

Future<PullRequest> getPullRequest({
required String owner,
required String repo,
required int number,
}) {
return github.pullRequests.get(RepositorySlug(owner, repo), number);
}

Future<GitCommit?> getPullRequestHeadCommit({
required String owner,
required String repo,
required int prNumber,
}) async {
final slug = RepositorySlug(owner, repo);
final pr = await github.pullRequests.get(slug, prNumber);
final sha = pr.head?.sha;

if (sha == null) {
return null;
}

final commit = await github.git.getCommit(slug, sha);

return commit;
}

Future<ChangeFileContent> getChangeFileContentWithPullRequest({
required String owner,
required String repo,
required int number,
required String path,
}) async {
final slug = RepositorySlug(owner, repo);
final pr = await github.pullRequests.get(slug, number);
final pr = await getPullRequest(
owner: owner,
number: number,
repo: repo,
);

final head = pr.head?.sha;
final base = pr.base?.sha;
Expand Down
13 changes: 10 additions & 3 deletions test/dart_action_template_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:dio_pull_request_checker/dio_pull_request_checker.dart';
import 'package:dio_pull_request_checker/github.dart';
import 'package:github/github.dart';
Expand Down Expand Up @@ -142,7 +140,16 @@ support check pull requets
path: path,
);

final currentChangeContent = File(path).readAsStringSync();
final currentChangeContent = '''# CHANGELOG
## Unreleased
- support check pull requets
## 0.0.1
Init project
''';

expect(content.after, currentChangeContent);
});
Expand Down

0 comments on commit 3d4b355

Please sign in to comment.