提交 d5663933 编写于 作者: T Tomas Vik

refactor: create util method to get extension cofiguration

Extracting configuration related logic came up in reviews multiple times.
Also, the VS Code logic returns missing properties as null.
This behaviour is not ideal because null function arguments overrdie the
default function arguments.

Related discussions:
- https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/merge_requests/115#note_432956679
上级 4adf3055
......@@ -29,6 +29,7 @@ export class DataProvider implements vscode.TreeDataProvider<ItemModel | vscode.
return [new ErrorItem('Fetching Issues and MRs failed')];
}
if (projects.length === 0) return [new vscode.TreeItem('No projects found')];
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
const customQueries =
vscode.workspace
.getConfiguration(CONFIG_NAMESPACE)
......
......@@ -224,10 +224,7 @@ const getIssuableGqlId = (issuable: RestIssuable) =>
export class GitLabNewService {
client: GraphQLClient;
instanceUrl: string;
constructor(instanceUrl: string) {
this.instanceUrl = instanceUrl;
constructor(readonly instanceUrl: string, readonly pipelineInstanceUrl?: string) {
const endpoint = new URL('/api/graphql', this.instanceUrl).href;
this.client = new GraphQLClient(endpoint, this.fetchOptions);
}
......
import * as vscode from 'vscode';
import * as assert from 'assert';
import { GitService } from './git_service';
import { GitLabNewService } from './gitlab/gitlab_new_service';
import { getInstanceUrl } from './utils/get_instance_url';
import { getExtensionConfiguration } from './utils/get_extension_configuration';
export function createGitService(workspaceFolder: string): GitService {
assert(workspaceFolder, 'git service requires workspaceFolder to function');
const { remoteName, pipelineGitRemoteName } = vscode.workspace.getConfiguration('gitlab');
// the getConfiguration() returns null for missing attributes, we need to convert them to
// undefined so that we can use optional properties and default function parameters
const { remoteName, pipelineGitRemoteName } = getExtensionConfiguration();
return new GitService({
workspaceFolder,
remoteName: remoteName || undefined,
pipelineGitRemoteName: pipelineGitRemoteName || undefined,
remoteName,
pipelineGitRemoteName,
});
}
......
......@@ -8,6 +8,7 @@ const { USER_COMMANDS } = require('./command_names');
const MAXIMUM_DISPLAYED_JOBS = 4;
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
const {
showStatusBarLinks,
showIssueLinkOnStatusBar,
......
......@@ -8,6 +8,7 @@ const { USER_COMMANDS } = require('./command_names');
let context = null;
let active = false;
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
const currentInstanceUrl = () =>
vscode.workspace.getConfiguration('gitlab').instanceUrl || GITLAB_COM_URL;
......
import * as vscode from 'vscode';
import { CONFIG_NAMESPACE } from '../constants';
interface ExtensionConfiguration {
remoteName?: string;
pipelineGitRemoteName?: string;
}
// VS Code returns a value or `null` but undefined is better for using default function arguments
const turnNullToUndefined = <T>(val: T | null | undefined): T | undefined => val ?? undefined;
export function getExtensionConfiguration(): ExtensionConfiguration {
const workspaceConfig = vscode.workspace.getConfiguration(CONFIG_NAMESPACE);
return {
remoteName: turnNullToUndefined(workspaceConfig.remoteName),
pipelineGitRemoteName: turnNullToUndefined(workspaceConfig.pipelineGitRemoteName),
};
}
......@@ -13,6 +13,7 @@ interface GitLabHttpAgentOptions {
export const getHttpAgentOptions = (): GitLabHttpAgentOptions => {
const result: GitLabHttpAgentOptions = {};
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
const { ignoreCertificateErrors, ca, cert, certKey } = vscode.workspace.getConfiguration(
'gitlab',
);
......@@ -40,6 +41,7 @@ export const getHttpAgentOptions = (): GitLabHttpAgentOptions => {
}
}
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
const { proxy } = vscode.workspace.getConfiguration('http');
result.proxy = proxy || undefined;
return result;
......
......@@ -68,6 +68,7 @@ async function heuristicInstanceUrl(workspaceFolder: string) {
}
export async function getInstanceUrl(workspaceFolder?: string): Promise<string> {
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
const { instanceUrl } = vscode.workspace.getConfiguration('gitlab');
// if the workspace setting exists, use it
if (instanceUrl) {
......
......@@ -100,6 +100,7 @@ describe('GitLab tree view', () => {
}),
]);
await tokenService.setToken(GITLAB_URL, 'abcd-secret');
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
await vscode.workspace.getConfiguration().update('gitlab.customQueries', customQuerySettings);
});
......@@ -111,6 +112,7 @@ describe('GitLab tree view', () => {
after(async () => {
server.close();
await tokenService.setToken(GITLAB_URL, undefined);
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
await vscode.workspace.getConfiguration().update('gitlab.customQueries', undefined);
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册