diff --git a/.github/workflows/tagPriorityLow.yml b/.github/workflows/tagPriorityLow.yml index 69fa4e862..0d79cf92a 100644 --- a/.github/workflows/tagPriorityLow.yml +++ b/.github/workflows/tagPriorityLow.yml @@ -2,6 +2,8 @@ name: tag low priority issues on: workflow_dispatch: + schedule: + - cron: '0 16 * * 4' permissions: issues: write @@ -37,15 +39,27 @@ jobs: // Querying Lastest Work Items const queryResult = await adoClient.queryById(process.env.query_id); - // Iterate over work items, including relations - // https://github.com/microsoft/azure-devops-node-api/blob/master/api/interfaces/WorkItemTrackingInterfaces.ts#L1485 - const workItemsDetails = await adoClient.getWorkItems(queryResult.workItems.map(wi => wi.id), null, null, 1); + // Work Item IDs + const workItemIds = queryResult.workItems.map(wi => wi.id); + + // getWorkItems has a limit of 200, so we need to batch the requests + const batchSize = 200; + const workItemsDetails = []; + + for (let i = 0; i < workItemIds.length; i += batchSize) { + const batchIds = workItemIds.slice(i, i + batchSize); + // Include relations in the response + // https://github.com/microsoft/azure-devops-node-api/blob/master/api/interfaces/WorkItemTrackingInterfaces.ts#L1485 + const batchDetails = await adoClient.getWorkItems(batchIds, null, null, 1); + workItemsDetails.push(...batchDetails); + } // Obtain GitHub Issue Number function getGitHubIssueNumber(workItem) { // Try using relations - const relation = workItem.relations.find(r => r.rel === 'Hyperlink' && r.url.includes('github.com')); + const relation = workItem?.relations?.find(r => r.rel === 'Hyperlink' && r.url.includes('github.com')); + if (relation) { const match = relation.url.match(/github.com\/[^/]+\/[^/]+\/issues\/(\d+)/); if (match) { @@ -54,7 +68,7 @@ jobs: } // Try using the title, which includes [GitHub #123] - const match = workItem.fields['System.Title'].match(/\[GitHub #(\d+)\]/); + const match = workItem?.fields['System.Title']?.match(/\[GitHub #(\d+)\]/); if (match) { return match[1]; } @@ -67,12 +81,26 @@ jobs: // Add priority-low label to GitHub issues const addLowPriorityLabel = async (issueNumber) => { + // Check if the issue already has the label + const { data: labels } = await github.rest.issues.listLabelsOnIssue({ + issue_number: issueNumber, + owner: context.repo.owner, + repo: context.repo.repo + }); + + if (labels.some(l => l.name === 'priority-low')) { + console.log(`Issue #${issueNumber} already has the label`); + return; + } + + // Add the label await github.rest.issues.addLabels({ issue_number: issueNumber, owner: context.repo.owner, repo: context.repo.repo, labels: ['priority-low'] }); + console.log(`Added label to issue #${issueNumber}`); } ghIssueNumbers.forEach(async (issueNumber) => {