-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
586 lines (548 loc) · 21.9 KB
/
index.ts
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
/* eslint-disable @typescript-eslint/no-require-imports */
import assert = require("assert");
import { pascalCase } from "change-case";
import { TextFile, cdk, github, JsonPatch } from "projen";
import { JobStep } from "projen/lib/github/workflows-model";
import { UpgradeDependenciesSchedule } from "projen/lib/javascript";
import { AlertOpenPrs } from "./alert-open-prs";
import { AutoApprove } from "./auto-approve";
import { AutoCloseCommunityIssues } from "./auto-close-community-issues";
import { Automerge } from "./automerge";
import { CdktfConfig } from "./cdktf-config";
import { CopyrightHeaders } from "./copyright-headers";
import { CustomizedLicense } from "./customized-license";
import { Dependabot } from "./dependabot";
import { DeprecatePackages } from "./deprecate-packages";
import { ForceRelease } from "./force-release";
import { GithubIssues } from "./github-issues";
import { LockIssues } from "./lock-issues";
import { PackageInfo } from "./package-info";
import { ProviderUpgrade } from "./provider-upgrade";
import { CheckForUpgradesScriptFile } from "./scripts/check-for-upgrades";
import { ShouldReleaseScriptFile } from "./scripts/should-release";
import { generateRandomCron, Schedule } from "./util/random-cron";
// ensure new projects start with 1.0.0 so that every following breaking change leads to an increased major version
const MIN_MAJOR_VERSION = 1;
export interface CdktfProviderProjectOptions extends cdk.JsiiProjectOptions {
readonly useCustomGithubRunner?: boolean;
readonly terraformProvider: string;
readonly cdktfVersion: string;
readonly constructsVersion: string;
readonly forceMajorVersion?: number;
/**
* defaults to "cdktf"
*/
readonly namespace?: string;
/**
* defaults to "cdktf"
* previously was "hashicorp". Used for GitHub org name and package scoping
*/
readonly githubNamespace?: string;
readonly mavenEndpoint?: string;
/**
* defaults to "HashiCorp"
*/
readonly nugetOrg?: string;
/**
* defaults to "hashicorp"
*/
readonly mavenOrg?: string;
/**
* defaults to "com.${mavenOrg}"
*/
readonly mavenGroupId?: string;
/**
* The year of the creation of the repository, for copyright purposes.
* Will fall back to the current year if not specified.
*/
readonly creationYear?: number;
/**
* Whether or not this prebuilt provider is deprecated.
* If true, no new versions will be published.
*/
readonly isDeprecated?: boolean;
/**
* An optional date when the project should be considered deprecated, to be used in the README text.
* If no date is provided, then the date of the build will be used by default.
*/
readonly deprecationDate?: string;
/**
* defaults to "HashiCorp, Inc."
*/
readonly licensee?: string;
}
const getMavenName = (providerName: string): string => {
return ["null", "random"].includes(providerName)
? `${providerName}_provider`
: providerName.replace(/-/gi, "_");
};
const githubActionPinnedVersions = {
"actions/checkout": "692973e3d937129bcbf40652eb9f2f61becf3332", // v4.1.7
"actions/download-artifact": "fa0a91b85d4f404e444e00e005971372dc801d16", // v4.1.8
"actions/github-script": "60a0d83039c74a4aee543508d2ffcb1c3799cdea", // v7.0.1
"actions/setup-dotnet": "6bd8b7f7774af54e05809fcc5431931b3eb1ddee", // v4.0.1
"actions/setup-go": "0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32", // v5.0.2
"actions/setup-java": "99b8673ff64fbf99d8d325f52d9a5bdedb8483e9", // v4.2.1
"actions/setup-node": "1e60f620b9541d16bece96c5465dc8ee9832be0b", // v4.0.3
"actions/setup-python": "39cd14951b08e74b54015e9e001cdefcf80e669f", // v5.1.1
"actions/stale": "28ca1036281a5e5922ead5184a1bbf96e5fc984e", // v9.0.0
"actions/upload-artifact": "89ef406dd8d7e03cfd12d9e0a4a378f454709029", // v4.3.5
"amannn/action-semantic-pull-request":
"0723387faaf9b38adef4775cd42cfd5155ed6017", // v5.5.3
"dessant/lock-threads": "1bf7ec25051fe7c00bdd17e6a7cf3d7bfb7dc771", // v5.0.1
"hashicorp/setup-copywrite": "32638da2d4e81d56a0764aa1547882fc4d209636", // v1.1.3
"hashicorp/setup-terraform": "b9cd54a3c349d3f38e8881555d616ced269862dd", // v3.1.2
"imjohnbo/issue-bot": "572eed14422c4d6ca37e870f97e7da209422f5bd", // v3.4.4
"peter-evans/create-pull-request": "c5a7806660adbe173f04e3e038b0ccdcd758773c", // v6.1.0
"slackapi/slack-github-action": "70cd7be8e40a46e8b0eced40b0de447bdb42f68e", // v1.26.0
};
export class CdktfProviderProject extends cdk.JsiiProject {
constructor(options: CdktfProviderProjectOptions) {
const {
terraformProvider,
workflowContainerImage,
cdktfVersion,
constructsVersion,
minNodeVersion,
jsiiVersion,
typescriptVersion,
isDeprecated,
deprecationDate,
authorName = "HashiCorp",
authorAddress = "https://hashicorp.com",
namespace = "cdktf",
githubNamespace = "cdktf",
mavenEndpoint = "https://hashicorp.oss.sonatype.org",
nugetOrg = "HashiCorp",
mavenOrg = "hashicorp",
} = options;
const [fqproviderName, providerVersion] = terraformProvider.split("@");
const providerName = fqproviderName.split("/").pop();
assert(providerName, `${terraformProvider} doesn't seem to be valid`);
assert(
!providerName.endsWith("-go"),
"providerName may not end with '-go' as this can conflict with repos for go packages"
);
const nugetName = `${nugetOrg}.${pascalCase(
namespace
)}.Providers.${pascalCase(providerName)}`;
const mavenGroupId = options.mavenGroupId ?? `com.${mavenOrg}`;
const mavenName = `${mavenGroupId}.${namespace}.providers.${getMavenName(
providerName
)}`;
const repository = `${githubNamespace}/${namespace}-provider-${providerName.replace(
/-/g,
""
)}`;
const repositoryUrl = `github.com/${repository}`;
const packageInfo: PackageInfo = {
npm: {
name: `@${githubNamespace}/provider-${providerName}`,
},
python: {
distName: `${githubNamespace}-${namespace}-provider-${providerName.replace(
/-/gi,
"_"
)}`,
module: `${githubNamespace}_${namespace}_provider_${providerName.replace(
/-/gi,
"_"
)}`,
},
publishToNuget: {
dotNetNamespace: nugetName,
packageId: nugetName,
},
publishToMaven: {
javaPackage: mavenName,
mavenGroupId: mavenGroupId,
mavenArtifactId: `${namespace}-provider-${providerName}`,
mavenEndpoint,
},
publishToGo: {
moduleName: `${repositoryUrl}-go`,
gitUserEmail: "[email protected]",
gitUserName: "CDK for Terraform Team",
packageName: providerName.replace(/-/g, ""),
// In order to use the copywrite action, we need to rebuild the full pre-publish steps workflow unfortunately
// If someone knows a better way to do this mutation with minimal custom code, please do so
prePublishSteps: [
{
name: "Checkout",
uses: "actions/checkout",
with: {
path: ".repo",
},
},
{
name: "Install Dependencies",
run: "cd .repo && yarn install --check-files --frozen-lockfile",
},
{
name: "Extract build artifact",
run: "tar --strip-components=1 -xzvf dist/js/*.tgz -C .repo",
},
{
name: "Move build artifact out of the way",
run: "mv dist dist.old",
},
{
name: "Create go artifact",
run: "cd .repo && npx projen package:go",
},
{
name: "Setup Copywrite tool",
uses: "hashicorp/setup-copywrite",
},
{
name: "Copy copywrite hcl file",
run: "cp .repo/.copywrite.hcl .repo/dist/go/.copywrite.hcl",
},
{
name: "Add headers using Copywrite tool",
run: "cd .repo/dist/go && copywrite headers",
},
{
name: "Remove copywrite hcl file",
run: "rm -f .repo/dist/go/.copywrite.hcl",
},
{
name: "Remove some text from the README that doesn't apply to Go",
run: [
"sed -i 's/# CDKTF prebuilt bindings for/# CDKTF Go bindings for/' .repo/dist/go/*/README.md",
// @see https://stackoverflow.com/a/49511949
// eslint-disable-next-line prettier/prettier
// prettier-ignore
`sed -i -e '/## ${isDeprecated ? "Deprecated" : "Available"} Packages/,/### Go/!b' -e '/### Go/!d;p; s/### Go/## Go Package/' -e 'd' .repo/dist/go/*/README.md`,
// sed -e is black magic and for whatever reason the string replace doesn't work so let's try it again:
// eslint-disable-next-line prettier/prettier
// prettier-ignore
`sed -i 's/### Go/## ${isDeprecated ? "Deprecated" : "Go"} Package/' .repo/dist/go/*/README.md`,
// Just straight up delete these full lines and everything in between them:
"sed -i -e '/API.typescript.md/,/You can also visit a hosted version/!b' -e 'd' .repo/dist/go/*/README.md",
`sed -i 's|Find auto-generated docs for this provider here:|Find auto-generated docs for this provider [here](https://${repositoryUrl}/blob/main/docs/API.go.md).|' .repo/dist/go/*/README.md`,
// Just straight up delete these full lines and everything in between them:
"sed -i -e '/### Provider Version/,/The provider version can be adjusted/!b' -e 'd' .repo/dist/go/*/README.md",
].join("\n"),
},
{
name: "Copy the README file to the parent directory",
run: "cp .repo/dist/go/*/README.md .repo/dist/go/README.md",
},
{
name: "Collect go Artifact",
run: "mv .repo/dist dist",
},
],
},
};
const workflowRunsOn = options.useCustomGithubRunner
? ["custom-linux-medium"] // 8 core, 32 GB
: ["ubuntu-latest"]; // 7 GB
super({
...options,
authorAddress,
authorName,
minNodeVersion,
workflowContainerImage,
workflowRunsOn,
licensed: false, // we do supply our own license file with a custom header
releaseToNpm: true,
name: packageInfo.npm.name,
description: `Prebuilt ${providerName} Provider for Terraform CDK (cdktf)`,
keywords: ["cdktf", "terraform", "cdk", "provider", providerName],
sampleCode: false,
jest: false,
authorOrganization: true,
defaultReleaseBranch: "main",
repository: `https://github.com/${repository}.git`,
mergify: false,
eslint: false,
depsUpgrade: !isDeprecated,
depsUpgradeOptions: {
workflowOptions: {
labels: ["automerge", "auto-approve", "dependencies"],
schedule: UpgradeDependenciesSchedule.WEEKLY,
},
},
python: packageInfo.python,
publishToNuget: packageInfo.publishToNuget,
publishToMaven: packageInfo.publishToMaven,
publishToGo: packageInfo.publishToGo,
releaseFailureIssue: true,
peerDependencyOptions: {
pinnedDevDependency: false,
},
workflowGitIdentity: {
name: "team-tf-cdk",
email: "[email protected]",
},
minMajorVersion: MIN_MAJOR_VERSION,
stale: true,
staleOptions: {
issues: {
staleLabel: "stale",
daysBeforeStale: 45,
staleMessage:
"45 days have passed since this issue was opened, and I assume other publishes have succeeded in the meantime. " +
"If no one removes the `stale` label or comments, I'm going to auto-close this issue in 14 days.",
daysBeforeClose: 14,
closeMessage:
"2 months have passed, so I'm closing this issue with the assumption that other publishes have succeeded in the meantime.",
},
pullRequest: {
staleLabel: "stale",
daysBeforeStale: 1,
staleMessage: `Closing this PR, if it has not merged there is most likely a CI or CDKTF issue preventing it from merging. If this has been a manual PR, please reopen it and add the \`no-auto-close\` label to prevent this from happening again.`,
daysBeforeClose: 0,
exemptLabels: ["no-auto-close"],
},
},
pullRequestTemplate: false,
docgen: false,
});
this.addDevDeps(
"dot-prop@^5.2.0",
"@actions/core@^1.1.0",
"@action-validator/core",
"@action-validator/cli"
);
// Default memory is 7GB: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
// Custom Runners we use have 32GB of memory
// The below numbers set heap limits that are ~1gb and ~0.5gb less, respectively, than the total available memory
const maxOldSpaceSize = options.useCustomGithubRunner ? "31744" : "6656";
// Golang needs more memory to build
this.tasks.addEnvironment(
"NODE_OPTIONS",
`--max-old-space-size=${maxOldSpaceSize}`
);
this.tasks.addEnvironment("CHECKPOINT_DISABLE", "1");
const validateTask = this.addTask("validate-workflows", {
exec: `find ./.github/workflows -type f -name "*.yml" -print0 | xargs -0 -n 1 npx action-validator`,
});
validateTask.description =
"Lint the YAML files generated by Projen to define GitHub Actions and Workflows, checking them against published JSON schemas";
this.postCompileTask.spawn(validateTask);
this.package.addPackageResolutions("@types/[email protected]");
const setSafeDirectory = {
name: "Set git config safe.directory",
run: "git config --global --add safe.directory $(pwd)",
};
((this.buildWorkflow as any).preBuildSteps as JobStep[]).push(
setSafeDirectory
);
(this.release as any).defaultBranch.workflow.jobs.release.steps.splice(
1,
0,
setSafeDirectory
);
// always publish a new GitHub release, even when publishing to a particular package manager fails
const releaseWorkflow = this.tryFindObjectFile(
".github/workflows/release.yml"
);
releaseWorkflow?.addOverride("jobs.release_github.needs", "release");
// ensure we don't fail if the release file is not present
const checkExistingTagStep = (
this.release as any
).defaultBranch.workflow.jobs.release.steps.find(
(s: object) => "id" in s && s.id === "check_tag_exists"
);
const oldExistingTagRun: string = checkExistingTagStep.run;
prettyAssertEqual(
oldExistingTagRun.split("\n")[0],
"TAG=$(cat dist/releasetag.txt)",
"release step changed, please check if the workaround still works!"
);
checkExistingTagStep.run = `if [ ! -f dist/releasetag.txt ]; then (echo "exists=true" >> $GITHUB_OUTPUT) && exit 0; fi\n${oldExistingTagRun}`;
if (!isDeprecated) {
const { upgrade, pr } = (this.upgradeWorkflow as any).workflows[0].jobs;
upgrade.steps.splice(1, 0, setSafeDirectory);
pr.steps.splice(1, 0, setSafeDirectory);
}
// Fix maven issue (https://github.com/cdklabs/publib/pull/777)
github.GitHub.of(this)?.tryFindWorkflow("release")?.file?.patch(
JsonPatch.add(
"/jobs/release_maven/steps/10/env/MAVEN_OPTS",
// See https://stackoverflow.com/questions/70153962/nexus-staging-maven-plugin-maven-deploy-failed-an-api-incompatibility-was-enco
"--add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.desktop/java.awt.font=ALL-UNNAMED"
)
);
this.pinGithubActionVersions(githubActionPinnedVersions);
new CdktfConfig(this, {
terraformProvider,
providerName,
fqproviderName,
providerVersion,
cdktfVersion,
constructsVersion,
jsiiVersion,
typescriptVersion,
packageInfo,
githubNamespace,
deprecationDate,
isDeprecated: !!isDeprecated,
});
new CustomizedLicense(this, options.creationYear, options.licensee);
new GithubIssues(this, { providerName });
new AutoApprove(this);
new AutoCloseCommunityIssues(this, { providerName });
new Automerge(this);
new LockIssues(this);
if (!isDeprecated) {
const upgradeScript = new CheckForUpgradesScriptFile(this, {
providerVersion,
fqproviderName,
});
new ProviderUpgrade(this, {
checkForUpgradesScriptPath: upgradeScript.path,
workflowRunsOn,
nodeHeapSize: maxOldSpaceSize,
});
new AlertOpenPrs(this, {
slackWebhookUrl: "${{ secrets.ALERT_PRS_SLACK_WEBHOOK_URL }}",
repository,
});
new ForceRelease(this, {
workflowRunsOn,
repositoryUrl,
});
new Dependabot(this);
}
new TextFile(this, ".github/CODEOWNERS", {
lines: [
"# These owners will be the default owners for everything in ",
"# the repo. Unless a later match takes precedence, ",
"# they will be requested for review when someone opens a ",
"# pull request.",
"* @cdktf/tf-cdk-team",
],
});
if (!isDeprecated) {
new ShouldReleaseScriptFile(this, {});
const releaseTask = this.tasks.tryFind("release")!;
this.removeTask("release");
this.addTask("release", {
description: releaseTask.description,
steps: releaseTask.steps,
env: (releaseTask as any)._env,
condition: "node ./scripts/should-release.js",
});
const releaseJobSteps: any[] = (
this.github?.tryFindWorkflow("release") as any
).jobs.release.steps;
const gitRemoteJob = releaseJobSteps.find((it) => it.id === "git_remote");
prettyAssertEqual(
gitRemoteJob.run,
'echo "latest_commit=$(git ls-remote origin -h ${{ github.ref }} | cut -f1)" >> $GITHUB_OUTPUT\ncat $GITHUB_OUTPUT',
"git_remote step in release workflow did not match expected string, please check if the workaround still works!"
);
const previousCommand = gitRemoteJob.run.replace("\n", " && ");
const cancelCommand =
'echo "latest_commit=release_cancelled" >> $GITHUB_OUTPUT'; // this cancels the release via a non-matching SHA;
gitRemoteJob.run = `node ./scripts/should-release.js && (${previousCommand}) || ${cancelCommand}`;
gitRemoteJob.name +=
" or cancel via faking a SHA if release was cancelled";
}
const staleWorkflow = this.tryFindObjectFile(".github/workflows/stale.yml");
staleWorkflow?.addOverride("on.schedule", [
{
cron: generateRandomCron({ project: this, maxHour: 4, hourOffset: 1 }),
},
]);
const upgradeWorkflow = this.tryFindObjectFile(
".github/workflows/upgrade-main.yml"
);
upgradeWorkflow?.addOverride("on.schedule", [
{
cron: generateRandomCron({
project: this,
maxHour: 0,
hourOffset: 1,
schedule: Schedule.Weekly,
}),
},
]);
// Submodule documentation generation
this.gitignore.exclude("API.md"); // ignore the old file, we now generate it in the docs folder
this.addDevDeps("jsii-docgen@^10.2.3");
if (jsiiVersion) {
// NOTE: the below is making a broad assumption that you're passing a range like "~5.3.0" to jsiiVersion
// If you use that field to pass a very specific version (e.g. "5.3.11") then this might break
this.addDevDeps(`jsii-rosetta@${jsiiVersion}`);
} else {
this.addDevDeps(`jsii-rosetta`);
}
const docgen = this.addTask("docgen", {
description: "Generate documentation for the project",
steps: [
{
exec: [
"rm -rf docs",
"rm -f API.md",
"mkdir docs",
"jsii-docgen --split-by-submodule -l typescript -l python -l java -l csharp -l go",
// There is no nice way to tell jsii-docgen to generate docs into a folder so I went this route
"mv *.*.md docs",
// Some part of the documentation are too long, we need to truncate them to ~10MB
"cd docs",
"ls ./ | xargs sed -i '150000,$ d' $1",
].join(" && "),
},
],
});
this.postCompileTask.spawn(docgen);
this.gitignore.include(`/docs/*.md`);
this.annotateGenerated(`/docs/*.md`);
// Setting the version in package.json so the golang docs have the correct version
const unconditionalBump = this.addTask("unconditional-bump", {
description: "Set the version in package.json to the current version",
steps: [
{
name: "Clear the changelog so that it doesn't get published twice",
exec: "rm -f $CHANGELOG",
},
{ builtin: "release/bump-version" },
],
env: {
OUTFILE: "package.json",
CHANGELOG: "dist/changelog.md",
BUMPFILE: "dist/version.txt",
RELEASETAG: "dist/releasetag.txt",
RELEASE_TAG_PREFIX: "",
MIN_MAJOR: String(MIN_MAJOR_VERSION),
},
});
this.preCompileTask.spawn(unconditionalBump);
// To bump correctly we need to have the completely cloned repo
(this.buildWorkflow as any).workflow.file.addOverride(
"jobs.build.steps.0.with.fetch-depth",
0
);
// Undo the changes after compilation
this.buildWorkflow?.addPostBuildSteps({
name: "Revert package.json version bump",
run: "git checkout package.json",
});
new CopyrightHeaders(this);
new DeprecatePackages(this, {
providerName,
packageInfo,
isDeprecated: !!isDeprecated,
});
}
private pinGithubActionVersions(pinnedVersions: Record<string, string>) {
// Use pinned versions of github actions
Object.entries(pinnedVersions).forEach(([name, sha]) => {
this.github?.actions.set(name, `${name}@${sha}`);
});
}
}
function prettyAssertEqual<T>(subject: T, expected: T, message?: string): void {
if (subject !== expected) {
throw new Error(
`${message ?? "Assertion failed"}: expected ${JSON.stringify(
expected
)} but got ${JSON.stringify(subject)}`
);
}
}