Skip to content

Commit

Permalink
Upgrade new Buffer to Buffer.from (node v10+)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbartho committed Feb 1, 2022
1 parent 202d727 commit e22faa1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
8 changes: 4 additions & 4 deletions source/platforms/bitbucket_cloud/BitBucketCloudAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export class BitBucketCloudAPI {
// API implementation
private api = async (url: string, headers: any = {}, body: any = {}, method: string, suppressErrors?: boolean) => {
if (this.credentials.type === "PASSWORD") {
headers["Authorization"] = `Basic ${new Buffer(
headers["Authorization"] = `Basic ${Buffer.from(
this.credentials.username + ":" + this.credentials.password
).toString("base64")}`
} else {
Expand All @@ -324,9 +324,9 @@ export class BitBucketCloudAPI {
this.oauthURL,
{
...headers,
Authorization: `Basic ${new Buffer(this.credentials.oauthKey + ":" + this.credentials.oauthSecret).toString(
"base64"
)}`,
Authorization: `Basic ${Buffer.from(
this.credentials.oauthKey + ":" + this.credentials.oauthSecret
).toString("base64")}`,
"Content-Type": "application/x-www-form-urlencoded",
},
params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("API testing - BitBucket Cloud", () => {
let textResult: string
const expectedJSONHeaders = {
"Content-Type": "application/json",
Authorization: `Basic ${new Buffer("username:password").toString("base64")}`,
Authorization: `Basic ${Buffer.from("username:password").toString("base64")}`,
}

function APIFactory(username: string, password: string, uuid: string) {
Expand Down Expand Up @@ -354,7 +354,7 @@ describe("API testing - BitBucket Cloud", () => {

const expectedAuthHeaders = {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${new Buffer("superOAUTHKey:superSecretOAUTH").toString("base64")}`,
Authorization: `Basic ${Buffer.from("superOAUTHKey:superSecretOAUTH").toString("base64")}`,
}
const expectedOAUTHRequestHeaders = {
"Content-Type": "application/json",
Expand Down
2 changes: 1 addition & 1 deletion source/platforms/bitbucket_server/BitBucketServerAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export class BitBucketServerAPI implements BitBucketServerAPIDSL {
if (this.repoCredentials.token) {
headers["Authorization"] = `Bearer ${this.repoCredentials.token}`
} else if (this.repoCredentials.password) {
headers["Authorization"] = `Basic ${new Buffer(
headers["Authorization"] = `Basic ${Buffer.from(
this.repoCredentials.username + ":" + this.repoCredentials.password
).toString("base64")}`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("API testing - BitBucket Server", () => {
const host = "http://localhost:7990"
const expectedJSONHeaders = {
"Content-Type": "application/json",
Authorization: `Basic ${new Buffer("username:password").toString("base64")}`,
Authorization: `Basic ${Buffer.from("username:password").toString("base64")}`,
}

function APIFactory({ password, token }: { password?: string; token?: string }) {
Expand Down
6 changes: 3 additions & 3 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class GitHubAPI {
const token = accessTokenForApp || this.token!

const host = process.env["DANGER_GITHUB_API_BASE_URL"] || process.env["GITHUB_URL"] || undefined
const options: GitHubNodeAPI.Options & { debug: boolean } = {
const options: ConstructorParameters<typeof GitHubNodeAPI>[0] & { debug: boolean } = {
debug: !!process.env.LOG_FETCH_REQUESTS,
baseUrl: host,
auth: `token ${token}`,
Expand Down Expand Up @@ -76,7 +76,7 @@ export class GitHubAPI {
}

const data = await this.getFileContents(path, repoSlug, ref)
const buffer = new Buffer(data.content, "base64")
const buffer = Buffer.from(data.content, "base64")
return buffer.toString()
}

Expand Down Expand Up @@ -421,7 +421,7 @@ export class GitHubAPI {
return res.ok
} catch (error) {
this.d(`Posting a status to: ${statusURL} failed, this is the response:`)
this.d(error.message)
this.d((error && (error as Error).message) || error)
}
}

Expand Down
41 changes: 32 additions & 9 deletions source/platforms/github/GitHubUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { basename } from "path"
import { components as OctokitOpenApiTypes } from "@octokit/openapi-types"
import { filepathContentsMapToUpdateGitHubBranch, BranchCreationConfig } from "memfs-or-file-map-to-github-branch"

import { sentence, href } from "../../runner/DangerUtils"
import { GitHubPRDSL, GitHubUtilsDSL } from "./../../dsl/GitHubDSL"
import { debug } from "../../debug"
import { filepathContentsMapToUpdateGitHubBranch, BranchCreationConfig } from "memfs-or-file-map-to-github-branch"

export type GetContentResponseData =
| OctokitOpenApiTypes["schemas"]["content-file"]
| OctokitOpenApiTypes["schemas"]["content-symlink"]
| OctokitOpenApiTypes["schemas"]["content-submodule"]
export function isFileContents(
response: GetContentResponseData
): response is OctokitOpenApiTypes["schemas"]["content-file"] {
return response.type === "file"
}

const d = debug("GitHub::Utils")

Expand Down Expand Up @@ -66,14 +78,18 @@ export const fileContentsGenerator = (
owner: repoSlug.split("/")[0],
}
try {
// response of getContents() can be one of 4 things. We are interested in file responses only
// https://developer.github.com/v3/repos/contents/#get-contents
const response = await api.repos.getContents(opts)
// response of getContent() can be one of 4 things. We are interested in file responses only
// https://docs.github.com/en/rest/reference/repos#get-repository-content
const response = await api.repos.getContent(opts)
if (!response || !response.data) {
return ""
}
if (Array.isArray(response.data)) {
// If we get an array, we have a directory
return ""
}
if (response && response.data && response.data.content) {
const buffer = new Buffer(response.data.content, response.data.encoding)
if (isFileContents(response.data) && response.data.content) {
const buffer = Buffer.from(response.data.content, response.data.encoding)
return buffer.toString()
} else {
return ""
Expand All @@ -94,7 +110,7 @@ export const createUpdatedIssueWithIDGenerator = (api: GitHub) => async (
// by label
const uniqueHeader = `Danger-Issue-ID-${id.replace(/ /g, "_")}`
const q = `user:${settings.owner} repo:${settings.repo} ${uniqueHeader}`
const { data: searchResults } = await api.search.issues({ q })
const { data: searchResults } = await api.search.issuesAndPullRequests({ q })
d(`Got ${searchResults.total_count} for ${uniqueHeader}`)

const body = `${content}\n\n${uniqueHeader}`
Expand All @@ -104,7 +120,14 @@ export const createUpdatedIssueWithIDGenerator = (api: GitHub) => async (
if (searchResults.total_count > 0 && searchResults.items[0]) {
const issueToUpdate = searchResults.items[0]
d(`Found: ${issueToUpdate}`)
const { data: issue } = await api.issues.update({ body, owner, repo, title, number: issueToUpdate.number, state })
const { data: issue } = await api.issues.update({
body,
owner,
repo,
title,
issue_number: issueToUpdate.number,
state,
})
return issue.html_url
} else {
const { data: issue } = await api.issues.create({ body, owner, repo, title })
Expand Down Expand Up @@ -161,7 +184,7 @@ export const createOrUpdatePR = (pr: GitHubPRDSL | undefined, api: GitHub) => as
if (existingPR) {
d("Updating existing PR")
return await api.pulls.update({
number: existingPR.number,
pull_number: existingPR.number,
base: config.baseBranch,
owner,
repo,
Expand Down

0 comments on commit e22faa1

Please sign in to comment.