Add Swin-T OBB config (#35) #24
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: | |
push: | |
branches: | |
- staging | |
paths: | |
- 'cfg/**/*.yaml' | |
jobs: | |
create-discussion: | |
runs-on: ubuntu-latest | |
permissions: | |
discussions: write | |
contents: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
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: Determine default branch | |
id: get_default_branch | |
run: | | |
echo "DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')" >> $GITHUB_ENV | |
- name: Fetch default branch | |
run: git fetch origin ${{ env.DEFAULT_BRANCH }} | |
- name: Determine target branch | |
id: determine_branch | |
run: | | |
if [ "${{ github.event_name }}" = "pull_request" ]; then | |
echo "TARGET_BRANCH=${{ github.event.pull_request.head.ref }}" >> $GITHUB_ENV | |
else | |
echo "TARGET_BRANCH=${{ github.ref }}" | sed 's|refs/heads/||' >> $GITHUB_ENV | |
fi | |
- name: Process new YAML files | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
TARGET_BRANCH: ${{ env.TARGET_BRANCH }} | |
run: | | |
set -e | |
# Compare against default branch to find new or modified YAML files | |
git diff origin/${DEFAULT_BRANCH} --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" | |
echo "Processing file: $file with title: $discussion_title" | |
# Construct the search query | |
search_query="repo:${GITHUB_REPOSITORY} in:title \"$discussion_title\"" | |
# Check if discussion already exists using the search API | |
existing_discussion=$(gh api graphql -f query=' | |
query($searchQuery:String!) { | |
search(query:$searchQuery, type: DISCUSSION, first:1) { | |
nodes { | |
... on Discussion { | |
title | |
} | |
} | |
} | |
}' \ | |
-f searchQuery="$search_query" \ | |
--jq '.data.search.nodes[0].title') | |
if [ -z "$existing_discussion" ]; then | |
# Parse YAML and extract metadata using external script | |
python scripts/parse_metadata.py "$file" | |
# Fetch the repository ID (existing code) | |
repository_id=$(gh api graphql -f query=' | |
query($repo: String!, $owner: String!) { | |
repository(name: $repo, owner: $owner) { | |
id | |
} | |
}' \ | |
-f repo="$(basename "$GITHUB_REPOSITORY")" \ | |
-f owner="$GITHUB_REPOSITORY_OWNER" \ | |
--jq '.data.repository.id') | |
# Check if repository ID was retrieved successfully | |
if [ -z "$repository_id" ]; then | |
echo "Error: Unable to retrieve repository ID." | |
exit 1 | |
fi | |
# Fetch the category ID for "General" | |
category_id=$(gh api graphql -f query=' | |
query($repo: String!, $owner: String!) { | |
repository(name: $repo, owner: $owner) { | |
discussionCategories(first: 100) { | |
nodes { | |
id | |
name | |
} | |
} | |
} | |
}' \ | |
-f repo="$(basename "$GITHUB_REPOSITORY")" \ | |
-f owner="$GITHUB_REPOSITORY_OWNER" \ | |
--jq '.data.repository.discussionCategories.nodes[] | select(.name=="General") | .id') | |
# Check if category ID was retrieved successfully | |
if [ -z "$category_id" ]; then | |
echo "Error: Unable to find the 'General' discussion category." | |
exit 1 | |
fi | |
# Create discussion using GraphQL and capture the URL | |
create_response=$(gh api graphql -f query=' | |
mutation($repositoryId: ID!, $categoryId: ID!, $title: String!, $body: String!) { | |
createDiscussion(input: {repositoryId: $repositoryId, categoryId: $categoryId, title: $title, body: $body}) { | |
discussion { | |
url | |
} | |
} | |
}' \ | |
-f repositoryId="$repository_id" \ | |
-f categoryId="$category_id" \ | |
-f title="$discussion_title" \ | |
-f body="$(cat discussion_body.txt)") | |
# Extract the discussion URL from the response | |
discussion_url=$(echo "$create_response" | jq -r '.data.createDiscussion.discussion.url') | |
# Check if discussion was created successfully | |
if [ -z "$discussion_url" ] || [ "$discussion_url" == "null" ]; then | |
echo "Error: Failed to create discussion." | |
exit 1 | |
fi | |
# 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 origin HEAD:"$TARGET_BRANCH" | |
# Clean up temporary files | |
rm discussion_body.txt | |
else | |
echo "Discussion '$discussion_title' already exists. Skipping." | |
fi | |
done |