-
Notifications
You must be signed in to change notification settings - Fork 1
165 lines (142 loc) · 5.98 KB
/
discussion.yaml
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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@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: 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.base.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