From ac268aaf56ddf7e9c3580fe06ce3636f2fdb2195 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sat, 2 Feb 2019 12:01:14 -0500 Subject: [PATCH 1/3] Updates the usage of octokit to remove warnings --- package.json | 2 +- source/platforms/github/GitHubAPI.ts | 16 ++++++++-------- source/runner/jsonToDSL.ts | 17 ++++++++++++----- yarn.lock | 28 +++++++++++----------------- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 000981a48..148377f42 100644 --- a/package.json +++ b/package.json @@ -124,7 +124,7 @@ }, "dependencies": { "@babel/polyfill": "^7.2.5", - "@octokit/rest": "^16.3.0", + "@octokit/rest": "^16.14.1", "chalk": "^2.3.0", "commander": "^2.18.0", "debug": "^4.1.1", diff --git a/source/platforms/github/GitHubAPI.ts b/source/platforms/github/GitHubAPI.ts index 63c2fe03f..39c2ab1b3 100644 --- a/source/platforms/github/GitHubAPI.ts +++ b/source/platforms/github/GitHubAPI.ts @@ -42,21 +42,21 @@ export class GitHubAPI { * but for now that's just a refactor someone can try. */ getExternalAPI = (accessTokenForApp?: string): GitHubNodeAPI => { + // A token should have been set by this point + 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 } = { debug: !!process.env.LOG_FETCH_REQUESTS, baseUrl: host, - headers: { - ...this.additionalHeaders, - }, + auth: `token ${token}`, } - // A token should have been set by this point - const token = accessTokenForApp || this.token! - const api = new GitHubNodeAPI(options) - api.authenticate({ type: "token", token: token }) + if (this.additionalHeaders) { + options.headers = this.additionalHeaders + } - return api + return new GitHubNodeAPI(options) } /** diff --git a/source/runner/jsonToDSL.ts b/source/runner/jsonToDSL.ts index 98c02efba..4d2da9441 100644 --- a/source/runner/jsonToDSL.ts +++ b/source/runner/jsonToDSL.ts @@ -60,14 +60,21 @@ const apiForDSL = (dsl: DangerDSLJSONType): OctoKit | BitBucketServerAPI => { const options: OctoKit.Options & { debug: boolean } = { debug: !!process.env.LOG_FETCH_REQUESTS, baseUrl: dsl.settings.github.baseURL, - headers: { - ...dsl.settings.github.additionalHeaders, - }, } - const api = new OctoKit(options) + // Peril will need changes for this + if ( + dsl.settings.github && + dsl.settings.github.additionalHeaders && + Object.keys(dsl.settings.github.additionalHeaders).length + ) { + options.headers = dsl.settings.github.additionalHeaders + } + if (dsl.settings.github && dsl.settings.github.accessToken) { - api.authenticate({ type: "token", token: dsl.settings.github.accessToken }) + options.auth = `token ${dsl.settings.github.accessToken}` } + + const api = new OctoKit(options) return api } diff --git a/yarn.lock b/yarn.lock index 993da3ea5..fa6ed1ba4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -693,7 +693,7 @@ resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.2.tgz#54c5a964462be3d4d78af631363c18d6fa91ac26" integrity sha512-yprFYuno9FtNsSHVlSWd+nRlmGoAbqbeCwOryP6sC/zoCjhpArcRMYp19EvpSUSizJAlsXEwJv+wcWS9XaXdMw== -"@octokit/endpoint@^3.0.0": +"@octokit/endpoint@^3.1.1": version "3.1.1" resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-3.1.1.tgz#ede9afefaa4d6b7584169e12346425c6fbb45cc3" integrity sha512-KPkoTvKwCTetu/UqonLs1pfwFO5HAqTv/Ksp9y4NAg//ZgUCpvJsT4Hrst85uEzJvkB8+LxKyR4Bfv2X8O4cmQ== @@ -703,12 +703,12 @@ universal-user-agent "^2.0.1" url-template "^2.0.8" -"@octokit/request@2.2.0": - version "2.2.0" - resolved "https://registry.yarnpkg.com/@octokit/request/-/request-2.2.0.tgz#f4b2d1ad7c4c8a0b148193610c912046961f8be5" - integrity sha512-4P9EbwKZ4xfyupVMb3KVuHmM+aO2fye3nufjGKz/qDssvdJj9Rlx44O0FdFvUp4kIzToy3AHLTOulEIDAL+dpg== +"@octokit/request@2.3.0": + version "2.3.0" + resolved "https://registry.yarnpkg.com/@octokit/request/-/request-2.3.0.tgz#da2672308bcf0b9376ef66f51bddbe5eb87cc00a" + integrity sha512-5YRqYNZOAaL7+nt7w3Scp6Sz4P2g7wKFP9npx1xdExMomk8/M/ICXVLYVam2wzxeY0cIc6wcKpjC5KI4jiNbGw== dependencies: - "@octokit/endpoint" "^3.0.0" + "@octokit/endpoint" "^3.1.1" is-plain-object "^2.0.4" node-fetch "^2.3.0" universal-user-agent "^2.0.1" @@ -728,16 +728,15 @@ node-fetch "^2.1.1" url-template "^2.0.8" -"@octokit/rest@^16.3.0": - version "16.3.0" - resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.3.0.tgz#98a24a3334312a87fff8a2a54c1dca4d0900d54d" - integrity sha512-u0HkROLB0nOSfJhkF5FKMg6I12m6cN5S3S73Lwtfgrs9u4LhgUCZN2hC2KDyIaT7nhvNe9Kx0PgxhhD6li6QsA== +"@octokit/rest@^16.14.1": + version "16.14.1" + resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.14.1.tgz#d7c65be7b8c78c889b121619422e7cf5335b1239" + integrity sha512-etBdt7br4aSUmgTnaO3pRodzVLhwGQTqUbMrEIdFBGjWlJYfoTa9LieIAtdig6UaDs6InPk0isguPZxU7vCTlQ== dependencies: - "@octokit/request" "2.2.0" + "@octokit/request" "2.3.0" before-after-hook "^1.2.0" btoa-lite "^1.0.0" lodash.get "^4.4.2" - lodash.pick "^4.4.0" lodash.set "^4.3.2" lodash.uniq "^4.5.0" octokit-pagination-methods "^1.1.0" @@ -5852,11 +5851,6 @@ lodash.once@^4.0.0: resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac" integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w= -lodash.pick@^4.4.0: - version "4.4.0" - resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3" - integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM= - lodash.set@^4.3.2: version "4.3.2" resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" From f5845eba7caae023da1f25686697a1628baf2a76 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sat, 2 Feb 2019 12:25:42 -0500 Subject: [PATCH 2/3] Fix a type error in tests --- source/platforms/github/_tests/_github_git.test.ts | 4 ++-- source/platforms/github/_tests/fixturedGitHubDSL.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/platforms/github/_tests/_github_git.test.ts b/source/platforms/github/_tests/_github_git.test.ts index bdd97f54d..994ba464d 100644 --- a/source/platforms/github/_tests/_github_git.test.ts +++ b/source/platforms/github/_tests/_github_git.test.ts @@ -62,9 +62,9 @@ describe("the dangerfile gitDSL", async () => { api.getPullRequestCommits = await requestWithFixturedJSON("github_commits.json") api.getFileContents = async (_path, _repoSlug, ref) => (await requestWithFixturedJSON(`static_file.${ref}.json`))() - // nodeGitHubAPI = new NodeGitHub() - nodeGitHubAPI.repos.getContents = async ({ ref }) => (await requestWithFixturedJSON(`static_file.${ref}.json`))() + const mockContents = async ({ ref }: any) => (await requestWithFixturedJSON(`static_file.${ref}.json`))() + nodeGitHubAPI.repos.getContents = mockContents as any gitJSONDSL = await github.getPlatformGitRepresentation() const githubJSONDSL = await github.getPlatformReviewDSLRepresentation() diff --git a/source/platforms/github/_tests/fixturedGitHubDSL.ts b/source/platforms/github/_tests/fixturedGitHubDSL.ts index 50cf85915..794a8f389 100644 --- a/source/platforms/github/_tests/fixturedGitHubDSL.ts +++ b/source/platforms/github/_tests/fixturedGitHubDSL.ts @@ -52,9 +52,9 @@ export const fixturedGitHubDSL = async (): Promise => { api.getPullRequestCommits = await requestWithFixturedJSON("github_commits.json") api.getFileContents = async (_path, _repoSlug, ref) => (await requestWithFixturedJSON(`static_file.${ref}.json`))() - // nodeGitHubAPI = new NodeGitHub() - nodeGitHubAPI.repos.getContents = async ({ ref }) => (await requestWithFixturedJSON(`static_file.${ref}.json`))() + const mockContents = async ({ ref }: any) => (await requestWithFixturedJSON(`static_file.${ref}.json`))() + nodeGitHubAPI.repos.getContents = mockContents as any gitJSONDSL = await github.getPlatformGitRepresentation() const githubJSONDSL = await github.getPlatformReviewDSLRepresentation() From 5fed3e3bbb200b2fab98b578bc2befd5bd74694b Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sat, 2 Feb 2019 12:33:20 -0500 Subject: [PATCH 3/3] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81525253b..94f3c1dd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Remove dead links to example danger files. - [@stevemoser][] - Allow danger-pr to work also on Bitbucket Server - [@f-meloni][] - Fix for nulls in modified_files - [@orta][] +- Use new APIs in OctoKit - [@orta][] # 7.0.4