Skip to content

Commit

Permalink
Fix: BitBucketServer does not encode filepath
Browse files Browse the repository at this point in the history
Having files under folders with white space will fail get content of file as it end in node-fetch error
TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
  • Loading branch information
Jan Machala committed May 7, 2020
1 parent 6c90cce commit 17ce5d5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ x

<!-- Your comment below this -->

- Fix danger failure on getting diff for files with spaces in file path [@HonzaMac]

<!-- Your comment above this -->

# 10.2.0
Expand Down
6 changes: 4 additions & 2 deletions source/platforms/bitbucket_server/BitBucketServerAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ export class BitBucketServerAPI implements BitBucketServerAPIDSL {

getStructuredDiffForFile = async (base: string, head: string, filename: string): Promise<BitBucketServerDiff[]> => {
const { repoSlug } = this.repoMetadata
const path = `rest/api/1.0/${repoSlug}/compare/diff/${filename}?withComments=false&from=${head}&to=${base}`
const path = `rest/api/1.0/${repoSlug}/compare/diff/${encodeURI(
filename
)}?withComments=false&from=${head}&to=${base}`
const res = await this.get(path)
throwIfNotOk(res)
return (await res.json()).diffs
Expand Down Expand Up @@ -241,7 +243,7 @@ export class BitBucketServerAPI implements BitBucketServerAPIDSL {

// The last two are "optional" in the protocol, but not really optional WRT the BBSAPI
getFileContents = async (filePath: string, repoSlug?: string, refspec?: string) => {
const path = `rest/api/1.0/${repoSlug}/` + `raw/${filePath}` + `?at=${refspec}`
const path = `rest/api/1.0/${repoSlug}/` + `raw/${encodeURI(filePath)}` + `?at=${refspec}`
const res = await this.get(path, undefined, true)
if (res.status === 404) {
return ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ describe("API testing - BitBucket Server", () => {

it("getStructuredDiffForFile", async () => {
jsonResult = () => ({ diffs: ["diff"] })
const result = await api.getStructuredDiffForFile("BASE", "HEAD", "filename.txt")
const result = await api.getStructuredDiffForFile("BASE", "HEAD", "path with space/filename.txt")

expect(api.fetch).toHaveBeenCalledWith(
`${host}/rest/api/1.0/projects/FOO/repos/BAR/compare/diff/` +
`filename.txt` +
`path%20with%20space/filename.txt` +
`?withComments=false&from=HEAD&to=BASE`,
{ method: "GET", body: null, headers: expectedJSONHeaders },
undefined
Expand Down Expand Up @@ -272,10 +272,10 @@ describe("API testing - BitBucket Server", () => {

it("getFileContents", async () => {
textResult = "contents..."
const result = await api.getFileContents("path/to/foo.txt", "projects/FOO/repos/BAR", "master")
const result = await api.getFileContents("path/folder with space/foo.txt", "projects/FOO/repos/BAR", "master")

expect(api.fetch).toHaveBeenCalledWith(
`${host}/rest/api/1.0/projects/FOO/repos/BAR/raw/path/to/foo.txt?at=master`,
`${host}/rest/api/1.0/projects/FOO/repos/BAR/raw/path/folder%20with%20space/foo.txt?at=master`,
{ method: "GET", body: null, headers: expectedJSONHeaders },
true
)
Expand Down

0 comments on commit 17ce5d5

Please sign in to comment.