forked from cncf/glossary
-
Notifications
You must be signed in to change notification settings - Fork 0
176 lines (147 loc) · 6.13 KB
/
check-outdated-content.yaml
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
# This workflow will check if a localized content is outdated or not
# by comparing English content in the old branch and the latest branch.
name: Check outdated content
on:
pull_request:
branches:
- 'dev-ko'
- 'dev-es'
paths:
- 'content/en/**.md'
jobs:
check-outdated-content:
name: Check outdated content
if: contains(fromJSON('["dev-ko","dev-es"]'), github.base_ref)
# Ref: https://docs.github.com/en/actions/learn-github-actions/expressions
#if: github.base_ref == 'dev-ko'
# Condition to run this workflow on the upstream repository
#if: github.repository == 'cncf/glossary'
runs-on: ubuntu-latest
# permissions:
# issues: write
# NOTE - In this workflow, "github.base_ref" refers to the old upstream/dev-xx.
# NOTE - In this workflow, "github.head_ref" refers to the latest forked/dev-xx or the latest upstream/main.
steps:
- name: Set up environment variables for the target L10n team
shell: bash
run: |
# Set L10n branch
L10N_BRANCH="${{github.base_ref}}"
echo "(DEBUG) L10N Branch: ${L10N_BRANCH}"
# Set output direcory
OUTPUT_DIR="./outdated"
# Set L10n code
L10N_CODE="${L10N_BRANCH//dev-/}"
echo "(DEBUG) L10N Code: ${L10N_CODE}"
# Set L10n directory
case "${L10N_BRANCH}" in
dev-pt)
L10N_DIR="content/pt-br/"
;;
dev-zh)
L10N_DIR="content/zh-cn/"
;;
dev-tw)
L10N_DIR="content/zh-tw/"
;;
*)
L10N_DIR="content/${L10N_CODE}/"
;;
esac
echo "(DEBUG) L10N Directory: ${L10N_DIR}"
# Set L10N_DIR, L10N_CODE, and OUTPUT_DIR as environment variables
# Ref: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable
echo "L10N_DIR=${L10N_DIR}" >> $GITHUB_ENV
echo "L10N_CODE=${L10N_CODE}" >> $GITHUB_ENV
echo "OUTPUT_DIR=${OUTPUT_DIR}" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # fetch all history for all tags and branches
- name: Check outdated
id: checker
shell: bash
run: |
##### DEBUG section, this will be removed later ###########
ls -al
git status
git branch
# Default environment variables
echo "GITHUB_REF: $GITHUB_REF"
echo "Extract branch: ${GITHUB_REF#refs/}"
# `github` context information
echo "(DEBUG) github.ref: ${{github.ref}}"
echo "(DEBUG) github.head_ref: ${{github.head_ref}}"
echo "(DEBUG) github.base_ref: ${{github.base_ref}}"
echo "(DEBUG) L10N_DIR: ${L10N_DIR}"
echo "(DEBUG) L10N_DIR: ${{ env.L10N_DIR }}"
#####################################################
# Get the lastest branch name from 'GITHUB_REF'
# The latest branch can be 'upstream/main' or 'forked/dev-ko' (rebased)
LATEST_BRANCH=${GITHUB_REF#refs/}
echo "(DEBUG) LATEST_BRANCH: ${LATEST_BRANCH}"
# Get the old branch from 'github.base_ref'
# The old branch can be 'upstream/dev-ko'
OLD_BRANCH="origin/${{github.base_ref}}"
echo "(DEBUG) OLD_BRANCH: ${OLD_BRANCH}"
L10N_INFO_JSON=$(cat <<EOF
{
"L10N_DIR": "${L10N_DIR}",
"L10N_CODE": "${L10N_CODE}"
}
EOF
)
# Make an output directory
if [[ ! -e $OUTPUT_DIR ]]; then
mkdir $OUTPUT_DIR
echo "${L10N_INFO_JSON}" > ${OUTPUT_DIR}/L10N_INFO.json
elif [[ ! -d $OUTPUT_DIR ]]; then
echo "$OUTPUT_DIR already exists but is not a directory" 1>&2
fi
# Check outdated only if a localized content exists
# Loop files in a localization directory, which is ${L10N_DIR} (e.g., content/ko/)
echo "(DEBUG) Check outdated"
for L10N_FILE_PATH in $(find ${L10N_DIR} -name '*.md'); do
echo "(DEBUG) L10N_FILE_PATH: ${L10N_FILE_PATH}"
# Note - extracting a pattern-based substring (https://stackoverflow.com/a/19482947)
FILE_PATH="${L10N_FILE_PATH#${L10N_DIR}}"
FILE_DIR=$(dirname ${FILE_PATH})
FILE_NAME=$(basename ${FILE_PATH})
echo "(DEBUG) FILE_PATH: ${FILE_PATH}"
echo "(DEBUG) FILE_DIR: ${FILE_DIR}"
echo "(DEBUG) FILE_NAME: ${FILE_NAME}"
echo "(DEBUG) Localized file path: $L10N_FILE_PATH"
echo "(DEBUG) Original file path: content/en/${FILE_PATH}"
# Create subdirectories
mkdir -p ${OUTPUT_DIR}/${FILE_DIR}
# Actually compare between the old and lastest English content and log diff in the file
if [[ -f "content/en/${FILE_PATH}" ]]; then
# File exists
# Check changes
git diff ${OLD_BRANCH}..${LATEST_BRANCH} -- content/en/${FILE_PATH} > temp.diff
if [[ -s "temp.diff" ]]; then
echo "(DEBUG) ${FILE_PATH} is outdated."
mv temp.diff ${OUTPUT_DIR}/${FILE_PATH}
fi
else
echo "(DEBUG) ${FILE_PATH} does not exist."
# File dose not exist (e.g, changed, renamed or removed)
echo "Could not find ${FILE_PATH} in content/en/" > ${OUTPUT_DIR}/${FILE_PATH}
echo "Need to check if it has been changed, renamed or removed" >> ${OUTPUT_DIR}/${FILE_PATH}
fi
done
echo "(DEBUG) The outdated files"
ls -al ${OUTPUT_DIR}
- name: Upload output
uses: actions/upload-artifact@v4
with:
name: ${{ env.L10N_CODE }}-outdated-checking-result
path: ${{ env.OUTPUT_DIR }}/
# - name: Create an issue from file
# uses: peter-evans/create-issue-from-file@v4
# with:
# title: An example issue
# content-filepath: ${{ steps.checker.outputs.output_path }}
# labels: |
# outdated
# lang/ko