Create GitHub Discussions #34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create GitHub Discussions | |
on: | |
pull_request: | |
types: [closed] | |
branches: | |
- main | |
paths: | |
- "src/content/apps/**" | |
workflow_dispatch: | |
inputs: | |
name: | |
description: "App Name" | |
required: true | |
type: string | |
author: | |
description: "App Author" | |
required: true | |
type: string | |
description: | |
description: "App Description" | |
required: true | |
type: string | |
jobs: | |
create-discussion: | |
if: > | |
github.event_name == 'workflow_dispatch' || | |
(github.event_name == 'pull_request' && | |
github.event.pull_request.merged == true) | |
runs-on: ubuntu-latest | |
permissions: | |
discussions: write | |
pull-requests: read | |
contents: read | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 # Fetch at least 2 commits to have history | |
- name: Get App Data | |
id: app-data | |
run: | | |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then | |
# For manual triggers, use the input parameters | |
echo "name=${{ inputs.name }}" >> $GITHUB_OUTPUT | |
echo "author=${{ inputs.author }}" >> $GITHUB_OUTPUT | |
echo "description=${{ inputs.description }}" >> $GITHUB_OUTPUT | |
else | |
# Debug information | |
echo "Base SHA: ${{ github.event.pull_request.base.sha }}" | |
echo "Head SHA: ${{ github.event.pull_request.head.sha }}" | |
echo "Changed files:" | |
git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | |
# For push events, get the changed files | |
CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep '^src/content/apps/.*\.md$' || true) | |
# Debug the result | |
echo "Matching MD files:" | |
echo "$CHANGED_FILES" | |
# Check if we found any changed files | |
if [ -z "$CHANGED_FILES" ]; then | |
echo "No relevant files changed" | |
exit 0 | |
fi | |
# Use the first changed file | |
CHANGED_FILE=$(echo "$CHANGED_FILES" | head -n 1) | |
# Extract metadata using more robust patterns | |
NAME=$(awk '/^name: / {gsub(/"/, "", $2); print $2}' "$CHANGED_FILE") | |
AUTHOR=$(awk '/^author: / {gsub(/"/, "", $2); print $2}' "$CHANGED_FILE") | |
# Extract description - handling multi-line content between description: and dateLaunched: | |
DESCRIPTION=$(awk '/^description: /{p=1;next} /^dateLaunched:/{p=0} p' "$CHANGED_FILE" | \ | |
sed 's/^"//;s/"$//;s/^[[:space:]]*//') | |
# Debug output | |
echo "Extracted Name: $NAME" | |
echo "Extracted Author: $AUTHOR" | |
echo "Extracted Description: $DESCRIPTION" | |
# Check if app already has a discussion by looking in votes file | |
if grep -q "\"$NAME\":" src/data/votes_*.json; then | |
echo "Discussion already exists for $NAME" | |
exit 0 | |
fi | |
# Verify we got all required data | |
if [ -z "$NAME" ] || [ -z "$AUTHOR" ] || [ -z "$DESCRIPTION" ]; then | |
echo "Failed to extract required metadata from $CHANGED_FILE" | |
exit 1 | |
fi | |
# Properly escape the values for GitHub Actions output | |
NAME="${NAME//'%'/'%25'}" | |
NAME="${NAME//$'\n'/'%0A'}" | |
NAME="${NAME//$'\r'/'%0D'}" | |
AUTHOR="${AUTHOR//'%'/'%25'}" | |
AUTHOR="${AUTHOR//$'\n'/'%0A'}" | |
AUTHOR="${AUTHOR//$'\r'/'%0D'}" | |
DESCRIPTION="${DESCRIPTION//'%'/'%25'}" | |
DESCRIPTION="${DESCRIPTION//$'\n'/'%0A'}" | |
DESCRIPTION="${DESCRIPTION//$'\r'/'%0D'}" | |
echo "name=$NAME" >> $GITHUB_OUTPUT | |
echo "author=$AUTHOR" >> $GITHUB_OUTPUT | |
echo "description=$DESCRIPTION" >> $GITHUB_OUTPUT | |
fi | |
echo "year=$(date +%Y)" >> $GITHUB_OUTPUT | |
- name: Create Discussion | |
uses: abirismyname/[email protected] | |
with: | |
title: "Vote ${{ steps.app-data.outputs.year }}: ${{ steps.app-data.outputs.name }} by ${{ steps.app-data.outputs.author }}" | |
body: | | |
**Vote for this app by giving it an upvote!** | |
${{ steps.app-data.outputs.description }} | |
repository-id: ${{ secrets.REPO_ID }} | |
category-id: ${{ secrets.CAT_ID }} | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |