Skip to content

Commit

Permalink
chore: Migrate ids using new 'MigrateIds' aspect (#239)
Browse files Browse the repository at this point in the history
### Description

This migrates our resources to the new naming mechanism using the new
`MigrateIds` aspect. The aspect can be removed after things are applied.
  • Loading branch information
Maed223 authored Jan 23, 2024
1 parent 8f82140 commit 2809694
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 79 deletions.
109 changes: 49 additions & 60 deletions lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { IssueLabel } from "@cdktf/provider-github/lib/issue-label";
import { BranchProtection } from "@cdktf/provider-github/lib/branch-protection";
import { TeamRepository } from "@cdktf/provider-github/lib/team-repository";
import { RepositoryWebhook } from "@cdktf/provider-github/lib/repository-webhook";
import { setOldId } from "./logical-id-override";

export interface ITeam {
id: string;
Expand Down Expand Up @@ -50,23 +49,19 @@ export class RepositorySetup extends Construct {
webhookUrl,
} = config;

setOldId(
new IssueLabel(this, `automerge-label`, {
color: "5DC8DB",
name: "automerge",
repository: repository.name,
provider,
}),
);

setOldId(
new IssueLabel(this, `no-auto-close-label`, {
color: "EE2222",
name: "no-auto-close",
repository: repository.name,
provider,
}),
);
new IssueLabel(this, `automerge-label`, {
color: "5DC8DB",
name: "automerge",
repository: repository.name,
provider,
});

new IssueLabel(this, `no-auto-close-label`, {
color: "EE2222",
name: "no-auto-close",
repository: repository.name,
provider,
}),

new IssueLabel(this, `auto-approve-label`, {
color: "8BF8BD",
Expand All @@ -76,56 +71,51 @@ export class RepositorySetup extends Construct {
});

if (protectMain) {
setOldId(
new BranchProtection(this, "main-protection", {
pattern: "main",
repositoryId: repository.name,
enforceAdmins: true,
allowsDeletions: false,
allowsForcePushes: false,
requiredPullRequestReviews: [
{
requiredApprovingReviewCount: 1,
requireCodeOwnerReviews: false, // NOTE: In the future, Security wants to enforce this, so be warned...
dismissStaleReviews: false,
},
],
requireConversationResolution: true,
requiredStatusChecks: [
{
strict: true,
contexts: protectMainChecks,
},
],
provider,
}),
);
new BranchProtection(this, "main-protection", {
pattern: "main",
repositoryId: repository.name,
enforceAdmins: true,
allowsDeletions: false,
allowsForcePushes: false,
requiredPullRequestReviews: [
{
requiredApprovingReviewCount: 1,
requireCodeOwnerReviews: false, // NOTE: In the future, Security wants to enforce this, so be warned...
dismissStaleReviews: false,
},
],
requireConversationResolution: true,
requiredStatusChecks: [
{
strict: true,
contexts: protectMainChecks,
},
],
provider,
})
}

setOldId(
new TeamRepository(this, "managing-team", {
repository: repository.name,
teamId: team.id,
permission: "admin",
provider,
}),
);
new TeamRepository(this, "managing-team", {
repository: repository.name,
teamId: team.id,
permission: "admin",
provider,
});

// Slack integration so we can be notified about new PRs and Issues
setOldId(
new RepositoryWebhook(this, "slack-webhook", {
repository: repository.name,
new RepositoryWebhook(this, "slack-webhook", {
repository: repository.name,

configuration: {
url: webhookUrl,
contentType: "json",
},
configuration: {
url: webhookUrl,
contentType: "json",
},

// We don't need to notify about PRs since they are auto-created
events: ["issues"],
provider,
}),
);
});

}
}

Expand Down Expand Up @@ -169,7 +159,6 @@ export class GithubRepository extends Construct {
topics,
provider,
});
setOldId(this.resource);

new RepositorySetup(this, "repository-setup", {
...config,
Expand Down
27 changes: 11 additions & 16 deletions lib/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Repository } from "@cdktf/provider-github/lib/repository";
import { DataGithubRepository } from "@cdktf/provider-github/lib/data-github-repository";
import { GithubProvider } from "@cdktf/provider-github/lib/provider";
import { ActionsSecret } from "@cdktf/provider-github/lib/actions-secret";
import { setOldId } from "./logical-id-override";

export class SecretFromVariable extends Construct {
public readonly name: string;
Expand Down Expand Up @@ -38,24 +37,20 @@ export class SecretFromVariable extends Construct {
repository: Repository | DataGithubRepository,
ghProvider: GithubProvider,
) {
const secret = setOldId(
new ActionsSecret(repository, `secret-${this.name}`, {
const secret = new ActionsSecret(repository, `secret-${this.name}`, {
plaintextValue: this.variable.value,
secretName: constantCase(this.name),
repository: repository.name,
provider: ghProvider,
});

this.secretNames.forEach((name) => {
new ActionsSecret(repository, `secret-${this.name}-alias-${name}`, {
plaintextValue: this.variable.value,
secretName: constantCase(this.name),
secretName: constantCase(name),
repository: repository.name,
provider: ghProvider,
}),
);

this.secretNames.forEach((name) => {
setOldId(
new ActionsSecret(repository, `secret-${this.name}-alias-${name}`, {
plaintextValue: this.variable.value,
secretName: constantCase(name),
repository: repository.name,
provider: ghProvider,
}),
);
});
});

return secret;
Expand Down
12 changes: 10 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
TerraformOutput,
RemoteBackend,
Annotations,
Aspects,
MigrateIds,
} from "cdktf";
import {
GithubRepository,
Expand Down Expand Up @@ -371,10 +373,15 @@ if (!stackNames.includes(primaryStackName)) {
}

stackNames.forEach((stackName) => {
new TerraformCdkProviderStack(app, stackName, primaryStackName === stackName);
const providerStack = new TerraformCdkProviderStack(
app,
stackName,
primaryStackName === stackName
);
Aspects.of(providerStack).add(new MigrateIds());
});

new CustomConstructsStack(app, "custom-constructs", [
const customConstructs = new CustomConstructsStack(app, "custom-constructs", [
{
name: "cdktf-tf-module-stack",
languages: ["typescript", "python", "csharp", "java", "go"],
Expand All @@ -393,5 +400,6 @@ new CustomConstructsStack(app, "custom-constructs", [
languages: ["typescript", "python"],
},
]);
Aspects.of(customConstructs).add(new MigrateIds());

app.synth();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"prettier --write"
]
}
}
}

0 comments on commit 2809694

Please sign in to comment.