Created
August 31, 2021 07:01
-
-
Save Xotabu4/6ff7d0e503d011bcf40714645fe15b39 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import { URL } from 'url'; | |
import got from 'got'; | |
import FormData from 'form-data'; | |
async function compress(srcFolder: string, zipFilePath: string) { | |
const archiver = require('archiver'); | |
const targetBasePath = path.dirname(zipFilePath); | |
if (targetBasePath === srcFolder) { | |
throw new Error('Source and target folder must be different.'); | |
} | |
try { | |
await fs.promises.access(srcFolder, fs.constants.R_OK | fs.constants.W_OK); | |
await fs.promises.access(targetBasePath, fs.constants.R_OK | fs.constants.W_OK); | |
} catch (e) { | |
throw new Error(`Permission error: ${e.message}`); | |
} | |
const output = fs.createWriteStream(zipFilePath); | |
const zipArchive = archiver('zip'); | |
return new Promise((resolve, reject) => { | |
output.on('close', resolve); | |
output.on('error', err => { | |
reject(err); | |
}); | |
zipArchive.pipe(output); | |
zipArchive.directory(srcFolder, false); | |
zipArchive.finalize(); | |
}); | |
} | |
export async function zipAndSendAllureReport() { | |
// http://username:password@example.com/ | |
if (!process.env.ALLURE_SERVER_URL) { | |
throw new Error(`ALLURE_SERVER_URL env variable is not set, report won't be published. | |
Make sure it is set in format http://username:[email protected]/`); | |
} | |
const allureServerUrl = new URL(process.env.ALLURE_SERVER_URL); | |
await compress('./output/allure-results', './output/allure-results.zip'); | |
console.log(`Created compressed ./allure-results.zip`); | |
const defaultGot = got.extend({ | |
prefixUrl: allureServerUrl, | |
responseType: 'json' | |
}); | |
const form = new FormData(); | |
console.log(`Uploading compressed ./allure-results.zip`); | |
form.append('allureResults', fs.createReadStream('./output/allure-results.zip')); | |
const resultsResp = await defaultGot<{ uuid: string }>('api/result', { | |
method: 'POST', | |
body: form | |
}); | |
console.log(`Upload done: ${JSON.stringify(resultsResp.body, null, 2)}`); | |
const resultsId = resultsResp.body.uuid; | |
const allureReportPath = process.env.CI_COMMIT_REF_SLUG ? process.env.CI_COMMIT_REF_SLUG : 'e2e'; | |
console.log(`Triggering report generation for ${allureReportPath}`); | |
const reportUrl = await defaultGot<{ url: string }>('api/report', { | |
method: 'POST', | |
json: { | |
reportSpec: { | |
path: [allureReportPath] | |
}, | |
results: [resultsId], | |
deleteResults: true | |
} | |
}); | |
console.log(`Report generation done: ${JSON.stringify(reportUrl.body, null, 2)}`); | |
console.log(`========================================================================`); | |
console.log(`REPORT URL: ${reportUrl.body.url}`); | |
console.log(`========================================================================`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment