Skip to content

Improve SMILEidx for PUFA molecules #95

Improve SMILEidx for PUFA molecules

Improve SMILEidx for PUFA molecules #95

name: AutocompleteMetadata
on:
pull_request_target:
paths:
- 'Molecules/membrane/**/metadata.yaml'
- 'Molecules/solution/**/metadata.yaml'
permissions:
contents: read
pull-requests: write
jobs:
add-metadata:
runs-on: ubuntu-latest
steps:
- name: Checkout BilayerData
uses: actions/checkout@v6
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
persist-credentials: false
- name: Clone Databank and install
run: |
git clone --depth 1 https://github.com/NMRlipids/FAIRMD_lipids.git "$RUNNER_TEMP/Databank"
pip install "$RUNNER_TEMP/Databank"
- name: Install the gh cli
uses: ksivamuthu/actions-setup-gh-cli@v3
with:
version: 2.83.0
- name: Find changed metadata.yaml files
id: files
env:
GH_TOKEN: ${{ github.token }}
run: |
changed_files=$(gh api --paginate \
"repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" \
--jq '.[] | select(.status != "removed") | .filename' \
| grep -E '^Molecules/(membrane|solution)/[^/]+/metadata\.yaml$' || true)
{
echo "changed_files<<EOF"
echo "$changed_files"
echo "EOF"
} >> "$GITHUB_OUTPUT"
echo "Changed files:"
echo "$changed_files"
- name: Run AddMetadata.py and create suggestions
if: steps.files.outputs.changed_files != ''
env:
CHANGED_FILES: ${{ steps.files.outputs.changed_files }}
run: |
printf '%s\n' "$CHANGED_FILES" | while IFS= read -r f; do
[ -n "$f" ] && python "$RUNNER_TEMP/Databank/developer/autocomplete_metadata.py" "$f"
done
- name: Suggest changes on PR
if: steps.files.outputs.changed_files != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
python3 << 'PYEOF'
import subprocess, json, re, os, sys
repo = os.environ['GITHUB_REPOSITORY']
pr_number = os.environ['PR_NUMBER']
head_sha = os.environ['HEAD_SHA']
diff_output = subprocess.check_output(['git', 'diff', 'HEAD', '--unified=0'], text=True)
print('=== git diff HEAD ===')
print(diff_output or '(empty — autocomplete made no changes)')
def head_line(filepath, line_num):
out = subprocess.check_output(['git', 'show', f'HEAD:{filepath}'], text=True)
lines = out.splitlines(keepends=True)
return lines[line_num - 1] if 0 < line_num <= len(lines) else ''
comments = []
current_file = None
current_old_start = 0
minus_lines = []
plus_lines = []
def flush_hunk():
if current_file is None or (not minus_lines and not plus_lines):
return
if not minus_lines:
# Pure addition after line current_old_start — anchor to that line
anchor_num = max(current_old_start, 1)
anchor = head_line(current_file, anchor_num)
body = '```suggestion\n' + anchor + ''.join(plus_lines) + '```'
comment = {'path': current_file, 'body': body, 'side': 'RIGHT', 'line': anchor_num}
else:
start_line = current_old_start
end_line = current_old_start + len(minus_lines) - 1
body = '```suggestion\n' + ''.join(plus_lines) + '```'
comment = {'path': current_file, 'body': body, 'side': 'RIGHT', 'line': end_line}
if start_line != end_line:
comment['start_line'] = start_line
comment['start_side'] = 'RIGHT'
print(f' hunk -> {current_file}:{comment["line"]} ({len(minus_lines)} removed, {len(plus_lines)} added)')
comments.append(comment)
for line in diff_output.splitlines(keepends=True):
if line.startswith('diff --git'):
flush_hunk(); minus_lines = []; plus_lines = []
elif line.startswith('+++ '):
current_file = line[6:].rstrip('\n')
elif line.startswith('@@ '):
flush_hunk(); minus_lines = []; plus_lines = []
m = re.match(r'@@ -(\d+)(?:,\d+)? ', line)
if m:
current_old_start = int(m.group(1))
elif line.startswith('-') and not line.startswith('---'):
minus_lines.append(line[1:])
elif line.startswith('+') and not line.startswith('+++'):
plus_lines.append(line[1:])
flush_hunk()
print(f'Built {len(comments)} suggestion(s)')
if not comments:
print('No changes to suggest'); sys.exit(0)
review = {'commit_id': head_sha, 'body': '', 'event': 'COMMENT', 'comments': comments}
result = subprocess.run(
['gh', 'api', f'repos/{repo}/pulls/{pr_number}/reviews',
'--method', 'POST', '--input', '-'],
input=json.dumps(review), text=True, capture_output=True
)
print('API response:', result.stdout[:300])
if result.stderr:
print('API error:', result.stderr[:300], file=sys.stderr)
if result.returncode != 0:
sys.exit(1)
print(f'Posted {len(comments)} inline suggestion(s)')
PYEOF