Skip to content

Commit 941aaaa

Browse files
committed
Mark removed types as deprecated
1 parent 0be19eb commit 941aaaa

File tree

8 files changed

+518
-6
lines changed

8 files changed

+518
-6
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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-run:
9+
type: boolean
10+
workflow_dispatch:
11+
inputs:
12+
dry-run:
13+
type: boolean
14+
jobs:
15+
deprecate:
16+
if: github.event_name != 'schedule' || github.repository == 'microsoft/DefinitelyTyped-tools'
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v3
20+
- uses: actions/setup-node@v3
21+
with:
22+
cache: yarn
23+
- run: yarn install --frozen-lockfile
24+
- run: yarn build
25+
- name: Parse declarations
26+
run: yarn workspace @definitelytyped/publisher parse
27+
- name: Mark removed types as deprecated${{ (inputs || github.event.inputs).dry-run && ' dry run' || '' }}
28+
run: node --require source-map-support/register packages/deprecate/${{ (inputs || github.event.inputs).dry-run && ' --dry-run' || '' }}
29+
env:
30+
GITHUB_TOKEN: ${{ github.token }}
31+
NPM_TOKEN: ${{ secrets.NPM_RETAG_TOKEN }}
32+
- if: always()
33+
uses: actions/upload-artifact@v3
34+
with:
35+
name: ${{ github.job }}
36+
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: 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.116-next.0",
12+
"@definitelytyped/utils": "0.0.115",
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 * as 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 { dryRun } = 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 (!dryRun) 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
}

0 commit comments

Comments
 (0)