From b5de6111e277a2e38db7aa46128918c0b2badc05 Mon Sep 17 00:00:00 2001 From: ivan katliarchuk Date: Sun, 2 Oct 2022 07:33:23 +0100 Subject: [PATCH 1/3] fix: gitlab api --- package.json | 1 - source/dsl/GitLabDSL.ts | 14 -------------- source/platforms/gitlab/GitLabAPI.ts | 8 ++++---- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 2ab6748ae..7c79d0830 100644 --- a/package.json +++ b/package.json @@ -149,7 +149,6 @@ "fast-json-patch": "^3.0.0-1", "get-stdin": "^6.0.0", "@gitbeaker/node": "^21.3.0", - "gitlab": "^10.0.1", "http-proxy-agent": "^5.0.0", "https-proxy-agent": "^5.0.1", "hyperlinker": "^1.0.0", diff --git a/source/dsl/GitLabDSL.ts b/source/dsl/GitLabDSL.ts index 47c6f253c..6f056e05c 100644 --- a/source/dsl/GitLabDSL.ts +++ b/source/dsl/GitLabDSL.ts @@ -1,5 +1,4 @@ // Please don't have includes in here that aren't inside the DSL folder, or the d.ts/flow defs break - // TODO: extract out from BitBucket specifically, or create our own type import { Gitlab } from "@gitbeaker/node" import { RepoMetaData } from "./BitBucketServerDSL" @@ -247,19 +246,6 @@ export interface GitLabMRCommit { committed_date: string } -export interface GitLabRepositoryFile { - file_name: string - file_path: string - size: number - encoding: "base64" - content: string - content_sha256: string - ref: string - blob_id: string - commit_id: string - last_commit_id: string -} - export interface GitLabCommit { id: string short_id: string diff --git a/source/platforms/gitlab/GitLabAPI.ts b/source/platforms/gitlab/GitLabAPI.ts index 90452993c..1448e4fe8 100644 --- a/source/platforms/gitlab/GitLabAPI.ts +++ b/source/platforms/gitlab/GitLabAPI.ts @@ -9,12 +9,12 @@ import { GitLabMRCommit, GitLabNote, GitLabUserProfile, - GitLabRepositoryFile, GitLabRepositoryCompare, GitLabApproval, } from "../../dsl/GitLabDSL" import { Gitlab } from "@gitbeaker/node" +import { RepositoryFileSchema } from "@gitbeaker/core/dist/types/services/RepositoryFiles" import { Env } from "../../ci_source/ci_source" import { debug } from "../../debug" @@ -229,14 +229,14 @@ class GitLabAPI { try { this.d("getFileContents", projectId, path, ref) - const response = (await api.show(projectId, path, ref)) as GitLabRepositoryFile - const result: string = Buffer.from(response.content, "base64").toString() + const response = (await api.show(projectId, path, ref)) as RepositoryFileSchema + const result: string = Buffer.from(response.content, response.encoding).toString() this.d("getFileContents", result) return result } catch (e) { this.d("getFileContents", e) // GitHubAPI.fileContents returns "" when the file does not exist, keep it consistent across providers - if ((e as any).response.status === 404) { + if ((e as any).response.statusCode === 404) { return "" } throw e From a1af3e2d14b1a9f33bbf8160ff38a2326f26ca0f Mon Sep 17 00:00:00 2001 From: ivan katliarchuk Date: Sun, 2 Oct 2022 08:10:37 +0100 Subject: [PATCH 2/3] fix: added getFileContents tests --- .../gitlab/_tests/_gitlab_api.test.ts | 16 ++++++++++++++ .../_tests/fixtures/getFileContents.json | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 source/platforms/gitlab/_tests/fixtures/getFileContents.json diff --git a/source/platforms/gitlab/_tests/_gitlab_api.test.ts b/source/platforms/gitlab/_tests/_gitlab_api.test.ts index a681cabeb..687e103cc 100644 --- a/source/platforms/gitlab/_tests/_gitlab_api.test.ts +++ b/source/platforms/gitlab/_tests/_gitlab_api.test.ts @@ -166,4 +166,20 @@ describe("GitLab API", () => { const { response } = loadFixture("getCompareChanges") expect(result).toEqual(response.diffs) }) + + it("getFileContents", async () => { + const { nockDone } = await nockBack("getFileContents.json") + const parameters: { filePath: string; ref: string; expected: string }[] = [ + { + filePath: "Gemfile", + ref: "master", + expected: "source 'https://rubygems.org'", + }, + ] + for (let el in parameters) { + let result = await api.getFileContents(parameters[el].filePath, api.repoMetadata.repoSlug, parameters[el].ref) + expect(result).toContain(parameters[el].expected) + } + nockDone() + }) }) diff --git a/source/platforms/gitlab/_tests/fixtures/getFileContents.json b/source/platforms/gitlab/_tests/fixtures/getFileContents.json new file mode 100644 index 000000000..b3b327689 --- /dev/null +++ b/source/platforms/gitlab/_tests/fixtures/getFileContents.json @@ -0,0 +1,22 @@ +[ + { + "scope": "https://gitlab.com", + "method": "GET", + "path": "/api/v4/projects/gitlab-org%2Fgitlab-foss/repository/files/Gemfile?ref=master", + "body": "", + "status": 200, + "response": { + "file_name": "Gemfile", + "file_path": "Gemfile", + "size": 4989, + "encoding": "base64", + "content_sha256": "d1db2dff734c9a5f59b9e994b2f610317a0bdbdf938d4dc2797bb0bd384233f6", + "ref": "master", + "blob_id": "78b0a38bcaae68ca1bd5fc967c9901c854f3d9ab", + "commit_id": "86461e8c72db13d6d334fd107d38aa1b021d030e", + "last_commit_id": "a16398e10f87edd229a12be2f1fc87cd4a76f902", + "execute_filemode": false, + "content": "c291cmNlICdodHRwczovL3J1YnlnZW1zLm9yZycK" + } + } +] From 43c202b562b045b3588ae519dde9c1aae0e34851 Mon Sep 17 00:00:00 2001 From: ivan katliarchuk Date: Sun, 2 Oct 2022 08:22:49 +0100 Subject: [PATCH 3/3] fix: added getFileContents tests with and without file --- source/platforms/gitlab/_tests/_gitlab_api.test.ts | 5 +++++ .../platforms/gitlab/_tests/fixtures/getFileContents.json | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/source/platforms/gitlab/_tests/_gitlab_api.test.ts b/source/platforms/gitlab/_tests/_gitlab_api.test.ts index 687e103cc..5f6cd4011 100644 --- a/source/platforms/gitlab/_tests/_gitlab_api.test.ts +++ b/source/platforms/gitlab/_tests/_gitlab_api.test.ts @@ -175,6 +175,11 @@ describe("GitLab API", () => { ref: "master", expected: "source 'https://rubygems.org'", }, + { + filePath: "FileNotExist", + ref: "master", + expected: "", + }, ] for (let el in parameters) { let result = await api.getFileContents(parameters[el].filePath, api.repoMetadata.repoSlug, parameters[el].ref) diff --git a/source/platforms/gitlab/_tests/fixtures/getFileContents.json b/source/platforms/gitlab/_tests/fixtures/getFileContents.json index b3b327689..b5359893e 100644 --- a/source/platforms/gitlab/_tests/fixtures/getFileContents.json +++ b/source/platforms/gitlab/_tests/fixtures/getFileContents.json @@ -18,5 +18,13 @@ "execute_filemode": false, "content": "c291cmNlICdodHRwczovL3J1YnlnZW1zLm9yZycK" } + }, + { + "scope": "https://gitlab.com", + "method": "GET", + "path": "/api/v4/projects/gitlab-org%2Fgitlab-foss/repository/files/FileNotExist?ref=master", + "body": "", + "status": 404, + "response": {} } ]