-
Notifications
You must be signed in to change notification settings - Fork 11
166 lines (143 loc) · 5.68 KB
/
add-playlist-workflow.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
154
155
156
157
158
159
160
161
162
163
164
165
166
name: Process Contribution New
on:
issues:
types: [opened, edited]
permissions:
contents: write
pull-requests: write
issues: write
jobs:
process-contribution:
if: contains(github.event.issue.labels.*.name, 'playlist-submission')
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: |
sudo apt update && sudo apt install jq curl -y
python -m pip install --upgrade pip
pip install jsonschema requests
- name: Validate JSON and process contribution
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_CONTEXT: ${{ toJson(github) }}
EVENT_ID: ${{ github.event.issue.id }}
ISSUE_BODY: ${{ github.event.issue.body }}
run: |
echo "eventid: $EVENT_ID, Github context: cool"
BODY="$ISSUE_BODY"
echo "body ===> $BODY"
PLAYLIST_TITLE=$(echo "$BODY" | grep "### Playlist Title" -A 2 | tail -n 1)
PLAYLIST_URL=$(echo "$BODY" | grep "### Playlist URL" -A 2 | tail -n 1)
PLAYLIST_SUMMARY=$(echo "$BODY" | grep "### Playlist Summary" -A 2 | tail -n 1)
PLAYLIST_CATEGORY=$(echo "$BODY" | grep "Category" -A 2 | tail -n 1)
CREATOR="${{ github.event.issue.user.login }}"
echo """
{
\"name\": \"$CREATOR\",
\"playlist_link\": \"$PLAYLIST_URL\",
\"summary\": \"$PLAYLIST_SUMMARY\",
\"title\": \"$PLAYLIST_TITLE\",
\"category\": \"$PLAYLIST_CATEGORY\",
\"user_profile_link\": \"https://github.com/$CREATOR\",
\"user_image\": \"https://avatars.githubusercontent.com/$CREATOR\"
}
""" | tee temp_playlist.json
# # Set outputs
# echo "creator=$CREATOR" >> $GITHUB_OUTPUT
# echo "playlist_title=$PLAYLIST_TITLE" >> $GITHUB_OUTPUT
# # Extract JSON from issue body
# Run the verify_playlist.py script
# Verify the playlist
python .github/scripts/verify_playlist.py temp_playlist.json
if [[ $? -ne 0 ]]; then
echo "Playlist verification failed"
exit 1
fi
echo "Playlist verification passed"
# Append to playlist.json
if [[ -f "playlist.json" ]]; then
json_data=$(<temp_playlist.json)
jq ". += [$json_data]" playlist.json > playlist_temp.json && mv playlist_temp.json playlist.json
else
echo "playlist.json not found!"
exit 1
fi
# Create a new branch
issue_number=$EVENT_ID
echo "eventid====> $EVENT_ID"
branch_name="contribution-$issue_number"
git checkout -b "$branch_name"
git add playlist.json
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git commit -m "Add contribution from issue #$issue_number"
git push origin "$branch_name"
# Create a pull request
pr_url="https://api.github.com/repos/${{github.repository}}/pulls"
pr_data=$(jq -n --arg title "Contribution from issue #$issue_number" \
--arg body "This PR adds the contribution from issue #$issue_number" \
--arg head "$branch_name" \
--arg base "main" \
'{title: $title, body: $body, head: $head, base: $base}')
response=$(curl -s -w "%{http_code}" -o response.json -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d "$pr_data" \
"$pr_url")
http_code=${response:(-3)}
if [[ $http_code -eq 201 ]]; then
pr_link=$(jq -r '.html_url' response.json)
echo "Pull request created successfully: $pr_link"
else
echo "Failed to create pull request"
jq '.' response.json
exit 1
fi
shell: sh
- 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: Add comment
if: always()
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'There was an error processing your contribution. Please check that your JSON is correctly formatted and includes all required fields.'
});
} catch (error) {
core.setFailed('Failed to post comment');
}
- 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.'
})