diff --git a/src/endpoints-to-methods.ts b/src/endpoints-to-methods.ts index 9b0f6f345..0ceaac359 100644 --- a/src/endpoints-to-methods.ts +++ b/src/endpoints-to-methods.ts @@ -88,25 +88,28 @@ function decorate( octokit.log.warn(decorations.deprecated); } - // There currently are no renamed parameters - // if (decorations.renamedParameters) { - // // @ts-ignore https://github.com/microsoft/TypeScript/issues/25488 - // const options = requestWithDefaults.endpoint.merge(...args); - // for (const [name, alias] of Object.entries( - // decorations.renamedParameters - // )) { - // if (name in options) { - // octokit.log.warn( - // `"${name}" parameter is deprecated for "octokit.${scope}.${methodName}()". Use "${alias}" instead` - // ); - // if (!(alias in options)) { - // options[alias] = options[name]; - // } - // delete options[name]; - // } - // } - // return requestWithDefaults(options); - // } + if (decorations.renamedParameters) { + // @ts-ignore https://github.com/microsoft/TypeScript/issues/25488 + const options = requestWithDefaults.endpoint.merge(...args); + + for (const [name, alias] of Object.entries( + decorations.renamedParameters + )) { + // There is currently no deprecated parameter that is optional, + // so we never hit the else branch below at this point. + /* istanbul ignore else */ + if (name in options) { + octokit.log.warn( + `"${name}" parameter is deprecated for "octokit.${scope}.${methodName}()". Use "${alias}" instead` + ); + if (!(alias in options)) { + options[alias] = options[name]; + } + delete options[name]; + } + } + return requestWithDefaults(options); + } // @ts-ignore https://github.com/microsoft/TypeScript/issues/25488 return requestWithDefaults(...args); diff --git a/test/deprecations.test.ts b/test/deprecations.test.ts index 3b5259fbc..f7bf3038c 100644 --- a/test/deprecations.test.ts +++ b/test/deprecations.test.ts @@ -52,72 +52,113 @@ describe("Deprecations", () => { expect(warnCalledCount).toEqual(1); }); - // it("deprecated parameter", async () => { - // const mock = fetchMock - // .sandbox() - // .getOnce("path:/repos/octocat/hello-world/commits/sha123", { - // ok: true - // }); - // const MyOctokit = Octokit.plugin(restEndpointMethods); - // let warnCalledCount = 0; - // const octokit = new MyOctokit({ - // request: { - // fetch: mock - // }, - // log: { - // warn: (deprecation: Error) => { - // warnCalledCount++; - // expect(deprecation).toMatch( - // '"commit_sha" parameter is deprecated for "octokit.repos.getCommit()". Use "ref" instead' - // ); - // } - // } - // }); - // // See https://developer.github.com/v3/issues/comments/#create-a-comment - // const { data } = await octokit.repos.getCommit({ - // owner: "octocat", - // repo: "hello-world", - // commit_sha: "sha123" - // }); - // expect(data).toStrictEqual({ ok: true }); - // expect(warnCalledCount).toEqual(1); - // }); - // it("deprecated parameter + new parameter", async () => { - // const mock = fetchMock.sandbox().post( - // "path:/repos/octocat/hello-world/issues/456/comments", - // { - // ok: true - // }, - // { - // body: { - // body: "Hello there!" - // } - // } - // ); - // const MyOctokit = Octokit.plugin(restEndpointMethods); - // let warnCalledCount = 0; - // const octokit = new MyOctokit({ - // request: { - // fetch: mock - // }, - // log: { - // warn: (deprecation: Error) => { - // warnCalledCount++; - // expect(deprecation).toMatch( - // '"number" parameter is deprecated for "octokit.issues.createComment()". Use "issue_number" instead' - // ); - // } - // } - // }); - // // See https://developer.github.com/v3/issues/comments/#create-a-comment - // const { data } = await octokit.issues.createComment({ - // owner: "octocat", - // repo: "hello-world", - // number: 123, - // issue_number: 456, - // body: "Hello there!" - // }); - // expect(data).toStrictEqual({ ok: true }); - // expect(warnCalledCount).toEqual(1); - // }); + it("deprecated parameter", async () => { + const mock = fetchMock + .sandbox() + .putOnce("path:/repos/octocat/hello-world/actions/secrets/MY_SECRET", { + ok: true, + }); + const MyOctokit = Octokit.plugin(restEndpointMethods); + + let warnCalledCount = 0; + const octokit = new MyOctokit({ + request: { + fetch: mock, + }, + log: { + warn: (deprecation: Error) => { + warnCalledCount++; + expect(deprecation).toMatch( + '"name" parameter is deprecated for "octokit.actions.createOrUpdateRepoSecret()". Use "secret_name" instead' + ); + }, + }, + }); + // See https://developer.github.com/v3/actions/secrets/#create-or-update-a-repository-secret + // The `:secret_name` URL parameter was `:name` until May 14, 2020 + // @ts-ignore + const { data } = await octokit.actions.createOrUpdateRepoSecret({ + owner: "octocat", + repo: "hello-world", + name: "MY_SECRET", + encrypted_value: "encrypted value 123", + key_id: "key id 123", + }); + expect(data).toStrictEqual({ ok: true }); + expect(warnCalledCount).toEqual(1); + }); + + it("deprecated parameter + new parameter", async () => { + const mock = fetchMock + .sandbox() + .putOnce("path:/repos/octocat/hello-world/actions/secrets/MY_SECRET2", { + ok: true, + }); + const MyOctokit = Octokit.plugin(restEndpointMethods); + + let warnCalledCount = 0; + const octokit = new MyOctokit({ + request: { + fetch: mock, + }, + log: { + warn: (deprecation: Error) => { + warnCalledCount++; + expect(deprecation).toMatch( + '"name" parameter is deprecated for "octokit.actions.createOrUpdateRepoSecret()". Use "secret_name" instead' + ); + }, + }, + }); + // See https://developer.github.com/v3/actions/secrets/#create-or-update-a-repository-secret + // The `:secret_name` URL parameter was `:name` until May 14, 2020 + // @ts-ignore + const { data } = await octokit.actions.createOrUpdateRepoSecret({ + owner: "octocat", + repo: "hello-world", + name: "MY_SECRET1", + secret_name: "MY_SECRET2", + encrypted_value: "encrypted value 123", + key_id: "key id 123", + }); + expect(data).toStrictEqual({ ok: true }); + expect(warnCalledCount).toEqual(1); + }); + + it("deprecated method + deprecated parameter", async () => { + const mock = fetchMock + .sandbox() + .putOnce("path:/repos/octocat/hello-world/actions/secrets/MY_SECRET", { + ok: true, + }); + const MyOctokit = Octokit.plugin(restEndpointMethods); + + const deprecations: string[] = []; + const octokit = new MyOctokit({ + request: { + fetch: mock, + }, + log: { + warn: (deprecation: Error) => { + deprecations.push(deprecation.toString()); + }, + }, + }); + // See https://developer.github.com/v3/actions/secrets/#create-or-update-a-repository-secret + // The `:secret_name` URL parameter was `:name` until May 14, 2020 + // @ts-ignore + const { data } = await octokit.actions.createOrUpdateSecretForRepo({ + owner: "octocat", + repo: "hello-world", + name: "MY_SECRET", + encrypted_value: "encrypted value 123", + key_id: "key id 123", + }); + expect(data).toStrictEqual({ ok: true }); + expect(deprecations.length).toEqual(2); + expect(deprecations.sort()).toStrictEqual([ + '"name" parameter is deprecated for "octokit.actions.createOrUpdateSecretForRepo()". Use "secret_name" instead', + "octokit.actions.createOrUpdateSecretForRepo() has been renamed to octokit.actions.createOrUpdateRepoSecret()", + ]); + }); });