Skip to content

Add new discussions to project #28

Add new discussions to project

Add new discussions to project #28

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 - Print Context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: |
echo "GitHub Context: $GITHUB_CONTEXT"
echo "Discussion ID: ${{ github.event.discussion.node_id }}"
echo "Discussion Number: ${{ github.event.discussion.number }}"
echo "Organization: ${{ github.organization }}"
- name: Add to project
uses: actions/github-script@v6
with:
github-token: ${{ steps.generate_token.outputs.token }}
script: |
const projectId = 'PVT_kwDOCAGLAM4Anzfw';
const contentId = context.payload.discussion.node_id;
const discussionNumber = context.payload.discussion.number;
const orgName = context.payload.organization.login;
console.log(`Adding discussion ${contentId} (number ${discussionNumber}) to project ${projectId}`);
console.log(`Organization: ${orgName}`);
try {
// First, try to get the discussion details
const { organization } = await github.graphql(`
query($orgName: String!, $number: Int!) {
organization(login: $orgName) {
discussion(number: $number) {
id
}
}
}
`, {
orgName,
number: discussionNumber
});
console.log('Discussion details:', JSON.stringify(organization, null, 2));
// 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: organization.discussion.id
});
console.log('Response:', JSON.stringify(response, null, 2));
} catch (error) {
console.error('Error:', error.message);
core.setFailed(error.message);
}