Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy results when adding to Test Queue #978

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 121 additions & 33 deletions client/components/AddTestToQueueWithConfirmation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@ function AddTestToQueueWithConfirmation({
buttonText = 'Add to Test Queue',
triggerUpdate = () => {}
}) {
const [errorMessage, setErrorMessage] = useState(false);
const [showPreserveReportDataMessage, setShowPreserveReportDataMessage] =
useState(false);
const [showConfirmation, setShowConfirmation] = useState(false);
const [canUseOldResults, setCanUseOldResults] = useState(false);
const [addTestPlanReport] = useMutation(ADD_TEST_QUEUE_MUTATION);
const [scheduleCollection] = useMutation(SCHEDULE_COLLECTION_JOB_MUTATION);
const { data: existingTestPlanReportsData } = useQuery(
EXISTING_TEST_PLAN_REPORTS,
{
variables: {
testPlanVersionId: testPlanVersion?.id
testPlanVersionId: testPlanVersion?.id,
directory: testPlanVersion?.testPlan?.directory
},
fetchPolicy: 'cache-and-network',
skip: !testPlanVersion?.id
}
);

const existingTestPlanReports =
existingTestPlanReportsData?.testPlanVersion.testPlanReports;
existingTestPlanReportsData?.existingTestPlanVersion?.testPlanReports;

const conflictingReportExists = existingTestPlanReports?.some(report => {
return (
Expand All @@ -49,6 +52,31 @@ function AddTestToQueueWithConfirmation({
);
});

let latestOldVersion;
let oldReportToCopyResultsFrom;

// Prioritize a conflicting report for the current version, otherwise
// check if any results data available from a previous result
if (
!conflictingReportExists &&
existingTestPlanReportsData?.oldTestPlanVersions?.length
) {
latestOldVersion =
existingTestPlanReportsData?.oldTestPlanVersions?.reduce((a, b) =>
new Date(a.updatedAt) > new Date(b.updatedAt) ? a : b
);

if (
new Date(latestOldVersion?.updatedAt) <
new Date(testPlanVersion?.updatedAt)
) {
oldReportToCopyResultsFrom = latestOldVersion?.testPlanReports.some(
({ at: { id: atId }, browser: { id: browserId } }) =>
atId == at?.id && browserId == browser?.id
);
}
}

const { triggerLoad, loadingMessage } = useTriggerLoad();
const buttonRef = useRef();

Expand Down Expand Up @@ -104,7 +132,14 @@ function AddTestToQueueWithConfirmation({
actions.push({
label: 'Add and run later',
onClick: async () => {
await addTestToQueue();
await addTestToQueue(
canUseOldResults
? {
copyResultsFromTestPlanReportId:
latestOldVersion.id
}
: {}
);
await closeWithUpdate();
}
});
Expand All @@ -113,7 +148,14 @@ function AddTestToQueueWithConfirmation({
actions.push({
label: 'Add and run with bot',
onClick: async () => {
const testPlanReport = await addTestToQueue();
const testPlanReport = await addTestToQueue(
canUseOldResults
? {
copyResultsFromTestPlanReportId:
latestOldVersion.id
}
: {}
);
await scheduleCollectionJob(testPlanReport);
await closeWithUpdate();
}
Expand Down Expand Up @@ -141,48 +183,94 @@ function AddTestToQueueWithConfirmation({
);
};

const renderErrorDialog = () => {
const renderPreserveReportDataDialog = () => {
let title;
let content;
let actions = [];

if (oldReportToCopyResultsFrom) {
title = 'Older Results Data Found';
content =
'Older results with the same AT, browser and test plan ' +
'were found for the report being created. Would you like ' +
'to copy the older results into the report or create a ' +
'completely new report?';
actions = [
{
label: 'Create empty report',
onClick: async () => {
setShowPreserveReportDataMessage(false);
if (hasAutomationSupport) {
setShowConfirmation(true);
} else {
await addTestToQueue();
}
}
},
{
label: 'Copy older results',
onClick: async () => {
setShowPreserveReportDataMessage(false);
setCanUseOldResults(true);

if (hasAutomationSupport) {
setShowConfirmation(true);
} else {
await addTestToQueue({
copyResultsFromTestPlanReportId:
latestOldVersion.id
});
}
}
}
];
} else {
title = 'Conflicting Report Found';
content =
'The report could not be created because an existing ' +
'report was found on the reports page with the same AT, ' +
'browser and test plan version. Would you like to return ' +
'the existing report back to the test queue?';
actions = [
{
label: 'Proceed',
onClick: async () => {
setShowPreserveReportDataMessage(false);
if (hasAutomationSupport) {
setShowConfirmation(true);
} else {
await addTestToQueue();
}
}
}
];
}

return (
<BasicModal
show={errorMessage}
title="Conflicting Report Found"
content={
'The report could not be created because an existing ' +
'report was found on the reports page with the same AT, ' +
'browser and test plan version. Would you like to return ' +
'the existing report back to the test queue?'
}
show={showPreserveReportDataMessage}
title={title}
content={content}
closeLabel="Cancel"
staticBackdrop={true}
actions={[
{
label: 'Proceed',
onClick: async () => {
setErrorMessage(false);
if (hasAutomationSupport) {
setShowConfirmation(true);
} else {
await addTestToQueue();
}
}
}
]}
actions={actions}
useOnHide
handleClose={async () => {
setErrorMessage(false);
setShowPreserveReportDataMessage(false);
}}
/>
);
};

const addTestToQueue = async () => {
const addTestToQueue = async ({ copyResultsFromTestPlanReportId } = {}) => {
let tpr;
await triggerLoad(async () => {
const res = await addTestPlanReport({
variables: {
testPlanVersionId: testPlanVersion.id,
atId: at.id,
browserId: browser.id
browserId: browser.id,
copyResultsFromTestPlanReportId
}
});
const testPlanReport =
Expand Down Expand Up @@ -212,8 +300,8 @@ function AddTestToQueueWithConfirmation({
disabled={disabled}
variant="secondary"
onClick={async () => {
if (conflictingReportExists) {
setErrorMessage(true);
if (conflictingReportExists || oldReportToCopyResultsFrom) {
setShowPreserveReportDataMessage(true);
} else {
if (hasAutomationSupport) {
setShowConfirmation(true);
Expand All @@ -227,7 +315,7 @@ function AddTestToQueueWithConfirmation({
>
{buttonText}
</Button>
{renderErrorDialog()}
{renderPreserveReportDataDialog()}
{renderConfirmation()}
</LoadingStatus>
);
Expand Down
23 changes: 21 additions & 2 deletions client/components/AddTestToQueueWithConfirmation/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ export const SCHEDULE_COLLECTION_JOB_MUTATION = gql`
`;

export const EXISTING_TEST_PLAN_REPORTS = gql`
query ExistingTestPlanReports($testPlanVersionId: ID!) {
testPlanVersion(id: $testPlanVersionId) {
query ExistingTestPlanReports(
$testPlanVersionId: ID!
$directory: String!
) {
existingTestPlanVersion: testPlanVersion(id: $testPlanVersionId) {
id
testPlanReports {
id
Expand All @@ -28,5 +31,21 @@ export const EXISTING_TEST_PLAN_REPORTS = gql`
}
}
}
oldTestPlanVersions: testPlanVersions(
phases: [CANDIDATE, RECOMMENDED]
directory: $directory
) {
id
updatedAt
testPlanReports {
id
at {
id
}
browser {
id
}
}
}
}
`;
2 changes: 2 additions & 0 deletions client/components/TestQueue/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,14 @@ export const ADD_TEST_QUEUE_MUTATION = gql`
$testPlanVersionId: ID!
$atId: ID!
$browserId: ID!
$copyResultsFromTestPlanReportId: ID
) {
findOrCreateTestPlanReport(
input: {
testPlanVersionId: $testPlanVersionId
atId: $atId
browserId: $browserId
copyResultsFromTestPlanReportId: $copyResultsFromTestPlanReportId
}
) {
populatedData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export const mockedTestPlanVersion = {
export default (
meQuery,
testPlanReportStatusDialogQuery,
initiatedByAutomationQuery
existingTestPlanReportsQuery
) => [
{
request: {
Expand Down Expand Up @@ -386,14 +386,15 @@ export default (
},
{
request: {
query: initiatedByAutomationQuery,
query: existingTestPlanReportsQuery,
variables: {
testPlanVersionId: '7'
testPlanVersionId: '7',
directory: 'combobox-select-only'
}
},
result: {
data: {
testPlanVersion: {
existingTestPlanVersion: {
id: '7',
testPlanReports: [
{
Expand All @@ -411,7 +412,8 @@ export default (
}
}
]
}
},
oldTestPlanVersions: []
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions client/tests/__mocks__/GraphQLMocks/TestQueuePageBaseMock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export default (
testPlanReportAtBrowserQuery,
initiatedByAutomationRunQuery
) => [
export default (testPlanReportAtBrowserQuery, existingTestPlanReportsQuery) => [
{
request: {
query: testPlanReportAtBrowserQuery,
Expand Down Expand Up @@ -73,14 +70,15 @@ export default (
},
{
request: {
query: initiatedByAutomationRunQuery,
query: existingTestPlanReportsQuery,
variables: {
testPlanVersionId: '1'
testPlanVersionId: '1',
directory: 'alert'
}
},
result: {
data: {
testPlanVersion: {
existingTestPlanVersion: {
id: '1',
testPlanReports: [
{
Expand All @@ -98,7 +96,8 @@ export default (
}
}
]
}
},
oldTestPlanVersions: []
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion server/graphql-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ const graphqlSchema = gql`
testPlanVersionId: ID!
atId: ID!
browserId: ID!
copyResultsFromTestPlanReportId: ID
}

"""
Expand Down Expand Up @@ -1101,7 +1102,10 @@ const graphqlSchema = gql`
"""
Get all TestPlanVersions.
"""
testPlanVersions(phases: [TestPlanVersionPhase]): [TestPlanVersion]!
testPlanVersions(
phases: [TestPlanVersionPhase]
directory: String
): [TestPlanVersion]!
"""
Get a particular TestPlanVersion by ID.
"""
Expand Down
Loading
Loading