From 2033498627d27ee6bea098e88005c97b2faf145d Mon Sep 17 00:00:00 2001 From: Eric Turgeon <4249848+ericbsd@users.noreply.github.com> Date: Fri, 9 May 2025 07:43:26 -0300 Subject: [PATCH] Update project_automation.yml --- .github/workflows/project_automation.yml | 196 +++++++++++++---------- 1 file changed, 115 insertions(+), 81 deletions(-) diff --git a/.github/workflows/project_automation.yml b/.github/workflows/project_automation.yml index 6eb3468..7695876 100644 --- a/.github/workflows/project_automation.yml +++ b/.github/workflows/project_automation.yml @@ -67,10 +67,10 @@ jobs: resource_node_id: ${{ github.event.pull_request.node_id }} status_value: ${{ env.in_review }} - issue_closed: - name: issue_closed + issue_or_pr_closed: + name: issue_or_pr_closed runs-on: ubuntu-latest - if: github.event_name == 'issues' && github.event.action == 'closed' + if: (github.event_name == 'issues' || github.event_name == 'pull_request') && github.event.action == 'closed' steps: - name: Check Close Reason and Update id: close-reason @@ -83,102 +83,136 @@ jobs: const isIssue = context.eventName === 'issues'; const nodeId = isIssue ? context.payload.issue.node_id : context.payload.pull_request.node_id; + // Log context for debugging + console.log(`Event: ${context.eventName}, Issue/PR Number: ${issueNumber}`); + console.log(`Repo Owner: ${repo.owner}, Repo Name: ${repo.repo}`); + console.log(`Node ID: ${nodeId}`); + + // Validate inputs + if (!issueNumber || !repo.owner || !repo.repo) { + throw new Error('Missing required context: issueNumber, repo.owner, or repo.repo'); + } + // Get issue/PR details - const { data: issue } = await github.rest.issues.get({ - owner: repo.owner, - repo: repo.repo, - issue_number: issueNumber, - }); + try { + const { data: issue } = await github.rest.issues.get({ + owner: repo.owner, + repo: repo.repo, + issue_number: issueNumber, + }); - const closeReason = issue.state_reason || 'none'; - let statusValue = ''; - let labelToAdd = ''; - let shouldArchive = false; + const closeReason = issue.state_reason || 'none'; + let statusValue = ''; + let labelToAdd = ''; + let shouldArchive = false; - if (closeReason === 'not_planned') { - labelToAdd = 'wontfix'; - shouldArchive = true; - } else if (closeReason === 'duplicate') { - labelToAdd = 'duplicate'; - shouldArchive = true; - } else if (closeReason === 'completed') { - statusValue = process.env.done; - } - - // Set outputs for status update if applicable - if (statusValue) { - core.setOutput('status_value', statusValue); - core.setOutput('node_id', nodeId); - } - - // Add label if applicable - if (labelToAdd) { - try { - await github.rest.issues.addLabels({ - owner: repo.owner, - repo: repo.repo, - issue_number: issueNumber, - labels: [labelToAdd], - }); - } catch (error) { - console.log(`Failed to add label ${labelToAdd}: ${error.message}`); + if (closeReason === 'not_planned') { + labelToAdd = 'wontfix'; + shouldArchive = true; + } else if (closeReason === 'duplicate') { + labelToAdd = 'duplicate'; + shouldArchive = true; + } else if (closeReason === 'completed') { + statusValue = process.env.done; } - } - // Archive the project item if needed (not supported by project-beta-automations) - if (shouldArchive) { - try { - const projectItems = await github.graphql(` - query($org: String!, $projectNumber: Int!) { - organization(login: $org) { - projectV2(number: $projectNumber) { - items(first: 100) { - nodes { - id - content { - ... on Issue { - id - } - ... on PullRequest { - id + // Set outputs for status update if applicable + if (statusValue) { + core.setOutput('status_value', statusValue); + core.setOutput('node_id', nodeId); + } + + // Add label if applicable + if (labelToAdd) { + try { + await github.rest.issues.addLabels({ + owner: repo.owner, + repo: repo.repo, + issue_number: issueNumber, + labels: [labelToAdd], + }); + } catch (error) { + console.log(`Failed to add label ${labelToAdd}: ${error.message}`); + } + } + + // Archive the project item if needed (not supported by project-beta-automations) + if (shouldArchive) { + try { + // Fetch project node ID + const projectData = await github.graphql(` + query($org: String!, $projectNumber: Int!) { + organization(login: $org) { + projectV2(number: $projectNumber) { + id + } + } + } + `, { + org: 'ghostbsd', + projectNumber: 4 + }); + + const projectId = projectData.organization.projectV2.id; + console.log(`Project ID: ${projectId}`); + + // Fetch project items + const projectItems = await github.graphql(` + query($org: String!, $projectNumber: Int!) { + organization(login: $org) { + projectV2(number: $projectNumber) { + items(first: 100) { + nodes { + id + content { + ... on Issue { + id + } + ... on PullRequest { + id + } } } } } } } - } - `, { - org: 'ghostbsd', - projectNumber: 4 - }); + `, { + org: 'ghostbsd', + projectNumber: 4 + }); - const projectItem = projectItems.organization.projectV2.items.nodes.find( - item => item.content.id === nodeId - ); + const projectItem = projectItems.organization.projectV2.items.nodes.find( + item => item.content.id === nodeId + ); - if (projectItem) { - await github.graphql(` - mutation($itemId: ID!) { - archiveProjectV2Item(input: { projectId: "${process.env.PROJECT_ID}", itemId: $itemId }) { - item { - id + if (projectItem) { + await github.graphql(` + mutation($projectId: ID!, $itemId: ID!) { + archiveProjectV2Item(input: { projectId: $projectId, itemId: $itemId }) { + item { + id + } } } - } - `, { - itemId: projectItem.id - }); - console.log(`Archived item with node ID ${nodeId} in project 4`); - } else { - console.log(`Item with node ID ${nodeId} not found in project 4`); + `, { + projectId: projectId, + itemId: projectItem.id + }); + console.log(`Archived item with node ID ${nodeId} in project 4`); + } else { + console.log(`Item with node ID ${nodeId} not found in project 4`); + } + } catch (error) { + console.log(`Failed to archive item: ${error.message}`); } - } catch (error) { - console.log(`Failed to archive item: ${error.message}`); } - } - core.setOutput('close_reason', closeReason); + core.setOutput('close_reason', closeReason); + } catch (error) { + console.log(`Failed to fetch issue/PR: ${error.message}`); + throw error; + } - name: Move to Project Board if: steps.close-reason.outputs.status_value