Test for sync-status-across-projects GH action #23
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: Sync Status Across Projects | |
on: | |
issues: | |
types: [edited, labeled, unlabeled, closed, reopened] | |
jobs: | |
sync-status: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Sync Status Field | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GH_TOKEN }} | |
script: | | |
const orgName = "${{ github.repository_owner }}"; | |
const sourceProjectNumber = 1314; | |
const sourceProjectId = 'PVT_kwDOAFK5-84Ak8oz'; | |
const targetProjectNumber = 1514; | |
const targetProjectId = 'PVT_kwDOAFK5-84Ar1s4'; | |
// Function to fetch fields for a project | |
const fetchProjectFields = async (projectNumber, orgName) => { | |
const { data } = await github.graphql(` | |
query { | |
organization(login: "${orgName}") { | |
projectV2(number: ${projectNumber}) { | |
id | |
fields(first: 20) { | |
nodes { | |
... on ProjectV2SingleSelectField { | |
id | |
name | |
options { | |
id | |
name | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
`); | |
return data.organization.projectV2.fields.nodes; | |
}; | |
// Fetch fields for the source project | |
const sourceProjectFields = await fetchProjectFields(sourceProjectNumber, orgName); | |
// Find the 'Status' field ID from the source project | |
const statusField = sourceProjectFields.find(field => field.name === 'Status'); | |
if (!statusField) { | |
console.log("No 'Status' field found in source project."); | |
return; | |
} | |
const statusFieldId = statusField.id; | |
console.log(`Found 'Status' field ID in source project: ${statusFieldId}`); | |
// Fetch the current issue status from the source project | |
const issueNodeId = context.payload.issue.node_id; | |
// Fetch the status field value for the current issue in the source project | |
const { resource } = await github.graphql(` | |
query($projectId: ID!, $issueId: ID!) { | |
node(id: $issueId) { | |
... on Issue { | |
projectV2Items(first: 10) { | |
nodes { | |
fieldValues(first: 10) { | |
nodes { | |
projectV2Field { | |
id | |
name | |
} | |
value | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
`, { | |
projectId: sourceProjectId, | |
issueId: issueNodeId | |
}); | |
// Extract the current status from the source project | |
const currentStatus = resource.node.projectItems.nodes.find(item => item.projectField.id === statusFieldId)?.fieldValues.nodes.find(field => field.projectField.id === statusFieldId)?.value; | |
if (!currentStatus) { | |
console.log("No status field found or status has not changed."); | |
return; | |
} | |
console.log(`Current status in source project: ${currentStatus}`); | |
// Fetch the fields for the target project | |
const targetProjectFields = await fetchProjectFields(targetProjectNumber, orgName); | |
// Find the 'Status' field ID in the target project | |
const targetStatusField = targetProjectFields.find(field => field.name === 'Status'); | |
if (!targetStatusField) { | |
console.log("No 'Status' field found in target project."); | |
return; | |
} | |
const targetStatusFieldId = targetStatusField.id; | |
console.log(`Found 'Status' field ID in target project: ${targetStatusFieldId}`); | |
// Now sync the status field in the target project | |
await github.graphql(` | |
mutation($projectId: ID!, $contentId: ID!, $status: String!) { | |
updateProjectV2ItemFieldValue(input: { | |
projectId: $projectId, | |
itemId: $contentId, | |
fieldId: $statusFieldId, | |
value: $status | |
}) { | |
projectV2Item { | |
id | |
} | |
} | |
} | |
`, { | |
projectId: targetProjectId, | |
contentId: issueNodeId, | |
status: currentStatus | |
}); | |
console.log(`Status synced successfully to target project for issue ID: ${issueNodeId}`); | |