Skip to content

Commit

Permalink
Write comments on forks for firehose (#165)
Browse files Browse the repository at this point in the history
* Write comments on forks for `firehose`

* Allow failure in writing a comment

* Remove deleteComment

* Global `continue-on-error: true`

* Add to internal

* Rev version

* Do not continue on error

* All artifacts

* if empty

* Add logging

* Log artifact

* add name

* Reset

* trigger on publish only

* Publish for both

* Small fixes

* Delete comment on empty body

* Remove arg
  • Loading branch information
mosuem authored Sep 13, 2023
1 parent e96fbdb commit 7740bef
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 72 deletions.
58 changes: 35 additions & 23 deletions .github/workflows/post_summaries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ on:
# do things like create comments on the PR, even if the original workflow couldn't.
workflow_call:
workflow_run:
workflows: [Health]
workflows:
- Publish
- Health
types:
- completed

Expand All @@ -17,6 +19,7 @@ jobs:
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
continue-on-error: true
steps:

# Download the output of the health workflow, consisting of the comment markdown and either
Expand All @@ -26,18 +29,18 @@ jobs:
with:
script: |
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "output"
})[0];
var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{ github.workspace }}/comment.zip', Buffer.from(download.data));
Expand All @@ -52,24 +55,33 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
var fs = require('fs');
var markdown = fs.readFileSync('./comment.md', 'utf8');
if (fs.existsSync('./commentId')) {
if (fs.existsSync('./comment.md')) {
var markdown = fs.readFileSync('./comment.md', 'utf8');
if (fs.existsSync('./commentId')) {
var comment_number = Number(fs.readFileSync('./commentId', 'utf8'));
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment_number,
body: markdown
});
} else {
var issue_number = Number(fs.readFileSync('./issueNumber', 'utf8'));
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
body: markdown
});
}
} else if (fs.existsSync('./commentId')) {
var comment_number = Number(fs.readFileSync('./commentId', 'utf8'));
await github.rest.issues.updateComment({
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment_number,
body: markdown
});
}
else{
var issue_number = Number(fs.readFileSync('./issueNumber', 'utf8'));
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
body: markdown
});
}
11 changes: 11 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ jobs:
PR_LABELS: "${{ join(github.event.pull_request.labels.*.name) }}"
run: dart pub global run firehose --validate ${{ fromJSON('{"true":"--use-flutter","false":"--no-use-flutter"}')[inputs.use-flutter] }}

- name: Save PR number
run: |
mkdir -p output/ && echo ${{ github.event.number }} > output/issueNumber
- name: Upload folder with number and markdown
uses: actions/upload-artifact@65d862660abb392b8c4a3d1195a2108db131dd05
with:
name: output
path: output/


publish:
if: ${{ github.event_name == 'push' }}

Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/publish_internal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ jobs:
PR_LABELS: "${{ join(github.event.pull_request.labels.*.name) }}"
run: dart pkgs/firehose/bin/firehose.dart --validate ${{ fromJSON('{"true":"--use-flutter","false":"--no-use-flutter"}')[env.use-flutter] }}

- name: Save PR number
run: |
mkdir -p output/ && echo ${{ github.event.number }} > output/issueNumber
- name: Upload folder with number and markdown
uses: actions/upload-artifact@65d862660abb392b8c4a3d1195a2108db131dd05
with:
name: output
path: output/

- name: Publish tagged package
if: ${{ github.event_name == 'push' }}
run: dart pkgs/firehose/bin/firehose.dart --publish ${{ fromJSON('{"true":"--use-flutter","false":"--no-use-flutter"}')[env.use-flutter] }}
4 changes: 4 additions & 0 deletions pkgs/firehose/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.28

- Fix [#156](https://github.com/dart-lang/ecosystem/issues/156).

## 0.3.27

- Fix Flutter support.
Expand Down
30 changes: 11 additions & 19 deletions pkgs/firehose/lib/firehose.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,22 @@ Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automati
if (results.hasSuccess) {
var commentText = '$_publishBotTag\n\n$markdownTable';

if (existingCommentId == null) {
await allowFailure(
github.createComment(
github.repoSlug!, github.issueNumber!, commentText),
logError: print,
);
} else {
await allowFailure(
github.updateComment(
github.repoSlug!, existingCommentId, commentText),
logError: print,
);
if (existingCommentId != null) {
var idFile = File('./output/commentId');
print('''
Saving existing comment id $existingCommentId to file ${idFile.path}''');
await idFile.create(recursive: true);
await idFile.writeAsString(existingCommentId.toString());
}

var commentFile = File('./output/comment.md');
print('Saving comment markdown to file ${commentFile.path}');
await commentFile.create(recursive: true);
await commentFile.writeAsString(commentText);
} else {
if (results.hasError && exitCode == 0) {
exitCode = 1;
}

if (existingCommentId != null) {
await allowFailure(
github.deleteComment(github.repoSlug!, existingCommentId),
logError: print,
);
}
}

github.close();
Expand Down
29 changes: 0 additions & 29 deletions pkgs/firehose/lib/src/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,6 @@ class Github {
});
}

/// Create a new comment on the given PR.
Future<String> createComment(
String repoSlug, String issueNumber, String commentText) async {
var result = await callRestApiPost(
Uri.parse(
'https://api.github.com/repos/$repoSlug/issues/$issueNumber/comments'),
jsonEncode({'body': commentText}),
);
var json = jsonDecode(result) as Map;
return json['url'] as String;
}

/// Find a comment on the PR matching the given criteria ([user],
/// [searchTerm]). Return the issue ID if a matching comment is found or null
/// if there's no match.
Expand Down Expand Up @@ -181,23 +169,6 @@ class Github {
return null;
}

/// Update the given PR comment with new text.
Future<String> updateComment(
String repoSlug, int commentId, String commentText) async {
var result = await callRestApiPatch(
Uri.parse(
'https://api.github.com/repos/$repoSlug/issues/comments/$commentId'),
jsonEncode({'body': commentText}),
);
var json = jsonDecode(result) as Map;
return json['url'] as String;
}

Future<void> deleteComment(String repoSlug, int commentId) async {
await callRestApiDelete(Uri.parse(
'https://api.github.com/repos/$repoSlug/issues/comments/$commentId'));
}

Future<List<GitFile>> listFilesForPR() async {
var result = await callRestApiGet(
Uri.parse(
Expand Down
2 changes: 1 addition & 1 deletion pkgs/firehose/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: firehose
description: A tool to automate publishing of Pub packages from GitHub actions.
version: 0.3.27
version: 0.3.28
repository: https://github.com/dart-lang/ecosystem/tree/main/pkgs/firehose

environment:
Expand Down

0 comments on commit 7740bef

Please sign in to comment.