Skip to content

Commit 06b3931

Browse files
committed
Mark removed types as deprecated
1 parent 68c3bd2 commit 06b3931

File tree

8 files changed

+255
-4
lines changed

8 files changed

+255
-4
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ jobs:
5151
uses: ./.github/workflows/retag.yml
5252
with:
5353
dry: true
54+
deprecate-dry:
55+
needs: build_and_test
56+
uses: ./.github/workflows/deprecate.yml
57+
with:
58+
dry: true
5459
publish_alpha:
5560
name: publish alpha release
5661
runs-on: ubuntu-latest

.github/workflows/deprecate.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Mark removed types as deprecated
2+
on:
3+
schedule:
4+
# https://crontab.guru/#0_0_*_*_0
5+
- cron: 0 0 * * 0
6+
workflow_call:
7+
inputs:
8+
dry:
9+
description: Dry run
10+
type: boolean
11+
workflow_dispatch:
12+
inputs:
13+
dry:
14+
description: Dry run
15+
type: boolean
16+
jobs:
17+
deprecate:
18+
if: github.event_name != 'schedule' || github.repository == 'microsoft/DefinitelyTyped-tools'
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v3
22+
- uses: actions/setup-node@v3
23+
with:
24+
cache: yarn
25+
- run: yarn install --frozen-lockfile
26+
- run: yarn build
27+
- name: Parse declarations
28+
run: yarn workspace @definitelytyped/publisher parse
29+
- name: Mark removed types as deprecated${{ (inputs || github.event.inputs).dry && ' dry run' || '' }}
30+
run: node --require source-map-support/register packages/deprecate/${{ (inputs || github.event.inputs).dry && ' --dry' || '' }}
31+
env:
32+
GITHUB_TOKEN: ${{ github.token }}
33+
NPM_TOKEN: ${{ secrets.NPM_RETAG_TOKEN }}
34+
- if: always()
35+
uses: actions/upload-artifact@v3
36+
with:
37+
name: ${{ github.job }}
38+
path: packages/definitions-parser/data/

