Command line parser #90
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Diff Protobin | |
on: | |
# This event occurs when there is activity on a pull request | |
pull_request_target: | |
# This filter looks for files that have been changed with the suffix bin or guildpoint | |
paths: | |
- '**/*.bin' | |
- '**/*.guildpoint' | |
# To reduce noise, this filter restricts to the default activity plus when a draft is changed to open | |
# Default activity types as listed in the documentation are "opened, synchronize, and reopened" | |
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request_target | |
types: [opened, ready_for_review, reopened, synchronize] | |
jobs: | |
custom-diff: | |
runs-on: ubuntu-latest | |
if: '!github.event.pull_request.draft' | |
permissions: write-all | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v2 | |
- name: Install ProtoC | |
run: sudo apt-get install -y protobuf-compiler | |
- name: Fetch PR commits | |
run: | | |
git fetch origin +refs/pull/${{ github.event.pull_request.number }}/head:refs/pull/${{ github.event.pull_request.number }}/head | |
git fetch origin ${{ github.base_ref }} | |
- name: Get Filenames | |
run: | | |
for file in $(git diff --name-only origin/${{ github.base_ref }} refs/pull/${{ github.event.pull_request.number }}/head | grep -E '.bin$|.guildpoint$'); do | |
mkdir -p $(dirname "$file") | |
if [ -n "$(git ls-tree "origin/${{ github.base_ref }}" -- "$file")" ]; then | |
git show origin/${{ github.base_ref }}:$file > $file._old | |
protoc --decode=guildpoint.Guildpoint xml_converter/proto/guildpoint.proto < $file._old > $file.textproto._old | |
else | |
touch $file.textproto._old | |
fi | |
if [ -n "$(git ls-tree "refs/pull/${{ github.event.pull_request.number }}/head" -- "$file")" ]; then | |
git show refs/pull/${{ github.event.pull_request.number }}/head:$file > $file._new | |
protoc --decode=guildpoint.Guildpoint xml_converter/proto/guildpoint.proto < $file._new > $file.textproto._new | |
else | |
touch $file.textproto._new | |
fi | |
diff -u $file.textproto._old --label old $file.textproto._new --label new > $file._diff || true | |
echo $file >> file_list.txt | |
done | |
- name: Get PR commit hash | |
id: prcommithash | |
run: | | |
PR_COMMIT_HASH=$(git log --format="%H" -n 1 refs/pull/${{ github.event.pull_request.number }}/head) | |
echo "Latest PR Commit Hash: $PR_COMMIT_HASH" | |
echo "pr_commit_hash="$PR_COMMIT_HASH >> $GITHUB_OUTPUT | |
- name: Post Comment | |
if: always() | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require('fs'); | |
const path = require('path'); | |
const issue_number = context.issue.number; | |
const githubToken = process.env.GITHUB_TOKEN; | |
// Get the existing comments. | |
const {data: comments} = await github.rest.pulls.listReviewComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: issue_number, | |
}); | |
const files = fs.readFileSync('file_list.txt', 'utf8').split("\n"); | |
for (let file of files) { | |
if (file == "") { | |
continue; | |
} | |
// Filter comments related to the current file | |
const file_comments = comments.filter(comment => comment.path === file); | |
// Sort comments by creation date in descending order | |
file_comments.sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); | |
// Get the most recent comment | |
const most_recent_comment = file_comments.length > 0 ? file_comments[0] : null; | |
const diff_contents = fs.readFileSync(file + "._diff", 'utf8') | |
let comment_body = [ | |
"<details>", | |
"<summary>Full Diff</summary>", | |
"", | |
"```diff", | |
diff_contents, | |
"```", | |
"</details>", | |
].join("\n"); | |
// Check if a similar comment already exists | |
if (most_recent_comment && most_recent_comment.body === comment_body) { | |
console.log(`Skipping file ${file}, identical comment already exists.`); | |
continue; | |
} | |
console.log(file) | |
await github.rest.pulls.createReviewComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: issue_number, | |
path: file, | |
body: comment_body, | |
commit_id: '${{ steps.prcommithash.outputs.pr_commit_hash }}', | |
subject_type: "file", | |
}); | |
} |