提交 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. ...@@ -29,6 +29,7 @@ export class DataProvider implements vscode.TreeDataProvider<ItemModel | vscode.
return [new ErrorItem('Fetching Issues and MRs failed')]; return [new ErrorItem('Fetching Issues and MRs failed')];
} }
if (projects.length === 0) return [new vscode.TreeItem('No projects found')]; 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 = const customQueries =
vscode.workspace vscode.workspace
.getConfiguration(CONFIG_NAMESPACE) .getConfiguration(CONFIG_NAMESPACE)
......
...@@ -224,10 +224,7 @@ const getIssuableGqlId = (issuable: RestIssuable) => ...@@ -224,10 +224,7 @@ const getIssuableGqlId = (issuable: RestIssuable) =>
export class GitLabNewService { export class GitLabNewService {
client: GraphQLClient; client: GraphQLClient;
instanceUrl: string; constructor(readonly instanceUrl: string, readonly pipelineInstanceUrl?: string) {
constructor(instanceUrl: string) {
this.instanceUrl = instanceUrl;
const endpoint = new URL('/api/graphql', this.instanceUrl).href; const endpoint = new URL('/api/graphql', this.instanceUrl).href;
this.client = new GraphQLClient(endpoint, this.fetchOptions); this.client = new GraphQLClient(endpoint, this.fetchOptions);
} }
......
import * as vscode from 'vscode';
import * as assert from 'assert'; import * as assert from 'assert';
import { GitService } from './git_service'; import { GitService } from './git_service';
import { GitLabNewService } from './gitlab/gitlab_new_service'; import { GitLabNewService } from './gitlab/gitlab_new_service';
import { getInstanceUrl } from './utils/get_instance_url'; import { getInstanceUrl } from './utils/get_instance_url';
import { getExtensionConfiguration } from './utils/get_extension_configuration';
export function createGitService(workspaceFolder: string): GitService { export function createGitService(workspaceFolder: string): GitService {
assert(workspaceFolder, 'git service requires workspaceFolder to function'); assert(workspaceFolder, 'git service requires workspaceFolder to function');
const { remoteName, pipelineGitRemoteName } = vscode.workspace.getConfiguration('gitlab'); const { remoteName, pipelineGitRemoteName } = getExtensionConfiguration();
// 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
return new GitService({ return new GitService({
workspaceFolder, workspaceFolder,
remoteName: remoteName || undefined, remoteName,
pipelineGitRemoteName: pipelineGitRemoteName || undefined, pipelineGitRemoteName,
}); });
} }
......
...@@ -8,6 +8,7 @@ const { USER_COMMANDS } = require('./command_names'); ...@@ -8,6 +8,7 @@ const { USER_COMMANDS } = require('./command_names');
const MAXIMUM_DISPLAYED_JOBS = 4; const MAXIMUM_DISPLAYED_JOBS = 4;
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
const { const {
showStatusBarLinks, showStatusBarLinks,
showIssueLinkOnStatusBar, showIssueLinkOnStatusBar,
......
...@@ -8,6 +8,7 @@ const { USER_COMMANDS } = require('./command_names'); ...@@ -8,6 +8,7 @@ const { USER_COMMANDS } = require('./command_names');
let context = null; let context = null;
let active = false; let active = false;
// FIXME: if you are touching this configuration statement, move the configuration to get_extension_configuration.ts
const currentInstanceUrl = () => const currentInstanceUrl = () =>
vscode.workspace.getConfiguration('gitlab').instanceUrl || GITLAB_COM_URL; 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 { ...@@ -13,6 +13,7 @@ interface GitLabHttpAgentOptions {
export const getHttpAgentOptions = (): GitLabHttpAgentOptions => { export const getHttpAgentOptions = (): GitLabHttpAgentOptions => {
const result: 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( const { ignoreCertificateErrors, ca, cert, certKey } = vscode.workspace.getConfiguration(
'gitlab', 'gitlab',
); );
...@@ -40,6 +41,7 @@ export const getHttpAgentOptions = (): GitLabHttpAgentOptions => { ...@@ -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'); const { proxy } = vscode.workspace.getConfiguration('http');
result.proxy = proxy || undefined; result.proxy = proxy || undefined;
return result; return result;
......
...@@ -68,6 +68,7 @@ async function heuristicInstanceUrl(workspaceFolder: string) { ...@@ -68,6 +68,7 @@ async function heuristicInstanceUrl(workspaceFolder: string) {
} }
export async function getInstanceUrl(workspaceFolder?: string): Promise<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'); const { instanceUrl } = vscode.workspace.getConfiguration('gitlab');
// if the workspace setting exists, use it // if the workspace setting exists, use it
if (instanceUrl) { if (instanceUrl) {
......
...@@ -100,6 +100,7 @@ describe('GitLab tree view', () => { ...@@ -100,6 +100,7 @@ describe('GitLab tree view', () => {
}), }),
]); ]);
await tokenService.setToken(GITLAB_URL, 'abcd-secret'); 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); await vscode.workspace.getConfiguration().update('gitlab.customQueries', customQuerySettings);
}); });
...@@ -111,6 +112,7 @@ describe('GitLab tree view', () => { ...@@ -111,6 +112,7 @@ describe('GitLab tree view', () => {
after(async () => { after(async () => {
server.close(); server.close();
await tokenService.setToken(GITLAB_URL, undefined); 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); 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.
先完成此消息的编辑!
想要评论请 注册