Skip to content

Commit 67ea4f4

Browse files
committed
CI/verify-coderef: add comment to PR when fails
1 parent a7b5604 commit 67ea4f4

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

.github/scripts/verify-code-references.sh

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,41 @@ EXIT_CODE=0
1717
TOTAL_REFS=0
1818
VALID_REFS=0
1919
INVALID_REFS=0
20+
COMMENT_FILE=""
21+
ERRORS_COLLECTED=""
22+
23+
# Parse arguments
24+
while [[ $# -gt 0 ]]; do
25+
case $1 in
26+
--pr-comment)
27+
COMMENT_FILE="$2"
28+
shift 2
29+
;;
30+
*)
31+
echo "Unknown option: $1"
32+
exit 1
33+
;;
34+
esac
35+
done
2036

2137
echo "Verifying code references in documentation..."
2238
echo "Repository root: ${REPO_ROOT}"
39+
if [[ -n "${COMMENT_FILE}" ]]; then
40+
echo "PR comment mode: will write to ${COMMENT_FILE}"
41+
fi
2342
echo ""
2443

44+
# Function to add error to collection
45+
add_error() {
46+
local error_msg="$1"
47+
if [[ -z "${ERRORS_COLLECTED}" ]]; then
48+
ERRORS_COLLECTED="${error_msg}"
49+
else
50+
ERRORS_COLLECTED="${ERRORS_COLLECTED}
51+
${error_msg}"
52+
fi
53+
}
54+
2555
# Find all markdown files with CODE_REFERENCE comments
2656
while IFS= read -r doc_file; do
2757
# Extract CODE_REFERENCE comments from this file
@@ -41,11 +71,15 @@ while IFS= read -r doc_file; do
4171

4272
TOTAL_REFS=$((TOTAL_REFS + 1))
4373

