Skip to content

Commit a788d31

Browse files
jiangxingitster
authored andcommitted
ci: new github-action for git-l10n code review
The repository of git-l10n is a fork of "git/git" on GitHub, and uses GitHub pull request for code review. A helper program "git-po-helper" can be used to check typos in ".po" files, validate syntax, and check commit messages. It would be convenient to integrate this helper program to CI and add comments in pull request. The new github-action workflow will be enabled for l10n related operations, such as: * Operations on a repository named as "git-po", such as a repository forked from "git-l10n/git-po". * Push to a branch that contains "l10n" in the name. * Pull request from a remote branch which has "l10n" in the name, such as: "l10n/fix-fuzzy-translations". The new l10n workflow listens to two types of github events: on: [push, pull_request_target] The reason we use "pull_request_target" instead of "pull_request" is that pull requests from forks receive a read-only GITHUB_TOKEN and workflows cannot write comments back to pull requests for security reasons. GitHub provides a "pull_request_target" event to resolve security risks by checking out the base commit from the target repository, and provide write permissions for the workflow. By default, administrators can set strict permissions for workflows. The following code is used to modify the permissions for the GITHUB_TOKEN and grant write permission in order to create comments in pull-requests. permissions: pull-requests: write This workflow will scan commits one by one. If a commit does not look like a l10n commit (no file in "po/" has been changed), the scan process will stop immediately. For a "push" event, no error will be reported because it is normal to push non-l10n commits merged from upstream. But for the "pull_request_target" event, errors will be reported. For this reason, additional option is provided for "git-po-helper". git-po-helper check-commits \ --github-action-event="${{ github.event_name }}" -- \ <base>..<head> The output messages of "git-po-helper" contain color codes not only for console, but also for logfile. This is because "git-po-helper" uses a package named "logrus" for logging, and I use an additional option "ForceColor" to initialize "logrus" to print messages in a user-friendly format in logfile output. These color codes help produce beautiful output for the log of workflow, but they must be stripped off when creating comments for pull requests. E.g.: perl -pe 's/\e\[[0-9;]*m//g' git-po-helper.out "git-po-helper" may generate two kinds of suggestions, errors and warnings. All the errors and warnings will be reported in the log of the l10n workflow. However, warnings in the log of the workflow for a successfully running "git-po-helper" can easily be ignored by users. For the "pull_request_target" event, this issue is resolved by creating an additional comment in the pull request. A l10n contributor should try to fix all the errors, and should pay attention to the warnings. Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 225bc32 commit a788d31

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

.github/workflows/l10n.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: git-l10n
2+
3+
on: [push, pull_request_target]
4+
5+
jobs:
6+
git-po-helper:
7+
if: >-
8+
endsWith(github.repository, '/git-po') ||
9+
contains(github.head_ref, 'l10n') ||
10+
contains(github.ref, 'l10n')
11+
runs-on: ubuntu-latest
12+
permissions:
13+
pull-requests: write
14+
steps:
15+
- name: Setup base and head objects
16+
id: setup-tips
17+
run: |
18+
if test "${{ github.event_name }}" = "pull_request_target"
19+
then
20+
base=${{ github.event.pull_request.base.sha }}
21+
head=${{ github.event.pull_request.head.sha }}
22+
else
23+
base=${{ github.event.before }}
24+
head=${{ github.event.after }}
25+
fi
26+
echo "::set-output name=base::$base"
27+
echo "::set-output name=head::$head"
28+
- name: Run partial clone
29+
run: |
30+
git -c init.defaultBranch=master init --bare .
31+
git remote add \
32+
--mirror=fetch \
33+
origin \
34+
https://github.com/${{ github.repository }}
35+
# Fetch tips that may be unreachable from github.ref:
36+
# - For a forced push, "$base" may be unreachable.
37+
# - For a "pull_request_target" event, "$head" may be unreachable.
38+
args=
39+
for commit in \
40+
${{ steps.setup-tips.outputs.base }} \
41+
${{ steps.setup-tips.outputs.head }}
42+
do
43+
case $commit in
44+
*[^0]*)
45+
args="$args $commit"
46+
;;
47+
*)
48+
# Should not fetch ZERO-OID.
49+
;;
50+
esac
51+
done
52+
git -c protocol.version=2 fetch \
53+
--progress \
54+
--no-tags \
55+
--no-write-fetch-head \
56+
--filter=blob:none \
57+
origin \
58+
${{ github.ref }} \
59+
$args
60+
- uses: actions/setup-go@v2
61+
with:
62+
go-version: '>=1.16'
63+
- name: Install git-po-helper
64+
run: go install github.com/git-l10n/git-po-helper@main
65+
- name: Install other dependencies
66+
run: |
67+
sudo apt-get update -q &&
68+
sudo apt-get install -q -y gettext
69+
- name: Run git-po-helper
70+
id: check-commits
71+
run: |
72+
exit_code=0
73+
git-po-helper check-commits \
74+
--github-action-event="${{ github.event_name }}" -- \
75+
${{ steps.setup-tips.outputs.base }}..${{ steps.setup-tips.outputs.head }} \
76+
>git-po-helper.out 2>&1 || exit_code=$?
77+
if test $exit_code -ne 0 || grep -q WARNING git-po-helper.out
78+
then
79+
# Remove ANSI colors which are proper for console logs but not
80+
# proper for PR comment.
81+
echo "COMMENT_BODY<<EOF" >>$GITHUB_ENV
82+
perl -pe 's/\e\[[0-9;]*m//g; s/\bEOF$//g' git-po-helper.out >>$GITHUB_ENV
83+
echo "EOF" >>$GITHUB_ENV
84+
fi
85+
cat git-po-helper.out
86+
exit $exit_code
87+
- name: Create comment in pull request for report
88+
uses: mshick/add-pr-comment@v1
89+
if: >-
90+
always() &&
91+
github.event_name == 'pull_request_target' &&
92+
env.COMMENT_BODY != ''
93+
with:
94+
repo-token: ${{ secrets.GITHUB_TOKEN }}
95+
repo-token-user-login: 'github-actions[bot]'
96+
message: >
97+
${{ steps.check-commits.outcome == 'failure' && 'Errors and warnings' || 'Warnings' }}
98+
found by [git-po-helper](https://github.com/git-l10n/git-po-helper#readme) in workflow
99+
[#${{ github.run_number }}](${{ env.GITHUB_SERVER_URL }}/${{ github.repository }}/actions/runs/${{ github.run_id }}):
100+
101+
```
102+
103+
${{ env.COMMENT_BODY }}
104+
105+
```

0 commit comments

Comments
 (0)