Skip to content

Commit cf74030

Browse files
committed
Mark removed types as deprecated
1 parent a43e58d commit cf74030

File tree

8 files changed

+263
-4
lines changed

8 files changed

+263
-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-run: true
54+
deprecate-dry-run:
55+
needs: build_and_test
56+
uses: ./.github/workflows/deprecate.yml
57+
with:
58+
dry-run: true
5459
publish_alpha:
5560
name: publish alpha release
5661
runs-on: ubuntu-latest

.github/workflows/deprecate.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Mark removed types as deprecated
2+
#description: Loop over npm @types packages and mark as deprecated any that no longer exist in the DT repo.
3+
on:
4+
schedule:
5+
# https://crontab.guru/#0_0_*_*_0
6+
- cron: 0 0 * * 0
7+
workflow_call:
8+
inputs:
9+
dry-run:
10+
type: boolean
11+
workflow_dispatch:
12+
inputs:
13+
dry-run:
14+
type: boolean
15+
jobs:
16+
deprecate:
17+
if: github.event_name != 'schedule' || github.repository == 'microsoft/DefinitelyTyped-tools'
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v3
21+
- uses: actions/setup-node@v3
22+
with:
23+
cache: yarn
24+
- run: yarn install --frozen-lockfile
25+
- run: yarn build
26+
- name: Parse declarations
27+
run: yarn workspace @definitelytyped/publisher parse
28+
- name: Mark removed types as deprecated${{ (inputs || github.event.inputs).dry-run && ' dry run' || '' }}
29+
run: node --require source-map-support/register packages/deprecate/${{ (inputs || github.event.inputs).dry-run && ' --dry-run' || '' }}
30+
env:
31+
GITHUB_TOKEN: ${{ github.token }}
32+
NPM_TOKEN: ${{ secrets.NPM_RETAG_TOKEN }}
33+
- if: always()
34+
uses: actions/upload-artifact@v3
35+
with:
36+
name: ${{ github.job }}
37+
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-run`
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.119",
12+
"@definitelytyped/utils": "^0.0.119",
13+
"@octokit/graphql": "^4.8.0",
14+
"libnpmsearch": "^5.0.3",
15+
"yargs": "^17.5.1"
16+
},
17+
"devDependencies": {
18+
"@types/libnpmsearch": "^2.0.3",
19+
"@types/yargs": "^17.0.10"
20+
}
21+
}

packages/deprecate/src/index.ts

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

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: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@
15631563
is-plain-object "^3.0.0"
15641564
universal-user-agent "^5.0.0"
15651565

1566-
"@octokit/graphql@^4.5.8":
1566+
"@octokit/graphql@^4.5.8", "@octokit/graphql@^4.8.0":
15671567
version "4.8.0"
15681568
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-4.8.0.tgz#664d9b11c0e12112cbf78e10f49a05959aa22cc3"
15691569
integrity sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg==
@@ -1909,6 +1909,14 @@
19091909
resolved "https://registry.yarnpkg.com/@types/json-stable-stringify/-/json-stable-stringify-1.0.33.tgz#099b0712d824d15e2660c20e1c16e6a8381f308c"
19101910
integrity sha512-qEWiQff6q2tA5gcJGWwzplQcXdJtm+0oy6IHGHzlOf3eFAkGE/FIPXZK9ofWgNSHVp8AFFI33PJJshS0ei3Gvw==
19111911

1912+
"@types/libnpmsearch@^2.0.3":
1913+
version "2.0.3"
1914+
resolved "https://registry.yarnpkg.com/@types/libnpmsearch/-/libnpmsearch-2.0.3.tgz#6a7bba71e533d5344cd04ceac4fe81b6b1ab9ceb"
1915+
integrity sha512-f/tTUDiOaUNk+m1mfvXO4/7ZasYUaKdosLgvzMdrFHNFqJENqwT9kKE+Gd6N3nsoD5kCZ7q4Pw7ApPIjXQArbA==
1916+
dependencies:
1917+
"@types/node" "*"
1918+
"@types/npm-registry-fetch" "*"
1919+
19121920
"@types/minimatch@*", "@types/minimatch@^3.0.3":
19131921
version "3.0.5"
19141922
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
@@ -2124,6 +2132,13 @@
21242132
dependencies:
21252133
"@types/yargs-parser" "*"
21262134

2135+
"@types/yargs@^17.0.10":
2136+
version "17.0.10"
2137+
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.10.tgz#591522fce85d8739bca7b8bb90d048e4478d186a"
2138+
integrity sha512-gmEaFwpj/7f/ROdtIlci1R1VYU1J4j95m8T+Tj3iBgiBFKg1foE/PSl93bBd5T9LDXNPo8UlNN6W0qwD8O5OaA==
2139+
dependencies:
2140+
"@types/yargs-parser" "*"
2141+
21272142
"@typescript-eslint/eslint-plugin@^4.8.1":
21282143
version "4.28.2"
21292144
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.28.2.tgz#7a8320f00141666813d0ae43b49ee8244f7cf92a"
@@ -6094,6 +6109,13 @@ libnpmpublish@^4.0.0:
60946109
semver "^7.1.3"
60956110
ssri "^8.0.1"
60966111

6112+
libnpmsearch@^5.0.3:
6113+
version "5.0.3"
6114+
resolved "https://registry.yarnpkg.com/libnpmsearch/-/libnpmsearch-5.0.3.tgz#ed502a4c2c70ea36723180455fae1357546b2184"
6115+
integrity sha512-Ofq76qKAPhxbiyzPf/5LPjJln26VTKwU9hIU0ACxQ6tNtBJ1CHmI7iITrdp7vNezhZc0FlkXwrIpqXjhBJZgLQ==
6116+
dependencies:
6117+
npm-registry-fetch "^13.0.0"
6118+
60976119
lines-and-columns@^1.1.6:
60986120
version "1.1.6"
60996121
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00"
@@ -6986,7 +7008,7 @@ npm-registry-fetch@^11.0.0:
69867008
minizlib "^2.0.0"
69877009
npm-package-arg "^8.0.0"
69887010

6989-
npm-registry-fetch@^13.0.1:
7011+
npm-registry-fetch@^13.0.0, npm-registry-fetch@^13.0.1:
69907012
version "13.1.1"
69917013
resolved "https://registry.yarnpkg.com/npm-registry-fetch/-/npm-registry-fetch-13.1.1.tgz#26dc4b26d0a545886e807748032ba2aefaaae96b"
69927014
integrity sha512-5p8rwe6wQPLJ8dMqeTnA57Dp9Ox6GH9H60xkyJup07FmVlu3Mk7pf/kIIpl9gaN5bM8NM+UUx3emUWvDNTt39w==
@@ -9594,6 +9616,11 @@ yargs-parser@^20.2.2, yargs-parser@^20.2.3:
95949616
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"
95959617
integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==
95969618

9619+
yargs-parser@^21.0.0:
9620+
version "21.0.1"
9621+
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.0.1.tgz#0267f286c877a4f0f728fceb6f8a3e4cb95c6e35"
9622+
integrity sha512-9BK1jFpLzJROCI5TzwZL/TU4gqjK5xiHV/RfWLOahrjAko/e4DJkRDZQXfvqAsiZzzYhgAzbgz6lg48jcm4GLg==
9623+
95979624
[email protected], yargs@^15.1.0, yargs@^15.3.1:
95989625
version "15.3.1"
95999626
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.3.1.tgz#9505b472763963e54afe60148ad27a330818e98b"
@@ -9641,6 +9668,19 @@ yargs@^16.2.0:
96419668
y18n "^5.0.5"
96429669
yargs-parser "^20.2.2"
96439670

9671+
yargs@^17.5.1:
9672+
version "17.5.1"
9673+
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.5.1.tgz#e109900cab6fcb7fd44b1d8249166feb0b36e58e"
9674+
integrity sha512-t6YAJcxDkNX7NFYiVtKvWUz8l+PaKTLiL63mJYWR2GnHq2gjEWISzsLp9wg3aY36dY1j+gfIEL3pIF+XlJJfbA==
9675+
dependencies:
9676+
cliui "^7.0.2"
9677+
escalade "^3.1.1"
9678+
get-caller-file "^2.0.5"
9679+
require-directory "^2.1.1"
9680+
string-width "^4.2.3"
9681+
y18n "^5.0.5"
9682+
yargs-parser "^21.0.0"
9683+
96449684
zero-fill@^2.2.3:
96459685
version "2.2.4"
96469686
resolved "https://registry.yarnpkg.com/zero-fill/-/zero-fill-2.2.4.tgz#b041320973dbcb03cd90193270ac8d4a3da05fc1"

0 commit comments

Comments
 (0)