-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hopefully these changes 'ill make it easier for developers
- Loading branch information
1 parent
3d95143
commit c3f3ace
Showing
14 changed files
with
197 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
config: | ||
bjerk-bot:bjerkbot-github-token: | ||
secure: AAABAA9O48OAPfAGncxn53I4/Xi3byjISs4i7D1AEm2cFZm5FmTDZjKJPRplea7T1+i0/DPk9lcM15Q9D0ttK1TvZXcmia8x | ||
bjerk-bot:organizations: | ||
- veltolini | ||
bjerk-bot:repositories-with-github-token: | ||
- repo: taksnor/infra | ||
- repo: taksnor/workflows | ||
- repo: getbranches/workflows | ||
bjerkio:org-wide-npm-expires-at: '2023-07-08' | ||
bjerkio:org-wide-npm-token: | ||
secure: AAABAKDA8LFLyp9CS4iTUfo/FJDd+DPtpG/+u4xKuA92TVr6L13Jp4Au1k/JJ/VQebb25H38/w2BDxZxf9qeNS2ZP9d5blku |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
name: bjerk-bot | ||
runtime: nodejs | ||
description: The Bjerk Bot | ||
config: | ||
pulumi:disable-default-providers: ['*'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as pulumi from '@pulumi/pulumi'; | ||
import { z } from 'zod'; | ||
|
||
const config = new pulumi.Config(); | ||
|
||
export const githubToken = config.requireSecret('bjerkbot-github-token'); | ||
|
||
const repositoriesWithGithubTokensConfigValue = config.requireObject( | ||
'repositories-with-github-token', | ||
); | ||
|
||
const repositoryZod = z.object({ | ||
repo: z.string(), | ||
invitationId: z.number().optional(), | ||
}); | ||
|
||
export const repositoriesWithGithubToken = z | ||
.array(repositoryZod) | ||
.parse(repositoriesWithGithubTokensConfigValue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import * as pulumi from '@pulumi/pulumi'; | ||
|
||
const config = new pulumi.Config('bjerkio'); | ||
|
||
export const orgWideNpmExpiresAt = config.require('org-wide-npm-expires-at'); | ||
export const orgWideNpmToken = config.requireSecret('org-wide-npm-token'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as github from '@pulumi/github'; | ||
import { githubToken } from '../../config'; | ||
import { provider } from './provider'; | ||
|
||
// TODO: Add a check to see if the token is going to expire. | ||
|
||
new github.ActionsOrganizationSecret( | ||
'bjerkio-github-token', | ||
{ | ||
plaintextValue: githubToken, | ||
secretName: 'BJERKBOT_GITHUB_TOKEN', | ||
visibility: 'all', | ||
}, | ||
{ provider, aliases: [{ name: 'bjerkio' }] }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as github from '@pulumi/github'; | ||
import { isAfter, subDays } from 'date-fns'; | ||
import { orgWideNpmToken, orgWideNpmExpiresAt } from './config'; | ||
import { provider } from './provider'; | ||
|
||
const expiresAt = new Date(orgWideNpmExpiresAt); | ||
|
||
// fail if the is going to expires in less than 14 days | ||
const fourteenDaysBefore = subDays(expiresAt, 14).getTime(); | ||
|
||
if (isAfter(new Date(), fourteenDaysBefore)) { | ||
throw new Error( | ||
'The npm token is going to expire in less than 14 days. Please update it.', | ||
); | ||
} | ||
|
||
const token = orgWideNpmToken; | ||
|
||
new github.ActionsOrganizationSecret( | ||
'bjerkio-npm-token', | ||
{ | ||
plaintextValue: token, | ||
secretName: 'NPM_TOKEN', | ||
visibility: 'all', | ||
}, | ||
{ provider }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as github from '@pulumi/github'; | ||
import { githubToken } from '../../config'; | ||
|
||
export const provider = new github.Provider('bjerkorg', { | ||
owner: 'bjerkio', | ||
token: githubToken, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as pulumi from '@pulumi/pulumi'; | ||
import * as github from '@pulumi/github'; | ||
import { githubToken, repositoriesWithGithubToken } from '../config'; | ||
import { invariant } from 'ts-invariant'; | ||
|
||
const dynamicProviders = new Map<string, github.Provider>(); | ||
|
||
export function getProvider(fullName: string): github.Provider { | ||
if (dynamicProviders.has(fullName)) { | ||
const provider = dynamicProviders.get(fullName); | ||
invariant(provider, 'Provider should exist'); | ||
return provider; | ||
} | ||
|
||
const [owner] = fullName.split('/'); | ||
|
||
const provider = new github.Provider(`dynamic-${fullName}`, { | ||
owner, | ||
token: githubToken, | ||
}); | ||
|
||
dynamicProviders.set(fullName, provider); | ||
|
||
return provider; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as pulumi from '@pulumi/pulumi'; | ||
import * as github from '@pulumi/github'; | ||
import { githubToken, repositoriesWithGithubToken } from '../config'; | ||
import { getProvider } from './dynamic-github-providers'; | ||
|
||
repositoriesWithGithubToken.map(({ repo: fullName, invitationId }) => { | ||
const [org, repository] = fullName.split('/'); | ||
|
||
const dependsOn: pulumi.Resource[] = []; | ||
|
||
const provider = getProvider(fullName); | ||
|
||
if (invitationId) { | ||
dependsOn.push( | ||
new github.UserInvitationAccepter( | ||
fullName, | ||
{ | ||
invitationId: String(invitationId), | ||
}, | ||
{ provider }, | ||
), | ||
); | ||
} | ||
|
||
new github.ActionsSecret( | ||
`${org}-${repository}`, | ||
{ | ||
secretName: 'BJERKBOT_GITHUB_TOKEN', | ||
plaintextValue: githubToken, | ||
repository, | ||
}, | ||
{ provider, dependsOn }, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,4 @@ | ||
import * as pulumi from '@pulumi/pulumi'; | ||
import * as github from '@pulumi/github'; | ||
import { repositories } from './repositories'; | ||
import './github/bjerkorg/org-wide-npm-token.ts'; | ||
import './github/bjerkorg/org-wide-github-token'; | ||
|
||
const config = new pulumi.Config(); | ||
|
||
const githubToken = config.requireSecret('bjerkbot-github-token'); | ||
|
||
const invites = repositories | ||
.filter(r => r.invitationId) | ||
.map(({ repo, invitationId }) => { | ||
return new github.UserInvitationAccepter(repo, { | ||
invitationId: String(invitationId), | ||
}); | ||
}); | ||
|
||
const reposWithToken = repositories.filter(r => r.token); | ||
const providers = new Map(); | ||
|
||
reposWithToken.map(({ repo }) => { | ||
const [org] = repo.split('/'); | ||
if (!providers.has(org)) { | ||
providers.set( | ||
org, | ||
new github.Provider(org, { | ||
owner: org, | ||
organization: org, | ||
token: githubToken, | ||
}), | ||
); | ||
} | ||
}); | ||
|
||
reposWithToken.map(({ repo }) => { | ||
const [org, repository] = repo.split('/'); | ||
return new github.ActionsSecret( | ||
`${org}-${repository}`, | ||
{ | ||
secretName: 'BJERKBOT_GITHUB_TOKEN', | ||
plaintextValue: githubToken, | ||
repository, | ||
}, | ||
{ provider: providers.get(org), dependsOn: invites }, | ||
); | ||
}); | ||
|
||
const organizations = config.requireObject<string[]>('organizations'); | ||
|
||
organizations.map(org => { | ||
if (!providers.has(org)) { | ||
providers.set( | ||
org, | ||
new github.Provider(org, { | ||
owner: org, | ||
}), | ||
); | ||
} | ||
}); | ||
|
||
organizations.map( | ||
org => | ||
new github.ActionsOrganizationSecret( | ||
org, | ||
{ | ||
secretName: 'BJERKBOT_GITHUB_TOKEN', | ||
plaintextValue: githubToken, | ||
visibility: 'all', | ||
}, | ||
{ provider: providers.get(org) }, | ||
), | ||
); | ||
import './github/repositories-with-github-tokens'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters