Skip to content

Commit

Permalink
Merge pull request #1320 from ivankatliarchuk/fix/getFileContents
Browse files Browse the repository at this point in the history
fix: gitlab api `diffForFile`
  • Loading branch information
orta committed Oct 4, 2022
2 parents c92a32e + 43c202b commit 9539b9d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
14 changes: 0 additions & 14 deletions 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"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions source/platforms/gitlab/GitLabAPI.ts
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions source/platforms/gitlab/_tests/_gitlab_api.test.ts
Expand Up @@ -166,4 +166,25 @@ 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'",
},
{
filePath: "FileNotExist",
ref: "master",
expected: "",
},
]
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()
})
})
30 changes: 30 additions & 0 deletions source/platforms/gitlab/_tests/fixtures/getFileContents.json
@@ -0,0 +1,30 @@
[
{
"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"
}
},
{
"scope": "https://gitlab.com",
"method": "GET",
"path": "/api/v4/projects/gitlab-org%2Fgitlab-foss/repository/files/FileNotExist?ref=master",
"body": "",
"status": 404,
"response": {}
}
]

0 comments on commit 9539b9d

Please sign in to comment.