Skip to content

Instantly share code, notes, and snippets.

@jeremieflrnt
Last active September 5, 2022 15:08
Show Gist options
  • Save jeremieflrnt/8e2e8abc16b9fcf2ca6b685742557bee to your computer and use it in GitHub Desktop.
Save jeremieflrnt/8e2e8abc16b9fcf2ca6b685742557bee to your computer and use it in GitHub Desktop.
Creating a issue on Jira based on latest failed report on Gitlab CI
import axios from 'axios';
import fs from 'fs';
createBugsOnJira();
interface reportJson {
suites: [ { suites: [ { specs: [ { title: string; ok: boolean; tests: [{ annotations: [{ type: string; description: string }] | [] }]; } ]; } ]; } ];
}
function createBugsOnJira() {
const postData = {
summary: 'E2E tests failure',
description: 'E2E production tests failed on latest run:\n'
};
const url = 'https://automation.atlassian.com/pro/hooks/your-webhook-id-here';
const axiosConfig = { headers: { 'Content-Type': 'application/json' } };
if (process.env.CI_JOB_STATUS === 'failed') { #if the job fails, we send the annotation, or title of failed tests
try {
postData.description = postData.description.concat(getFailureDescriptionsFromReport('out/report/report.json'), '\n');
} catch (e) {
console.error('Error reading report directory or JSON report(s):\n', e);
}
postData.description = postData.description.concat(
'\n\n',
`For more details, go to the failed job test_e2e at ${process.env.CI_JOB_URL}/artifacts/file/playwright/out/report/index.html`
);
axios
.post(url, postData, axiosConfig)
.then(res => { console.log('Bug sent'); })
.catch(err => { console.error('Error sending bug:\n', err); });
} else {
axios
.post(url, { summary: postData.summary, close: 1 }, axiosConfig)
.then(res => { console.log('Bug closed'); })
.catch(err => { console.error('Error closing bug:\n', err); });
}
}
function getFailureDescriptionsFromReport(filePath: string) {
const report: reportJson = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
return report.suites
.flatMap(suites => suites.suites)
.flatMap(suite => suite.specs)
.filter(spec => !spec.ok)
.map(item => {
if (item.tests[0].annotations.length === 0) {
return item.title;
} else {
return item.tests[0].annotations.find(annotation => annotation.type === 'Url')?.description;
}
})
.join('\n');
}
test_e2e:
stage: e2e
allow_failure: false
image: mcr.microsoft.com/playwright:focal
before_script:
- cd playwright
- npm i
- npx playwright install
script:
- npm run test:all-ci
after_script:
- npm run create-jira # npm script is: "create-jira": "ts-node resources/utils/createBugsOnJira.ts"
artifacts:
paths:
- playwright/out/report/
when: on_failure
reports:
junit: playwright/out/report/report.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment