Skip to content

aw test-1

aw test-1 #5

name: Set Default Issue Priority
on:
issues:
types: [opened]
jobs:
set-priority:
runs-on: ubuntu-latest
steps:
- name: Set Issue Priority to Normal
uses: actions/github-script@v6
with:
github-token: ${{secrets.AW_GH_TOKEN}}
script: |
const issueNumber = context.payload.issue.number;
const org = "department-of-veterans-affairs";
const repo = "tmf-auth-exp-design-patterns";
const projectNumber = 1314;
async function setDefaultPriority() {
try {
const ghToken = process?.env?.['AW_GH_TOKEN']
if(!ghToken) {
console.log('token not found... check secrets for repo')
}
// Get the project ID and fields using GraphQL
const { data: { organization } } = await github.graphql(`
query($org: String!, $projectNumber: Int!) {
organization(login: $org) {
projectV2(number: $projectNumber) {
id
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
items(first: 10, orderBy: {field: POSITION, direction: DESC}) {
nodes {
id
content {
... on Issue {
id
number
}
}
}
}
}
}
}
`, {
org: org,
projectNumber: projectNumber
});
const project = organization.projectV2;
console.log(`Found project with ID: ${project.id}`);
const priorityField = project.fields.nodes.find(field => field.name === "Priority");
if (!priorityField) {
console.log("Priority field not found in the project");
return;
}
const normalOption = priorityField.options.find(option => option.name.toLowerCase() === "normal");
if (!normalOption) {
console.log("Normal priority option not found");
return;
}
console.log(`Found "Normal" priority option with ID: ${normalOption.id}`);
const projectItem = project.items.nodes.find(item => item.content && item.content.number === issueNumber);
if (!projectItem) {
console.log("Issue not found in the project");
return;
}
console.log(`Found issue in project with item ID: ${projectItem.id}`);
// Update the Priority field for the issue in the project
const result = await github.graphql(`
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
updateProjectV2ItemFieldValue(
input: {
projectId: $projectId,
itemId: $itemId,
fieldId: $fieldId,
value: {
singleSelectOptionId: $optionId
}
}
) {
projectV2Item {
id
}
}
}
`, {
projectId: project.id,
itemId: projectItem.id,
fieldId: priorityField.id,
optionId: normalOption.id
});
console.log("Issue priority successfully set to 'Normal'");
console.log("Result:", result);
} catch (error) {
console.error("Error:", error.message);
if (error.request) {
console.error("GraphQL query:", error.request.query);
console.error("Variables:", error.request.variables);
}
}
}
setDefaultPriority();