-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ci(pr-test): use GitHub API to get changed files + diff #8952
Conversation
If this is ok, I'd like to reflect the changes in mdn/content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM and promising, thanks! 🎉
Just some comments:
.github/workflows/pr-test.yml
Outdated
# note that we should get the absolute path of the changed images | ||
run: | | ||
DIFF_IMAGES=$(cat ${{ env.CHANGED_FILES }} | egrep -i "^files/.*/.*\.(png|jpeg|jpg|gif|svg|webp)$" | xargs readlink -e | tr "\n" " ") | ||
echo "GIT_DIFF_IMAGES=$DIFF_IMAGES" >> $GITHUB_ENV |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I've ignored to export absolute path here, see testing pr (yin1999#8):
- failed with image (unused): https://github.com/yin1999/translated-content/actions/runs/3293923349
- passed with image (used): https://github.com/yin1999/translated-content/actions/runs/3293947316/jobs/5430964574
Now, this seems ok.
marked as draft until I figure out the workflow run (https://github.com/yin1999/translated-content/actions/runs/3293826528/jobs/5430706523). It seems to be not right. |
316915f
to
0b93110
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
0b93110
to
7d9ae48
Compare
get diff file and changed files' name from GitHub API is much faster.
7d9ae48
to
e7ecb31
Compare
9427e36
to
c9dfc12
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a test I've made before
.github/workflows/pr-test.yml
Outdated
gh api repos/{owner}/{repo}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }} \ | ||
--paginate \ | ||
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename' > ${{ env.CHANGED_FILES }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For there maybe too many files has been changed in a single PR, the gh pr view
command used before can only fetch up to 100 changed files. But for almost all the PR, the commit in it would be less than 30
commits. So we can use github rest API to get changed files. (See https://docs.github.com/rest/commits/commits?apiVersion=2022-11-28#compare-two-commits). This API will limit 30 commits per page. So for those must few PRs, we can use --paginate
(see options in https://cli.github.com/manual/gh_api) to fetch all the commits.
Addition: The API limitation from GitHub Actions with GitHub Token
would be 1000 requests per hour. for a single repo This should be enough for most of cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can check this PR as an example: #9513
PR info: https://api.github.com/repos/mdn/translated-content/pulls/9513
Compare info (all the 183 files in in the single rest response, see field files
): https://api.github.com/repos/mdn/translated-content/compare/a9df62bb4885e5d9cdccd3834d5a24c921ef7cfa...16f5fc0db4ab1f5cb3858ab7878c428c19e9a79e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is ok. Should we try it in Markdown lint (PR) first? There are some failed workflows (cannot get changed files), and it is less important than the current CI.
@@ -132,7 +124,7 @@ jobs: | |||
# directory. | |||
# The purpose of this is for the PR Review Companion to later | |||
# be able to use this raw diff file for the benefit of analyzing. | |||
git diff --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} > $BUILD_OUT_ROOT/DIFF | |||
wget https://github.com/${{ github.repository }}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }}.diff -O $BUILD_OUT_ROOT/DIFF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can see the diff url in https://docs.github.com/rest/commits/commits#compare-two-commits
This may avoid an additional api call (making it harder to reach the limitation)
@yin1999 This has probably been discussed before, but why can we actually not use |
Actually not (see: actions/checkout#520), the If we need to fetch commits that are in a single PR (branch), we need to use And I've created another PR for markdown-lint (PR), to deal with the workflow failure. If the solution seems ok, could we try on markdown-lint. Because this workflow may be more important for us. |
Done in 6faf7b6. I've tested this in my repo, see: https://github.com/yin1999/translated-content/actions/runs/3630604417/jobs/6124154109 |
Thank you @yin1999, this is awesome! 🎉 |
Description
get diff file and changed files' name from GitHub API is much faster.
Motivation
as
gh
is an built-in GitHub cli in Action runner, we can use this tool to get changed files, which is much faster thangit diff
command (must fetch the history commits).Additional details
using
gh
cliOnly used 0 seconds.
https://github.com/yin1999/translated-content/actions/runs/3580409184/jobs/6022509363#step:6:4
using
git diff
(used bytechnote-space/get-diff-action
)used 40 seconds.
https://github.com/mdn/translated-content/actions/runs/3163384214/jobs/5150888786#step:7:1
conclusion
Using
gh
cli could speed up the PR test workflow. But when we use this, we must get the diff file fromhttps://github.com/ower/repo/compare/{base}...{head}.diff
and this diff file just show the diff in changed files (which may differ fromgit diff
which will collected the changes in the main branch).