1+ name : Add Project from Issue with PostImage
2+
3+ on :
4+ issues :
5+ types : [opened, edited]
6+
7+ jobs :
8+ add-project :
9+ if : contains(github.event.issue.title, '[ADD-PROJECT]')
10+ runs-on : ubuntu-latest
11+ steps :
12+ - name : Checkout code
13+ uses : actions/checkout@v2
14+
15+ - name : Parse project data and extract image URL
16+ id : parse
17+ run : |
18+ ISSUE_BODY="${{ github.event.issue.body }}"
19+ echo "$ISSUE_BODY" > issue_body.txt
20+
21+ # Parse fields from the issue body
22+ STUDENT_NAME=$(grep -oP 'Student Name\(s\): \K.*' issue_body.txt || echo "")
23+ PROJECT_NAME=$(grep -oP 'Project Name: \K.*' issue_body.txt || echo "")
24+ DESCRIPTION=$(grep -oP 'Description: \K.*' issue_body.txt || echo "")
25+ TAGS=$(grep -oP 'Tags: \K.*' issue_body.txt || echo "")
26+ GITHUB_LINK=$(grep -oP 'GitHub Link: \K.*' issue_body.txt || echo "")
27+ PROJECT_LINK=$(grep -oP 'Project Link: \K.*' issue_body.txt || echo "")
28+
29+ # Extract PostImage URL from the issue body
30+ # First try to find a specific PostImage URL
31+ IMAGE_URL=$(grep -oP 'Image URL: \K(https?://(?:i\.)?postimg\.cc/\S+)' issue_body.txt || echo "")
32+
33+ # If not found, look for any postimg.cc URL in the body
34+ if [ -z "$IMAGE_URL" ]; then
35+ IMAGE_URL=$(grep -oP 'https?://(?:i\.)?postimg\.cc/\S+' issue_body.txt | head -1 || echo "")
36+ fi
37+
38+ # If still not found, check for markdown image syntax with postimg.cc links
39+ if [ -z "$IMAGE_URL" ]; then
40+ IMAGE_URL=$(grep -oP '!\[.*?\]\((https?://(?:i\.)?postimg\.cc/\S+)\)' issue_body.txt | grep -oP 'https?://(?:i\.)?postimg\.cc/\S+' | head -1 || echo "")
41+ fi
42+
43+ # If no PostImage URL found, use a default placeholder
44+ if [ -z "$IMAGE_URL" ]; then
45+ IMAGE_URL="https://via.placeholder.com/300x200?text=No+Image"
46+ fi
47+
48+ # Ensure direct image URL format if it's a PostImage URL
49+ if [[ "$IMAGE_URL" == *postimg.cc* ]] && [[ "$IMAGE_URL" != *i.postimg.cc* ]]; then
50+ # Extract the image ID from the URL
51+ IMAGE_ID=$(echo "$IMAGE_URL" | grep -oP 'postimg\.cc/\K[^/]+' || echo "")
52+ if [ -n "$IMAGE_ID" ]; then
53+ IMAGE_URL="https://i.postimg.cc/$IMAGE_ID/image.jpg"
54+ fi
55+ fi
56+
57+ echo "IMAGE_URL=$IMAGE_URL" >> $GITHUB_ENV
58+
59+ # Parse tags into JSON array format
60+ TAGS_JSON="[$(echo "$TAGS" | sed 's/, */", "/g' | sed 's/^/"/' | sed 's/$/"/' )]"
61+
62+ # Create JSON object
63+ echo 'NEW_PROJECT_JSON<<EOF' >> $GITHUB_ENV
64+ echo "{" >> $GITHUB_ENV
65+ echo ' "studentName": "'"$STUDENT_NAME"'",' >> $GITHUB_ENV
66+ echo ' "projectName": "'"$PROJECT_NAME"'",' >> $GITHUB_ENV
67+ echo ' "description": "'"$DESCRIPTION"'",' >> $GITHUB_ENV
68+ echo ' "tags": '"$TAGS_JSON"',' >> $GITHUB_ENV
69+ echo ' "githubLink": "'"$GITHUB_LINK"'",' >> $GITHUB_ENV
70+ echo ' "projectLink": "'"$PROJECT_LINK"'",' >> $GITHUB_ENV
71+ echo ' "image": "'"$IMAGE_URL"'"' >> $GITHUB_ENV
72+ echo "}" >> $GITHUB_ENV
73+ echo 'EOF' >> $GITHUB_ENV
74+
75+ - name : Update data.json
76+ run : |
77+ # Read current data.json
78+ CURRENT_DATA=$(cat data.json)
79+
80+ # Get highest existing ID
81+ MAX_ID=$(echo $CURRENT_DATA | jq 'map(.id) | max')
82+ NEW_ID=$((MAX_ID + 1))
83+
84+ # Add new project with new ID
85+ NEW_PROJECT_WITH_ID=$(echo "$NEW_PROJECT_JSON" | jq --arg id "$NEW_ID" '. + {id: $id|tonumber}')
86+
87+ # Add new project to array
88+ UPDATED_DATA=$(echo $CURRENT_DATA | jq --argjson new "$NEW_PROJECT_WITH_ID" '. + [$new]')
89+
90+ # Write updated data back to file
91+ echo $UPDATED_DATA | jq . > data.json
92+
93+ - name : Commit and push changes
94+ run : |
95+ git config --global user.name 'GitHub Actions Bot'
96+ git config --global user.email 'actions@github.com'
97+ git add data.json
98+ git commit -m "Add new project from issue #${{ github.event.issue.number }} with PostImage"
99+ git push
100+
101+ - name : Comment on issue
102+ uses : actions/github-script@v5
103+ with :
104+ github-token : ${{ secrets.GITHUB_TOKEN }}
105+ script : |
106+ github.rest.issues.createComment({
107+ issue_number: context.issue.number,
108+ owner: context.repo.owner,
109+ repo: context.repo.repo,
110+ body: 'Project added successfully to data.json with image URL: `${{ env.IMAGE_URL }}`! The changes have been committed.'
111+ })
112+
113+ github.rest.issues.update({
114+ issue_number: context.issue.number,
115+ owner: context.repo.owner,
116+ repo: context.repo.repo,
117+ state: 'closed'
118+ })
0 commit comments