提交 0640748b 编写于 作者: T Tomas Vik

Merge branch '359-warn-users-that-they-should-use-supported-gitlab-version' into 'main'

Resolve "Warn users that they should use supported GitLab version"

See merge request gitlab-org/gitlab-vscode-extension!252
...@@ -6,3 +6,5 @@ export const ADDED = 'added'; ...@@ -6,3 +6,5 @@ export const ADDED = 'added';
export const DELETED = 'deleted'; export const DELETED = 'deleted';
export const RENAMED = 'renamed'; export const RENAMED = 'renamed';
export const MODIFIED = 'modified'; export const MODIFIED = 'modified';
export const DO_NOT_SHOW_VERSION_WARNING = 'DO_NOT_SHOW_VERSION_WARNING';
export const MINIMUM_VERSION = 13.5;
...@@ -28,6 +28,7 @@ const { ...@@ -28,6 +28,7 @@ const {
createComment, createComment,
} = require('./commands/mr_discussion_commands'); } = require('./commands/mr_discussion_commands');
const { fileDecorationProvider } = require('./review/file_decoration_provider'); const { fileDecorationProvider } = require('./review/file_decoration_provider');
const { checkVersion } = require('./utils/check_version');
vscode.gitLabWorkflow = { vscode.gitLabWorkflow = {
sidebarDataProviders: [], sidebarDataProviders: [],
...@@ -107,6 +108,9 @@ const registerCiCompletion = context => { ...@@ -107,6 +108,9 @@ const registerCiCompletion = context => {
context.subscriptions.push(subscription); context.subscriptions.push(subscription);
}; };
/**
* @param {vscode.ExtensionContext} context
*/
const activate = context => { const activate = context => {
const outputChannel = vscode.window.createOutputChannel('GitLab Workflow'); const outputChannel = vscode.window.createOutputChannel('GitLab Workflow');
initializeLogging(line => outputChannel.appendLine(line)); initializeLogging(line => outputChannel.appendLine(line));
...@@ -121,6 +125,8 @@ const activate = context => { ...@@ -121,6 +125,8 @@ const activate = context => {
gitExtensionWrapper.init(); gitExtensionWrapper.init();
context.subscriptions.push(gitExtensionWrapper); context.subscriptions.push(gitExtensionWrapper);
vscode.window.registerFileDecorationProvider(fileDecorationProvider); vscode.window.registerFileDecorationProvider(fileDecorationProvider);
checkVersion(gitExtensionWrapper, context);
}; };
exports.activate = activate; exports.activate = activate;
...@@ -171,4 +171,8 @@ export class WrappedRepository { ...@@ -171,4 +171,8 @@ export class WrappedRepository {
hasSameRootAs(repository: Repository): boolean { hasSameRootAs(repository: Repository): boolean {
return this.rootFsPath === repository.rootUri.fsPath; return this.rootFsPath === repository.rootUri.fsPath;
} }
getVersion() {
return this.getGitLabService().getVersion();
}
} }
...@@ -37,7 +37,7 @@ import { ...@@ -37,7 +37,7 @@ import {
} from './graphql/get_project'; } from './graphql/get_project';
import { createDiffNoteMutation, GqlDiffPositionInput } from './graphql/create_diff_comment'; import { createDiffNoteMutation, GqlDiffPositionInput } from './graphql/create_diff_comment';
import { removeLeadingSlash } from '../utils/remove_leading_slash'; import { removeLeadingSlash } from '../utils/remove_leading_slash';
import { log } from '../log'; import { log, logError } from '../log';
interface CreateNoteResult { interface CreateNoteResult {
createNote: { createNote: {
...@@ -147,6 +147,16 @@ export class GitLabNewService { ...@@ -147,6 +147,16 @@ export class GitLabNewService {
}; };
} }
async getVersion(): Promise<string | undefined> {
try {
const result = await crossFetch(`${this.instanceUrl}/api/v4/version`, this.fetchOptions);
return (await result.json())?.version;
} catch (e) {
logError(e);
return undefined;
}
}
async getProject(projectPath: string): Promise<GitLabProject | undefined> { async getProject(projectPath: string): Promise<GitLabProject | undefined> {
const options: GetProjectQueryOptions = { projectPath }; const options: GetProjectQueryOptions = { projectPath };
const result = await this.client.request<GetProjectQueryResult>(queryGetProject, options); const result = await this.client.request<GetProjectQueryResult>(queryGetProject, options);
......
import * as vscode from 'vscode';
import { DO_NOT_SHOW_VERSION_WARNING } from '../constants';
import { gitExtensionWrapper } from '../git/git_extension_wrapper';
import { getVersionForEachRepo } from './check_version';
import * as logMock from '../log';
describe('check_version', () => {
describe('getVersionForEachRepo', () => {
let state: Record<string, any>;
let mockedRepositories: any[];
const context = {
workspaceState: {
get(key: string) {
return state[key];
},
update(key: string, value: any) {
state[key] = value;
},
},
};
const createMockRepo = (version: string) => ({
name: 'Test Repo',
getVersion: getVersion.mockResolvedValue(version),
});
beforeEach(() => {
state = {
[DO_NOT_SHOW_VERSION_WARNING]: false,
};
jest.resetAllMocks();
jest
.spyOn(gitExtensionWrapper, 'repositories', 'get')
.mockImplementation(() => mockedRepositories);
jest.spyOn(logMock, 'log');
});
const getVersion = jest.fn();
it('does nothing when there are no repos', async () => {
mockedRepositories = [];
await getVersionForEachRepo(gitExtensionWrapper, context as vscode.ExtensionContext);
expect(getVersion).not.toHaveBeenCalled();
});
it.each`
version
${'13.5.0'}
${'13.6.3'}
${'13.6.0-pre'}
${'abc13.5def'}
`('gets $version successfully', async ({ version }) => {
mockedRepositories = [createMockRepo(`${version}`)];
await getVersionForEachRepo(gitExtensionWrapper, context as vscode.ExtensionContext);
expect(vscode.window.showErrorMessage).not.toHaveBeenCalled();
});
it(`shows warning when version is below 13.5`, async () => {
mockedRepositories = [createMockRepo(`13.4.2`)];
await getVersionForEachRepo(gitExtensionWrapper, context as vscode.ExtensionContext);
expect(vscode.window.showErrorMessage).toHaveBeenCalled();
});
it('logs match warning if there is no regex match', async () => {
const BAD_VERSION = 'abcdefg';
mockedRepositories = [createMockRepo(BAD_VERSION)];
await getVersionForEachRepo(gitExtensionWrapper, context as vscode.ExtensionContext);
expect(logMock.log).toHaveBeenCalledWith(`Could not match version from "${BAD_VERSION}"`);
});
it('stores user preference for not showing the warning', async () => {
mockedRepositories = [createMockRepo('13.4')];
(vscode.window.showErrorMessage as jest.Mock).mockResolvedValue('Do not show again');
await getVersionForEachRepo(gitExtensionWrapper, context as vscode.ExtensionContext);
expect(state[DO_NOT_SHOW_VERSION_WARNING]).toBe(true);
});
it('does not show warning if user said they do not want to see it', async () => {
mockedRepositories = [createMockRepo('13.4')];
state = {
[DO_NOT_SHOW_VERSION_WARNING]: true,
};
await getVersionForEachRepo(gitExtensionWrapper, context as vscode.ExtensionContext);
expect(vscode.window.showErrorMessage).not.toHaveBeenCalled();
});
});
});
import * as vscode from 'vscode';
import { DO_NOT_SHOW_VERSION_WARNING, MINIMUM_VERSION } from '../constants';
import { GitExtensionWrapper } from '../git/git_extension_wrapper';
import { log } from '../log';
export const getVersionForEachRepo = async (
gitExtensionWrapper: GitExtensionWrapper,
context: vscode.ExtensionContext,
): Promise<void> => {
const DO_NOT_SHOW_AGAIN_TEXT = 'Do not show again';
await Promise.all(
gitExtensionWrapper.repositories.map(async repo => {
const version = await repo.getVersion();
const versionMatch = version?.match(/\d+\.\d+/);
if (!versionMatch) {
log(`Could not match version from "${version}"`);
return;
}
const versionNumber = versionMatch[0];
const parsedVersionNumber = parseFloat(versionNumber);
if (parsedVersionNumber >= MINIMUM_VERSION) return;
const warningMessage = `This extension requires GitLab version ${MINIMUM_VERSION} or later. Repo "${repo.name}" is currently using ${version}.`;
log(warningMessage);
if (!context.workspaceState.get(DO_NOT_SHOW_VERSION_WARNING)) {
const action = await vscode.window.showErrorMessage(warningMessage, DO_NOT_SHOW_AGAIN_TEXT);
if (action === DO_NOT_SHOW_AGAIN_TEXT)
await context.workspaceState.update(DO_NOT_SHOW_VERSION_WARNING, true);
}
}),
).catch(error => log(error));
};
/**
* Check that current repo version is greater than 13.5
* If not, show dismissible error message
*/
export async function checkVersion(
gitExtensionWrapper: GitExtensionWrapper,
context: vscode.ExtensionContext,
): Promise<void> {
await getVersionForEachRepo(gitExtensionWrapper, context);
gitExtensionWrapper.onRepositoryCountChanged(() =>
getVersionForEachRepo(gitExtensionWrapper, context),
);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册