Skip to content

Commit aeb5e30

Browse files
authored
New: [AEA-5544] - Modify Slackbot to respond to mentions rather than slash commands (#16)
## Summary 🎫 [AEA-5544](https://nhsd-jira.digital.nhs.uk/browse/AEA-5544) Modify Slackbot to respond to mentions rather than slash commands :sparkles: New Feature ### Details This pull request modifies the Slackbot to respond to @mentions rather than slash commands, aligning with updated interaction requirements. Changes Included - Removed support for Slack slash commands. - Updated Slackbot Lambda to handle @mentions and reply in thread. - Added linting for Python code. - Integrated AWS Powertools for: - Logging - Fetching parameters from SSM - Added unit tests using pytest. - Configured a dev container for testing and development setup.
1 parent 1492597 commit aeb5e30

39 files changed

+1822
-399
lines changed

.devcontainer/devcontainer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"AmazonWebServices.aws-toolkit-vscode",
2929
"redhat.vscode-yaml",
3030
"ms-python.python",
31+
"ms-python.black-formatter",
3132
"ms-python.flake8",
3233
"eamodio.gitlens",
3334
"github.vscode-pull-request-github",
@@ -51,9 +52,18 @@
5152
"python.analysis.extraPaths": [],
5253
"python.testing.unittestEnabled": false,
5354
"python.testing.pytestEnabled": true,
55+
"python.testing.pytestArgs": [
56+
"packages/slackBotFunction",
57+
"packages/createIndexFunction"
58+
],
5459
"python.linting.pylintEnabled": false,
5560
"python.linting.flake8Enabled": true,
5661
"python.linting.enabled": true, // required to format on save
62+
"python.formatting.provider": "black",
63+
"black-formatter.args": ["--line-length=120"],
64+
"[python]": {
65+
"editor.defaultFormatter": "ms-python.black-formatter"
66+
},
5767
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
5868
"editor.formatOnPaste": false, // required
5969
"editor.formatOnType": false, // required
@@ -62,7 +72,7 @@
6272
"cSpell.words": ["fhir", "Formik", "pino", "serialisation"],
6373
"eslint.useFlatConfig": true,
6474
"eslint.format.enable": true
65-
}
75+
}
6676
}
6777
},
6878
"postCreateCommand": "rm -f ~/.docker/config.json; git config --global --add safe.directory /workspaces/eps-assist-me; make install; direnv allow ."

.flake8

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
22
max-line-length = 120
3-
exclude = .*,venv,node_modules
3+
exclude = .*,venv,node_modules,cdk.out
44
max-complexity = 10
5-
ignore = F821
5+
ignore = F821,E203

.gitallowed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ sha256:[a-f0-9]{64}
2323
.*=\s*"[><=!~^,0-9\s\.]+"
2424
app = App\(token=bot_token\)
2525
token=bot_token
26+
token="test-token"
2627
token: slackBotToken
2728
token: props\.slackBotToken
2829
secretValue: JSON\.stringify\(\{token: props\.slackBotToken\}\)
30+
token = slack_event_data\["bot_token"\]
31+
client = WebClient\(token=token\)

.github/workflows/pull_request.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ jobs:
5656
needs: [get_issue_number, get_commit_id]
5757
uses: ./.github/workflows/cdk_package_code.yml
5858
with:
59-
STACK_NAME: epsam
59+
STACK_NAME: epsam-pr-${{needs.get_issue_number.outputs.issue_number}}
6060
VERSION_NUMBER: PR-${{ needs.get_issue_number.outputs.issue_number }}
6161
COMMIT_ID: ${{ needs.get_commit_id.outputs.commit_id }}
6262

