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

Handled automatic deletion of expired scratch orgs #2144

Merged
merged 12 commits into from
Nov 3, 2023
12 changes: 10 additions & 2 deletions src/js/components/orgs/playgroundCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ const PlaygroundOrgCard = ({
const readyToDeleteOrg =
isWaitingToDeleteOrg && !org.currently_refreshing_changes;
const action = isWaitingToDeleteOrg;

if (readyToDeleteOrg) {
setIsWaitingToDeleteOrg(null);
if (org.has_unsaved_changes) {
Expand All @@ -132,7 +131,16 @@ const PlaygroundOrgCard = ({
doDeleteOrg();
}
}
}, [doDeleteOrg, doRefreshOrg, isWaitingToDeleteOrg, org]);
}, [doDeleteOrg, doRefreshOrg, isWaitingToDeleteOrg, org, dispatch]);
useEffect(() => {
if (
org &&
org?.expires_at !== null &&
new Date(org?.expires_at) < new Date()
) {
doDeleteOrg();
}
}, []);

/* istanbul ignore next */
if (!(project || epic || task)) {
Expand Down
13 changes: 11 additions & 2 deletions src/js/components/orgs/taskOrgCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Card from '@salesforce/design-system-react/components/card';
import classNames from 'classnames';
import React, { ReactNode, useCallback, useState } from 'react';
import React, { ReactNode, useCallback, useEffect, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import AssignTaskRoleModal from '@/js/components/githubUsers/assignTaskRole';
Expand Down Expand Up @@ -180,6 +180,7 @@ const TaskOrgCard = ({
handleRefresh?.(org);
}
}, [handleRefresh, org]);

const doCreateOrg = useCallback(() => {
handleCreate(type);
}, [handleCreate, type]);
Expand All @@ -202,7 +203,15 @@ const TaskOrgCard = ({
// We consider an org out-of-date if it is not based on the first commit.
const orgOutOfDate = Boolean(org && orgCommitIdx !== 0);
const testOrgOutOfDate = type === ORG_TYPES.QA && orgOutOfDate;

useEffect(() => {
if (
org &&
org?.expires_at !== null &&
new Date(org?.expires_at) < new Date()
) {
doDeleteOrg();
}
}, []);
return (
<div
className="slds-size_1-of-1
Expand Down
2 changes: 1 addition & 1 deletion test/js/components/epics/detail.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const defaultOrg = {
owner: sampleUser1.id,
owner_gh_username: sampleGitHubUser1.username,
owner_gh_id: sampleGitHubUser1.id,
expires_at: '2019-09-16T12:58:53.721Z',
expires_at: new Date(new Date().getTime() + 86400000).toISOString(),
latest_commit: '617a51',
latest_commit_url: '/test/commit/url/',
latest_commit_at: '2019-08-16T12:58:53.721Z',
Expand Down
23 changes: 22 additions & 1 deletion test/js/components/orgs/playgroundCard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const defaultOrg = {
owner: 'user-id',
owner_gh_username: 'user-name',
owner_gh_id: 123456,
expires_at: '2019-09-16T12:58:53.721Z',
expires_at: new Date(new Date().getTime() + 86400000).toISOString(),
latest_commit: '617a512',
latest_commit_url: '/test/commit/url/',
latest_commit_at: '2019-08-16T12:58:53.721Z',
Expand Down Expand Up @@ -236,6 +236,27 @@ describe('<PlaygroundOrgCard/>', () => {

const args = deleteObject.mock.calls[0][0];

expect(args.objectType).toBe('scratch_org');
expect(args.object.id).toEqual(defaultOrg.id);
});
});
describe('delete expired org', () => {
test('checks for expiry status and then deletes org', async () => {
const org = {
...defaultOrg,
expires_at: new Date().toISOString(),
unsaved_changes: {},
total_unsaved_changes: 0,
has_unsaved_changes: false,
};
const { findByText } = setup({ org });
expect.assertions(3);
await findByText('Deleting Org…');

expect(deleteObject).toHaveBeenCalledTimes(1);

const args = deleteObject.mock.calls[0][0];

expect(args.objectType).toBe('scratch_org');
expect(args.object.id).toEqual(defaultOrg.id);
});
Expand Down
23 changes: 22 additions & 1 deletion test/js/components/orgs/taskOrgCards.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const defaultOrgs = {
owner: sampleUser1.id,
owner_gh_username: sampleGitHubUser1.username,
owner_gh_id: sampleGitHubUser1.id,
expires_at: '2019-09-16T12:58:53.721Z',
expires_at: new Date(new Date().getTime() + 86400000).toISOString(),
latest_commit: '617a512-longlong',
latest_commit_url: '/test/commit/url/',
latest_commit_at: '2019-08-16T12:58:53.721Z',
Expand Down Expand Up @@ -913,7 +913,28 @@ describe('<TaskOrgCards/>', () => {
expect(args.object.id).toBe('org-id');
});
});
test('checks and then deletes expired org', async () => {
const { findByText } = setup({
orgs: {
...defaultOrgs,
Dev: {
...defaultOrgs.Dev,
expires_at: new Date().toISOString(),
unsaved_changes: {},
total_unsaved_changes: 0,
has_unsaved_changes: false,
},
},
});
await findByText('Deleting Org…');
expect.assertions(3);
expect(deleteObject).toHaveBeenCalledTimes(1);

const deleteArgs = deleteObject.mock.calls[0][0];

expect(deleteArgs.objectType).toBe('scratch_org');
expect(deleteArgs.object.id).toBe('org-id');
});
describe('Dev org', () => {
test('refreshes and then deletes org', async () => {
const { findByText, getByText } = setup({
Expand Down
2 changes: 1 addition & 1 deletion test/js/components/projects/detail.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const defaultOrg = {
owner: 'my-user',
owner_gh_username: 'currentUser',
owner_gh_id: 123456,
expires_at: '2019-09-16T12:58:53.721Z',
expires_at: new Date(new Date().getTime() + 86400000).toISOString(),
latest_commit: '617a51',
latest_commit_url: '/test/commit/url/',
latest_commit_at: '2019-08-16T12:58:53.721Z',
Expand Down
2 changes: 1 addition & 1 deletion test/js/components/tasks/detail.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const defaultOrg = {
owner: sampleUser1.id,
owner_gh_username: sampleGitHubUser1.username,
owner_gh_id: sampleGitHubUser1.id,
expires_at: '2019-09-16T12:58:53.721Z',
expires_at: new Date(new Date().getTime() + 86400000).toISOString(),
latest_commit: '617a51',
latest_commit_url: '/test/commit/url/',
latest_commit_at: '2019-08-16T12:58:53.721Z',
Expand Down
Loading