refactor: save system #19
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: Assign to Sprint | |
on: | |
issues: | |
types: [opened] | |
jobs: | |
assign-to-sprint: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Extract Sprint from Issue Body | |
id: extract_sprint | |
run: | | |
echo "Reading issue body..." | |
ISSUE_BODY="${{ github.event.issue.body }}" | |
# Extract the sprint value from the issue body (e.g., "Current Iteration") | |
echo "Parsing sprint..." | |
sprint=$(echo "$ISSUE_BODY" | grep -oP '(?<=Iteration: )[^\\n]+') | |
echo "sprint=$sprint" >> $GITHUB_ENV | |
- name: Assign to Sprint in GitHub Project | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const projectId = "3"; // Replace with your project ID | |
const sprintFieldId = "PVTIF_lADOCk7XB84Akbryzgco_94"; // Sprint field ID | |
const sprintValue = process.env.sprint.trim(); // Extracted from issue body | |
if (sprintValue) { | |
const issueId = context.payload.issue.node_id; | |
// Retrieve iterations from the project | |
const iterations = await github.graphql(` | |
query ($projectId: ID!) { | |
node(id: $projectId) { | |
... on ProjectV2 { | |
fields(first: 20) { | |
nodes { | |
... on ProjectV2IterationField { | |
options { | |
id | |
name | |
startDate | |
endDate | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
`, { projectId }); | |
const iterationField = iterations.node.fields.nodes.find( | |
field => field.name === "Sprint" | |
); | |
if (!iterationField) { | |
throw new Error("Sprint field not found in the project."); | |
} | |
const now = new Date(); | |
// Resolve "Current Iteration" and "Next Iteration" | |
let targetIteration; | |
if (sprintValue === "Current Iteration") { | |
targetIteration = iterationField.options.find(option => { | |
const start = new Date(option.startDate); | |
const end = new Date(option.endDate); | |
return start <= now && now <= end; // Check if current date falls within iteration | |
}); | |
} else if (sprintValue === "Next Iteration") { | |
targetIteration = iterationField.options.find(option => { | |
const start = new Date(option.startDate); | |
return start > now; // Find the first future iteration | |
}); | |
} else { | |
// Match iteration name directly | |
targetIteration = iterationField.options.find( | |
option => option.name === sprintValue | |
); | |
} | |
if (!targetIteration) { | |
throw new Error(`Iteration '${sprintValue}' not found.`); | |
} | |
// Assign issue to the sprint | |
await github.graphql(` | |
mutation ($projectId: ID!, $itemId: ID!, $fieldId: ID!, $valueId: ID!) { | |
updateProjectV2ItemFieldValue( | |
input: { | |
projectId: $projectId, | |
itemId: $itemId, | |
fieldId: $fieldId, | |
value: { iterationId: $valueId } | |
} | |
) { | |
projectV2Item { | |
id | |
} | |
} | |
} | |
`, { | |
projectId: projectId, | |
itemId: issueId, | |
fieldId: sprintFieldId, | |
valueId: targetIteration.id | |
}); | |
} else { | |
console.log("No sprint specified in the issue."); | |
} |