From efaf1b74acf42a6ab80a5f2a1d96e46da34722a0 Mon Sep 17 00:00:00 2001 From: Tomas Vik Date: Wed, 30 Jun 2021 13:03:21 +0200 Subject: [PATCH] fix: inserting snippets not working for newly created snippets --- src/gitlab/gitlab_new_service.test.ts | 23 +++++++++++++++++++ src/gitlab/gitlab_new_service.ts | 11 +++++++-- src/gitlab/graphql/get_snippets.ts | 2 ++ .../fixtures/graphql/snippets.json | 11 ++++++--- test/integration/insert_snippet.test.js | 2 +- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/gitlab/gitlab_new_service.test.ts b/src/gitlab/gitlab_new_service.test.ts index 1db99da..9f69bf8 100644 --- a/src/gitlab/gitlab_new_service.test.ts +++ b/src/gitlab/gitlab_new_service.test.ts @@ -1,8 +1,13 @@ import { GraphQLClient } from 'graphql-request'; +import crossFetch from 'cross-fetch'; import { GitLabNewService } from './gitlab_new_service'; +import * as snippetsResponse from '../../test/integration/fixtures/graphql/snippets.json'; jest.mock('graphql-request'); jest.mock('../services/token_service'); +jest.mock('cross-fetch'); + +const crossFetchCallArgument = () => (crossFetch as jest.Mock).mock.calls[0][0]; describe('gitlab_new_service', () => { describe('GraphQL client initialization', () => { @@ -17,4 +22,22 @@ describe('gitlab_new_service', () => { expect(GraphQLClient).toHaveBeenCalledWith(endpointUrl, expect.anything()); }); }); + + describe('getSnippetContent', () => { + it.each` + rawPath | branch + ${'/gitlab-org/gitlab-vscode-extension/-/snippets/111/raw/master/okr.md'} | ${'master'} + ${'/gitlab-org/gitlab-vscode-extension/-/snippets/111/raw/main/okr.md'} | ${'main'} + ${'/gitlab-org/security/gitlab-vscode-extension/-/snippets/222/raw/customBranch/folder/test1.js'} | ${'customBranch'} + `('parses the repository branch from blob rawPath', async ({ rawPath, branch }) => { + (crossFetch as jest.Mock).mockResolvedValue({ ok: true, text: () => '' }); + const service = new GitLabNewService('https://example.com'); + const snippet = snippetsResponse.project.snippets.nodes[0]; + const blob = snippet.blobs.nodes[0]; + + await service.getSnippetContent(snippet, { ...blob, rawPath }); + + expect(crossFetchCallArgument()).toMatch(`/files/${branch}/`); + }); + }); }); diff --git a/src/gitlab/gitlab_new_service.ts b/src/gitlab/gitlab_new_service.ts index c45e2df..8770ab7 100644 --- a/src/gitlab/gitlab_new_service.ts +++ b/src/gitlab/gitlab_new_service.ts @@ -190,11 +190,18 @@ export class GitLabNewService { })); } - // TODO change this method to use GraphQL when https://gitlab.com/gitlab-org/gitlab/-/issues/260316 is done + // TODO change this method to use GraphQL once the lowest supported GitLab version is 14.1.0 async getSnippetContent(snippet: GqlSnippet, blob: GqlBlob): Promise { + const getBranch = (rawPath: string) => { + // raw path example: "/gitlab-org/gitlab-vscode-extension/-/snippets/111/raw/master/okr.md" + const result = rawPath.match(/\/-\/snippets\/\d+\/raw\/([^/]+)\//); + assert(result, `The rawPath is malformed ${rawPath}`); + return result[1]; + }; const projectId = getRestIdFromGraphQLId(snippet.projectId); const snippetId = getRestIdFromGraphQLId(snippet.id); - const url = `${this.instanceUrl}/api/v4/projects/${projectId}/snippets/${snippetId}/files/master/${blob.path}/raw`; + const branch = getBranch(blob.rawPath); + const url = `${this.instanceUrl}/api/v4/projects/${projectId}/snippets/${snippetId}/files/${branch}/${blob.path}/raw`; const result = await crossFetch(url, this.fetchOptions); if (!result.ok) { throw new FetchError(`Fetching snippet from ${url} failed`, result); diff --git a/src/gitlab/graphql/get_snippets.ts b/src/gitlab/graphql/get_snippets.ts index 9d8380f..55d18a7 100644 --- a/src/gitlab/graphql/get_snippets.ts +++ b/src/gitlab/graphql/get_snippets.ts @@ -14,6 +14,7 @@ export const queryGetSnippets = gql` nodes { name path + rawPath } } } @@ -29,6 +30,7 @@ export interface GetSnippetsQueryOptions { export interface GqlBlob { name: string; path: string; + rawPath: string; } export interface GqlSnippet { diff --git a/test/integration/fixtures/graphql/snippets.json b/test/integration/fixtures/graphql/snippets.json index fff3459..dc6e2ca 100644 --- a/test/integration/fixtures/graphql/snippets.json +++ b/test/integration/fixtures/graphql/snippets.json @@ -5,30 +5,35 @@ "nodes": [ { "id": "gid://gitlab/ProjectSnippet/111", + "projectId": "gid://gitlab/Project/278964", "title": "Test snippet", "description": "test description", "blobs": { "nodes": [ { "name": "test.js", - "path": "test.js" + "path": "test.js", + "rawPath": "/gitlab-org/gitlab-vscode-extension/-/snippets/111/raw/master/test.js" } ] } }, { "id": "gid://gitlab/ProjectSnippet/222", + "projectId": "gid://gitlab/Project/278964", "title": "Test snippet", "description": "test description", "blobs": { "nodes": [ { "name": "test1.js", - "path": "test1.js" + "path": "test1.js", + "rawPath": "/gitlab-org/gitlab-vscode-extension/-/snippets/222/raw/main/test1.js" }, { "name": "test2.js", - "path": "test2.js" + "path": "test2.js", + "rawPath": "/gitlab-org/gitlab-vscode-extension/-/snippets/222/raw/main/test2.js" } ] } diff --git a/test/integration/insert_snippet.test.js b/test/integration/insert_snippet.test.js index c08a9bc..dc39d43 100644 --- a/test/integration/insert_snippet.test.js +++ b/test/integration/insert_snippet.test.js @@ -28,7 +28,7 @@ describe('Insert snippet', async () => { 'snippet content', ), createTextEndpoint( - '/projects/278964/snippets/222/files/master/test2.js/raw', + '/projects/278964/snippets/222/files/main/test2.js/raw', 'second blob content', ), graphql.query('GetSnippets', (req, res, ctx) => { -- GitLab