diff --git a/.github/docs/trigger-azdo-pipeline-setup.md b/.github/docs/trigger-azdo-pipeline-setup.md index 0d0108d1f550..35bc58eacb72 100644 --- a/.github/docs/trigger-azdo-pipeline-setup.md +++ b/.github/docs/trigger-azdo-pipeline-setup.md @@ -19,20 +19,29 @@ GitHub Actions ──► GitHub OIDC Provider ──► Azure AD (federated cred > (e.g. in the `dotnet` org). The workflow uses **manual OIDC token exchange via > `curl`** instead, which works everywhere that `id-token: write` is allowed. -## Azure Pipelines comment cleanup +## CI comment cleanup `azure-pipelines-comment-cleanup.yml` collapses standalone `/azp run` commands (including named-pipeline variants such as `/azp run maui-pr, maui-pr-uitests`) -and success-only acknowledgments from `azure-pipelines[bot]` as **resolved** on -pull requests. Comments remain expandable; nothing is deleted. Failure messages, -authorization notices, mixed success/warning responses, and ordinary discussion -remain visible. +and successful-start acknowledgments from `azure-pipelines[bot]` as **resolved** +on pull requests, including acknowledgments with the standard "There may be +pipelines that require an authorized user to comment /azp run to run" hint. +It also collapses workflow-failure notices from `github-actions[bot]`: marked +review failures, agent-job failures, failed workflow status messages, and +security-scanning failure notices. Comments remain expandable; nothing is +deleted. Substantive review reports (including Tests Failure Analysis), actual +Azure Pipelines failures, standalone authorization warnings, and ordinary +discussion remain visible. This is independent of pipeline dispatch and does not change which pipelines run or who can request them. It uses the repository's `GITHUB_TOKEN`, does not check -out or execute PR code, and needs no additional secret. Once merged into the -default branch, it handles newly created or edited comments; it does not sweep -historical comments. +out or execute PR code, and needs no additional secret. Created or edited comment +events clean up the triggering PR. An hourly sweep reconciles all open PRs, +including historical comments, because comments posted with `GITHUB_TOKEN` do not +trigger another `issue_comment` workflow. A manual dispatch can target a single +`pr_number` (including a closed PR), or scan all open PRs when omitted. Already +minimized comments are left unchanged. Automatic cleanup activates after merge +into the default branch. ## Prerequisites diff --git a/.github/workflows/azure-pipelines-comment-cleanup.yml b/.github/workflows/azure-pipelines-comment-cleanup.yml index 25aef3d7aa35..26e60d386187 100644 --- a/.github/workflows/azure-pipelines-comment-cleanup.yml +++ b/.github/workflows/azure-pipelines-comment-cleanup.yml @@ -1,8 +1,16 @@ -name: Azure Pipelines Comment Cleanup +name: CI Comment Cleanup on: issue_comment: types: [created, edited] + schedule: + - cron: '17 * * * *' + workflow_dispatch: + inputs: + pr_number: + description: 'PR number to clean up (omit to scan all open PRs)' + required: false + type: number permissions: {} @@ -10,38 +18,95 @@ jobs: minimize: if: >- github.repository == 'dotnet/maui' && - github.event.issue.pull_request && - (contains(github.event.comment.body, '/azp') || - github.event.comment.user.login == 'azure-pipelines[bot]') + (github.event_name != 'issue_comment' || + (github.event.issue.pull_request && + (contains(github.event.comment.body, '/azp') || + github.event.comment.user.login == 'azure-pipelines[bot]' || + github.event.comment.user.login == 'github-actions[bot]'))) runs-on: ubuntu-latest - timeout-minutes: 5 + timeout-minutes: 10 permissions: pull-requests: write steps: - - name: Minimize Azure Pipelines commands and success acknowledgments + - name: Minimize CI commands, acknowledgments, and workflow failure notices uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + env: + PR_NUMBER: ${{ inputs.pr_number || '' }} with: script: | const { owner, repo } = context.repo; - // Re-read edits so a delayed event cannot hide a newer discussion. - const { data: comment } = await github.rest.issues.getComment({ - owner, repo, comment_id: context.payload.comment.id - }); - const body = comment.body.trim(); - const isCommand = /^\/azp[ \t]+run(?:[ \t]+[^\r\n]+)?$/i.test(body); - const isSuccess = comment.user.login === 'azure-pipelines[bot]' && - comment.user.type === 'Bot' && - /^(?:\s*)?\s*Azure Pipelines:
\s*Successfully started running [1-9][0-9]* pipeline\(s\)\.
\s*<\/samp>$/.test(body); - if (!isCommand && !isSuccess) { - core.info('Leaving discussion, warnings, and unsuccessful pipeline responses visible.'); - return; + const runUrlPrefix = `${process.env.GITHUB_SERVER_URL}/${owner}/${repo}/actions/runs/`; + function shouldMinimize(comment) { + const body = comment.body.trim(); + const isCommand = /^\/azp[ \t]+run(?:[ \t]+[^\r\n]+)?$/i.test(body); + const isBot = comment.author?.__typename === 'Bot'; + const isSuccess = isBot && comment.author.login === 'azure-pipelines' && + /^(?:\s*)?\s*Azure Pipelines:
\s*Successfully started running [1-9][0-9]* pipeline\(s\)\.
\s*(?:There may be pipelines that require an authorized user to comment \/azp run to run\.
\s*)?<\/samp>$/.test(body); + const isActionsFailure = isBot && comment.author.login === 'github-actions' && + body.includes(runUrlPrefix) && + (/^/.test(body) || + /^Agent job \[[0-9]+\]\([^\r\n)]+\) failed\.(?:\r?\n|$)/.test(body) || + /^\u274c [^\r\n]*\b(?:failed|failure|timed out|timed_out|cancelled|canceled)\b/i.test(body) || + /^\u26a0\ufe0f Security scanning failed for \[/.test(body)); + return isCommand || isSuccess || isActionsFailure; } - await github.graphql( - `mutation($id: ID!) { - minimizeComment(input: { subjectId: $id, classifier: RESOLVED }) { - minimizedComment { isMinimized } + + let prNumbers; + if (context.eventName === 'issue_comment') { + prNumbers = [context.payload.issue.number]; + } else if (process.env.PR_NUMBER) { + const number = Number(process.env.PR_NUMBER); + if (!Number.isSafeInteger(number) || number <= 0) { + throw new Error('A positive PR number is required.'); + } + prNumbers = [number]; + } else { + const pulls = await github.paginate(github.rest.pulls.list, { + owner, repo, state: 'open', sort: 'updated', direction: 'desc', per_page: 100 + }); + prNumbers = pulls.map(pr => pr.number); + } + + // GITHUB_TOKEN comments do not trigger issue_comment; reconcile them on the schedule. + for (const number of prNumbers) { + let cursor = null; + do { + // Read live bodies and minimized state, including on delayed edit events. + const { repository } = await github.graphql( + `query($owner: String!, $repo: String!, $number: Int!, $cursor: String) { + repository(owner: $owner, name: $repo) { + pullRequest(number: $number) { + comments(first: 100, after: $cursor) { + nodes { + id databaseId body isMinimized + author { login __typename } + } + pageInfo { hasNextPage endCursor } + } + } + } + }`, + { owner, repo, number, cursor } + ); + if (!repository.pullRequest) { + throw new Error(`PR #${number} was not found.`); } - }`, - { id: comment.node_id } - ); - core.info(`Minimized Azure Pipelines comment ${comment.id} as resolved.`); + const { comments } = repository.pullRequest; + for (const comment of comments.nodes) { + if (comment.isMinimized || !shouldMinimize(comment)) continue; + const result = await github.graphql( + `mutation($id: ID!) { + minimizeComment(input: { subjectId: $id, classifier: RESOLVED }) { + minimizedComment { isMinimized } + } + }`, + { id: comment.id } + ); + if (!result.minimizeComment.minimizedComment.isMinimized) { + throw new Error(`Could not minimize comment ${comment.databaseId}.`); + } + core.info(`Minimized CI comment ${comment.databaseId} on PR #${number} as resolved.`); + } + cursor = comments.pageInfo.hasNextPage ? comments.pageInfo.endCursor : null; + } while (cursor); + }