74+
# Get relative path for documentation file
75+
doc_file_rel="${doc_file#"${REPO_ROOT}"/}"
76+
4477
# Check if the source file exists
4578
source_file="${REPO_ROOT}/${file_path}"
4679
if [[ ! -f "$source_file" ]]; then
4780
echo -e "${RED}${NC} Invalid reference in ${doc_file}:${line_num}"
4881
echo " File not found: ${file_path}"
82+
add_error "- **${doc_file_rel}:${line_num}** - File not found: \`${file_path}\`"
4983
INVALID_REFS=$((INVALID_REFS + 1))
5084
EXIT_CODE=1
5185
continue
@@ -57,6 +91,7 @@ while IFS= read -r doc_file; do
5791
echo -e "${RED}${NC} Invalid reference in ${doc_file}:${line_num}"
5892
echo " Line range L${start_line}-L${end_line} exceeds file length (${total_lines} lines)"
5993
echo " File: ${file_path}"
94+
add_error "- **${doc_file_rel}:${line_num}** - Line range L${start_line}-L${end_line} exceeds file length (${total_lines} lines) in \`${file_path}\`"
6095
INVALID_REFS=$((INVALID_REFS + 1))
6196
EXIT_CODE=1
6297
continue
@@ -102,25 +137,29 @@ while IFS= read -r doc_file; do
102137
echo " ${file_path}#L${start_line}-L${end_line}"
103138
echo " Local code differs from GitHub (${branch})"
104139
echo " This may indicate uncommitted changes or branch divergence"
140+
add_error "- **${doc_file_rel}:${line_num}** - Code reference to \`${file_path}#L${start_line}-L${end_line}\` differs from GitHub (\`${branch}\` branch). The referenced code may have been modified locally but not yet merged to \`${branch}\`."
105141
INVALID_REFS=$((INVALID_REFS + 1))
106142
EXIT_CODE=1
107143
fi
108144
else
109145
echo -e "${YELLOW}${NC} Could not parse GitHub URL in ${doc_file}:${line_num}"
110146
echo " URL: ${github_url}"
147+
add_error "- **${doc_file_rel}:${line_num}** - Could not parse GitHub URL: \`${github_url}\`"
111148
INVALID_REFS=$((INVALID_REFS + 1))
112149
EXIT_CODE=1
113150
fi
114151
else
115152
echo -e "${YELLOW}${NC} Mismatched line range in ${doc_file}:${line_num}"
116153
echo " CODE_REFERENCE comment specifies: L${start_line}-L${end_line}"
117154
echo " But GitHub URL has different line range"
155+
add_error "- **${doc_file_rel}:${line_num}** - CODE_REFERENCE comment specifies L${start_line}-L${end_line} but GitHub URL has different line range"
118156
INVALID_REFS=$((INVALID_REFS + 1))
119157
EXIT_CODE=1
120158
fi
121159
else
122160
echo -e "${YELLOW}${NC} No GitHub URL found for reference in ${doc_file}:${line_num}"
123161
echo " Expected rust reference block with GitHub URL"
162+
add_error "- **${doc_file_rel}:${line_num}** - No GitHub URL found for reference (expected rust reference block with GitHub URL)"
124163
INVALID_REFS=$((INVALID_REFS + 1))
125164
EXIT_CODE=1
126165
fi
@@ -145,6 +184,37 @@ if [[ $EXIT_CODE -eq 0 ]]; then
145184
echo -e "${GREEN}✓ All code references are valid!${NC}"
146185
else
147186
echo -e "${RED}✗ Some code references are invalid. Please update the documentation.${NC}"
187+
188+
# If in PR comment mode, write the comment to file
189+
if [[ -n "${COMMENT_FILE}" ]]; then
190+
cat > "${COMMENT_FILE}" <<EOF
191+
## ⚠️ Code Reference Verification Failed
192+
193+
The documentation contains code references that do not match the current state of the codebase on the \`develop\` branch.
194+
195+
### Issues Found
196+
197+
${ERRORS_COLLECTED}
198+
199+
### Action Required
200+
201+
**The code referenced in the documentation must be merged to \`develop\` before documentation can be added/modified.**
202+
203+
Please follow this workflow:
204+
1. Merge the code changes to \`develop\` first (this PR or a separate code PR)
205+
2. Create a follow-up PR with the documentation updates that reference the merged code
206+
3. The verification will pass once the code is available on \`develop\`
207+
208+
See the [documentation guidelines](https://o1-labs.github.io/mina-rust/developers/documentation-guidelines) for more information about the two-PR workflow.
209+
EOF
210+
echo ""
211+
echo "PR comment written to: ${COMMENT_FILE}"
212+
fi
148213
fi
149214

150-
exit $EXIT_CODE
215+
# In PR comment mode, don't fail the workflow - just post the comment
216+
if [[ -n "${COMMENT_FILE}" ]]; then
217+
exit 0
218+
else
219+
exit $EXIT_CODE
220+
fi

.github/workflows/docs.yaml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,29 @@ jobs:
5151
# - ./github/workflows/lint.yaml
5252
toolchain: nightly
5353

54-
- name: Verify code references in documentation
54+
- name: Verify code references in documentation (PR mode)
55+
if: github.event_name == 'pull_request'
56+
id: verify-pr
57+
run: |
58+
bash .github/scripts/verify-code-references.sh --pr-comment /tmp/pr-comment.md
59+
if [ -f /tmp/pr-comment.md ]; then
60+
echo "has_errors=true" >> $GITHUB_OUTPUT
61+
echo "Comment file created, will post to PR"
62+
cat /tmp/pr-comment.md
63+
else
64+
echo "has_errors=false" >> $GITHUB_OUTPUT
65+
echo "No errors found, no comment needed"
66+
fi
67+
68+
- name: Comment PR on verification failure
69+
if: github.event_name == 'pull_request' && steps.verify-pr.outputs.has_errors == 'true'
70+
uses: thollander/actions-comment-pull-request@v3
71+
with:
72+
file-path: /tmp/pr-comment.md
73+
comment-tag: code-reference-verification
74+
75+
- name: Verify code references in documentation (non-PR mode)
76+
if: github.event_name != 'pull_request'
5577
run: bash .github/scripts/verify-code-references.sh
5678

5779
- name: Build documentation

0 commit comments

Comments
 (0)