Discussion workflow #7
Workflow file for this run
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 Discussion for New YAML Configs | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened] | |
push: | |
branches: | |
- staging | |
- discussion_workflow | |
paths: | |
- 'cfg/**/*.yaml' | |
jobs: | |
create-discussion: | |
runs-on: ubuntu-latest | |
permissions: | |
discussions: write | |
contents: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.x' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install PyYAML | |
- name: Install GitHub CLI | |
run: | | |
(type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \ | |
&& sudo mkdir -p -m 755 /etc/apt/keyrings \ | |
&& out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \ | |
&& cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \ | |
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \ | |
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ | |
&& sudo apt update \ | |
&& sudo apt install gh -y | |
- name: Setup tmate session | |
uses: mxschmitt/action-tmate@v3 | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
- name: Process new YAML files | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
run: | | |
# Compare against main branch to find new or modified YAML files | |
git diff origin/main --name-only | grep "^cfg/.*\.yaml$" | while read file; do | |
# Get folder and filename | |
folder=$(dirname "$file" | xargs basename) | |
filename=$(basename "$file") | |
discussion_title="$folder/$filename" | |
# Check if discussion already exists | |
existing_discussion=$(gh api graphql -f query=' | |
query($owner:String!, $repo:String!, $search:String!) { | |
repository(owner:$owner, name:$repo) { | |
discussions(first:1, query:$search) { | |
nodes { | |
title | |
} | |
} | |
} | |
}' -f owner=$GITHUB_REPOSITORY_OWNER -f repo=$(basename $GITHUB_REPOSITORY) -f search="$discussion_title" --jq '.data.repository.discussions.nodes[0].title') | |
if [ -z "$existing_discussion" ]; then | |
# Parse YAML and extract metadata using external script | |
python scripts/parse_metadata.py "$file" | |
# Create discussion and capture the URL | |
discussion_url=$(gh discussion create \ | |
--title "$discussion_title" \ | |
--body-file discussion_body.txt \ | |
--category "General" \ | |
--json url \ | |
--jq '.url') | |
# Update YAML with discussion link using external script | |
python scripts/update_yaml.py "$file" "$discussion_url" | |
# Commit and push changes | |
git config --global user.name "GitHub Action" | |
git config --global user.email "action@github.com" | |
git add "$file" | |
git commit -m "Add discussion link to $discussion_title" | |
git push | |
else | |
echo "Discussion '$discussion_title' already exists. Skipping." | |
fi | |
done |