Skip to content

Commit

Permalink
revert: fix: custom constructs don't use the Slack webhook
Browse files Browse the repository at this point in the history
This reverts commit bf04f74.
  • Loading branch information
xiehan committed Dec 21, 2023
1 parent bf04f74 commit 772eca5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
28 changes: 13 additions & 15 deletions lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface RepositoryConfig {
team: ITeam;
protectMain?: boolean;
protectMainChecks?: string[];
webhookUrl?: string;
webhookUrl: string;
provider: GithubProvider;
}

Expand Down Expand Up @@ -112,22 +112,20 @@ export class RepositorySetup extends Construct {
);

// Slack integration so we can be notified about new PRs and Issues
if (webhookUrl) {
setOldId(
new RepositoryWebhook(this, "slack-webhook", {
repository: repository.name,
setOldId(
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,
}),
);
}
// We don't need to notify about PRs since they are auto-created
events: ["issues"],
provider,
}),
);
}
}

Expand Down
54 changes: 30 additions & 24 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ type StackShards = {
};

const allProviders: Record<string, string> = JSON.parse(
fs.readFileSync(path.join(__dirname, "provider.json"), "utf8"),
fs.readFileSync(path.join(__dirname, "provider.json"), "utf8")
);

const shardedStacks: StackShards = JSON.parse(
fs.readFileSync(path.join(__dirname, "sharded-stacks.json"), "utf8"),
fs.readFileSync(path.join(__dirname, "sharded-stacks.json"), "utf8")
);

interface GitUrls {
Expand All @@ -60,8 +60,8 @@ function getShardedStackProviders(name: string): Record<string, string> {

return Object.fromEntries(
Object.entries(allProviders).filter(([key]) =>
stackProvidersList.includes(key),
),
stackProvidersList.includes(key)
)
);
}

Expand Down Expand Up @@ -100,13 +100,13 @@ class TerraformCdkProviderStack extends TerraformStack {
this.createRepositoryManagerRepo(
slackWebhook,
githubProvider,
githubTeam,
githubTeam
);
this.createProviderProjectRepo(
slackWebhook,
secrets.npmSecret,
githubProvider,
githubTeam,
githubTeam
);
}

Expand Down Expand Up @@ -157,7 +157,7 @@ class TerraformCdkProviderStack extends TerraformStack {
slackWebhook: TerraformVariable,
npmSecret: SecretFromVariable,
githubProvider: GithubProvider,
githubTeam: DataGithubTeam,
githubTeam: DataGithubTeam
) {
const templateRepository = new GithubRepository(
this,
Expand All @@ -166,7 +166,7 @@ class TerraformCdkProviderStack extends TerraformStack {
team: githubTeam,
webhookUrl: slackWebhook.stringValue,
provider: githubProvider,
},
}
);

npmSecret.for(templateRepository.resource, githubProvider);
Expand All @@ -179,7 +179,7 @@ class TerraformCdkProviderStack extends TerraformStack {
private createRepositoryManagerRepo(
slackWebhook: TerraformVariable,
githubProvider: GithubProvider,
githubTeam: DataGithubTeam,
githubTeam: DataGithubTeam
) {
const selfTokens = [
new SecretFromVariable(this, "tf-cloud-token"),
Expand All @@ -202,13 +202,13 @@ class TerraformCdkProviderStack extends TerraformStack {
private validateProviderNames(providers: Record<string, string>) {
// validate that providers contain only valid names (-go suffix is forbidden)
const goSuffixProviders = Object.keys(providers).filter((key) =>
key.endsWith("-go"),
key.endsWith("-go")
);
if (goSuffixProviders.length > 0) {
Annotations.of(this).addError(
`Providers contain a provider key with a suffix -go which is not allowed due to conflicts with go package repositories. Please remove the -go suffix from these provider keys ${goSuffixProviders.join(
", ",
)}`,
", "
)}`
);
}

Expand All @@ -222,13 +222,13 @@ class TerraformCdkProviderStack extends TerraformStack {

const sanitizedProviderName = providerName.replace(/-/g, "");
return key !== sanitizedProviderName;
},
}
);
if (notMatchingProviders.length > 0) {
Annotations.of(this).addError(
`Provider name and provider key do not match for ${notMatchingProviders.join(
", ",
)}. This leads to issues when deploying go packages. Please rename the provider key to match the provider name.`,
", "
)}. This leads to issues when deploying go packages. Please rename the provider key to match the provider name.`
);
}
}
Expand All @@ -242,7 +242,7 @@ class CustomConstructsStack extends TerraformStack {
name: string;
languages: ("typescript" | "python" | "csharp" | "java" | "go")[];
topics?: string[];
}[],
}[]
) {
super(scope, name);
const githubProvider = new GithubProvider(this, "github-provider-cdktf", {
Expand All @@ -261,6 +261,10 @@ class CustomConstructsStack extends TerraformStack {
name: "custom-constructs",
},
});
const slackWebhook = new TerraformVariable(this, "slack-webhook", {
type: "string",
});
slackWebhook.overrideLogicalId("slack-webhook");

const secrets = new PublishingSecretSet(this, "secret-set");

Expand All @@ -271,10 +275,10 @@ class CustomConstructsStack extends TerraformStack {
language === "typescript"
? "js"
: language === "csharp"
? "dotnet"
: language
? "dotnet"
: language
}`;
}),
})
);

const repo = new GithubRepositoryFromExistingRepository(
Expand All @@ -283,10 +287,11 @@ class CustomConstructsStack extends TerraformStack {
{
repositoryName: repoName,
team: githubTeam,
webhookUrl: slackWebhook.stringValue,
provider: githubProvider,
protectMain: true,
protectMainChecks,
},
}
);

secrets.forGitHub(repo.resource, githubProvider);
Expand All @@ -311,6 +316,7 @@ class CustomConstructsStack extends TerraformStack {
topics,
team: githubTeam,
protectMain: false,
webhookUrl: slackWebhook.stringValue,
provider: githubProvider,
});
}
Expand All @@ -331,10 +337,10 @@ const allProviderNames = Object.keys(allProviders);
const shardProviderSet = new Set(allProvidersInShards);
const allProviderSet = new Set(allProviderNames);
const missingProvidersInShards = new Set(
[...allProviderSet].filter((provider) => !shardProviderSet.has(provider)),
[...allProviderSet].filter((provider) => !shardProviderSet.has(provider))
);
const missingProvidersInAllProviders = new Set(
[...shardProviderSet].filter((provider) => !allProviderSet.has(provider)),
[...shardProviderSet].filter((provider) => !allProviderSet.has(provider))
);

if (shardProviderSet.size < allProvidersInShards.length) {
Expand All @@ -345,15 +351,15 @@ if (missingProvidersInShards.size > 0) {
throw new Error(
`One or more providers present in provider.json are missing in sharded-stacks.json: ${[
...missingProvidersInShards,
]}`,
]}`
);
}

if (missingProvidersInAllProviders.size > 0) {
throw new Error(
`One or more providers present in sharded-stacks.json are missing in provider.json: ${[
...missingProvidersInAllProviders,
]}`,
]}`
);
}

Expand Down

0 comments on commit 772eca5

Please sign in to comment.