Create GitHub Discussions #70
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 | |
repository-projects: read | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
ref: main # Explicitly checkout main branch | |
fetch-depth: 0 # Fetch all history | |
- name: Get App Dataf | |
id: app-data | |
run: | | |
# Debug git status | |
git status | |
echo "Current SHA: $(git rev-parse HEAD)" | |
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 | |
# Get the files changed in this PR | |
CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep '^src/content/apps/.*\.md$' || true) | |
echo "Found changed files:" | |
echo "$CHANGED_FILES" | |
if [ -z "$CHANGED_FILES" ]; then | |
echo "No relevant files changed" | |
exit 1 | |
fi | |
# Use the first changed file | |
CHANGED_FILE=$(echo "$CHANGED_FILES" | head -n 1) | |
echo "Processing file: $CHANGED_FILE" | |
echo "File contents:" | |
cat "$CHANGED_FILE" | |
# Extract metadata using yq or manual parsing | |
NAME=$(grep '^name: ' "$CHANGED_FILE" | cut -d'"' -f2) | |
AUTHOR=$(grep '^author: ' "$CHANGED_FILE" | cut -d'"' -f2) | |
# Extract description - capture everything between the quotes | |
DESCRIPTION=$(sed -n '/^description: /,/dateLaunched:/p' "$CHANGED_FILE" | \ | |
sed '1s/^description: "//;$s/dateLaunched.*//;s/"$//' | \ | |
tr '\n' ' ' | sed 's/ */ /g') | |
echo "---Extracted Name: $NAME" | |
echo "Extracted Author: $AUTHOR" | |
echo "Extracted Description: $DESCRIPTION" | |
# 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 | |
# 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 1 | |
fi | |
# Set outputs | |
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 2024: ${{ 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: R_kgDONX26mg | |
category-id: DIC_kwDONX26ms4Ck8X2 | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |