|
| 1 | +/* |
| 2 | +Copyright 2024 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/** |
| 18 | + * Flaky test reporter, creating & updating GitHub issues |
| 19 | + * Only intended to run from within GitHub Actions |
| 20 | + */ |
| 21 | + |
| 22 | +import type { Reporter, TestCase } from "@playwright/test/reporter"; |
| 23 | + |
| 24 | +const REPO = "element-hq/element-web"; |
| 25 | +const LABEL = "Z-Flaky-Test"; |
| 26 | +const ISSUE_TITLE_PREFIX = "Flaky playwright test: "; |
| 27 | + |
| 28 | +class FlakyReporter implements Reporter { |
| 29 | + private flakes = new Set<string>(); |
| 30 | + |
| 31 | + public onTestEnd(test: TestCase): void { |
| 32 | + const title = `${test.location.file.split("playwright/e2e/")[1]}: ${test.title}`; |
| 33 | + if (test.outcome() === "flaky") { |
| 34 | + this.flakes.add(title); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public async onExit(): Promise<void> { |
| 39 | + if (this.flakes.size === 0) { |
| 40 | + console.log("No flakes found"); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + console.log("Found flakes: "); |
| 45 | + for (const flake of this.flakes) { |
| 46 | + console.log(flake); |
| 47 | + } |
| 48 | + |
| 49 | + const { GITHUB_TOKEN, GITHUB_API_URL, GITHUB_SERVER_URL, GITHUB_REPOSITORY, GITHUB_RUN_ID } = process.env; |
| 50 | + if (!GITHUB_TOKEN) return; |
| 51 | + |
| 52 | + const body = `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}`; |
| 53 | + |
| 54 | + const headers = { Authorization: `Bearer ${GITHUB_TOKEN}` }; |
| 55 | + // Fetch all existing issues with the flaky-test label. |
| 56 | + const issuesRequest = await fetch(`${GITHUB_API_URL}/repos/${REPO}/issues?labels=${LABEL}`, { headers }); |
| 57 | + const issues = await issuesRequest.json(); |
| 58 | + for (const flake of this.flakes) { |
| 59 | + const title = ISSUE_TITLE_PREFIX + "`" + flake + "`"; |
| 60 | + const existingIssue = issues.find((issue) => issue.title === title); |
| 61 | + |
| 62 | + if (existingIssue) { |
| 63 | + console.log(`Found issue ${existingIssue.number} for ${flake}, adding comment...`); |
| 64 | + await fetch(`${existingIssue.url}/comments`, { |
| 65 | + method: "POST", |
| 66 | + headers, |
| 67 | + body: JSON.stringify({ body }), |
| 68 | + }); |
| 69 | + } else { |
| 70 | + console.log(`Creating new issue for ${flake}...`); |
| 71 | + await fetch(`${GITHUB_API_URL}/repos/${REPO}/issues`, { |
| 72 | + method: "POST", |
| 73 | + headers, |
| 74 | + body: JSON.stringify({ |
| 75 | + title, |
| 76 | + body, |
| 77 | + labels: [LABEL], |
| 78 | + }), |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export default FlakyReporter; |
0 commit comments