Skip to content

Commit

Permalink
fixup! Show a warning notification on fetch token error when starting…
Browse files Browse the repository at this point in the history
… a workspace
  • Loading branch information
vinokurig committed Sep 17, 2024
1 parent e7fe0bf commit dfb4e13
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/* eslint-disable @typescript-eslint/no-unused-vars */

import { api } from '@eclipse-che/common';
import common, { api } from '@eclipse-che/common';
import { V1Status } from '@kubernetes/client-node';
import { dump } from 'js-yaml';
import { AnyAction } from 'redux';
Expand All @@ -21,6 +21,7 @@ import { ThunkDispatch } from 'redux-thunk';

import { container } from '@/inversify.config';
import { isRunningDevWorkspacesClusterLimitExceeded } from '@/services/backend-client/devWorkspaceClusterApi';
import * as factoryApi from '@/services/backend-client/factoryApi';
import { fetchServerConfig } from '@/services/backend-client/serverConfigApi';
import { WebsocketClient } from '@/services/backend-client/websocketClient';
import devfileApi from '@/services/devfileApi';
Expand Down Expand Up @@ -145,6 +146,8 @@ const mockOnStart = jest.fn();
const mockUpdate = jest.fn();
const mockUpdateAnnotation = jest.fn();

const refreshFactoryOauthTokenSpy = jest.spyOn(factoryApi, 'refreshFactoryOauthToken');

describe('DevWorkspace store, actions', () => {
const devWorkspaceClient = container.get(DevWorkspaceClient);
let storeBuilder: FakeStoreBuilder;
Expand Down Expand Up @@ -490,6 +493,61 @@ describe('DevWorkspace store, actions', () => {

expect(actions).toStrictEqual(expectedActions);
});
it('should refresh token', async () => {
// given
const projects = [
{
name: 'project',
git: {
remotes: {
origin: 'origin:project',
},
},
},
];
const devWorkspace = new DevWorkspaceBuilder().withProjects(projects).build();
const store = storeBuilder.withDevWorkspaces({ workspaces: [devWorkspace] }).build();
refreshFactoryOauthTokenSpy.mockResolvedValueOnce();

// when
await store.dispatch(testStore.actionCreators.startWorkspace(devWorkspace));

// then
expect(refreshFactoryOauthTokenSpy).toHaveBeenCalledWith('origin:project');
});

it('should dispatch notification on refresh token failure', async () => {
// given
const projects = [
{
name: 'project',
git: {
remotes: {
origin: 'origin:project',
},
},
},
];
const devWorkspace = new DevWorkspaceBuilder().withProjects(projects).build();
const store = storeBuilder.withDevWorkspaces({ workspaces: [devWorkspace] }).build();
const error = {
response: {
data: { attributes: { provider: 'github' }, message: 'test message' },
},
};
jest.spyOn(common.helpers.errors, 'includesAxiosResponse').mockImplementation(() => true);
refreshFactoryOauthTokenSpy.mockRejectedValueOnce(error);

// when
await store.dispatch(testStore.actionCreators.startWorkspace(devWorkspace));

// then
const actions = store.getActions();
expect(actions[0].type).toBe('UPDATE_WARNING');
expect(actions[0].warning).toBe(
"GitHub might not be operational, please check the provider's status page.",
);
});
});

describe('updateWorkspaceWithDefaultDevfile', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
getLifeTimeMs,
updateEditor,
} from '@/store/Workspaces/devWorkspaces/updateEditor';
import { includesAxiosResponse } from '@eclipse-che/common/lib/helpers/errors';

export const onStatusChangeCallbacks = new Map<string, (status: string) => void>();

Expand Down Expand Up @@ -311,12 +312,13 @@ export const actionCreators: ActionCreators = {
return;
}
try {
try {
await OAuthService.refreshTokenIfProjectExists(workspace);
} catch (e: any) {
await OAuthService.refreshTokenIfProjectExists(workspace);
} catch (e: any) {
if (includesAxiosResponse(e)) {
// Do not interrupt the workspace start, but show a warning notification.
const attributes = e.response.data.attributes;
let message = e.response.data.message;
const response = e.response;
const attributes = response.data.attributes;
let message = response.data.message;
let provider = '';
if (attributes !== undefined && attributes.provider !== undefined) {
const providerAttribute: string = attributes.provider;
Expand All @@ -329,6 +331,7 @@ export const actionCreators: ActionCreators = {
}
}
if (provider.length > 0) {
// TODO add status page url for each provider when https://github.com/eclipse-che/che/issues/23142 is fixed
message = `${provider} might not be operational, please check the provider's status page.`;
}
dispatch({
Expand All @@ -337,6 +340,8 @@ export const actionCreators: ActionCreators = {
warning: message,
});
}
}
try {
await dispatch(
DevWorkspacesCluster.actionCreators.requestRunningDevWorkspacesClusterLimitExceeded(),
);
Expand Down

0 comments on commit dfb4e13

Please sign in to comment.