Skip to content

Commit 53fbf8a

Browse files
committed
chore(gha): update mobsuccess.yml GitHub workflow
1 parent 77ecccf commit 53fbf8a

1 file changed

Lines changed: 131 additions & 1 deletion

File tree

‎.github/workflows/mobsuccess.yml‎

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ on:
2323
jobs:
2424
MobsuccessPRCompliance:
2525
name: "Mobsuccess PR Compliance"
26-
runs-on: ubuntu-24.04
26+
runs-on: ubuntu-slim
2727
timeout-minutes: 2
2828
steps:
2929
- uses: mobsuccess-devops/github-actions-mobsuccess@master
@@ -32,4 +32,134 @@ jobs:
3232
amplify-uri: ${{ secrets.AWS_AMPLIFY_URI }}${{ vars.AWS_AMPLIFY_URI }}
3333
storybook-amplify-uri: ${{ secrets.AWS_STORYBOOK_AMPLIFY_URI }}${{ vars.AWS_STORYBOOK_AMPLIFY_URI }}
3434
action: "validate-pr"
35+
36+
# blob:none keeps this cheap even on the monolith: full commit graph (needed
37+
# for the base...head merge base), file contents fetched only on demand.
38+
- name: Checkout
39+
if: github.event_name == 'pull_request'
40+
uses: actions/checkout@v6
41+
with:
42+
fetch-depth: 0
43+
filter: blob:none
44+
45+
# A .gitignore pattern that only reaches INSIDE a directory (dir/, dir/*,
46+
# dir/**) does not ignore a symlink or file at `dir` itself, so `git add -A`
47+
# stages it. Merging that commit makes git delete the ignored directory to
48+
# put the tracked entry in its place — silently, with no trash and no reflog
49+
# for untracked content. Already happened in the org: panoramai#433.
50+
- name: Ignored directory replacement
51+
if: github.event_name == 'pull_request'
52+
env:
53+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
54+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
55+
run: |
56+
set -uo pipefail
57+
fail=0
58+
59+
# Fail closed. This guard stands in front of an irreversible deletion, so
60+
# it must never report success because git itself failed: an empty result
61+
# from a command that errored would otherwise read as "nothing to flag".
62+
# The runner's default shell is `bash -e`, but nothing here leans on that:
63+
# check-ignore's non-zero exit is this script's core predicate, so every
64+
# git call whose silence would be mistaken for success is checked by hand.
65+
#
66+
# printf rather than echo throughout: some shells expand backslash escapes
67+
# in echo, which would turn a literal \n inside a path back into a real
68+
# newline and undo the escaping below.
69+
die() { printf '%s\n' "::error::$(esc "$1")"; exit 1; }
70+
71+
# GitHub parses workflow commands one line at a time, so every value
72+
# interpolated into one is escaped: a path may legally contain a newline,
73+
# which would otherwise let it forge a second command. Property values
74+
# need `:` and `,` too, as those delimit the property list.
75+
esc() { local s=${1//'%'/%25}; s=${s//$'\r'/%0D}; s=${s//$'\n'/%0A}; printf '%s' "$s"; }
76+
esc_prop() { local s; s=$(esc "$1"); s=${s//:/%3A}; s=${s//,/%2C}; printf '%s' "$s"; }
77+
78+
work=$(mktemp -d)
79+
trap 'rm -rf "$work"' EXIT
80+
81+
# -z keeps paths literal. Without it git C-quotes any path containing a
82+
# control character, and that quoted string matches neither the repo nor
83+
# the ignore rules — so a crafted path could slip past the check. Literal
84+
# paths are why the escaping above is required, not optional.
85+
git diff --raw -z --no-renames --diff-filter=AMT "$BASE_SHA...$HEAD_SHA" > "$work/diff" \
86+
|| die "Cannot diff $BASE_SHA...$HEAD_SHA. Refusing to pass without having checked."
87+
git ls-tree -r -z --name-only "$HEAD_SHA" > "$work/tree" \
88+
|| die "Cannot list the tree at $HEAD_SHA. Refusing to pass without having checked."
89+
90+
# Probe sandbox: HEAD's .gitignore files and nothing else. Probing cannot
91+
# happen in the real worktree — once the symlink exists on disk, git
92+
# refuses to resolve a pathspec "beyond a symbolic link".
93+
sandbox="$work/sandbox"
94+
mkdir -p "$sandbox"
95+
git -C "$sandbox" init -q .
96+
97+
# Only ignore rules committed to the repo may decide the verdict. A
98+
# runner's global core.excludesFile and the info/exclude the init
99+
# template writes both reach --no-index, and would make the result
100+
# depend on the machine rather than on the PR.
101+
: > "$sandbox/.git/info/exclude"
102+
ignored() {
103+
git -c core.excludesFile=/dev/null -C "$sandbox" \
104+
check-ignore -q --no-index -- "$1"
105+
}
106+
107+
while IFS= read -r -d '' gi; do
108+
case "$gi" in .gitignore|*/.gitignore) ;; *) continue ;; esac
109+
mkdir -p "$sandbox/$(dirname "$gi")"
110+
git show "$HEAD_SHA:$gi" > "$sandbox/$gi" \
111+
|| die "Cannot read $gi at $HEAD_SHA. Refusing to pass without having checked."
112+
done < "$work/tree"
113+
114+
# --raw -z emits ":<modes> <shas> <status>" and the path as two separate
115+
# NUL-terminated records, so read them in pairs.
116+
while IFS= read -r -d '' meta && IFS= read -r -d '' path; do
117+
mode=$(printf '%s' "$meta" | awk '{print $2}')
118+
# A tracked directory is normal; only blobs and symlinks land at a path.
119+
case "$mode" in 040000|000000) continue ;; esac
120+
121+
# Hole test, decided by git's own matcher rather than pattern parsing,
122+
# so negations, nesting and precedence are handled for free: children
123+
# of the path are ignored, the path itself is not. check-ignore uses
124+
# lstat, so a symlink to a real directory still reads as "not a dir".
125+
if ! ignored "$path" && ignored "$path/__probe__"; then
126+
kind="file"
127+
if [ "$mode" = "120000" ]; then
128+
t=$(git show "$HEAD_SHA:$path") \
129+
|| die "Cannot read the symlink target of $path at $HEAD_SHA."
130+
kind="symlink -> $t"
131+
fi
132+
printf '%s\n' "::error file=$(esc_prop "$path")::$(esc "Tracking '$path' ($kind) will DELETE the ignored directory of the same name when this is merged or checked out — its contents are gitignored but the path itself is not.")"
133+
fail=1
134+
fi
135+
136+
# Independent rule: a tracked symlink must not escape the worktree.
137+
if [ "$mode" = "120000" ]; then
138+
target=$(git show "$HEAD_SHA:$path") \
139+
|| die "Cannot read the symlink target of $path at $HEAD_SHA."
140+
# normpath is pure string work: it must not resolve against the
141+
# worktree, where the symlink already exists.
142+
resolved=$(python3 -c 'import posixpath,sys; print(posixpath.normpath(posixpath.join(posixpath.dirname(sys.argv[1]), sys.argv[2])))' "$path" "$target") \
143+
|| die "Cannot resolve the symlink target of $path."
144+
escape=0
145+
case "$target" in /*) escape=1 ;; esac
146+
case "$resolved" in ..*) escape=1 ;; esac
147+
if [ "$escape" -eq 1 ]; then
148+
printf '%s\n' "::error file=$(esc_prop "$path")::$(esc "Symlink '$path' -> '$target' points outside the repository. Tracked symlinks must stay repo-internal.")"
149+
fail=1
150+
fi
151+
fi
152+
done < "$work/diff"
153+
154+
if [ "$fail" -ne 0 ]; then
155+
echo ""
156+
echo "Remove the offending entry from the commit: git rm --cached <path>"
157+
echo ""
158+
echo "Do NOT 'fix' the .gitignore instead: 'dir/*' plus '!dir/keep' cannot be"
159+
echo "rewritten as a bare 'dir', because git will not re-include anything inside"
160+
echo "an ignored directory. A bare 'dir' is correct only where there is no"
161+
echo "negation to preserve."
162+
exit 1
163+
fi
164+
echo "No ignored-directory replacement detected."
35165
# DO NOT EDIT: END

0 commit comments

Comments
 (0)