Add new discussions to project #34
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: Add new discussions to project | |
on: | |
discussion: | |
types: [created, reopened] | |
jobs: | |
add-to-project: | |
name: Add discussion to project | |
runs-on: ubuntu-latest | |
steps: | |
- name: Generate token | |
id: generate_token | |
uses: tibdex/github-app-token@v2 | |
with: | |
app_id: ${{ secrets.APP_ID }} | |
private_key: ${{ secrets.APP_PRIVATE_KEY }} | |
- name: Debug Info | |
run: | | |
echo "Event: ${{ github.event_name }}" | |
echo "Action: ${{ github.event.action }}" | |
echo "Discussion ID: ${{ github.event.discussion.node_id }}" | |
echo "Discussion Number: ${{ github.event.discussion.number }}" | |
echo "Organization: ${{ github.repository_owner }}" | |
- name: Add to project | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ steps.generate_token.outputs.token }} | |
script: | | |
const projectId = 'PVT_kwDOCAGLAM4Anzfw'; | |
const discussionId = context.payload.discussion.node_id; | |
const orgName = context.payload.organization.login; | |
const repoName = context.payload.repository.name; | |
console.log(`Adding discussion ${discussionId} to project ${projectId}`); | |
console.log(`Organization: ${orgName}, Repository: ${repoName}`); | |
try { | |
// First, verify the discussion exists | |
const { repository } = await github.graphql(` | |
query($owner: String!, $name: String!, $number: Int!) { | |
repository(owner: $owner, name: $name) { | |
discussion(number: $number) { | |
id | |
} | |
} | |
} | |
`, { | |
owner: orgName, | |
name: repoName, | |
number: context.payload.discussion.number | |
}); | |
if (!repository.discussion) { | |
throw new Error(`Discussion not found: ${context.payload.discussion.number}`); | |
} | |
console.log(`Verified discussion ID: ${repository.discussion.id}`); | |
// Now try to add it to the project | |
const response = await github.graphql(` | |
mutation($projectId: ID!, $contentId: ID!) { | |
addProjectV2ItemById(input: {projectId: $projectId, contentId: $contentId}) { | |
item { | |
id | |
} | |
} | |
} | |
`, { | |
projectId: projectId, | |
contentId: repository.discussion.id | |
}); | |
console.log('Added to project:', JSON.stringify(response, null, 2)); | |
} catch (error) { | |
console.error('Error:', error.message); | |
core.setFailed(error.message); | |
} |