diff --git a/src/ci_config_validator.js b/src/ci_config_validator.js index db1e08a3f317799d7fd4fcf7e613d1d7823f8233..8e9943642f7b938222d11d793a9cf148a5ffa4e6 100644 --- a/src/ci_config_validator.js +++ b/src/ci_config_validator.js @@ -1,5 +1,6 @@ const vscode = require('vscode'); const gitLabService = require('./gitlab_service'); +const { getCurrentWorkspaceFolder } = require('./services/workspace_service'); const { showInformationMessage, showErrorMessage } = vscode.window; @@ -12,7 +13,7 @@ async function validate() { } const content = editor.document.getText(); - const response = await gitLabService.validateCIConfig(content); + const response = await gitLabService.validateCIConfig(await getCurrentWorkspaceFolder(), content); if (!response) { showInformationMessage('GitLab Workflow: Failed to validate CI configuration.'); diff --git a/src/commands/create_snippet.js b/src/commands/create_snippet.js index b2f5a97ebf890ae6793c8c7926327e642e526ac7..feac83cf1f0dfd69058a28bd27f57d0ca2fe9b6b 100644 --- a/src/commands/create_snippet.js +++ b/src/commands/create_snippet.js @@ -30,7 +30,7 @@ const contextOptions = [ }, ]; -async function uploadSnippet(project, editor, visibility, context) { +async function uploadSnippet(project, editor, visibility, context, workspaceFolder) { let content = ''; const fileName = editor.document.fileName.split('/').reverse()[0]; @@ -57,7 +57,7 @@ async function uploadSnippet(project, editor, visibility, context) { data.id = project.restId; } - const snippet = await gitLabService.createSnippet(data); + const snippet = await gitLabService.createSnippet(workspaceFolder, data); openers.openUrl(snippet.web_url); } @@ -90,7 +90,7 @@ async function createSnippet() { const context = await vscode.window.showQuickPick(contextOptions); if (context) { - uploadSnippet(project, editor, visibility.type, context.type); + uploadSnippet(project, editor, visibility.type, context.type, workspaceFolder); } } } else { diff --git a/src/gitlab_service.ts b/src/gitlab_service.ts index 427da9c5bc862ef8e2be3b7e53ed9ee28b437532..1e97a82cd5758c45aab0507eb677db1c9f8aff1e 100644 --- a/src/gitlab_service.ts +++ b/src/gitlab_service.ts @@ -4,7 +4,6 @@ import { basename } from 'path'; import { tokenService } from './services/token_service'; import { UserFriendlyError } from './errors/user_friendly_error'; import { ApiError } from './errors/api_error'; -import { getCurrentWorkspaceFolder } from './services/workspace_service'; import { createGitLabNewService, createGitService } from './service_factory'; import { GitRemote } from './git/git_remote_parser'; import { handleError, logError } from './log'; @@ -149,9 +148,9 @@ export async function fetchCurrentPipelineProject(workspaceFolder: string) { } } -export async function fetchCurrentUser(): Promise { +export async function fetchCurrentUser(workspaceFolder: string): Promise { try { - const { response: user } = await fetch(await getCurrentWorkspaceFolder(), '/user'); + const { response: user } = await fetch(workspaceFolder, '/user'); if (!user) throw new Error('Could not retrieve current user.'); return user; } catch (e) { @@ -169,10 +168,10 @@ async function fetchFirstUserByUsername(workspaceFolder: string | undefined, use } } -export async function fetchVersion() { +export async function fetchVersion(workspaceFolder: string) { try { if (!versionCache) { - const { response } = await fetch(await getCurrentWorkspaceFolder(), '/version'); + const { response } = await fetch(workspaceFolder, '/version'); versionCache = response.version; } } catch (e) { @@ -232,7 +231,7 @@ export async function fetchIssuables(params: CustomQuery, workspaceFolder: strin }; let issuable = null; - const version = await fetchVersion(); + const version = await fetchVersion(workspaceFolder); const project = await fetchCurrentProjectSwallowError(workspaceFolder); if (!version || !project) return []; @@ -303,7 +302,7 @@ export async function fetchIssuables(params: CustomQuery, workspaceFolder: strin */ if (reviewer) { if (reviewer === '') { - const user = await fetchCurrentUser(); + const user = await fetchCurrentUser(workspaceFolder); reviewer = user.username; } search.append('reviewer_username', reviewer); @@ -350,10 +349,7 @@ export async function fetchIssuables(params: CustomQuery, workspaceFolder: strin // Issue to deprecate this filter: https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/issues/311 if (pipelineId) { if (pipelineId === 'branch') { - const workspace = await getCurrentWorkspaceFolder(); - if (workspace) { - pipelineId = (await fetchLastPipelineForCurrentBranch(workspace))?.id; - } + pipelineId = (await fetchLastPipelineForCurrentBranch(workspaceFolder))?.id; } search.append('pipeline_id', `${pipelineId}`); } @@ -385,9 +381,12 @@ export async function fetchIssuables(params: CustomQuery, workspaceFolder: strin return issuable.map(normalizeAvatarUrl(await getInstanceUrl())); } -export async function fetchLastJobsForCurrentBranch(pipeline: RestPipeline): Promise { +export async function fetchLastJobsForCurrentBranch( + workspaceFolder: string, + pipeline: RestPipeline, +): Promise { const { response } = await fetch( - await getCurrentWorkspaceFolder(), + workspaceFolder, `/projects/${pipeline.project_id}/pipelines/${pipeline.id}/jobs`, ); let jobs: RestJob[] = response; @@ -423,9 +422,12 @@ export async function fetchOpenMergeRequestForCurrentBranch( return null; } -export async function fetchLastPipelineForMr(mr: RestIssuable): Promise { +export async function fetchLastPipelineForMr( + workspaceFolder: string, + mr: RestIssuable, +): Promise { const path = `/projects/${mr.project_id}/merge_requests/${mr.iid}/pipelines`; - const { response: pipelines } = await fetch(await getCurrentWorkspaceFolder(), path); + const { response: pipelines } = await fetch(workspaceFolder, path); return pipelines.length > 0 ? pipelines[0] : null; } @@ -445,7 +447,7 @@ export async function fetchPipelineAndMrForCurrentBranch( const mr = await turnErrorToNull(fetchOpenMergeRequestForCurrentBranch(workspaceFolder)); if (mr) { - const pipeline = await turnErrorToNull(fetchLastPipelineForMr(mr)); + const pipeline = await turnErrorToNull(fetchLastPipelineForMr(workspaceFolder, mr)); if (pipeline) return { mr, pipeline }; } const pipeline = await turnErrorToNull(fetchLastPipelineForCurrentBranch(workspaceFolder)); @@ -504,7 +506,7 @@ export async function fetchMRIssues( } // TODO specify the correct interface when we convert `create_snippet.js` -export async function createSnippet(data: { id: string }) { +export async function createSnippet(workspaceFolder: string, data: { id: string }) { let snippet; let path = '/snippets'; @@ -513,7 +515,7 @@ export async function createSnippet(data: { id: string }) { } try { - const { response } = await fetch(await getCurrentWorkspaceFolder(), path, 'POST', data); + const { response } = await fetch(workspaceFolder, path, 'POST', data); snippet = response; } catch (e) { handleError(new UserFriendlyError('Failed to create your snippet.', e)); @@ -522,11 +524,11 @@ export async function createSnippet(data: { id: string }) { return snippet; } -export async function validateCIConfig(content: string) { +export async function validateCIConfig(workspaceFolder: string, content: string) { let validCIConfig = null; try { - const { response } = await fetch(await getCurrentWorkspaceFolder(), '/ci/lint', 'POST', { + const { response } = await fetch(workspaceFolder, '/ci/lint', 'POST', { content, }); validCIConfig = response; @@ -545,7 +547,7 @@ interface Discussion { export async function renderMarkdown(markdown: string, workspaceFolder: string) { let rendered = { html: markdown }; - const version = await fetchVersion(); + const version = await fetchVersion(workspaceFolder); if (!version) { return markdown; } diff --git a/src/openers.ts b/src/openers.ts index 15a7ce4a76c10c42b43743dcf710bf989203ce7d..3e884eb522b53a7e82d2056c8892191b0d72ae2b 100644 --- a/src/openers.ts +++ b/src/openers.ts @@ -27,7 +27,7 @@ export const openUrl = async (url: string): Promise => * @param {string} linkTemplate */ async function getLink(linkTemplate: string, workspaceFolder: string) { - const user = await gitLabService.fetchCurrentUser(); + const user = await gitLabService.fetchCurrentUser(workspaceFolder); const project = await gitLabService.fetchCurrentProject(workspaceFolder); assert(project, 'Failed to fetch project'); diff --git a/src/status_bar.ts b/src/status_bar.ts index 0fa65e8d5634095d987a41cd3e45c6955cfe3607..b2e919fbc398c624a2882ca4cae35f00159d6911 100644 --- a/src/status_bar.ts +++ b/src/status_bar.ts @@ -110,7 +110,7 @@ export class StatusBar { if (status === 'running' || status === 'failed') { try { - const jobs = await gitLabService.fetchLastJobsForCurrentBranch(pipeline); + const jobs = await gitLabService.fetchLastJobsForCurrentBranch(workspaceFolder, pipeline); if (jobs) { statusText = createStatusTextFromJobs(jobs, status); }