tag low priority issues #10
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: | |
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); | |
// 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); | |
// 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) => { | |
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(',')); | |