-
Notifications
You must be signed in to change notification settings - Fork 15
142 lines (125 loc) · 5.62 KB
/
Copy pathAutocompleteMetadata.yml
File metadata and controls
142 lines (125 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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