|
24 | 24 | workflow_dispatch: # This allows manual triggering of the workflow
|
25 | 25 |
|
26 | 26 | permissions:
|
27 |
| - pull-requests: write |
28 | 27 | discussions: write
|
29 |
| - contents: write |
| 28 | + contents: read |
30 | 29 |
|
31 | 30 | jobs:
|
32 | 31 | generate-and-publish:
|
@@ -80,14 +79,64 @@ jobs:
|
80 | 79 | echo "TITLE_DATE=$TITLE_DATE" >> $GITHUB_ENV
|
81 | 80 |
|
82 | 81 | - name: Create GitHub Discussion
|
83 |
| - run: | |
84 |
| - gh api \ |
85 |
| - --method POST \ |
86 |
| - -H "Accept: application/vnd.github+json" \ |
87 |
| - -H "X-GitHub-Api-Version: 2022-11-28" \ |
88 |
| - /repos/${{ github.repository }}/discussions \ |
89 |
| - -f title="${{ env.TITLE_DATE }}: This week in OpenDAL" \ |
90 |
| - -f body="$(cat .github/scripts/weekly_update/weekly_update.md)" \ |
91 |
| - -f category_id="$(gh api /repos/${{ github.repository }}/discussions/categories | jq '.[] | select(.name=="General") | .id')" |
92 |
| - env: |
93 |
| - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 82 | + uses: actions/github-script@v7 |
| 83 | + with: |
| 84 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + script: | |
| 86 | + const fs = require('fs'); |
| 87 | +
|
| 88 | + // Read the weekly update content directly from file |
| 89 | + const weeklyUpdateContent = fs.readFileSync('.github/scripts/weekly_update/weekly_update.md', 'utf8'); |
| 90 | + const discussionTitle = `${process.env.TITLE_DATE}: This week in OpenDAL`; |
| 91 | +
|
| 92 | + // Get repository node ID using GraphQL |
| 93 | + const repoQuery = ` |
| 94 | + query { |
| 95 | + repository(owner: "${context.repo.owner}", name: "${context.repo.repo}") { |
| 96 | + id |
| 97 | + discussionCategories(first: 10) { |
| 98 | + nodes { |
| 99 | + id |
| 100 | + name |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + `; |
| 106 | +
|
| 107 | + const repoResult = await github.graphql(repoQuery); |
| 108 | + const repositoryId = repoResult.repository.id; |
| 109 | +
|
| 110 | + // Find the "General" category |
| 111 | + const categories = repoResult.repository.discussionCategories.nodes; |
| 112 | + const generalCategory = categories.find(category => category.name === 'General'); |
| 113 | +
|
| 114 | + if (!generalCategory) { |
| 115 | + core.setFailed("Could not find 'General' discussion category. Make sure Discussions are enabled for this repository."); |
| 116 | + return; |
| 117 | + } |
| 118 | +
|
| 119 | + // Create the discussion using GraphQL API |
| 120 | + const createDiscussionMutation = ` |
| 121 | + mutation { |
| 122 | + createDiscussion(input: { |
| 123 | + repositoryId: "${repositoryId}", |
| 124 | + categoryId: "${generalCategory.id}", |
| 125 | + body: ${JSON.stringify(weeklyUpdateContent)}, |
| 126 | + title: ${JSON.stringify(discussionTitle)} |
| 127 | + }) { |
| 128 | + discussion { |
| 129 | + id |
| 130 | + url |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + `; |
| 135 | +
|
| 136 | + try { |
| 137 | + const result = await github.graphql(createDiscussionMutation); |
| 138 | + console.log(`Successfully created discussion: ${result.createDiscussion.discussion.url}`); |
| 139 | + } catch (error) { |
| 140 | + console.error('Error creating discussion:', error.message); |
| 141 | + core.setFailed(`Error creating discussion: ${error.message}`); |
| 142 | + } |
0 commit comments