From 2b0e6c97600f884956919c1a6efa8b65fa8b5e1e Mon Sep 17 00:00:00 2001 From: Wenxuan Zhao Date: Tue, 6 Nov 2018 20:08:10 -0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=95=E8=BF=87=E6=A3=80=E6=9F=A5=EF=BC=9A?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=A7=84=E5=88=99=20=E2=80=94=E2=80=94=20?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=96=87=E7=AB=A0=E5=88=86=E7=B1=BB=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E5=8F=98=E6=9B=B4=E4=B8=80=E8=87=B4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 使用 POSIX shell 来保证脚本的可移植性 * 使用未转义的文件名(文件名中切不可有换行符) + 在无更改时返回错误退出 - 移除 news 文章分类 --- scripts/check.sh | 2 +- scripts/check/analyze.sh | 7 +++++- scripts/check/check.sh | 2 +- scripts/check/collect.sh | 13 ++++++++-- scripts/check/common.inc.sh | 48 ++++++++++++++++++++++++++++++++++++- scripts/check/identify.sh | 12 ++++++++-- 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/scripts/check.sh b/scripts/check.sh index ba5707fbf6..59fcfa31f8 100644 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # PR 检查脚本 set -e diff --git a/scripts/check/analyze.sh b/scripts/check/analyze.sh index 9aa47d5664..880bf1b488 100644 --- a/scripts/check/analyze.sh +++ b/scripts/check/analyze.sh @@ -22,7 +22,12 @@ do_analyze() { # 统计每个类别的每个操作 REGEX="$(get_operation_regex "$STAT" "$TYPE")" OTHER_REGEX="${OTHER_REGEX}|${REGEX}" - eval "${TYPE}_${STAT}=\"\$(grep -Ec '$REGEX' /tmp/changes)\"" || true + CHANGES_FILE="/tmp/changes_${TYPE}_${STAT}" + eval "grep -E '$REGEX' /tmp/changes" \ + | sed 's/^[^\/]*\///g' \ + | sort > "$CHANGES_FILE" || true + sed 's/^.*\///g' "$CHANGES_FILE" > "${CHANGES_FILE}_basename" + eval "${TYPE}_${STAT}=$(wc -l < "$CHANGES_FILE")" eval echo "${TYPE}_${STAT}=\$${TYPE}_${STAT}" done done diff --git a/scripts/check/check.sh b/scripts/check/check.sh index a527c225ab..42713e1db0 100644 --- a/scripts/check/check.sh +++ b/scripts/check/check.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # 检查脚本状态 set -e diff --git a/scripts/check/collect.sh b/scripts/check/collect.sh index dc6293e280..3f8e0f0388 100644 --- a/scripts/check/collect.sh +++ b/scripts/check/collect.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # PR 文件变更收集 set -e @@ -31,7 +31,16 @@ git --no-pager show --summary "${MERGE_BASE}..HEAD" echo "[收集] 写出文件变更列表……" -git diff "$MERGE_BASE" HEAD --no-renames --name-status > /tmp/changes +RAW_CHANGES="$(git diff "$MERGE_BASE" HEAD --no-renames --name-status -z \ + | tr '\0' '\n')" +[ -z "$RAW_CHANGES" ] && { + echo "[收集] 无变更,退出……" + exit 1 +} +echo "$RAW_CHANGES" | while read -r STAT; do + read -r NAME + echo "${STAT} ${NAME}" +done > /tmp/changes echo "[收集] 已写出文件变更列表:" cat /tmp/changes { [ -z "$(cat /tmp/changes)" ] && echo "(无变更)"; } || true diff --git a/scripts/check/common.inc.sh b/scripts/check/common.inc.sh index 6012bc2fe5..2bc0334930 100644 --- a/scripts/check/common.inc.sh +++ b/scripts/check/common.inc.sh @@ -10,9 +10,10 @@ export TSL_DIR='translated' # 已翻译 export PUB_DIR='published' # 已发布 # 定义匹配规则 -export CATE_PATTERN='(news|talk|tech)' # 类别 +export CATE_PATTERN='(talk|tech)' # 类别 export FILE_PATTERN='[0-9]{8} [a-zA-Z0-9_.,() -]*\.md' # 文件名 +# 获取用于匹配操作的正则表达式 # 用法:get_operation_regex 状态 类型 # # 状态为: @@ -26,5 +27,50 @@ export FILE_PATTERN='[0-9]{8} [a-zA-Z0-9_.,() -]*\.md' # 文件名 get_operation_regex() { STAT="$1" TYPE="$2" + echo "^${STAT}\\s+\"?$(eval echo "\$${TYPE}_DIR")/" } + +# 确保两个变更文件一致 +# 用法:ensure_identical X类型 X状态 Y类型 Y状态 是否仅比较文件名 +# +# 状态为: +# - A:添加 +# - M:修改 +# - D:删除 +# 类型为: +# - SRC:未翻译 +# - TSL:已翻译 +# - PUB:已发布 +ensure_identical() { + TYPE_X="$1" + STAT_X="$2" + TYPE_Y="$3" + STAT_Y="$4" + NAME_ONLY="$5" + SUFFIX= + [ -n "$NAME_ONLY" ] && SUFFIX="_basename" + + X_FILE="/tmp/changes_${TYPE_X}_${STAT_X}${SUFFIX}" + Y_FILE="/tmp/changes_${TYPE_Y}_${STAT_Y}${SUFFIX}" + + cmp "$X_FILE" "$Y_FILE" 2> /dev/null +} + +# 检查文章分类 +# 用法:check_category 类型 状态 +# +# 状态为: +# - A:添加 +# - M:修改 +# - D:删除 +# 类型为: +# - SRC:未翻译 +# - TSL:已翻译 +check_category() { + TYPE="$1" + STAT="$2" + + CHANGES="/tmp/changes_${TYPE}_${STAT}" + ! grep -Eqv "^${CATE_PATTERN}/" "$CHANGES" +} diff --git a/scripts/check/identify.sh b/scripts/check/identify.sh index f8e4c44160..51a501517f 100644 --- a/scripts/check/identify.sh +++ b/scripts/check/identify.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # 匹配 PR 规则 set -e @@ -27,31 +27,39 @@ rule_bypass_check() { # 添加原文:添加至少一篇原文 rule_source_added() { [ "$SRC_A" -ge 1 ] \ + && check_category SRC A \ && [ "$TOTAL" -eq "$SRC_A" ] && echo "匹配规则:添加原文 ${SRC_A} 篇" } # 申领翻译:只能申领一篇原文 rule_translation_requested() { [ "$SRC_M" -eq 1 ] \ + && check_category SRC M \ && [ "$TOTAL" -eq 1 ] && echo "匹配规则:申领翻译" } # 提交译文:只能提交一篇译文 rule_translation_completed() { [ "$SRC_D" -eq 1 ] && [ "$TSL_A" -eq 1 ] \ + && ensure_identical SRC D TSL A \ + && check_category SRC D \ + && check_category TSL A \ && [ "$TOTAL" -eq 2 ] && echo "匹配规则:提交译文" } # 校对译文:只能校对一篇 rule_translation_revised() { [ "$TSL_M" -eq 1 ] \ + && check_category TSL M \ && [ "$TOTAL" -eq 1 ] && echo "匹配规则:校对译文" } # 发布译文:发布多篇译文 rule_translation_published() { [ "$TSL_D" -ge 1 ] && [ "$PUB_A" -ge 1 ] && [ "$TSL_D" -eq "$PUB_A" ] \ - && [ "$TOTAL" -eq $(($TSL_D + $PUB_A)) ] \ + && ensure_identical SRC D TSL A 1 \ + && check_category TSL D \ + && [ "$TOTAL" -eq $((TSL_D + PUB_A)) ] \ && echo "匹配规则:发布译文 ${PUB_A} 篇" }