Skip to content

Create post from an issue #38

Create post from an issue

Create post from an issue #38

Workflow file for this run

# Infinite love to @javierarce for sharing this code on his site. I stole it immediately.
name: Create post from an issue
on:
issue_comment:
types: [created, edited]
jobs:
build:
name: Create post from github issue
runs-on: ubuntu-latest
permissions:
issues: write
contents: write
if: github.event.comment.body == 'publish'
&& contains(github.event.issue.labels.*.name, 'post')
&& github.event.issue.user.login == 'bomberstudios'
steps:
- name: Checkout main branch
uses: actions/checkout@master
- name: Create astro post file
if: success()
run: |
DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
POST_TITLE=$(sed -E 's/[:space:]/-/g ; s/[:punct:]//g ; s/[A-Z]/\L&/g ; s/#//g' <<<'${{ github.event.issue.title }}')
# Create assets directory if it doesn't exist
mkdir -p public/assets/posts/"${DATE:0:10}-${POST_TITLE}"
# Process the body to download assets and update URLs
BODY='${{ github.event.issue.body }}'
# Download images and videos from GitHub user content
while IFS= read -r url; do
if [[ $url =~ ^https://github\.com/user-attachments/assets/ ]]; then
FILENAME=$(basename "$url")
curl -L "$url" -o "public/assets/posts/${DATE:0:10}-${POST_TITLE}/$FILENAME"
# Update URL in body to point to local asset
BODY=${BODY//$url/\/assets\/posts\/${DATE:0:10}-${POST_TITLE}\/$FILENAME}
fi
done < <(echo "$BODY" | grep -o 'https://github.com/user-attachments/assets/[^ )}"]*')
# Create post file with updated body
cat <<EOF > $POST_DIRECTORY/"${POST_TITLE}".md
---
title: "${{ github.event.issue.title }}"
date: "${{ github.event.issue.created_at }}"
---
$BODY
EOF
env:
POST_DIRECTORY: 'src/content/blog'
- name: Commit files
if: success()
run: |
git config --local user.name "$GIT_USER_NAME"
git config --local user.email "$GIT_USER_EMAIL"
git add --all
if [[ `git status --porcelain` ]]; then
git commit -m "$DEFAULT_COMMIT_MESSAGE" -a
fi
env:
GIT_USER_NAME: 'Ale Muñoz'
GIT_USER_EMAIL: 'bomberstudios@gmail.com'
DEFAULT_COMMIT_MESSAGE: 'New post created from GitHub issue'
- name: Push changes
if: success()
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: master
- name: Label issue
uses: actions/github-script@v7
with:
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ["published"]
})
- name: Add comment
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "Post published, closing!"
})
- name: Close issue
uses: actions/github-script@v7
with:
script: |
github.rest.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: "closed"
})