-
Notifications
You must be signed in to change notification settings - Fork 11
153 lines (128 loc) · 5.11 KB
/
process-contribution.yml
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
143
144
145
146
147
148
149
150
151
152
153
name: Process Contribution
on:
issues:
types: [opened, edited]
permissions:
contents: write
pull-requests: write
issues: write
jobs:
process-contribution:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install jsonschema requests
- name: Validate JSON and process contribution
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
import json
import os
import requests
import sys
import subprocess
def get_issue_data(issue_number):
url = f"https://api.github.com/repos/${{github.repository}}/issues/{issue_number}"
headers = {
"Authorization": f"token {os.environ['GITHUB_TOKEN']}",
"Accept": "application/vnd.github.v3+json"
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(f"Failed to fetch issue data: {response.text}")
sys.exit(1)
return response.json()
# Get the issue number from the event payload
with open(os.environ['GITHUB_EVENT_PATH'], 'r') as f:
event_data = json.load(f)
issue_number = event_data['issue']['number']
# Get the issue data
issue_data = get_issue_data(issue_number)
issue_body = issue_data.get('body', '')
if not issue_body:
print("Issue body is empty")
sys.exit(1)
# Extract JSON from issue body
try:
start = issue_body.index('{')
end = issue_body.rindex('}') + 1
json_str = issue_body[start:end]
json_data = json.loads(json_str)
except (ValueError, json.JSONDecodeError) as e:
print(f"Failed to extract JSON from issue body: {e}")
sys.exit(1)
# Write the extracted JSON to a temporary file
with open('temp_playlist.json', 'w') as f:
json.dump(json_data, f)
# Run the verify_playlist.py script
result = subprocess.run(['python', '.github/scripts/verify_playlist.py', 'temp_playlist.json'], capture_output=True, text=True)
if result.returncode != 0:
print(f"Playlist verification failed: {result.stdout}\n{result.stderr}")
sys.exit(1)
print("Playlist verification passed")
# If validation passes, append to playlist.json
with open('playlist.json', 'r+') as f:
playlists = json.load(f)
playlists.append(json_data)
f.seek(0)
json.dump(playlists, f, indent=2)
# Create a new branch
branch_name = f"contribution-{issue_number}"
os.system(f"git checkout -b {branch_name}")
os.system("git add playlist.json")
os.system('git config user.name "github-actions[bot]"')
os.system('git config user.email "github-actions[bot]@users.noreply.github.com"')
os.system(f'git commit -m "Add contribution from issue #{issue_number}"')
os.system(f"git push origin {branch_name}")
# Create a pull request
pr_url = f"https://api.github.com/repos/${{github.repository}}/pulls"
pr_data = {
"title": f"Contribution from issue #{issue_number}",
"body": f"This PR adds the contribution from issue #{issue_number}",
"head": branch_name,
"base": "main"
}
headers = {
"Authorization": f"token {os.environ['GITHUB_TOKEN']}",
"Accept": "application/vnd.github.v3+json"
}
response = requests.post(pr_url, json=pr_data, headers=headers)
if response.status_code == 201:
print(f"Pull request created successfully: {response.json()['html_url']}")
else:
print(f"Failed to create pull request: {response.text}")
sys.exit(1)
shell: python
- name: Comment on issue
if: success()
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
await github.rest.issues.createComment({
...context.repo,
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.name,
body: 'Thank you for your contribution! A pull request has been created with your changes. The repository owner will review and merge it soon.'
});
- name: Comment on issue if failed
if: failure()
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.name,
body: 'There was an error processing your contribution. Please check that your JSON is correctly formatted and includes all required fields.'
})