Skip to content

Commit 1445c7c

Browse files
authored
Create check-example-sync-conflict.yml (#279)
* Create check-example-sync-conflict.yml * Update check-example-sync-conflict.yml * Update check-example-sync-conflict.yml * Update check-example-sync-conflict.yml
1 parent 807de8d commit 1445c7c

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
name: Check for example tutorial sync conflicts
2+
3+
# When a PR touches sdk/next/tutorials/example/, check if any open docs-sync
4+
# PRs on cosmos/example modify the same files. If so, post or update a warning
5+
# comment on this PR.
6+
7+
on:
8+
pull_request:
9+
paths:
10+
- "sdk/next/tutorials/example/**"
11+
12+
jobs:
13+
check-conflict:
14+
name: Check for open sync PR conflict
15+
runs-on: ubuntu-latest
16+
permissions:
17+
pull-requests: write
18+
19+
steps:
20+
- name: Check for conflicting sync PR on cosmos/example
21+
env:
22+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23+
EXAMPLE_TOKEN: ${{ secrets.EXAMPLE_REPO_TOKEN }}
24+
PR_NUMBER: ${{ github.event.pull_request.number }}
25+
run: |
26+
# Get files changed in this PR that are in the tutorials folder
27+
THIS_PR_FILES=$(gh pr view "$PR_NUMBER" \
28+
--repo cosmos/docs \
29+
--json files \
30+
--jq '[.files[].path | select(startswith("sdk/next/tutorials/example/"))] | sort')
31+
32+
echo "Files changed in this PR: $THIS_PR_FILES"
33+
34+
# Get all open docs-sync PRs on cosmos/example (use cross-repo PAT)
35+
SYNC_PRS=$(GH_TOKEN="$EXAMPLE_TOKEN" gh pr list \
36+
--repo cosmos/example \
37+
--label "docs-sync" \
38+
--state open \
39+
--json number,url)
40+
41+
echo "Open sync PRs: $SYNC_PRS"
42+
43+
if [ "$SYNC_PRS" = "[]" ] || [ -z "$SYNC_PRS" ]; then
44+
echo "No open sync PRs on cosmos/example, all clear."
45+
exit 0
46+
fi
47+
48+
# Collect all files from all open sync PRs, mapped to docs site paths
49+
ALL_SYNC_FILES="[]"
50+
SYNC_PR_URLS=""
51+
52+
while IFS= read -r pr; do
53+
SYNC_PR_NUMBER=$(echo "$pr" | jq -r '.number')
54+
SYNC_PR_URL=$(echo "$pr" | jq -r '.url')
55+
SYNC_PR_URLS="$SYNC_PR_URLS $SYNC_PR_URL"
56+
57+
SYNC_FILES=$(GH_TOKEN="$EXAMPLE_TOKEN" gh pr view "$SYNC_PR_NUMBER" \
58+
--repo cosmos/example \
59+
--json files \
60+
--jq '[.files[].path
61+
| select(startswith("docs/"))
62+
| sub("^docs/([0-9]+-)?"; "sdk/next/tutorials/example/")
63+
| sub("\\.md$"; ".mdx")
64+
] | sort')
65+
66+
ALL_SYNC_FILES=$(jq -n \
67+
--argjson a "$ALL_SYNC_FILES" \
68+
--argjson b "$SYNC_FILES" \
69+
'$a + $b | unique | sort')
70+
done < <(echo "$SYNC_PRS" | jq -c '.[]')
71+
72+
echo "All sync PR files (mapped): $ALL_SYNC_FILES"
73+
74+
# Find overlapping files
75+
OVERLAP=$(jq -n \
76+
--argjson a "$THIS_PR_FILES" \
77+
--argjson b "$ALL_SYNC_FILES" \
78+
'[$a[], $b[]] | group_by(.) | map(select(length > 1)) | map(.[0])')
79+
80+
echo "Overlapping files: $OVERLAP"
81+
82+
MARKER="<!-- docs-sync-conflict-check -->"
83+
84+
if [ "$OVERLAP" = "[]" ] || [ -z "$OVERLAP" ]; then
85+
echo "No overlapping files, all clear."
86+
# If a previous warning comment exists, update it to say all clear
87+
EXISTING_COMMENT=$(gh api "repos/cosmos/docs/issues/$PR_NUMBER/comments" \
88+
--jq ".[] | select(.body | contains(\"$MARKER\")) | .id" | head -1)
89+
if [ -n "$EXISTING_COMMENT" ]; then
90+
gh api "repos/cosmos/docs/issues/comments/$EXISTING_COMMENT" \
91+
-X PATCH \
92+
-f body="$MARKER
93+
✅ **Sync conflict resolved** — no overlapping files with open sync PRs on \`cosmos/example\`."
94+
fi
95+
exit 0
96+
fi
97+
98+
OVERLAP_LIST=$(echo "$OVERLAP" | jq -r '.[] | "- `\(.)`"')
99+
SYNC_PR_LINKS=$(echo "$SYNC_PR_URLS" | tr ' ' '\n' | grep -v '^$' | sed 's/^/- /')
100+
101+
COMMENT_BODY=$(cat <<EOF
102+
${MARKER}
103+
⚠️ **Potential sync conflict detected**
104+
105+
This PR modifies example tutorial files that are also modified in open sync PR(s) on \`cosmos/example\`:
106+
${SYNC_PR_LINKS}
107+
108+
**Overlapping files:**
109+
${OVERLAP_LIST}
110+
111+
These files are kept in sync between the two repos. Merging this PR before the sync PR is resolved may cause conflicts. Please coordinate with the sync PR author or wait until it is merged first.
112+
EOF
113+
)
114+
115+
# Update existing comment or post a new one
116+
EXISTING_COMMENT=$(gh api "repos/cosmos/docs/issues/$PR_NUMBER/comments" \
117+
--jq ".[] | select(.body | contains(\"$MARKER\")) | .id" | head -1)
118+
119+
if [ -n "$EXISTING_COMMENT" ]; then
120+
gh api "repos/cosmos/docs/issues/comments/$EXISTING_COMMENT" \
121+
-X PATCH \
122+
-f body="$COMMENT_BODY"
123+
echo "Updated existing warning comment."
124+
else
125+
gh pr comment "$PR_NUMBER" --repo cosmos/docs --body "$COMMENT_BODY"
126+
echo "Posted new warning comment."
127+
fi

0 commit comments

Comments
 (0)