-
Notifications
You must be signed in to change notification settings - Fork 3.7k
191 lines (173 loc) · 8.08 KB
/
generateTranslations.yml
File metadata and controls
191 lines (173 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
name: Generate static translations
on:
pull_request:
types: [opened, synchronize]
paths: ['src/languages/en.ts']
workflow_dispatch:
inputs:
PULL_REQUEST_URL:
description: 'The full URL of the E/App pull request'
required: true
type: string
jobs:
generateTranslations:
runs-on: blacksmith-4vcpu-ubuntu-2404
if: ${{ github.event_name == 'workflow_dispatch' || !github.event.pull_request.head.repo.fork }}
steps:
- name: Get PR details
id: pr-data
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "::notice::🔧 Manual workflow dispatch triggered"
PR_INFO=$(gh pr view "${{ inputs.PULL_REQUEST_URL }}" --json number,baseRefName,headRefOid)
{
echo "PR_NUMBER=$(echo "$PR_INFO" | jq -r '.number')"
echo "BASE_REF=$(echo "$PR_INFO" | jq -r '.baseRefName')"
echo "HEAD_SHA=$(echo "$PR_INFO" | jq -r '.headRefOid')"
} >> "$GITHUB_OUTPUT"
echo "::notice::✅ Processing PR #$(echo "$PR_INFO" | jq -r '.number')"
else
echo "::notice::🤖 Automatic PR trigger activated"
{
echo "PR_NUMBER=${{ github.event.pull_request.number }}"
echo "BASE_REF=${{ github.event.pull_request.base.ref }}"
echo "HEAD_SHA=${{ github.event.pull_request.head.sha }}"
} >> "$GITHUB_OUTPUT"
echo "::notice::✅ Processing PR #${{ github.event.pull_request.number }}"
fi
env:
GITHUB_TOKEN: ${{ github.token }}
# v6
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
with:
ref: ${{ steps.pr-data.outputs.HEAD_SHA }}
- name: Setup Node
uses: ./.github/actions/composite/setupNode
- name: Setup tmate
if: runner.debug == '1'
uses: mxschmitt/action-tmate@e5c7151931ca95bad1c6f4190c730ecf8c7dde48
timeout-minutes: 60
with:
limit-access-to-actor: true
- name: Check if English translations were modified
id: check-en-changes
uses: ./.github/actions/javascript/getPullRequestIncrementalChanges
with:
FILE_PATHS: '["src/languages/en.ts"]'
GITHUB_TOKEN: ${{ github.token }}
PULL_REQUEST_NUMBER: ${{ github.event_name == 'workflow_dispatch' && steps.pr-data.outputs.PR_NUMBER || '' }}
- name: Fetch base ref for fallback
if: steps.check-en-changes.outputs.HAS_CHANGES == 'true'
run: git fetch --no-tags --depth=1 --no-recurse-submodules origin +refs/heads/${{ steps.pr-data.outputs.BASE_REF }}:refs/heads/${{ steps.pr-data.outputs.BASE_REF }}
- name: Run generateTranslations for added translations
if: steps.check-en-changes.outputs.HAS_CHANGES == 'true'
run: npx ts-node ./scripts/generateTranslations.ts --verbose --yes --pr-number=${{ steps.pr-data.outputs.PR_NUMBER }} --compare-ref=${{ steps.pr-data.outputs.BASE_REF }}
env:
GITHUB_TOKEN: ${{ github.token }}
OPENAI_API_KEY: ${{ secrets.PROPOSAL_POLICE_API_KEY }}
- name: Check if there are any changes after running generateTranslations
if: steps.check-en-changes.outputs.HAS_CHANGES == 'true'
id: checkTranslationDiff
run: |
if git diff --quiet HEAD; then
echo "✅ No changes detected"
echo "HAS_DIFF=false" >> "$GITHUB_OUTPUT"
else
echo "🦜 Polyglot Parrot detected changes! 🦜"
echo "HAS_DIFF=true" >> "$GITHUB_OUTPUT"
fi
- name: Hide any existing Polyglot Parrot comments
continue-on-error: true
if: steps.check-en-changes.outputs.HAS_CHANGES == 'true' && steps.checkTranslationDiff.outputs.HAS_DIFF == 'true'
run: |
EXISTING_COMMENTS="$(gh pr view ${{ steps.pr-data.outputs.PR_NUMBER }} --json comments --jq '.comments[] | select(.body | startswith("## 🦜 Polyglot Parrot! 🦜")) | .id')"
if [[ -z "$EXISTING_COMMENTS" ]]; then
echo "🦗 No existing Polyglot Parrot comments found"
else
echo "Found existing Polyglot Parrot comment(s), hiding as outdated..."
echo "$EXISTING_COMMENTS" | while read -r comment_id; do
# shellcheck disable=SC2016
gh api graphql -f query='
mutation($commentId: ID!) {
minimizeComment(input: {
subjectId: $commentId,
classifier: OUTDATED
}) {
minimizedComment {
isMinimized
}
}
}' -f commentId="$comment_id" || echo "Failed to minimize comment $comment_id"
done
fi
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Post diff in PR as a comment
if: steps.check-en-changes.outputs.HAS_CHANGES == 'true' && steps.checkTranslationDiff.outputs.HAS_DIFF == 'true'
run: |
# Create temp files and set up cleanup
TEMP_COMMENT_FILE=$(mktemp)
TEMP_PATCH_FILE=$(mktemp --suffix=.patch)
trap 'rm -f "$TEMP_COMMENT_FILE" "$TEMP_PATCH_FILE"' EXIT
# Generate the diff and save to patch file
git diff HEAD > "$TEMP_PATCH_FILE"
# Ensure the patch ends with a newline to prevent "corrupt patch" errors
echo "" >> "$TEMP_PATCH_FILE"
# Check file size (65536 chars is ~64KB, let's use 60KB as safety margin)
PATCH_SIZE=$(wc -c < "$TEMP_PATCH_FILE")
readonly MAX_COMMENT_SIZE=61440 # 60KB in bytes
readonly PARROT_HEADER="## 🦜 Polyglot Parrot! 🦜"
readonly PARROT_INTRO="_Squawk!_ Looks like you added some shiny new English strings. Allow me to parrot them back to you in other tongues:"
PARROT_FOOTER="$(cat <<'EOF'
> [!NOTE]
> You can apply these changes to your branch by copying the patch to your clipboard, then running `pbpaste | git apply`
EOF
)"
readonly PARROT_FOOTER
readonly WORKFLOW_RUN_LINK="[View workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
if [ "$PATCH_SIZE" -le "$MAX_COMMENT_SIZE" ]; then
# Small diff - include in comment
{
echo "$PARROT_HEADER"
echo
echo "$PARROT_INTRO"
echo
echo '<details>'
echo '<summary>View the translation diff</summary>'
echo
echo '```diff'
cat "$TEMP_PATCH_FILE"
echo '```'
echo
echo '</details>'
echo
echo "$PARROT_FOOTER 😉"
echo
echo "${WORKFLOW_RUN_LINK}"
} > "$TEMP_COMMENT_FILE"
else
# Large diff - upload as gist and link
echo "📁 Creating gist for large diff..."
GIST_URL=$(gh gist create "$TEMP_PATCH_FILE" --desc "🦜 Polyglot Parrot translations for PR #${{ steps.pr-data.outputs.PR_NUMBER }}" --filename "translations.patch")
GIST_ID="${GIST_URL##*/}"
PARROT_FOOTER_WITH_CMD="${PARROT_FOOTER}, or directly by running \`gh gist view --raw ${GIST_ID} | git apply\`"
{
echo "$PARROT_HEADER"
echo
echo "$PARROT_INTRO"
echo
echo "The diff is too large to include in this comment _($((PATCH_SIZE / 1000))KB)_, so I've created a gist for you:"
echo
echo "📋 **[View the translation diff here](${GIST_URL})** 📋"
echo
echo "$PARROT_FOOTER_WITH_CMD 😉"
echo
echo "${WORKFLOW_RUN_LINK}"
} > "$TEMP_COMMENT_FILE"
fi
# Post comment using the temp file
gh pr comment ${{ steps.pr-data.outputs.PR_NUMBER }} --body-file "$TEMP_COMMENT_FILE"
env:
# Use OS_BOTIFY_TOKEN so that we can create a gist
GITHUB_TOKEN: ${{ secrets.OS_BOTIFY_TOKEN }}