Skip to content

Commit 4325cc4

Browse files
authored
workflow to run stylecheck (#187)
* workflow to run stylecheck * change to trigger check * temp files * secret for LLM * fix archive name * include commit ID * fix date format again * post full text of revision where suggestions are outside the PR diff * control execution with label * fixed label condition * fixed success criteria * include developer style rules * run if label removed
1 parent 3312991 commit 4325cc4

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

.github/workflows/stylecheck.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: PR Review Suggestions
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, labeled, unlabeled]
6+
paths:
7+
- '**.md'
8+
- '**.mdx'
9+
10+
jobs:
11+
suggest:
12+
if: contains(github.event.pull_request.labels.*.name, 'stylecheck') && !contains(github.event.pull_request.labels.*.name, 'stylecheck-complete')
13+
runs-on: ubuntu-latest
14+
permissions:
15+
pull-requests: write
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Set up Python
23+
uses: actions/setup-python@v4
24+
with:
25+
python-version: '3.x'
26+
27+
- name: Install dependencies
28+
run: |
29+
python -m pip install --upgrade pip
30+
pip install requests
31+
32+
- name: Run stylecheck on changed files
33+
id: stylecheck
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.STYLECHECK }}
36+
LLM_JWT: ${{ secrets.LLM_JWT }}
37+
REPO_OWNER: ${{ github.repository_owner }}
38+
REPO_NAME: ${{ github.event.repository.name }}
39+
PR_NUMBER: ${{ github.event.pull_request.number }}
40+
COMMIT_SHA: ${{ github.event.pull_request.head.sha }}
41+
run: |
42+
echo "DATE=$(date +'%Y-%m-%d-%H%M%S')" >> $GITHUB_ENV
43+
mkdir -p temp
44+
for file in $(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -E '\.mdx?$'); do
45+
python stylecheck.py --style=style/developer.md "$file"
46+
done
47+
- name: Add success label
48+
if: success() && steps.stylecheck.outcome == 'success'
49+
run: |
50+
gh pr edit ${{ github.event.pull_request.number }} --add-label "stylecheck-complete"
51+
env:
52+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53+
- name: Archive temp files
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: stylecheck-${{ env.DATE }}
57+
path: temp/**

style/sample-common.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This integration links Datadog's monitoring system with Devrev's incident manage
66

77
- Automated Incident Creation: When an incident is triggered in Datadog, the snap-in automatically creates a corresponding incident in DevRev.
88
- Automated Incident Updation: When an incident is updated in Datadog, the changes are reflected in DevRev.
9-
- Real-Time Synchronization: The snap-in ensures that incidents are created or updated in real-time, reducing delays between detection and resolution. DevRev captures the necessary incident details from the Datadog payload, allowing teams to address and mitigate issues.
9+
- Real-time Synchronization: The snap-in ensures that incidents are created or updated in real-time, reducing delays between detection and resolution. DevRev captures the necessary incident details from the Datadog payload, allowing teams to address and mitigate issues.
1010
- Seamless integration: The snap-in is built to streamline workflows, minimizing the need for manual intervention.
1111

1212
### Installing the Snap-in

stylecheck.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def parse_diff_hunks(diff_text):
116116
'path': path,
117117
'line': old_start + old_lines - 1,
118118
'side': 'RIGHT',
119-
'body': f'```suggestion\n{added_lines}\n```'
119+
'body': f'```suggestion\n{added_lines}\n```',
120+
'commit_id': os.environ.get('COMMIT_SHA')
120121
}
121122
if old_start < comment['line']:
122123
comment['start_line'] = old_start
@@ -128,29 +129,33 @@ def parse_diff_hunks(diff_text):
128129

129130
return comments
130131

131-
def post_review_comment(owner, repo, pr_number, comment):
132-
comment['commit_id'] = os.environ.get('COMMIT')
132+
def post_review_comment(comment):
133+
owner = os.environ.get('REPO_OWNER')
134+
repo = os.environ.get('REPO_NAME')
135+
pr = os.environ.get('PR_NUMBER')
133136

134137
if comment.get('line'):
135138
# Suggestion
136-
url = f'https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}/comments'
139+
url = f'https://api.github.com/repos/{owner}/{repo}/pulls/{pr}/comments'
137140
message = f"Posted comment on line {comment['line']}."
138141
else:
139142
# Timeline comment
140-
url = f'https://api.github.com/repos/{owner}/{repo}/issues/{pr_number}/comments'
143+
url = f'https://api.github.com/repos/{owner}/{repo}/issues/{pr}/comments'
141144
message = f"Posted comment on timeline."
142145

143146
headers = {
144-
'Authorization': f"token {os.environ.get('STYLECHECK')}",
147+
'Authorization': f"token {os.environ.get('GITHUB_TOKEN')}",
145148
'Accept': 'application/vnd.github.v3+json'
146149
}
147150
try:
148151
response = requests.post(url, headers=headers, json=comment)
149152
response.raise_for_status()
150153
print(message)
154+
return 0
151155
except Exception as e:
152156
print(
153157
f"Failed to post comment. Error: {type(e)} {e}")
158+
return 1
154159

155160
def my_writer(data, file, note=None):
156161
if (data):
@@ -192,11 +197,27 @@ def main(args):
192197

193198
suggestions = parse_diff_hunks(diff)
194199
my_writer(suggestions, f"temp/{doc_name}_suggestions.json", 'suggestions')
200+
failures = 0
195201
if (args.suggest):
196-
post_review_comment(os.environ.get('OWNER'), os.environ.get('REPO'), os.environ.get('PR'), {'body': comment_text})
197202
for suggestion in suggestions:
198203
time.sleep(1)
199-
post_review_comment(os.environ.get('OWNER'), os.environ.get('REPO'), os.environ.get('PR'), suggestion)
204+
failures += post_review_comment(suggestion)
205+
if failures > 0:
206+
comment_text += f"""
207+
208+
I made suggestions where possible but couldn't add them everywhere. My complete revision is below.
209+
210+
<details><summary>Full text</summary>
211+
212+
```
213+
{revision}
214+
```
215+
216+
</details>
217+
218+
"""
219+
post_review_comment({'body': comment_text})
220+
200221

201222
if __name__ == "__main__":
202223
parser = argparse.ArgumentParser(description="Check writing style of markdown file")

0 commit comments

Comments
 (0)