Skip to content

Commit 8bd0250

Browse files
fix-the-autoannotate-metadata-workflow (#421)
* testing wf * testing * testing... * Update Molecules/membrane/BOGUS/metadata.yaml Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Added CAS key secret to allow retrieval of CAS numbers in autocomplete_metadata git wf * Remove the test directory. Now only the changes to the workflow remain. --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent cfdbc03 commit 8bd0250

1 file changed

Lines changed: 7 additions & 81 deletions

File tree

.github/workflows/AutocompleteMetadata.yml

Lines changed: 7 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -52,91 +52,17 @@ jobs:
5252
if: steps.files.outputs.changed_files != ''
5353
env:
5454
CHANGED_FILES: ${{ steps.files.outputs.changed_files }}
55+
CAS_API_KEY: ${{ secrets.CAS_API_KEY }}
5556
run: |
5657
printf '%s\n' "$CHANGED_FILES" | while IFS= read -r f; do
5758
[ -n "$f" ] && python "$RUNNER_TEMP/Databank/developer/autocomplete_metadata.py" "$f"
5859
done
5960
6061
- name: Suggest changes on PR
6162
if: steps.files.outputs.changed_files != ''
62-
env:
63-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64-
PR_NUMBER: ${{ github.event.pull_request.number }}
65-
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
66-
run: |
67-
python3 << 'PYEOF'
68-
import subprocess, json, re, os, sys
69-
70-
repo = os.environ['GITHUB_REPOSITORY']
71-
pr_number = os.environ['PR_NUMBER']
72-
head_sha = os.environ['HEAD_SHA']
73-
74-
diff_output = subprocess.check_output(['git', 'diff', 'HEAD', '--unified=0'], text=True)
75-
print('=== git diff HEAD ===')
76-
print(diff_output or '(empty — autocomplete made no changes)')
77-
78-
def head_line(filepath, line_num):
79-
out = subprocess.check_output(['git', 'show', f'HEAD:{filepath}'], text=True)
80-
lines = out.splitlines(keepends=True)
81-
return lines[line_num - 1] if 0 < line_num <= len(lines) else ''
82-
83-
comments = []
84-
current_file = None
85-
current_old_start = 0
86-
minus_lines = []
87-
plus_lines = []
88-
89-
def flush_hunk():
90-
if current_file is None or (not minus_lines and not plus_lines):
91-
return
92-
if not minus_lines:
93-
# Pure addition after line current_old_start — anchor to that line
94-
anchor_num = max(current_old_start, 1)
95-
anchor = head_line(current_file, anchor_num)
96-
body = '```suggestion\n' + anchor + ''.join(plus_lines) + '```'
97-
comment = {'path': current_file, 'body': body, 'side': 'RIGHT', 'line': anchor_num}
98-
else:
99-
start_line = current_old_start
100-
end_line = current_old_start + len(minus_lines) - 1
101-
body = '```suggestion\n' + ''.join(plus_lines) + '```'
102-
comment = {'path': current_file, 'body': body, 'side': 'RIGHT', 'line': end_line}
103-
if start_line != end_line:
104-
comment['start_line'] = start_line
105-
comment['start_side'] = 'RIGHT'
106-
print(f' hunk -> {current_file}:{comment["line"]} ({len(minus_lines)} removed, {len(plus_lines)} added)')
107-
comments.append(comment)
108-
109-
for line in diff_output.splitlines(keepends=True):
110-
if line.startswith('diff --git'):
111-
flush_hunk(); minus_lines = []; plus_lines = []
112-
elif line.startswith('+++ '):
113-
current_file = line[6:].rstrip('\n')
114-
elif line.startswith('@@ '):
115-
flush_hunk(); minus_lines = []; plus_lines = []
116-
m = re.match(r'@@ -(\d+)(?:,\d+)? ', line)
117-
if m:
118-
current_old_start = int(m.group(1))
119-
elif line.startswith('-') and not line.startswith('---'):
120-
minus_lines.append(line[1:])
121-
elif line.startswith('+') and not line.startswith('+++'):
122-
plus_lines.append(line[1:])
123-
124-
flush_hunk()
125-
126-
print(f'Built {len(comments)} suggestion(s)')
127-
if not comments:
128-
print('No changes to suggest'); sys.exit(0)
129-
130-
review = {'commit_id': head_sha, 'body': '', 'event': 'COMMENT', 'comments': comments}
131-
result = subprocess.run(
132-
['gh', 'api', f'repos/{repo}/pulls/{pr_number}/reviews',
133-
'--method', 'POST', '--input', '-'],
134-
input=json.dumps(review), text=True, capture_output=True
135-
)
136-
print('API response:', result.stdout[:300])
137-
if result.stderr:
138-
print('API error:', result.stderr[:300], file=sys.stderr)
139-
if result.returncode != 0:
140-
sys.exit(1)
141-
print(f'Posted {len(comments)} inline suggestion(s)')
142-
PYEOF
63+
uses: reviewdog/action-suggester@v1
64+
with:
65+
tool_name: addmetadata
66+
github_token: ${{ secrets.GITHUB_TOKEN }}
67+
fail_level: any # surface errors instead of exiting 0
68+
reviewdog_flags: -tee # echo reviewdog's stdout/stderr to the log

0 commit comments

Comments
 (0)