Add new discussions to project #20
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 org-wide discussions to project | |
on: | |
discussion: | |
types: [created, reopened] | |
jobs: | |
add-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: Add Discussion to Project | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ steps.generate_token.outputs.token }} | |
script: | | |
const orgLogin = 'stampchain-io'; | |
const projectNumber = 3; | |
console.log(`Organization: ${orgLogin}, Project Number: ${projectNumber}`); | |
console.log('Discussion URL:', context.payload.discussion.html_url); | |
// First, get the project ID | |
const getProjectIdQuery = ` | |
query($org: String!, $number: Int!) { | |
organization(login: $org){ | |
projectV2(number: $number) { | |
id | |
} | |
} | |
} | |
`; | |
let projectId; | |
try { | |
console.log('Fetching Project ID...'); | |
const projectResult = await github.graphql(getProjectIdQuery, { | |
org: orgLogin, | |
number: projectNumber | |
}); | |
projectId = projectResult.organization.projectV2.id; | |
console.log('Project ID:', projectId); | |
} catch (error) { | |
console.error('Error fetching Project ID:', error); | |
throw error; | |
} | |
// Now, add the discussion to the project | |
const addToProjectMutation = ` | |
mutation($projectId: ID!, $contentId: ID!) { | |
addProjectV2ItemById(input: {projectId: $projectId, contentId: $contentId}) { | |
item { | |
id | |
} | |
} | |
} | |
`; | |
try { | |
console.log('Adding discussion to project...'); | |
const result = await github.graphql(addToProjectMutation, { | |
projectId: projectId, | |
contentId: context.payload.discussion.node_id | |
}); | |
console.log('Added to project:', JSON.stringify(result, null, 2)); | |
} catch (error) { | |
console.error('Error adding to project:', error); | |
if (error.errors) { | |
error.errors.forEach((e, i) => console.error(`Error ${i + 1}:`, e)); | |
} | |
throw error; | |
} |