6363
release_code:
6464
needs: [get_issue_number, package_code, get_commit_id]
6565
uses: ./.github/workflows/cdk_release_code.yml
6666
with:
67-
STACK_NAME: epsam
67+
STACK_NAME: epsam-pr-${{needs.get_issue_number.outputs.issue_number}}
6868
TARGET_ENVIRONMENT: dev-pr
6969
VERSION_NUMBER: PR-${{ needs.get_issue_number.outputs.issue_number }}
7070
COMMIT_ID: ${{ needs.get_commit_id.outputs.commit_id }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
**/newman/
55
**/public/
66
**/coverage/
7+
.coverage
78
**/node_modules/
89
.#*
910
__pycache__/

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ repos:
2121
hooks:
2222
- id: flake8
2323

24+
- repo: https://github.com/psf/black
25+
rev: "24.10.0"
26+
hooks:
27+
- id: black
28+
language_version: python3
29+
args: [--line-length=120]
30+
2431
- repo: local
2532
hooks:
2633
- id: lint-githubactions

Makefile

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ install: install-python install-hooks install-node
1010

1111
install-python:
1212
poetry install
13+
cd packages/slackBotFunction && pip install -r requirements.txt && pip install -r requirements-test.txt
14+
cd packages/createIndexFunction && pip install -r requirements.txt && pip install -r requirements-test.txt
1315

1416
install-hooks: install-python
1517
poetry run pre-commit install --install-hooks --overwrite
@@ -27,7 +29,7 @@ git-secrets-docker-setup:
2729
export LOCAL_WORKSPACE_FOLDER=$(pwd)
2830
docker build -f https://raw.githubusercontent.com/NHSDigital/eps-workflow-quality-checks/refs/tags/v4.0.4/dockerfiles/nhsd-git-secrets.dockerfile -t git-secrets .
2931

30-
lint: lint-githubactions lint-githubaction-scripts
32+
lint: lint-githubactions lint-githubaction-scripts lint-black lint-flake8
3133

3234
lint-githubactions:
3335
actionlint
@@ -36,12 +38,23 @@ lint-githubaction-scripts:
3638
shellcheck ./scripts/*.sh
3739
shellcheck .github/scripts/*.sh
3840

39-
test: compile-node
40-
npm run test --workspace packages/cdk
41+
lint-black:
42+
poetry run black .
43+
44+
lint-flake8:
45+
poetry run flake8 .
46+
47+
test:
48+
cd packages/slackBotFunction && PYTHONPATH=. COVERAGE_FILE=coverage/.coverage python -m pytest
49+
cd packages/createIndexFunction && PYTHONPATH=. COVERAGE_FILE=coverage/.coverage python -m pytest
4150

4251
clean:
4352
rm -rf packages/cdk/coverage
4453
rm -rf packages/cdk/lib
54+
rm -rf packages/slackBotFunction/coverage
55+
rm -rf packages/slackBotFunction/.coverage
56+
rm -rf packages/createIndexFunction/coverage
57+
rm -rf packages/createIndexFunction/.coverage
4558
rm -rf cdk.out
4659
rm -rf .build
4760

@@ -66,7 +79,7 @@ aws-login:
6679
cfn-guard:
6780
./scripts/run_cfn_guard.sh
6881

69-
cdk-deploy: guard-stack_name
82+
cdk-deploy: guard-STACK_NAME
7083
REQUIRE_APPROVAL="$${REQUIRE_APPROVAL:-any-change}" && \
7184
VERSION_NUMBER="$${VERSION_NUMBER:-undefined}" && \
7285
COMMIT_ID="$${COMMIT_ID:-undefined}" && \
@@ -76,7 +89,7 @@ cdk-deploy: guard-stack_name
7689
--ci true \
7790
--require-approval $${REQUIRE_APPROVAL} \
7891
--context accountId=$$ACCOUNT_ID \
79-
--context stackName=$$stack_name \
92+
--context stackName=$$STACK_NAME \
8093
--context versionNumber=$$VERSION_NUMBER \
8194
--context commitId=$$COMMIT_ID \
8295
--context logRetentionInDays=$$LOG_RETENTION_IN_DAYS \
@@ -99,12 +112,12 @@ cdk-diff:
99112
npx cdk diff \
100113
--app "npx ts-node --prefer-ts-exts packages/cdk/bin/EpsAssistMeApp.ts" \
101114
--context accountId=$$ACCOUNT_ID \
102-
--context stackName=$$stack_name \
115+
--context stackName=$$STACK_NAME \
103116
--context versionNumber=$$VERSION_NUMBER \
104117
--context commitId=$$COMMIT_ID \
105118
--context logRetentionInDays=$$LOG_RETENTION_IN_DAYS
106119

107-
cdk-watch: guard-stack_name
120+
cdk-watch: guard-STACK_NAME
108121
REQUIRE_APPROVAL="$${REQUIRE_APPROVAL:-any-change}" && \
109122
VERSION_NUMBER="$${VERSION_NUMBER:-undefined}" && \
110123
COMMIT_ID="$${COMMIT_ID:-undefined}" && \
@@ -115,7 +128,7 @@ cdk-watch: guard-stack_name
115128
--ci true \
116129
--require-approval $${REQUIRE_APPROVAL} \
117130
--context accountId=$$ACCOUNT_ID \
118-
--context stackName=$$stack_name \
131+
--context stackName=$$STACK_NAME \
119132
--context versionNumber=$$VERSION_NUMBER \
120133
--context commitId=$$COMMIT_ID \
121134
--context logRetentionInDays=$$LOG_RETENTION_IN_DAYS \

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ When the token expires, you may need to reauthorise using `make aws-login`
103103
For deployment, the following environment variables are required:
104104

105105
- `ACCOUNT_ID`: AWS Account ID
106-
- `stack_name`: Name of the CloudFormation stack
106+
- `STACK_NAME`: Name of the CloudFormation stack
107107
- `VERSION_NUMBER`: Version number for the deployment
108108
- `COMMIT_ID`: Git commit ID
109109
- `LOG_RETENTION_IN_DAYS`: CloudWatch log retention period
@@ -137,10 +137,10 @@ There are `make` commands that are run as part of the CI pipeline and help alias
137137
#### CDK targets
138138
These are used to do common commands related to cdk
139139

140-
- `cdk-deploy` Builds and deploys the code to AWS. Requires `stack_name` environment variable.
140+
- `cdk-deploy` Builds and deploys the code to AWS. Requires `STACK_NAME` environment variable.
141141
- `cdk-synth` Converts the CDK code to cloudformation templates.
142142
- `cdk-diff` Runs cdk diff, comparing the deployed stack with the local CDK code to identify differences.
143-
- `cdk-watch` Syncs the code and CDK templates to AWS. This keeps running and automatically uploads changes to AWS. Requires `stack_name` environment variable.
143+
- `cdk-watch` Syncs the code and CDK templates to AWS. This keeps running and automatically uploads changes to AWS. Requires `STACK_NAME` environment variable.
144144

145145
#### Clean and deep-clean targets
146146

@@ -150,9 +150,11 @@ These are used to do common commands related to cdk
150150
#### Linting and testing
151151

152152
- `lint` Runs lint for GitHub Actions and scripts.
153+
- `lint-black` Runs black formatter on Python code.
154+
- `lint-flake8` Runs flake8 linter on Python code.
153155
- `lint-githubactions` Lints the repository's GitHub Actions workflows.
154156
- `lint-githubaction-scripts` Lints all shell scripts in `.github/scripts` using ShellCheck.
155-
- `test` Runs unit tests for CDK code.
157+
- `test` Runs unit tests for Lambda functions.
156158
- `cfn-guard` Runs cfn-guard against CDK resources.
157159
- `pre-commit` Runs pre-commit hooks on all files.
158160

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {Construct} from "constructs"
2+
import {RemovalPolicy} from "aws-cdk-lib"
3+
import {
4+
TableV2,
5+
AttributeType,
6+
Billing,
7+
TableEncryptionV2
8+
} from "aws-cdk-lib/aws-dynamodb"
9+
import {Key} from "aws-cdk-lib/aws-kms"
10+
11+
export interface DynamoDbTableProps {
12+
readonly tableName: string
13+
readonly partitionKey: {
14+
name: string
15+
type: AttributeType
16+
}
17+
readonly timeToLiveAttribute?: string
18+
}
19+
20+
export class DynamoDbTable extends Construct {
21+
public readonly table: TableV2
22+
public readonly kmsKey: Key
23+
24+
constructor(scope: Construct, id: string, props: DynamoDbTableProps) {
25+
super(scope, id)
26+
27+
this.kmsKey = new Key(this, "TableKey", {
28+
enableKeyRotation: true,
29+
description: `KMS key for ${props.tableName} DynamoDB table encryption`,
30+
removalPolicy: RemovalPolicy.DESTROY
31+
})
32+
this.kmsKey.addAlias(`alias/${props.tableName}-dynamodb-key`)
33+
34+
this.table = new TableV2(this, props.tableName, {
35+
tableName: props.tableName,
36+
partitionKey: props.partitionKey,
37+
billing: Billing.onDemand(),
38+
timeToLiveAttribute: props.timeToLiveAttribute,
39+
pointInTimeRecovery: true,
40+
removalPolicy: RemovalPolicy.DESTROY,
41+
encryption: TableEncryptionV2.customerManagedKey(this.kmsKey)
42+
})
43+
}
44+
}

packages/cdk/constructs/OpenSearchCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {Construct} from "constructs"
22
import {CfnCollection, CfnSecurityPolicy, CfnAccessPolicy} from "aws-cdk-lib/aws-opensearchserverless"
33

44
export interface OpenSearchCollectionProps {
5-
collectionName: string
6-
principals: Array<string>
5+
readonly collectionName: string
6+
readonly principals: Array<string>
77
}
88

99
export class OpenSearchCollection extends Construct {

0 commit comments

Comments
 (0)