packages/deprecate/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# deprecate
2+
3+
[![Mark removed types as deprecated](https://github.com/microsoft/DefinitelyTyped-tools/actions/workflows/deprecate.yml/badge.svg)](https://github.com/microsoft/DefinitelyTyped-tools/actions/workflows/deprecate.yml)
4+
5+
Loop over npm `@types` packages and mark as deprecated any that no longer exist in the DT repo.
6+
7+
## Use
8+
9+
```sh
10+
yarn workspace @definitelytyped/publisher parse
11+
node packages/deprecate/
12+
```
13+
14+
1. [Parse declarations](../publisher/README.md#parse-the-definitions).
15+
2. Run this package's script.
16+
17+
### Options
18+
19+
<dl><dt>
20+
21+
`--dry`
22+
23+
</dt><dd>
24+
25+
Don't actually mark anything as deprecated, just show what would be done.
26+
27+
</dd></dl>
28+
29+
### Environment variables
30+
31+
<dl><dt>
32+
33+
`GITHUB_TOKEN`
34+
35+
</dt><dd>
36+
37+
Required.
38+
Used to talk to [GitHub's GraphQL API](https://docs.github.com/en/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql), to find the commit/PR that removed the types.
39+
That data is public and a GitHub Actions [automatic token](https://docs.github.com/en/actions/security-guides/automatic-token-authentication) is sufficient.
40+
41+
</dd><dt>
42+
43+
[`NPM_TOKEN`](https://docs.npmjs.com/about-access-tokens)
44+
45+
</dt><dd>
46+
47+
Not required for a dry run.
48+
Only used to actually mark `@types` packages as deprecated.
49+
50+
</dd></dl>
51+
52+
## Logs
53+
54+
GitHub Actions runs this package's script weekly.
55+
You can [examine the logs](https://github.com/microsoft/DefinitelyTyped-tools/actions/workflows/deprecate.yml).

packages/deprecate/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@definitelytyped/deprecate",
3+
"version": "1.0.0",
4+
"description": "Loop over npm @types packages and mark as deprecated any that no longer exist in the DT repo.",
5+
"license": "MIT",
6+
"main": "dist/index.js",
7+
"scripts": {
8+
"build": "tsc --build"
9+
},
10+
"dependencies": {
11+
"@definitelytyped/definitions-parser": "0.0.113-next.18",
12+
"@definitelytyped/utils": "0.0.113-next.17",
13+
"@octokit/graphql": "^4.8.0",
14+
"libnpmsearch": "^5.0.3",
15+
"yargs": "^17.4.1"
16+
},
17+
"devDependencies": {
18+
"@types/libnpmsearch": "^2.0.3"
19+
}
20+
}

packages/deprecate/src/index.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env node
2+
3+
import process from "process";
4+
import { AllPackages, getDefinitelyTyped } from "@definitelytyped/definitions-parser";
5+
import { NpmPublishClient } from "@definitelytyped/utils";
6+
import { graphql } from "@octokit/graphql";
7+
import search from "libnpmsearch";
8+
import yargs from "yargs";
9+
10+
main();
11+
12+
async function main() {
13+
const { dry } = yargs.argv;
14+
const options = { definitelyTypedPath: undefined, progress: false, parseInParallel: false };
15+
const dt = await getDefinitelyTyped(options, console);
16+
const allPackages = await AllPackages.read(dt);
17+
const client = await NpmPublishClient.create(process.env.NPM_TOKEN!);
18+
// Loop over npm @types packages and mark as deprecated any that no longer exist in the DT repo.
19+
let from = 0;
20+
let results;
21+
do {
22+
const opts = { limit: 250, from };
23+
// Won't return already-deprecated packages.
24+
results = await search("@types", opts);
25+
for (const result of results) {
26+
const types = result.name.slice("@types/".length);
27+
// Skip ones that exist, either in the types/ directory or notNeededPackages.json.
28+
if (allPackages.tryGetLatestVersion(types) || allPackages.getNotNeededPackage(types)) continue;
29+
const msg = await fetchMsg(types);
30+
if (!msg) {
31+
console.log(`Could not find the commit that removed types/${types}/.`);
32+
continue;
33+
}
34+
console.log(`Deprecating ${result.name}: ${msg}`);
35+
if (!dry) await client.deprecate(result.name, "*", msg);
36+
}
37+
from += results.length;
38+
// The registry API clamps limit at 250 and from at 5,000, so we can only loop over 5,250 packages, for now.
39+
} while (results.length >= 250 && from <= 5000);
40+
}
41+
42+
/** Reference the commit/PR that removed the named types. */
43+
async function fetchMsg(types: string) {
44+
const {
45+
repository: {
46+
defaultBranchRef: {
47+
target: {
48+
history: {
49+
nodes: [commit],
50+
},
51+
},
52+
},
53+
},
54+
} = await graphql(
55+
`
56+
query ($path: String!) {
57+
repository(name: "DefinitelyTyped", owner: "DefinitelyTyped") {
58+
defaultBranchRef {
59+
target {
60+
... on Commit {
61+
history(first: 1, path: $path) {
62+
nodes {
63+
associatedPullRequests(first: 1) {
64+
nodes {
65+
url
66+
}
67+
}
68+
messageHeadline
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}
76+
`,
77+
{
78+
headers: { authorization: `token ${process.env.GITHUB_TOKEN}` },
79+
path: `types/${types}/`,
80+
}
81+
);
82+
if (!commit) return;
83+
const {
84+
associatedPullRequests: {
85+
nodes: [pullRequest],
86+
},
87+
messageHeadline,
88+
} = commit;
89+
const subject = messageHeadline.replace(new RegExp(String.raw`^\[${types}] `), "").replace(/ \(#[0-9]+\)$/, "");
90+
return pullRequest ? `${subject} ${pullRequest.url}` : subject;
91+
}

packages/deprecate/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "dist"
6+
},
7+
"references": [{ "path": "../definitions-parser/" }, { "path": "../utils/" }]
8+
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"files": [],
44
"references": [
55
{ "path": "packages/definitions-parser" },
6+
{ "path": "packages/deprecate" },
67
{ "path": "packages/dts-critic" },
78
{ "path": "packages/dtslint" },
89
{ "path": "packages/dtslint-runner" },
910
{ "path": "packages/header-parser" },
1011
{ "path": "packages/perf" },
1112
{ "path": "packages/publisher" },
13+
{ "path": "packages/retag" },
1214
{ "path": "packages/typescript-versions" },
13-
{ "path": "packages/utils" },
14-
{ "path": "packages/retag" }
15+
{ "path": "packages/utils" }
1516
]
1617
}

yarn.lock

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@
15341534
is-plain-object "^3.0.0"
15351535
universal-user-agent "^5.0.0"
15361536

1537-
"@octokit/graphql@^4.5.8":
1537+
"@octokit/graphql@^4.5.8", "@octokit/graphql@^4.8.0":
15381538
version "4.8.0"
15391539
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-4.8.0.tgz#664d9b11c0e12112cbf78e10f49a05959aa22cc3"
15401540
integrity sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg==
@@ -1862,6 +1862,14 @@
18621862
resolved "https://registry.yarnpkg.com/@types/json-stable-stringify/-/json-stable-stringify-1.0.33.tgz#099b0712d824d15e2660c20e1c16e6a8381f308c"
18631863
integrity sha512-qEWiQff6q2tA5gcJGWwzplQcXdJtm+0oy6IHGHzlOf3eFAkGE/FIPXZK9ofWgNSHVp8AFFI33PJJshS0ei3Gvw==
18641864

1865+
"@types/libnpmsearch@^2.0.3":
1866+
version "2.0.3"
1867+
resolved "https://registry.yarnpkg.com/@types/libnpmsearch/-/libnpmsearch-2.0.3.tgz#6a7bba71e533d5344cd04ceac4fe81b6b1ab9ceb"
1868+
integrity sha512-f/tTUDiOaUNk+m1mfvXO4/7ZasYUaKdosLgvzMdrFHNFqJENqwT9kKE+Gd6N3nsoD5kCZ7q4Pw7ApPIjXQArbA==
1869+
dependencies:
1870+
"@types/node" "*"
1871+
"@types/npm-registry-fetch" "*"
1872+
18651873
"@types/minimatch@*", "@types/minimatch@^3.0.3":
18661874
version "3.0.5"
18671875
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
@@ -5845,6 +5853,13 @@ libnpmpublish@^4.0.0:
58455853
semver "^7.1.3"
58465854
ssri "^8.0.1"
58475855

5856+
libnpmsearch@^5.0.3:
5857+
version "5.0.3"
5858+
resolved "https://registry.yarnpkg.com/libnpmsearch/-/libnpmsearch-5.0.3.tgz#ed502a4c2c70ea36723180455fae1357546b2184"
5859+
integrity sha512-Ofq76qKAPhxbiyzPf/5LPjJln26VTKwU9hIU0ACxQ6tNtBJ1CHmI7iITrdp7vNezhZc0FlkXwrIpqXjhBJZgLQ==
5860+
dependencies:
5861+
npm-registry-fetch "^13.0.0"
5862+
58485863
lines-and-columns@^1.1.6:
58495864
version "1.1.6"
58505865
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00"
@@ -6729,7 +6744,7 @@ npm-registry-fetch@^11.0.0:
67296744
minizlib "^2.0.0"
67306745
npm-package-arg "^8.0.0"
67316746

6732-
npm-registry-fetch@^13.0.1:
6747+
npm-registry-fetch@^13.0.0, npm-registry-fetch@^13.0.1:
67336748
version "13.1.1"
67346749
resolved "https://registry.yarnpkg.com/npm-registry-fetch/-/npm-registry-fetch-13.1.1.tgz#26dc4b26d0a545886e807748032ba2aefaaae96b"
67356750
integrity sha512-5p8rwe6wQPLJ8dMqeTnA57Dp9Ox6GH9H60xkyJup07FmVlu3Mk7pf/kIIpl9gaN5bM8NM+UUx3emUWvDNTt39w==
@@ -9337,6 +9352,11 @@ yargs-parser@^20.2.2, yargs-parser@^20.2.3:
93379352
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"
93389353
integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==
93399354

9355+
yargs-parser@^21.0.0:
9356+
version "21.0.1"
9357+
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.0.1.tgz#0267f286c877a4f0f728fceb6f8a3e4cb95c6e35"
9358+
integrity sha512-9BK1jFpLzJROCI5TzwZL/TU4gqjK5xiHV/RfWLOahrjAko/e4DJkRDZQXfvqAsiZzzYhgAzbgz6lg48jcm4GLg==
9359+
93409360
[email protected], yargs@^15.1.0, yargs@^15.3.1:
93419361
version "15.3.1"
93429362
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.3.1.tgz#9505b472763963e54afe60148ad27a330818e98b"
@@ -9384,6 +9404,19 @@ yargs@^16.2.0:
93849404
y18n "^5.0.5"
93859405
yargs-parser "^20.2.2"
93869406

9407+
yargs@^17.4.1:
9408+
version "17.5.1"
9409+
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.5.1.tgz#e109900cab6fcb7fd44b1d8249166feb0b36e58e"
9410+
integrity sha512-t6YAJcxDkNX7NFYiVtKvWUz8l+PaKTLiL63mJYWR2GnHq2gjEWISzsLp9wg3aY36dY1j+gfIEL3pIF+XlJJfbA==
9411+
dependencies:
9412+
cliui "^7.0.2"
9413+
escalade "^3.1.1"
9414+
get-caller-file "^2.0.5"
9415+
require-directory "^2.1.1"
9416+
string-width "^4.2.3"
9417+
y18n "^5.0.5"
9418+
yargs-parser "^21.0.0"
9419+
93879420
zero-fill@^2.2.3:
93889421
version "2.2.4"
93899422
resolved "https://registry.yarnpkg.com/zero-fill/-/zero-fill-2.2.4.tgz#b041320973dbcb03cd90193270ac8d4a3da05fc1"

0 commit comments

Comments
 (0)