1
0
mirror of synced 2026-01-05 12:07:35 -05:00

Update action-scripts to use the ProjectsV2 GraphQL API (#32474)

This commit is contained in:
Steve Guntrip
2022-11-15 10:16:02 +00:00
committed by GitHub
parent d025160d0f
commit 5d0d782319
3 changed files with 59 additions and 39 deletions

View File

@@ -4,7 +4,7 @@ import { graphql } from '@octokit/graphql'
// Pull out the node ID of a project field
export function findFieldID(fieldName, data) {
const field = data.organization.projectNext.fields.nodes.find((field) => field.name === fieldName)
const field = data.organization.projectV2.fields.nodes.find((field) => field.name === fieldName)
if (field && field.id) {
return field.id
@@ -15,14 +15,12 @@ export function findFieldID(fieldName, data) {
// Pull out the node ID of a single select field value
export function findSingleSelectID(singleSelectName, fieldName, data) {
const field = data.organization.projectNext.fields.nodes.find((field) => field.name === fieldName)
const field = data.organization.projectV2.fields.nodes.find((field) => field.name === fieldName)
if (!field) {
throw new Error(`A field called "${fieldName}" was not found. Check if the field was renamed.`)
}
const singleSelect = JSON.parse(field.settings).options.find(
(field) => field.name === singleSelectName
)
const singleSelect = field.options.find((field) => field.name === singleSelectName)
if (singleSelect && singleSelect.id) {
return singleSelect.id
@@ -41,11 +39,11 @@ export async function addItemsToProject(items, project) {
const mutations = items.map(
(item, index) => `
item_${index}: addProjectNextItem(input: {
item_${index}: addProjectV2ItemById(input: {
projectId: $project
contentId: "${item}"
}) {
projectNextItem {
item {
id
}
}
@@ -62,16 +60,14 @@ export async function addItemsToProject(items, project) {
project,
headers: {
authorization: `token ${process.env.TOKEN}`,
'GraphQL-Features': 'projects_next_graphql',
},
})
// The output of the mutation is
// {"item_0":{"projectNextItem":{"id":ID!}},...}
// Pull out the ID for each new item
const newItemIDs = Object.entries(newItems).map((item) => item[1].projectNextItem.id)
console.log(`New item IDs: ${newItemIDs}`)
const newItemIDs = Object.entries(newItems).map((item) => item[1].item.id)
return newItemIDs
}
@@ -139,7 +135,7 @@ export async function isGitHubOrgMember(login) {
// Formats a date object into the required format for projects
export function formatDateForProject(date) {
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
return date.toISOString()
}
// Given a date object and optional turnaround time
@@ -173,7 +169,7 @@ export function calculateDueDate(datePosted, turnaround = 2) {
// - "Contributor type" (as variable passed with the request)
// - "Feature" (as {feature})
// - "Author" (as {author})"
export function generateUpdateProjectNextItemFieldMutation({
export function generateUpdateProjectV2ItemFieldMutation({
item,
author,
turnaround = 2,
@@ -184,8 +180,8 @@ export function generateUpdateProjectNextItemFieldMutation({
// Build the mutation to update a single project field
// Specify literal=true to indicate that the value should be used as a string, not a variable
function generateMutationToUpdateField({ item, fieldID, value, literal = false }) {
const parsedValue = literal ? `value: "${value}"` : `value: ${value}`
function generateMutationToUpdateField({ item, fieldID, value, fieldType, literal = false }) {
const parsedValue = literal ? `${fieldType}: "${value}"` : `${fieldType}: ${value}`
// Strip all non-alphanumeric out of the item ID when creating the mutation ID to avoid a GraphQL parsing error
// (statistically, this should still give us a unique mutation ID)
@@ -193,13 +189,13 @@ export function generateUpdateProjectNextItemFieldMutation({
set_${fieldID.slice(1)}_item_${item.replaceAll(
/[^a-z0-9]/g,
''
)}: updateProjectNextItemField(input: {
)}: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: "${item}"
fieldId: ${fieldID}
${parsedValue}
value: { ${parsedValue} }
}) {
projectNextItem {
projectV2Item {
id
}
}
@@ -224,39 +220,46 @@ export function generateUpdateProjectNextItemFieldMutation({
item,
fieldID: '$statusID',
value: '$statusValueID',
fieldType: 'singleSelectOptionId',
})}
${generateMutationToUpdateField({
item,
fieldID: '$datePostedID',
value: formatDateForProject(datePosted),
fieldType: 'date',
literal: true,
})}
${generateMutationToUpdateField({
item,
fieldID: '$reviewDueDateID',
value: formatDateForProject(dueDate),
fieldType: 'date',
literal: true,
})}
${generateMutationToUpdateField({
item,
fieldID: '$contributorTypeID',
value: '$contributorType',
fieldType: 'singleSelectOptionId',
})}
${generateMutationToUpdateField({
item,
fieldID: '$sizeTypeID',
value: '$sizeType',
fieldType: 'singleSelectOptionId',
})}
${generateMutationToUpdateField({
item,
fieldID: '$featureID',
value: feature,
fieldType: 'text',
literal: true,
})}
${generateMutationToUpdateField({
item,
fieldID: '$authorID',
value: author,
fieldType: 'text',
literal: true,
})}
}
@@ -274,5 +277,5 @@ export default {
findSingleSelectID,
formatDateForProject,
calculateDueDate,
generateUpdateProjectNextItemFieldMutation,
generateUpdateProjectV2ItemFieldMutation,
}