Skip to content

Commit

Permalink
feat: handle BOT_NAME and BOT_LOCATION configuration via env (#5583)
Browse files Browse the repository at this point in the history
Handle better env var names (GCF_SHORT_FUNCTION_NAME -> BOT_NAME and
GCF_LOCATION -> BOT_LOCATION)

fix: ensure that Cloud Function URL uses underscored name of bot
test: fix test at runtime, not docker build time
test: set test env variables at runtime not build time
  • Loading branch information
chingor13 authored Nov 25, 2024
1 parent 4667a4e commit 2bbb00c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
16 changes: 7 additions & 9 deletions packages/gcf-utils/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ FROM node:16-stretch

USER root

ENV PROJECT_ID=repo-automation-bots
ENV GCF_SHORT_FUNCTION_NAME=snippet_bot
ENV INSTALLATION_ID=9602930
ENV GCF_LOCATION=us-central1

COPY . /workspace
RUN cd /workspace/packages/gcf-utils && \
COPY . /code
RUN cd /code && \
npm --version && \
npm i && \
npm run system-test
npm i
WORKDIR /code

ENTRYPOINT [ "npm" ]
CMD ["run", "system-test"]
28 changes: 27 additions & 1 deletion packages/gcf-utils/cloudbuild-test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
steps:
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '--network=cloudbuild', '-t', 'test-result', '-f', 'packages/gcf-utils/Dockerfile', '.']
id: build
dir: packages/gcf-utils
args:
- 'build'
- '--network=cloudbuild'
- '-t'
- 'test-image'
- '-f'
- 'Dockerfile'
- '.'
- name: test-image
waitFor: ['build']
dir: /code
env:
- 'PROJECT_ID=$PROJECT_ID'
- 'GCF_SHORT_FUNCTION_NAME=snippet_bot'
- 'INSTALLATION_ID=9602930'
- 'GCF_LOCATION=us-central1'

- name: test-image
waitFor: ['build']
dir: /code
env:
- 'PROJECT_ID=$PROJECT_ID'
- 'BOT_NAME=snippet_bot'
- 'INSTALLATION_ID=9602930'
- 'BOT_LOCATION=us-central1'

options:
logging: CLOUD_LOGGING_ONLY
17 changes: 11 additions & 6 deletions packages/gcf-utils/src/gcf-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ export async function getBotSecrets(
options: BotSecretsOptions = {}
): Promise<BotSecrets> {
const projectId = options.projectId ?? process.env.PROJECT_ID;
const botName = options.botName ?? process.env.GCF_SHORT_FUNCTION_NAME;
const botName =
options.botName ??
process.env.GCF_SHORT_FUNCTION_NAME ??
process.env.BOT_NAME;
const secretsClient =
options.secretsClient ??
new SecretManagerV1.SecretManagerServiceClient({
Expand Down Expand Up @@ -276,8 +279,9 @@ export class GCFBootstrapper {
options = {
...{
projectId: process.env.PROJECT_ID,
functionName: process.env.GCF_SHORT_FUNCTION_NAME,
location: process.env.GCF_LOCATION,
functionName:
process.env.GCF_SHORT_FUNCTION_NAME ?? process.env.BOT_NAME,
location: process.env.GCF_LOCATION ?? process.env.BOT_LOCATION,
payloadBucket: process.env.WEBHOOK_TMP,
taskCaller: process.env.TASK_CALLER_SERVICE_ACCOUNT,
},
Expand All @@ -304,13 +308,13 @@ export class GCFBootstrapper {
this.projectId = options.projectId;
if (!options.functionName) {
throw new Error(
'Missing required `functionName`. Please provide as a constructor argument or set the GCF_SHORT_FUNCTION_NAME env variable.'
'Missing required `functionName`. Please provide as a constructor argument or set the GCF_SHORT_FUNCTION_NAME or BOT_NAME env variable.'
);
}
this.functionName = options.functionName;
if (!options.location) {
throw new Error(
'Missing required `location`. Please provide as a constructor argument or set the GCF_LOCATION env variable.'
'Missing required `location`. Please provide as a constructor argument or set the GCF_LOCATION or BOT_LOCATION env variable.'
);
}
this.location = options.location;
Expand Down Expand Up @@ -994,7 +998,8 @@ export class GCFBootstrapper {
): Promise<string> {
if (this.taskTargetEnvironment === 'functions') {
// https://us-central1-repo-automation-bots.cloudfunctions.net/merge_on_green
return `https://${location}-${projectId}.cloudfunctions.net/${botName}`;
const functionName = botName.replace(/-/g, '_');
return `https://${location}-${projectId}.cloudfunctions.net/${functionName}`;
} else if (this.taskTargetEnvironment === 'run') {
if (this.cloudRunURL) {
return this.cloudRunURL;
Expand Down
6 changes: 3 additions & 3 deletions packages/gcf-utils/test/gcf-bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@ describe('GCFBootstrapper', () => {
'X-GitHub-Delivery': 'some-request-id',
'Content-Type': 'application/json',
}),
url: 'https://my-location-my-project.cloudfunctions.net/my-function-name',
url: 'https://my-location-my-project.cloudfunctions.net/my_function_name',
},
},
});
Expand Down Expand Up @@ -1911,7 +1911,7 @@ describe('GCFBootstrapper', () => {
'X-GitHub-Delivery': 'some-request-id',
'Content-Type': 'application/json',
}),
url: 'https://my-location-my-project.cloudfunctions.net/my-function-name',
url: 'https://my-location-my-project.cloudfunctions.net/my_function_name',
},
},
});
Expand Down Expand Up @@ -2116,7 +2116,7 @@ describe('GCFBootstrapper', () => {
'X-GitHub-Delivery': 'some-request-id',
'Content-Type': 'application/json',
}),
url: 'https://my-location-my-project.cloudfunctions.net/my-function-name-backend',
url: 'https://my-location-my-project.cloudfunctions.net/my_function_name_backend',
},
},
});
Expand Down

0 comments on commit 2bbb00c

Please sign in to comment.