Skip to content

Commit

Permalink
[7.6] [failed-test-report] if one test fails twice don't creat… (#59161)
Browse files Browse the repository at this point in the history
* [failed-test-report] if one test fails twice don't create two issues

* fix type check error

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
Spencer and elasticmachine authored Mar 3, 2020
1 parent 0e06f32 commit d2fcbb4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
15 changes: 13 additions & 2 deletions packages/kbn-test/src/failed_tests_reporter/github_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export interface GithubIssue {
body: string;
}

/**
* Minimal GithubIssue type that can be easily replicated by dry-run helpers
*/
export interface GithubIssueMini {
number: GithubIssue['number'];
body: GithubIssue['body'];
html_url: GithubIssue['html_url'];
}

type RequestOptions = AxiosRequestConfig & {
safeForDryRun?: boolean;
maxAttempts?: number;
Expand Down Expand Up @@ -162,7 +171,7 @@ export class GithubApi {
}

async createIssue(title: string, body: string, labels?: string[]) {
const resp = await this.request(
const resp = await this.request<GithubIssueMini>(
{
method: 'POST',
url: Url.resolve(BASE_URL, 'issues'),
Expand All @@ -173,11 +182,13 @@ export class GithubApi {
},
},
{
body,
number: 999,
html_url: 'https://dryrun',
}
);

return resp.data.html_url;
return resp.data;
}

private async request<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ describe('updateFailureIssue()', () => {
'https://build-url',
{
html_url: 'https://github.com/issues/1234',
labels: ['some-label'],
number: 1234,
title: 'issue title',
body: dedent`
# existing issue body
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-test/src/failed_tests_reporter/report_failure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { TestFailure } from './get_failures';
import { GithubIssue, GithubApi } from './github_api';
import { GithubIssueMini, GithubApi } from './github_api';
import { getIssueMetadata, updateIssueMetadata } from './issue_metadata';

export async function createFailureIssue(buildUrl: string, failure: TestFailure, api: GithubApi) {
Expand All @@ -44,7 +44,7 @@ export async function createFailureIssue(buildUrl: string, failure: TestFailure,
return await api.createIssue(title, body, ['failed-test']);
}

export async function updateFailureIssue(buildUrl: string, issue: GithubIssue, api: GithubApi) {
export async function updateFailureIssue(buildUrl: string, issue: GithubIssueMini, api: GithubApi) {
// Increment failCount
const newCount = getIssueMetadata(issue.body, 'test.failCount', 0) + 1;
const newBody = updateIssueMetadata(issue.body, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import { REPO_ROOT, run, createFailError, createFlagError } from '@kbn/dev-utils';
import globby from 'globby';

import { getFailures } from './get_failures';
import { GithubApi } from './github_api';
import { getFailures, TestFailure } from './get_failures';
import { GithubApi, GithubIssueMini } from './github_api';
import { updateFailureIssue, createFailureIssue } from './report_failure';
import { getIssueMetadata } from './issue_metadata';
import { readTestReport } from './test_report';
Expand Down Expand Up @@ -73,6 +73,11 @@ export function runFailedTestsReporterCli() {
absolute: true,
});

const newlyCreatedIssues: Array<{
failure: TestFailure;
newIssue: GithubIssueMini;
}> = [];

for (const reportPath of reportPaths) {
const report = await readTestReport(reportPath);
const messages = Array.from(getReportMessageIter(report));
Expand All @@ -94,12 +99,22 @@ export function runFailedTestsReporterCli() {
continue;
}

const existingIssue = await githubApi.findFailedTestIssue(
let existingIssue: GithubIssueMini | undefined = await githubApi.findFailedTestIssue(
i =>
getIssueMetadata(i.body, 'test.class') === failure.classname &&
getIssueMetadata(i.body, 'test.name') === failure.name
);

if (!existingIssue) {
const newlyCreated = newlyCreatedIssues.find(
({ failure: f }) => f.classname === failure.classname && f.name === failure.name
);

if (newlyCreated) {
existingIssue = newlyCreated.newIssue;
}
}

if (existingIssue) {
const newFailureCount = await updateFailureIssue(buildUrl, existingIssue, githubApi);
const url = existingIssue.html_url;
Expand All @@ -110,11 +125,12 @@ export function runFailedTestsReporterCli() {
continue;
}

const newIssueUrl = await createFailureIssue(buildUrl, failure, githubApi);
const newIssue = await createFailureIssue(buildUrl, failure, githubApi);
pushMessage('Test has not failed recently on tracked branches');
if (updateGithub) {
pushMessage(`Created new issue: ${newIssueUrl}`);
pushMessage(`Created new issue: ${newIssue.html_url}`);
}
newlyCreatedIssues.push({ failure, newIssue });
}

// mutates report to include messages and writes updated report to disk
Expand Down

0 comments on commit d2fcbb4

Please sign in to comment.