diff --git a/server/models/services/CollectionJobService.js b/server/models/services/CollectionJobService.js index 5113a9fcc..c457c9d24 100644 --- a/server/models/services/CollectionJobService.js +++ b/server/models/services/CollectionJobService.js @@ -27,7 +27,7 @@ const { const runnableTestsResolver = require('../../resolvers/TestPlanReport/runnableTestsResolver'); const getGraphQLContext = require('../../graphql-context'); const { getBotUserByAtId } = require('./UserService'); -const { getAtVersions } = require('./AtService'); +const getAtVersionWithRequirements = require('../../util/getAtVersionWithRequirements'); const axiosConfig = { headers: { @@ -36,63 +36,6 @@ const axiosConfig = { timeout: 1000 }; -const getAtVersionWithRequirements = async ( - atId, - exactAtVersion, - minimumAtVersion, - transaction -) => { - try { - if (exactAtVersion) { - return exactAtVersion; - } - - if (!minimumAtVersion) { - throw new Error( - 'Either exactAtVersion or minimumAtVersion must be provided' - ); - } - - const isMinimumVersionSupported = - await minimumAtVersion.supportedByAutomation; - if (isMinimumVersionSupported) { - return minimumAtVersion; - } - - const matchingAts = await getAtVersions({ - where: { - atId, - releasedAt: { [Op.gte]: minimumAtVersion.releasedAt } - }, - pagination: { - order: [['releasedAt', 'ASC']] - }, - transaction - }); - - const supportedAts = await Promise.all( - matchingAts.map(async version => { - const supportedByAutomation = await version.supportedByAutomation; - return supportedByAutomation ? version.toJSON() : null; - }) - ); - - const latestSupportedAt = supportedAts.filter(Boolean)[0]; - - if (!latestSupportedAt) { - throw new Error( - `No suitable AT version found for automation for AT ${atId} ` + - `with minimumAtVersion ${minimumAtVersion?.name}` - ); - } - - return latestSupportedAt; - } catch (error) { - console.error('Error while determining AT version:', error); - throw error; - } -}; - // association helpers to be included with Models' results /** @@ -714,7 +657,19 @@ const restartCollectionJob = async ({ id }, { transaction }) => { return null; } - return triggerWorkflow(job, [], { transaction }); + const testPlanReport = await getTestPlanReportById({ + id: job.testPlanRun.testPlanReportId, + transaction + }); + + const atVersion = await getAtVersionWithRequirements( + testPlanReport.at.id, + testPlanReport.exactAtVersion, + testPlanReport.minimumAtVersion, + transaction + ); + + return triggerWorkflow(job, [], atVersion, { transaction }); }; /** diff --git a/server/scripts/populate-test-data/pg_dump_test_data.sql b/server/scripts/populate-test-data/pg_dump_test_data.sql index 6a1302143..0be975b1e 100644 --- a/server/scripts/populate-test-data/pg_dump_test_data.sql +++ b/server/scripts/populate-test-data/pg_dump_test_data.sql @@ -31,6 +31,8 @@ $$; -- Data for Name: AtVersion; Type: TABLE DATA; Schema: public; Owner: atr -- +INSERT INTO "AtVersion" ("atId", "name", "releasedAt") VALUES (2, '2023.3.3', '2023-12-02'); + -- INSERT INTO "AtVersion" ("atId", "name", "releasedAt") VALUES (2, '2019.3', '2022-05-02'); -- INSERT INTO "AtVersion" ("atId", "name", "releasedAt") VALUES (2, '2020.1', '2022-05-02'); -- INSERT INTO "AtVersion" ("atId", "name", "releasedAt") VALUES (2, '2020.2', '2022-05-02'); diff --git a/server/tests/integration/automation-scheduler.test.js b/server/tests/integration/automation-scheduler.test.js index 0e25b318c..276feb76f 100644 --- a/server/tests/integration/automation-scheduler.test.js +++ b/server/tests/integration/automation-scheduler.test.js @@ -13,6 +13,7 @@ const AtLoader = require('../../models/loaders/AtLoader'); const BrowserLoader = require('../../models/loaders/BrowserLoader'); const getGraphQLContext = require('../../graphql-context'); const { COLLECTION_JOB_STATUS } = require('../../util/enums'); +const getAtVersionWithRequirements = require('../../util/getAtVersionWithRequirements'); let mockAutomationSchedulerServer; let apiServer; @@ -241,21 +242,28 @@ describe('Automation controller', () => { data: { id: '999', status: 'QUEUED' } }); - const { - testPlanReport: { - runnableTests, - testPlanVersion: { - gitSha: testPlanVersionGitSha, - testPlan: { directory: testPlanName } - } - } - } = await query( + const { testPlanReport } = await query( ` query { testPlanReport(id: "${testPlanReportId}") { runnableTests { id } + minimumAtVersion { + name + id + releasedAt + supportedByAutomation + } + exactAtVersion { + name + id + releasedAt + supportedByAutomation + } + at { + id + } testPlanVersion { testPlan { directory @@ -268,18 +276,37 @@ describe('Automation controller', () => { { transaction } ); + const { + runnableTests, + testPlanVersion: { + gitSha: testPlanVersionGitSha, + testPlan: { directory: testPlanName } + }, + minimumAtVersion, + exactAtVersion, + at + } = testPlanReport; + const testIds = runnableTests.map(({ id }) => id); const collectionJob = await scheduleCollectionJobByMutation({ transaction }); + const atVersion = await getAtVersionWithRequirements( + at.id, + exactAtVersion, + minimumAtVersion, + transaction + ); + const expectedRequestBody = { testPlanVersionGitSha, testIds, testPlanName, jobId: parseInt(collectionJob.scheduleCollectionJob.id), - transactionId: transaction.id + transactionId: transaction.id, + atVersion }; expect(axiosPostMock).toHaveBeenCalledWith( diff --git a/server/util/getAtVersionWithRequirements.js b/server/util/getAtVersionWithRequirements.js new file mode 100644 index 000000000..a3a419071 --- /dev/null +++ b/server/util/getAtVersionWithRequirements.js @@ -0,0 +1,61 @@ +const { Op } = require('sequelize'); +const { getAtVersions } = require('../models/services/AtService'); + +const getAtVersionWithRequirements = async ( + atId, + exactAtVersion, + minimumAtVersion, + transaction +) => { + try { + if (exactAtVersion) { + return exactAtVersion; + } + + if (!minimumAtVersion) { + throw new Error( + 'Either exactAtVersion or minimumAtVersion must be provided' + ); + } + + const isMinimumVersionSupported = + await minimumAtVersion.supportedByAutomation; + if (isMinimumVersionSupported) { + return minimumAtVersion; + } + + const matchingAts = await getAtVersions({ + where: { + atId, + releasedAt: { [Op.gte]: minimumAtVersion.releasedAt } + }, + pagination: { + order: [['releasedAt', 'ASC']] + }, + transaction + }); + + const supportedAts = await Promise.all( + matchingAts.map(async version => { + const supportedByAutomation = await version.supportedByAutomation; + return supportedByAutomation ? version.toJSON() : null; + }) + ); + + const latestSupportedAt = supportedAts.filter(Boolean)[0]; + + if (!latestSupportedAt) { + throw new Error( + `No suitable AT version found for automation for AT ${atId} ` + + `with minimumAtVersion ${minimumAtVersion?.name}` + ); + } + + return latestSupportedAt; + } catch (error) { + console.error('Error while determining AT version:', error); + throw error; + } +}; + +module.exports = getAtVersionWithRequirements;