Skip to content

tag low priority issues #4

tag low priority issues

tag low priority issues #4

name: tag low priority issues
on:
workflow_dispatch:
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) {
console.log(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) => {
await github.rest.issues.addLabels({
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['priority-low']
});
}
ghIssueNumbers.forEach(async (issueNumber) => {
await addLowPriorityLabel(issueNumber);
});
core.setOutput('Tagged Issues', ghIssueNumbers.join(','));