Repository navigation
Expand file tree
/
Copy pathdelete-orphan-translation-files.yml
More file actions
151 lines (127 loc) · 5.48 KB
/
Copy pathdelete-orphan-translation-files.yml
File metadata and controls
151 lines (127 loc) · 5.48 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
name: Delete orphan translation files
# Deletes orphan translation files because Juno cannot remove docs-internal moves or deletes.
on:
workflow_dispatch:
schedule:
- cron: '20 16 * * 1' # Mondays at 16:20 UTC.
permissions:
contents: write
jobs:
delete-orphan-translation-files:
if: github.repository == 'github/docs-internal'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- language: zh
language_dir: translations/zh-cn
language_repo: github/docs-internal.zh-cn
- language: es
language_dir: translations/es-es
language_repo: github/docs-internal.es-es
- language: pt
language_dir: translations/pt-br
language_repo: github/docs-internal.pt-br
- language: ru
language_dir: translations/ru-ru
language_repo: github/docs-internal.ru-ru
- language: ja
language_dir: translations/ja-jp
language_repo: github/docs-internal.ja-jp
- language: fr
language_dir: translations/fr-fr
language_repo: github/docs-internal.fr-fr
- language: de
language_dir: translations/de-de
language_repo: github/docs-internal.de-de
- language: ko
language_dir: translations/ko-kr
language_repo: github/docs-internal.ko-kr
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
- name: Checkout the language-specific repo
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
repository: ${{ matrix.language_repo }}
token: ${{ secrets.DOCS_BOT_PAT_BASE }}
path: ${{ matrix.language_dir }}
- uses: ./.github/actions/node-npm-setup
- name: Delete orphan files
run: |
npm run delete-orphan-translation-files -- ${{ matrix.language_dir }}
- name: Debug deleted files
working-directory: ${{ matrix.language_dir }}
run: git status
- name: Git config
working-directory: ${{ matrix.language_dir }}
run: |
git config --global user.name "docs-bot"
git config --global user.email "77750099+docs-bot@users.noreply.github.com"
# Prefer auto-merge so required checks can finish before merge.
# If GitHub reports missing branch rules, unstable, or not mergeable, fall back to direct merge.
- name: Git commit and push, create and merge PR
working-directory: ${{ matrix.language_dir }}
env:
# GH_TOKEN gives gh docs-bot permissions for translation repos.
GH_TOKEN: ${{ secrets.DOCS_BOT_PAT_BASE }}
run: |
# Exit when the deletion script found no orphan files.
changes=$(git diff --name-only | wc -l)
untracked=$(git status --untracked-files --short | wc -l)
if [[ $changes -eq 0 ]] && [[ $untracked -eq 0 ]]; then
echo "There are no changes to commit or untracked files. Exiting."
exit 0
fi
retry_command() {
local max_attempts=3
local attempt=1
while [ $attempt -le $max_attempts ]; do
echo "Attempt $attempt: $@"
"$@" && return 0
((attempt++))
sleep 3
done
echo "Max attempts reached. Command failed after $max_attempts attempts."
return 1
}
git status
current_timestamp=$(date '+%Y-%m-%d-%H%M%S')
branch_name="delete-orphan-files-$current_timestamp"
git checkout -b "$branch_name"
current_daystamp=$(date '+%Y-%m-%d')
git commit -a -m "Delete orphan files ($current_daystamp)"
git push origin "$branch_name"
echo "Creating pull request..."
gh pr create \
--title "Delete orphan files ($current_daystamp)" \
--body '👋 humans. This PR was generated by the [Delete orphan translation files](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) workflow run.
' \
--repo "${{ matrix.language_repo }}" \
--label "workflow-generated" \
--head=$branch_name
echo "Merge created PR..."
auto_merge_err=$(mktemp)
trap 'rm -f "$auto_merge_err"' EXIT
if retry_command gh pr merge --merge --auto --delete-branch "$branch_name" 2>"$auto_merge_err"; then
:
else
cat "$auto_merge_err"
if grep -qE "does not have required protected branch rules|unstable status|Pull request is not in a mergeable state" "$auto_merge_err"; then
echo "Auto-merge unavailable; PR appears immediately mergeable. Falling back to direct merge."
retry_command gh pr merge --merge --delete-branch "$branch_name"
else
echo "Auto-merge failed with an unexpected error."
exit 1
fi
fi
- uses: ./.github/actions/create-workflow-failure-issue
id: create-failure-issue
if: ${{ failure() && github.event_name != 'workflow_dispatch' }}
with:
token: ${{ secrets.DOCS_BOT_PAT_BASE }}
- uses: ./.github/actions/slack-alert
if: ${{ failure() && github.event_name != 'workflow_dispatch' }}
with:
slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }}
issue_url: ${{ steps.create-failure-issue.outputs.issue_url }}