Skip to content

Commit 7248b89

Browse files
authored
fix: handle deprecated parameters (#87)
1 parent 5807be8 commit 7248b89

File tree

2 files changed

+131
-87
lines changed

2 files changed

+131
-87
lines changed

src/endpoints-to-methods.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,28 @@ function decorate(
8888
octokit.log.warn(decorations.deprecated);
8989
}
9090

91-
// There currently are no renamed parameters
92-
// if (decorations.renamedParameters) {
93-
// // @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
94-
// const options = requestWithDefaults.endpoint.merge(...args);
95-
// for (const [name, alias] of Object.entries(
96-
// decorations.renamedParameters
97-
// )) {
98-
// if (name in options) {
99-
// octokit.log.warn(
100-
// `"${name}" parameter is deprecated for "octokit.${scope}.${methodName}()". Use "${alias}" instead`
101-
// );
102-
// if (!(alias in options)) {
103-
// options[alias] = options[name];
104-
// }
105-
// delete options[name];
106-
// }
107-
// }
108-
// return requestWithDefaults(options);
109-
// }
91+
if (decorations.renamedParameters) {
92+
// @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
93+
const options = requestWithDefaults.endpoint.merge(...args);
94+
95+
for (const [name, alias] of Object.entries(
96+
decorations.renamedParameters
97+
)) {
98+
// There is currently no deprecated parameter that is optional,
99+
// so we never hit the else branch below at this point.
100+
/* istanbul ignore else */
101+
if (name in options) {
102+
octokit.log.warn(
103+
`"${name}" parameter is deprecated for "octokit.${scope}.${methodName}()". Use "${alias}" instead`
104+
);
105+
if (!(alias in options)) {
106+
options[alias] = options[name];
107+
}
108+
delete options[name];
109+
}
110+
}
111+
return requestWithDefaults(options);
112+
}
110113

111114
// @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
112115
return requestWithDefaults(...args);

test/deprecations.test.ts

Lines changed: 109 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -52,72 +52,113 @@ describe("Deprecations", () => {
5252
expect(warnCalledCount).toEqual(1);
5353
});
5454

55-
// it("deprecated parameter", async () => {
56-
// const mock = fetchMock
57-
// .sandbox()
58-
// .getOnce("path:/repos/octocat/hello-world/commits/sha123", {
59-
// ok: true
60-
// });
61-
// const MyOctokit = Octokit.plugin(restEndpointMethods);
62-
// let warnCalledCount = 0;
63-
// const octokit = new MyOctokit({
64-
// request: {
65-
// fetch: mock
66-
// },
67-
// log: {
68-
// warn: (deprecation: Error) => {
69-
// warnCalledCount++;
70-
// expect(deprecation).toMatch(
71-
// '"commit_sha" parameter is deprecated for "octokit.repos.getCommit()". Use "ref" instead'
72-
// );
73-
// }
74-
// }
75-
// });
76-
// // See https://developer.github.com/v3/issues/comments/#create-a-comment
77-
// const { data } = await octokit.repos.getCommit({
78-
// owner: "octocat",
79-
// repo: "hello-world",
80-
// commit_sha: "sha123"
81-
// });
82-
// expect(data).toStrictEqual({ ok: true });
83-
// expect(warnCalledCount).toEqual(1);
84-
// });
85-
// it("deprecated parameter + new parameter", async () => {
86-
// const mock = fetchMock.sandbox().post(
87-
// "path:/repos/octocat/hello-world/issues/456/comments",
88-
// {
89-
// ok: true
90-
// },
91-
// {
92-
// body: {
93-
// body: "Hello there!"
94-
// }
95-
// }
96-
// );
97-
// const MyOctokit = Octokit.plugin(restEndpointMethods);
98-
// let warnCalledCount = 0;
99-
// const octokit = new MyOctokit({
100-
// request: {
101-
// fetch: mock
102-
// },
103-
// log: {
104-
// warn: (deprecation: Error) => {
105-
// warnCalledCount++;
106-
// expect(deprecation).toMatch(
107-
// '"number" parameter is deprecated for "octokit.issues.createComment()". Use "issue_number" instead'
108-
// );
109-
// }
110-
// }
111-
// });
112-
// // See https://developer.github.com/v3/issues/comments/#create-a-comment
113-
// const { data } = await octokit.issues.createComment({
114-
// owner: "octocat",
115-
// repo: "hello-world",
116-
// number: 123,
117-
// issue_number: 456,
118-
// body: "Hello there!"
119-
// });
120-
// expect(data).toStrictEqual({ ok: true });
121-
// expect(warnCalledCount).toEqual(1);
122-
// });
55+
it("deprecated parameter", async () => {
56+
const mock = fetchMock
57+
.sandbox()
58+
.putOnce("path:/repos/octocat/hello-world/actions/secrets/MY_SECRET", {
59+
ok: true,
60+
});
61+
const MyOctokit = Octokit.plugin(restEndpointMethods);
62+
63+
let warnCalledCount = 0;
64+
const octokit = new MyOctokit({
65+
request: {
66+
fetch: mock,
67+
},
68+
log: {
69+
warn: (deprecation: Error) => {
70+
warnCalledCount++;
71+
expect(deprecation).toMatch(
72+
'"name" parameter is deprecated for "octokit.actions.createOrUpdateRepoSecret()". Use "secret_name" instead'
73+
);
74+
},
75+
},
76+
});
77+
// See https://developer.github.com/v3/actions/secrets/#create-or-update-a-repository-secret
78+
// The `:secret_name` URL parameter was `:name` until May 14, 2020
79+
// @ts-ignore
80+
const { data } = await octokit.actions.createOrUpdateRepoSecret({
81+
owner: "octocat",
82+
repo: "hello-world",
83+
name: "MY_SECRET",
84+
encrypted_value: "encrypted value 123",
85+
key_id: "key id 123",
86+
});
87+
expect(data).toStrictEqual({ ok: true });
88+
expect(warnCalledCount).toEqual(1);
89+
});
90+
91+
it("deprecated parameter + new parameter", async () => {
92+
const mock = fetchMock
93+
.sandbox()
94+
.putOnce("path:/repos/octocat/hello-world/actions/secrets/MY_SECRET2", {
95+
ok: true,
96+
});
97+
const MyOctokit = Octokit.plugin(restEndpointMethods);
98+
99+
let warnCalledCount = 0;
100+
const octokit = new MyOctokit({
101+
request: {
102+
fetch: mock,
103+
},
104+
log: {
105+
warn: (deprecation: Error) => {
106+
warnCalledCount++;
107+
expect(deprecation).toMatch(
108+
'"name" parameter is deprecated for "octokit.actions.createOrUpdateRepoSecret()". Use "secret_name" instead'
109+
);
110+
},
111+
},
112+
});
113+
// See https://developer.github.com/v3/actions/secrets/#create-or-update-a-repository-secret
114+
// The `:secret_name` URL parameter was `:name` until May 14, 2020
115+
// @ts-ignore
116+
const { data } = await octokit.actions.createOrUpdateRepoSecret({
117+
owner: "octocat",
118+
repo: "hello-world",
119+
name: "MY_SECRET1",
120+
secret_name: "MY_SECRET2",
121+
encrypted_value: "encrypted value 123",
122+
key_id: "key id 123",
123+
});
124+
expect(data).toStrictEqual({ ok: true });
125+
expect(warnCalledCount).toEqual(1);
126+
});
127+
128+
it("deprecated method + deprecated parameter", async () => {
129+
const mock = fetchMock
130+
.sandbox()
131+
.putOnce("path:/repos/octocat/hello-world/actions/secrets/MY_SECRET", {
132+
ok: true,
133+
});
134+
const MyOctokit = Octokit.plugin(restEndpointMethods);
135+
136+
const deprecations: string[] = [];
137+
const octokit = new MyOctokit({
138+
request: {
139+
fetch: mock,
140+
},
141+
log: {
142+
warn: (deprecation: Error) => {
143+
deprecations.push(deprecation.toString());
144+
},
145+
},
146+
});
147+
// See https://developer.github.com/v3/actions/secrets/#create-or-update-a-repository-secret
148+
// The `:secret_name` URL parameter was `:name` until May 14, 2020
149+
// @ts-ignore
150+
const { data } = await octokit.actions.createOrUpdateSecretForRepo({
151+
owner: "octocat",
152+
repo: "hello-world",
153+
name: "MY_SECRET",
154+
encrypted_value: "encrypted value 123",
155+
key_id: "key id 123",
156+
});
157+
expect(data).toStrictEqual({ ok: true });
158+
expect(deprecations.length).toEqual(2);
159+
expect(deprecations.sort()).toStrictEqual([
160+
'"name" parameter is deprecated for "octokit.actions.createOrUpdateSecretForRepo()". Use "secret_name" instead',
161+
"octokit.actions.createOrUpdateSecretForRepo() has been renamed to octokit.actions.createOrUpdateRepoSecret()",
162+
]);
163+
});
123164
});

0 commit comments

Comments
 (0)