-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
973 lines (856 loc) · 25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/**
* @import {GraphqlResponseError, graphql as GraphQl} from '@octokit/graphql'
* @import {Packument} from 'pacote'
* @import {PackageJson} from 'type-fest'
* @import {Context, Human, NpmOrgs, NpmOrg, NpmPermissionLong, NpmPermissionShort, NpmRole, NpmTeam, PackageManifest, Package, Repo, Team} from './util/types.js'
*/
/**
* @typedef BranchRefData
* @property {string | undefined} name
*
* @typedef DependencyGraphManifestsData
* @property {Array<PackageManifest>} nodes
*
* @typedef EdgeData
* @property {string} cursor
* @property {NodeData} node
*
* @typedef NodeData
* @property {boolean} isArchived
* @property {string} name
* @property {BranchRefData | undefined} defaultBranchRef
*
* @typedef NpmUserData
* @property {unknown} cidr_whitelist
* @property {string} created
* @property {boolean} email_verified
* @property {string} email
* @property {string} freenode
* @property {string} fullname
* @property {string} github
* @property {string} homepage
* @property {string} name
* @property {unknown} tfa
* @property {string} twitter
* @property {string} updated
*
* @typedef ObjectObject
* @property {string} text
*
* @typedef ObjectRepository
* @property {ObjectObject | undefined} object
*
* @typedef ObjectResponse
* @property {ObjectRepository} repository
*
* @typedef OrganizationResponse
* @property {OrganizationData} organization
*
* @typedef OrganizationData
* @property {RepositoriesData} repositories
*
* @typedef PageInfoData
* @property {boolean} hasNextPage
*
* @typedef RepositoriesData
* @property {Array<EdgeData>} edges
* @property {PageInfoData} pageInfo
*
* @typedef RepositoryData
* @property {DependencyGraphManifestsData} dependencyGraphManifests
*
* @typedef RepositoryResponse
* @property {RepositoryData} repository
*/
import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import {graphql} from '@octokit/graphql'
import chalk from 'chalk'
import pSeries from 'p-series'
import {fetch} from 'undici'
import yaml from 'yaml'
import {dependencyGraphAccept} from './util/constants.js'
import {find} from './util/find.js'
import {interpolate} from './util/interpolate.js'
/**
* @param {string} name
* @returns {Promise<unknown>}
*/
async function loadYaml(name) {
const url = new URL('../config/' + name + '.yml', import.meta.url)
const document = await fs.readFile(url, 'utf8')
return yaml.parse(document)
}
/**
* @returns {Promise<undefined>}
*/
export async function npmTools() {
// Note: ghToken needs `admin:org` and `repo` scopes.
const ghToken = process.env.GH_TOKEN || process.env.GITHUB_TOKEN
// Note: npmToken must be granted by an owner of all orgs.
const npmToken = process.env.NPM_TOKEN
assert(ghToken, 'expected `ghToken` to be set')
assert(npmToken, 'expected `npmToken` to be set')
const npmUserResponse = await fetch(
'https://registry.npmjs.org/-/npm/v1/user',
{headers: {Authorization: 'Bearer ' + npmToken}}
)
const npmUserData = /** @type {NpmUserData} */ (await npmUserResponse.json())
const npmTokenOwner = npmUserData.name
console.error(chalk.blue('ℹ') + ' authenticated as %s', npmTokenOwner)
/** @type {Context} */
const context = {
// Name of the whole collective.
collective: 'unifiedjs',
humans: /** @type {Array<Human>} */ (await loadYaml('unified-humans')),
ghToken,
npmOrgs: /** @type {NpmOrgs} */ (await loadYaml('npm-organizations')),
npmTeams: /** @type {Array<NpmTeam>} */ (await loadYaml('npm-teams')),
ghQuery: wrap(
graphql.defaults({headers: {authorization: 'token ' + ghToken}})
),
npmTokenOwner,
npmToken,
teams: /** @type {Array<Team>} */ (await loadYaml('unified-teams'))
}
/** @type {Array<() => Promise<undefined>>} */
const organizationTasks = []
for (const npmOrg of context.npmOrgs.orgs) {
organizationTasks.push(async function () {
await organizationRun(context, npmOrg)
})
}
await pSeries(organizationTasks)
console.warn(chalk.green('✓') + ' done')
}
/**
* @param {Context} context
* @param {NpmOrg} npmOrg
* @returns {Promise<undefined>}
*/
async function organizationRun(context, npmOrg) {
// Repos.
/** @type {Array<Repo>} */
const repositories = []
let done = false
/** @type {string | undefined} */
let cursor
console.warn(chalk.bold('repos') + ' for %s', npmOrg.github)
while (!done) {
const data = /** @type {OrganizationResponse} */ (
// eslint-disable-next-line no-await-in-loop
await context.ghQuery(
`
query($cursor: String, $org: String!) {
organization(login: $org) {
repositories(first: 100, after: $cursor) {
edges {
cursor
node {
defaultBranchRef { name }
isArchived
name
}
}
pageInfo { hasNextPage }
}
}
}
`,
{cursor, org: npmOrg.github}
)
)
const repos = data.organization.repositories
for (const edge of repos.edges) {
repositories.push({
archived: edge.node.isArchived,
defaultBranch: edge.node.defaultBranchRef?.name,
name: edge.node.name
})
cursor = edge.cursor
}
done = !repos.pageInfo.hasNextPage
}
/** @type {Array<() => Promise<undefined>>} */
const repoTasks = []
/** @type {Array<Package>} */
const packages = []
for (const repo of repositories) {
repoTasks.push(async function () {
const results = await repoRun(context, repo, npmOrg)
packages.push(...results)
})
}
await pSeries(repoTasks)
const [orgPackageResponse, orgTeamResponse, orgUserResponse] =
await Promise.all([
fetch(
'https://registry.npmjs.org/-/org/' +
encodeURIComponent(npmOrg.npm) +
'/package',
{headers: {Authorization: 'Bearer ' + context.npmToken}}
),
fetch(
'https://registry.npmjs.org/-/org/' +
encodeURIComponent(npmOrg.npm) +
'/team',
{headers: {Authorization: 'Bearer ' + context.npmToken}}
),
fetch(
'https://registry.npmjs.org/-/org/' +
encodeURIComponent(npmOrg.npm) +
'/user',
{headers: {Authorization: 'Bearer ' + context.npmToken}}
)
])
if (!orgPackageResponse.ok || !orgUserResponse.ok) {
console.warn(
' ' +
chalk.blue('ℹ') +
' could not get packages or members, does the `%s` org exist?',
npmOrg.npm
)
return
}
if (!orgTeamResponse.ok) {
console.warn(
chalk.red('✖') +
' could not get teams for `%s`, make sure the current token’s user (`%s`) is an owner',
npmOrg.npm,
context.npmTokenOwner
)
return
}
/** @type {Array<() => Promise<undefined>>} */
const tasks = []
const orgPackage = /** @type {Record<string, NpmPermissionShort>} */ (
await orgPackageResponse.json()
)
const orgTeam = /** @type {Array<string>} */ (await orgTeamResponse.json())
const orgUser = /** @type {Record<string, NpmRole>} */ (
await orgUserResponse.json()
)
// Packages.
// Adding and permissions are based on teams.
// See `team/index` for that.
// But we can look for packages that need to be removed.
for (const name of Object.keys(orgPackage)) {
const info = packages.find(function (y) {
return name === y.name
})
if (!info) {
tasks.push(async function () {
const packageArchived = await deprecated(context, name)
if (!packageArchived) {
console.error(
' ' +
chalk.red('✖') +
' npm package `%s` should not be in org `%s` (or it should be deprecated)',
name,
npmOrg.npm
)
}
})
}
}
// Members.
const data = {npmOrg: npmOrg.npm, orgTeam: npmOrg.unified, org: npmOrg.github}
const admins = find(context, interpolate(data, context.npmOrgs.admin))
const members = find(context, interpolate(data, context.npmOrgs.member))
const owners = find(context, interpolate(data, context.npmOrgs.owner))
const people = [...new Set([...admins, ...members, ...owners])]
/** @type {Array<string>} */
const expectedMembers = []
for (const github of people) {
const human = context.humans.find(function (h) {
return h.github === github
})
assert(human, 'could not find human for `@' + github + '`')
if (!human.npm) {
console.warn(
' ' + chalk.red('✖') + ' member `@%s` is not on npm',
human.github
)
continue
}
expectedMembers.push(human.npm)
const actualRole = Object.hasOwn(orgUser, human.npm)
? orgUser[human.npm]
: undefined
const expectedRole =
owners.includes(github) || human.npm === context.npmTokenOwner
? 'owner'
: admins.includes(github)
? 'admin'
: 'developer'
if (actualRole === expectedRole) {
continue
}
if (
(actualRole === 'owner' &&
(expectedRole === 'admin' || expectedRole === 'developer')) ||
(actualRole === 'admin' && expectedRole === 'developer')
) {
console.error(
' ' +
chalk.red('✖') +
' unexpected user `%s` with more rights (`%s`) than expected (`%s`), ignoring',
human.npm,
actualRole,
expectedRole
)
continue
}
// Update or add: both the same request.
tasks.push(async function () {
try {
await fetch(
'https://registry.npmjs.org/-/org/' +
encodeURIComponent(npmOrg.npm) +
'/user',
{
body: JSON.stringify({role: expectedRole, user: human.npm}),
headers: {Authorization: 'Bearer ' + context.npmToken},
method: 'PUT'
}
)
console.warn(
' ' + chalk.green('✔') + ' add npm user `~%s` to `%s` as `%s`',
human.npm,
npmOrg.npm,
expectedRole
)
} catch {
console.error(
' ' +
chalk.red('✖') +
' could not add npm user `~%s` to `%s`, make sure the current token’s user (`%s`) is an owner of the org',
human.npm,
npmOrg.npm,
context.npmTokenOwner
)
}
})
}
// Remove.
for (const login of Object.keys(orgUser)) {
if (!expectedMembers.includes(login)) {
console.error(
' ' + chalk.red('✖') + ' npm user `~%s` should not be in `%s`',
login,
npmOrg.npm
)
}
}
// Teams.
/** @type {Array<string>} */
const existingTeams = []
for (const team of orgTeam) {
const index = team.indexOf(':')
assert(index !== -1)
const orgName = team.slice(0, index)
assert(orgName === npmOrg.npm)
const teamName = team.slice(index + 1)
existingTeams.push(teamName)
}
assert(context.npmTeams)
for (const npmTeam of context.npmTeams) {
tasks.push(async function () {
// Create a team if it doesn’t already exist.
if (!existingTeams.includes(npmTeam.name)) {
await fetch(
'https://registry.npmjs.org/-/org/' +
encodeURIComponent(npmOrg.npm) +
'/team',
{
body: JSON.stringify({
// Note: there’s no way to get the description on the npm API,
// so we can’t update that if it’s incorrect.
description: interpolate(context, npmTeam.description),
name: npmTeam.name
}),
headers: {Authorization: 'Bearer ' + context.npmToken},
method: 'PUT'
}
)
console.info(
' ' + chalk.green('✓') + ' team %s created',
npmTeam.name
)
}
await teamRun(context, npmOrg, packages, npmTeam)
})
}
// Wait for all tasks.
await pSeries(tasks)
}
/**
* @param {Context} context
* @param {NpmOrg} npmOrg
* @param {Repo} repo
* @param {string} manifestFilename
* @returns {Promise<Package | undefined>}
*/
async function packageRun(context, npmOrg, repo, manifestFilename) {
// Get `package.json`.
const target = (repo.defaultBranch || 'master') + ':' + manifestFilename
/** @type {string | undefined} */
let objectText
try {
const response = /** @type {ObjectResponse} */ (
await context.ghQuery(
`
query($name: String!, $org: String!, $target: String!) {
repository(name: $name, owner: $org) {
object(expression: $target) {
... on Blob { text }
}
}
}
`,
{name: repo.name, org: npmOrg.github, target}
)
)
objectText = response.repository.object?.text
} catch (error) {
console.warn(
' ' + chalk.blue('ℹ') + ' could not request package at `%s`',
target,
error
)
}
/** @type {PackageJson | undefined} */
let packageData
if (objectText) {
try {
packageData = JSON.parse(objectText)
} catch {
console.warn(
' ' + chalk.blue('ℹ') + ' package at `%s` is invalid',
target
)
}
}
if (!packageData || !packageData.name || packageData.private) {
console.warn(
' ' + chalk.blue('ℹ') + ' package %s at `%s` is private',
packageData?.name ? '`' + packageData.name + '`' : 'without name',
target
)
return
}
// Get collaborators.
const response = await fetch(
'https://registry.npmjs.org/-/package/' +
encodeURIComponent(packageData.name) +
'/collaborators',
{headers: {Authorization: 'Bearer ' + context.npmToken}}
)
if (!response.ok) {
console.warn(
' ' +
chalk.red('✖') +
' could not get collaborators for `%s`. Is it published?',
packageData.name
)
return
}
const actualCollaborators =
/** @type {Record<string, NpmPermissionShort>} */ (await response.json())
/** @type {Record<string, NpmPermissionLong>} */
const permissions = {}
/** @type {Record<NpmPermissionLong, number>} */
const score = {'read-only': 0, 'read-write': 1}
for (const team of context.npmTeams) {
const members = find(
context,
interpolate(
{
npmOrg: npmOrg.npm,
orgTeam: npmOrg.unified,
org: npmOrg.github
},
team.member
)
)
for (const login of members) {
const permission = Object.hasOwn(permissions, login)
? permissions[login]
: undefined
if (permission) {
if (score[team.permission] > score[permission]) {
permissions[login] = team.permission
}
} else {
permissions[login] = team.permission
}
}
}
/** @type {Array<string>} */
const expectedCollaborators = []
for (const github of Object.keys(permissions)) {
const permission = permissions[github]
const human = context.humans.find(function (h) {
return h.github === github
})
// We don’t need to warn for this as it’ll be warned about on the org level.
if (human && human.npm) {
const actualPermission = Object.hasOwn(actualCollaborators, human.npm)
? actualCollaborators[human.npm]
: undefined
const expectedPermission = permission === 'read-write' ? 'write' : 'read'
// Different permissions.
if (actualPermission === undefined) {
console.warn(
' wait what? Some collaborator is missing? %s, %s, %j',
human.name,
packageData.name,
actualCollaborators
)
} else if (actualPermission !== expectedPermission) {
console.error(
' ' + chalk.red('✖') + ' ~%s should not have %s rights in `%s`',
human.name,
actualPermission,
packageData.name
)
}
expectedCollaborators.push(human.npm)
}
}
// Remove.
for (const actual of Object.keys(actualCollaborators)) {
if (!expectedCollaborators.includes(actual)) {
console.error(
' ' + chalk.red('✖') + ' ~%s should not be a collaborator on `%s`',
actual,
packageData.name
)
}
}
// We don’t do missing here: people are added to teams, so they’ll be warned
// about in the team pipeline.
const packageArchived = await deprecated(context, packageData.name)
if (repo.archived) {
if (!packageArchived) {
console.error(
' ' +
chalk.red('✖') +
'unexpected undeprecated package `%s` in archived repo `%s`',
packageData.name,
repo.name
)
}
} else if (packageArchived) {
console.info(
' ' +
chalk.blue('ℹ') +
' unexpected deprecated package `%s` in unarchived repo `%s`',
packageData.name,
repo.name
)
}
return {name: packageData.name, repo: repo.name}
}
/**
* @param {Context} context
* @param {Repo} repo
* @param {NpmOrg} npmOrg
* @returns {Promise<Array<Package>>}
*/
async function repoRun(context, repo, npmOrg) {
if (!repo.defaultBranch) {
console.warn(
' ' + chalk.blue('ℹ') + ' repo %s has no branches yet',
repo.name
)
return []
}
if (repo.archived) {
console.warn(' ' + chalk.blue('ℹ') + ' repo %s is archived', repo.name)
}
/** @type {RepositoryResponse} */
let response
try {
response = /** @type {RepositoryResponse} */ (
await context.ghQuery(
`
query($name: String!, $org: String!) {
repository(name: $name, owner: $org) {
dependencyGraphManifests {
nodes {
blobPath
exceedsMaxSize
filename
parseable
}
}
}
}
`,
{
headers: {Accept: dependencyGraphAccept},
name: repo.name,
org: npmOrg.github
}
)
)
} catch {
console.error(
' ' +
chalk.red('✖') +
' could not get manifests for %s: maybe they are loading? (in which case, running this again will probably work)',
repo.name
)
return []
}
const manifestNodes = response.repository.dependencyGraphManifests.nodes
/** @type {Array<Package>} */
const packages = []
/** @type {Array<() => Promise<undefined>>} */
const tasks = []
for (const manifest of manifestNodes) {
// Only include `package.json`s, not locks.
if (path.posix.basename(manifest.filename) !== 'package.json') {
continue
}
if (manifest.exceedsMaxSize) {
console.error(
' ' + chalk.red('✖') + ' manifest %s in %s is too big',
manifest.filename,
repo.name
)
continue
}
if (!manifest.parseable) {
console.error(
' ' + chalk.red('✖') + ' manifest %s in %s is not parseable',
manifest.filename,
repo.name
)
continue
}
tasks.push(async function () {
const result = await packageRun(context, npmOrg, repo, manifest.filename)
if (result) {
packages.push(result)
}
})
}
console.warn(' ' + chalk.bold('packages') + ' for %s', repo.name)
await pSeries(tasks)
return packages
}
/**
* @param {Context} context
* @param {NpmOrg} npmOrg
* @param {Array<Package>} npmOrgPackages
* @param {NpmTeam} npmTeam
* @returns {Promise<undefined>}
*/
async function teamRun(context, npmOrg, npmOrgPackages, npmTeam) {
console.info(' ' + chalk.bold('team') + ' %s', npmTeam.name)
const [actualTeamResponse, actualPackagesResponse] = await Promise.all([
fetch(
'https://registry.npmjs.org/-/team/' +
encodeURIComponent(npmOrg.npm) +
'/' +
encodeURIComponent(npmTeam.name) +
'/user',
{headers: {Authorization: 'Bearer ' + context.npmToken}}
),
fetch(
'https://registry.npmjs.org/-/team/' +
encodeURIComponent(npmOrg.npm) +
'/' +
encodeURIComponent(npmTeam.name) +
'/package',
{headers: {Authorization: 'Bearer ' + context.npmToken}}
)
])
// Team members.
const actualTeam = /** @type {Array<string>} */ (
await actualTeamResponse.json()
)
/** @type {Array<() => Promise<undefined>>} */
const tasks = []
const expectedTeamGithub = find(
context,
interpolate(
{
npmOrg: npmOrg.npm,
orgTeam: npmOrg.unified,
org: npmOrg.github
},
npmTeam.member
)
)
/** @type {Array<string>} */
const expectedTeam = []
for (const github of expectedTeamGithub) {
const human = context.humans.find(function (h) {
return h.github === github
})
// We don’t need to warn for this as it’ll be warned about on the org level.
if (human && human.npm) {
expectedTeam.push(human.npm)
}
}
// Remove (manual).
for (const login of actualTeam) {
if (!expectedTeam.includes(login)) {
console.info(
' ' + chalk.red('✖') + ' ~%s should not be in team %s',
login,
npmTeam.name
)
}
}
// Add.
for (const login of expectedTeam) {
if (!actualTeam.includes(login)) {
tasks.push(async function () {
assert(npmOrg.npm)
await fetch(
'https://registry.npmjs.org/-/team/' +
encodeURIComponent(npmOrg.npm) +
'/' +
encodeURIComponent(npmTeam.name) +
'/user',
{
body: JSON.stringify({user: login}),
headers: {Authorization: 'Bearer ' + context.npmToken},
method: 'PUT'
}
)
console.info(
' ' + chalk.green('✔') + ' add ~%s to %s',
login,
npmTeam.name
)
})
}
}
// Packages.
const actualPackages = /** @type {Record<string, NpmPermissionShort>} */ (
await actualPackagesResponse.json()
)
// We don’t need to log for removing packages: each team manages all packages.
// Packages that need to be removed are thus at the org level.
// Logging for removal thus happens there.
// Add packages / fix permissions.
for (const packageInfo of npmOrgPackages) {
const name = packageInfo.name
/** @type {NpmPermissionLong | undefined} */
const actualPermission = Object.hasOwn(actualPackages, name)
? actualPackages[name] === 'write'
? 'read-write'
: 'read-only'
: undefined
// Fine.
if (actualPermission === npmTeam.permission) {
continue
}
// Update or add: both the same request.
tasks.push(async function () {
assert(npmOrg.npm)
try {
await fetch(
'https://registry.npmjs.org/-/team/' +
encodeURIComponent(npmOrg.npm) +
'/' +
encodeURIComponent(npmTeam.name) +
'/package',
{
body: JSON.stringify({
package: name,
permissions: npmTeam.permission
}),
headers: {Authorization: 'Bearer ' + context.npmToken},
method: 'PUT'
}
)
console.info(
' ' + chalk.green('✔') + ' add %s to %s with %s',
name,
npmTeam.name,
npmTeam.permission
)
} catch {
console.info(
' ' +
chalk.red('✖') +
' could not add %s to %s, make sure the current token’s user (%s) is an owner',
name,
npmTeam.name,
context.npmTokenOwner
)
}
})
}
// Wait for all tasks.
await pSeries(tasks)
}
/**
* @param {Context} context
* @param {string} name
* @returns {Promise<boolean>}
*/
async function deprecated(context, name) {
const manifestResponse = await fetch(
'https://registry.npmjs.org/' + encodeURIComponent(name),
{headers: {Authorization: 'Bearer ' + context.npmToken}}
)
const packument = /** @type {Packument} */ (await manifestResponse.json())
const version = packument.versions[packument['dist-tags'].latest]
return 'deprecated' in version
}
/**
* @param {GraphQl} internalFunction
* @returns {GraphQl}
*/
function wrap(internalFunction) {
// @ts-expect-error: fine.
return wrappedFunction
/**
* @param {Parameters<GraphQl>} parameters
* @returns {ReturnType<GraphQl>}
*/
function wrappedFunction(...parameters) {
return attempt().catch(retry)
/**
* @returns {ReturnType<GraphQl>}
*/
function attempt() {
return internalFunction(...parameters)
}
/**
* @param {unknown} error
* @returns {Promise<unknown>}
*/
function retry(error) {
const exception = /** @type {GraphqlResponseError<unknown>} */ (error)
console.error('wrap err:', exception)
console.error('to do:', 'does `status` really not exist?')
const after =
// @ts-expect-error: does `status` really not exist?
exception && exception.status === 403
? exception.headers['retry-after']
: undefined
if (!after) {
throw exception
}
return new Promise(function (resolve, reject) {
setTimeout(delayed, Number.parseInt(String(after), 10) * 1000)
/**
* @returns {undefined}
*/
function delayed() {
attempt().then(resolve, reject)
}
})
}
}
}