提交 54d4abd8 编写于 作者: T Tomas Vik

Merge branch '397-fix-inserting-snippets' into 'main'

fix: inserting snippets not working for newly created snippets

See merge request gitlab-org/gitlab-vscode-extension!301
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}/`);
});
});
});
......@@ -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<string> {
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);
......
......@@ -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 {
......
......@@ -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"
}
]
}
......
......@@ -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) => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册