-
Notifications
You must be signed in to change notification settings - Fork 11
150 lines (129 loc) · 5.23 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
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.number }}
ISSUE_BODY: ${{ github.event.issue.body }}
run: |
BODY="$ISSUE_BODY"
PLAYLIST_TITLE=$(echo "$BODY" | grep "### Playlist Title" -A 2 | tail -n 1)
PLAYLIST_URL=$(echo "$BODY" | grep "### YouTube Playlist URL" -A 2 | tail -n 1)
PLAYLIST_SUMMARY=$(echo "$BODY" | sed -n '/### Playlist Summary/,/### Category/{//!p}' | sed '$d')
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
# 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
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 "$(printf "### Description\n\nThis PR adds the contribution from issue #$issue_number by @%s.\n\n#### Playlist Details:\n\`\`\`json\n%s\n\`\`\`" "$CREATOR" "$(cat temp_playlist.json)")" \
--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: bash
- name: Comment on issue
if: success()
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: 'Thank you for your contribution! A pull request has been created with your changes. The repository owner will review and merge it soon.'
});
} 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: |
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');
}