-
-
Notifications
You must be signed in to change notification settings - Fork 55
119 lines (104 loc) · 4.38 KB
/
create-discussions.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
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 app name and check if it exists in votes file
NAME=$(grep -m 1 '^name: ' "$CHANGED_FILE" | sed -E 's/^name: "(.+)"$/\1/')
# 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
# Continue with existing code for new apps only...
AUTHOR=$(grep -m 1 '^author: ' "$CHANGED_FILE" | sed -E 's/^author: "(.+)"$/\1/')
DESCRIPTION=$(grep -m 1 '^description: ' "$CHANGED_FILE" | sed -E 's/^description: "(.+)"$/\1/')
# 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 }}