saleor-dashboard/.github/workflows/cypressTestsHelpers/getTestsResults.js
Karolina Rakoczy 36c14bd9a0
3481 if there is more than 20 failed tests add another notification to saleor-qa channel (#3622)
* Add slack notification

* Add correct webhook url

* Create new file with helpers

* add default

* add missing checkout
2023-05-17 10:24:43 +02:00

79 lines
2.2 KiB
JavaScript

const { Command } = require("commander");
const { GraphQLClient } = require("graphql-request");
const program = new Command();
const client = new GraphQLClient("https://dashboard.cypress.io/graphql");
const statusAndID = dashboardUrl => getTestsStatusAndId(dashboardUrl);
const failedTestCases = runId => getFailedTestCases(runId);
async function getTestsStatusAndId(dashboardUrl) {
const getProjectRegex = /\/projects\/([^\/]*)/;
const getRunRegex = /\/runs\/([^\/]*)/;
const requestVariables = {
projectId: dashboardUrl.match(getProjectRegex)[1],
buildNumber: dashboardUrl.match(getRunRegex)[1],
};
const throwErrorAfterTimeout = setTimeout(function () {
throw new Error("Run have still running status, after all tests executed");
}, 1200000);
const data = await waitForTestsToFinish(requestVariables);
clearTimeout(throwErrorAfterTimeout);
return { status: data.status, runId: data.id };
}
async function waitForTestsToFinish(requestVariables) {
return new Promise((resolve, reject) => {
client
.request(
`query ($projectId: String!, $buildNumber: ID!) {
runByBuildNumber(buildNumber: $buildNumber, projectId: $projectId) {
status,
id
}
}`,
requestVariables,
)
.then(response => {
if (response.runByBuildNumber.status === "RUNNING") {
setTimeout(async function () {
resolve(await waitForTestsToFinish(requestVariables));
}, 10000);
} else {
resolve(response.runByBuildNumber);
}
});
});
}
async function getFailedTestCases(runId) {
const requestVariables = {
input: {
runId,
testResultState: ["FAILED"],
},
};
return new Promise((resolve, reject) => {
client
.request(
`query RunTestResults($input: TestResultsTableInput!) {
testResults(input: $input) {
... on TestResult {
...RunTestResult
}
}
}
fragment RunTestResult on TestResult { id titleParts state}`,
requestVariables,
)
.then(response => {
resolve(response.testResults);
});
});
}
module.exports = { statusAndID, failedTestCases };