tag low priority issues #23
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: tag low priority issues | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: '0 16 * * 4' | |
permissions: | |
issues: write | |
jobs: | |
tag-low-priority-issues: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: '20.x' | |
- run: npm install azure-devops-node-api | |
- uses: actions/github-script@v7 | |
env: | |
ado_token: '${{ secrets.ADO_PERSONAL_ACCESS_TOKEN }}' | |
query_id: '6777cf01-7065-42ca-99ca-ba0d7b77d8fd' | |
with: | |
script: | | |
const azdev = require('azure-devops-node-api') | |
// Get the ADO client | |
try { | |
const orgUrl = "https://dev.azure.com/microsoft"; | |
const adoAuthHandler = azdev.getPersonalAccessTokenHandler(process.env.ado_token); | |
const adoConnection = new azdev.WebApi(orgUrl, adoAuthHandler); | |
adoClient = await adoConnection.getWorkItemTrackingApi(); | |
} catch (e) { | |
console.error(e); | |
core.setFailed('Could not connect to ADO'); | |
return; | |
} | |
// Querying Lastest Work Items | |
const queryResult = await adoClient.queryById(process.env.query_id); | |
// 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')); | |
if (relation) { | |
const match = relation.url.match(/github.com\/[^/]+\/[^/]+\/issues\/(\d+)/); | |
if (match) { | |
return match[1]; | |
} | |
} | |
// Try using the title, which includes [GitHub #123] | |
const match = workItem?.fields['System.Title']?.match(/\[GitHub #(\d+)\]/); | |
if (match) { | |
return match[1]; | |
} | |
return null; | |
} | |
// Map ADO work items to GitHub number, remove nulls | |
const ghIssueNumbers = workItemsDetails.map(wi => getGitHubIssueNumber(wi)).filter(n => n !== null); | |
// 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) => { | |
await addLowPriorityLabel(issueNumber); | |
}); | |
core.setOutput('Tagged Issues', ghIssueNumbers.join(',')); | |