async function adjustSingleItemLabels()

in github-projects/map-labels/mapLabelsToAttributes.js [100:146]


async function adjustSingleItemLabels(octokit, options) {
    var _a;
    const { issueNode, projectNumber, projectId, mapping, owner, dryRun } = options;
    const { content: issue, id: itemId } = issueNode;
    const labels = issue.labels.nodes;
    const updatedFields = [];
    // Get fields for each mappable label
    for (const label of labels) {
        const fieldUpdate = mapping[label.name];
        if (!fieldUpdate) {
            continue;
        }
        const fieldName = Object.keys(fieldUpdate)[0];
        const value = fieldUpdate[fieldName];
        console.log('Finding option for value', { fieldName, value });
        // Get field id
        const optionForValue = await getOptionIdForValue(octokit, { projectNumber, fieldName, value, owner });
        if (!optionForValue) {
            continue;
        }
        // Check if the field is already set
        const existingField = issueNode.fieldValues.nodes.find((field) => field.__typename === 'ProjectV2ItemFieldSingleSelectValue' && field.field.name === fieldName);
        const fieldLookup = await getFieldLookupObj(octokit, { projectNumber, owner });
        if (existingField) {
            const existingFieldValue = (_a = fieldLookup[fieldName]) === null || _a === void 0 ? void 0 : _a.options.find((e) => e.id === existingField.optionId);
            console.log(`Field "${fieldName}" is already set to "${existingFieldValue === null || existingFieldValue === void 0 ? void 0 : existingFieldValue.name}" (${existingField.optionId}), skipping update`);
            continue;
        }
        // update field
        console.log(`Updating field "${fieldName}" to "${value}" (${optionForValue.optionId})`);
        const updateParams = {
            projectId,
            itemId,
            fieldId: optionForValue.fieldId,
            optionId: optionForValue.optionId,
            fieldName,
        };
        if (dryRun) {
            console.log('Dry run: skipping update for parameters', updateParams);
        }
        else {
            await (0, projectsGraphQL_1.gqlUpdateFieldValue)(octokit, updateParams);
        }
        updatedFields.push(fieldName);
    }
    return updatedFields;
}