Skip to content

Commit

Permalink
OPSEXP-2656 Add slack file upload composite action (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
gionn authored Nov 6, 2024
1 parent b5166f4 commit 59a39f5
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 33 deletions.
46 changes: 46 additions & 0 deletions .github/actions/slack-file-upload/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: slack-file-upload
description: Upload file to slack channel
inputs:
slack-token:
description: 'Slack API token'
required: true
slack-channel-id:
description: 'Slack channel ID'
required: true
file-path:
description: 'File to upload'
required: true
file-title:
description: 'Title of file'
python-version:
description: 'Python version'
required: false
default: '3.9'
runs:
using: composite
steps:
- name: Setup Python
uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
id: setup-python
with:
python-version: ${{ inputs.python-version }}

- name: Workaround for hashFiles not working outside current workspace
shell: bash
run: cp ${{ github.action_path }}/requirements.txt requirements-slack.txt

- uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a # v4.1.2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements-slack.txt') }}

- name: Install requirements via pip
shell: bash
run: ${{ steps.setup-python.outputs.python-path }} -m pip install -r ${{ github.action_path }}/requirements.txt

- name: Upload ${{ inputs.file-path }} to Slack
shell: bash
env:
SLACK_BOT_TOKEN: ${{ inputs.slack-token }}
SLACK_CHANNEL_ID: ${{ inputs.slack-channel-id }}
run: ${{ steps.setup-python.outputs.python-path }} ${{ github.action_path }}/slack_file_upload.py "${{ inputs.file-path }}" "${{ inputs.file-title }}"
1 change: 1 addition & 0 deletions .github/actions/slack-file-upload/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
slack_sdk==3.33.3
33 changes: 33 additions & 0 deletions .github/actions/slack-file-upload/slack_file_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import sys
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

def upload_file_to_slack(token, channel_id, file_path, title):
client = WebClient(token=token)

try:
response = client.files_upload_v2(
channel=channel_id,
file=file_path,
title=title
)
print(f"File uploaded successfully: {response['file']['id']}")
except SlackApiError as e:
print(f"Error uploading file: {e.response['error']}")
raise e

if __name__ == "__main__":
if 'SLACK_BOT_TOKEN' not in os.environ:
raise ValueError('SLACK_BOT_TOKEN is not set.')
if 'SLACK_CHANNEL_ID' not in os.environ:
raise ValueError('SLACK_CHANNEL_ID is not set.')
if len(sys.argv) < 2:
raise ValueError('File path must be provided as the first argument.')

SLACK_BOT_TOKEN = os.environ['SLACK_BOT_TOKEN']
CHANNEL_ID = os.environ['SLACK_CHANNEL_ID']
FILE_PATH = sys.argv[1]
TITLE = sys.argv[2] if len(sys.argv) > 2 else None

upload_file_to_slack(SLACK_BOT_TOKEN, CHANNEL_ID, FILE_PATH, TITLE)
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,14 @@ updates:
catch-all:
patterns:
- "*"
- package-ecosystem: "github-actions"
directory: "/.github/actions/slack-file-upload"
schedule:
interval: "weekly"
groups:
catch-all:
patterns:
- "*"
- package-ecosystem: "github-actions"
directory: "/.github/actions/update-deployment-runtime-versions"
schedule:
Expand Down
67 changes: 35 additions & 32 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ Here follows the list of GitHub Actions topics available in the current document
- [free-hosted-runner-disk-space](#free-hosted-runner-disk-space)
- [get-branch-name](#get-branch-name)
- [get-build-info](#get-build-info)
- [gh-cache-cleanup-on-merge](#gh-cache-cleanup-on-merge)
- [git-check-existing-tag](#git-check-existing-tag)
- [get-commit-message](#get-commit-message)
- [gh-cache-cleanup-on-merge](#gh-cache-cleanup-on-merge)
- [git-commit-changes](#git-commit-changes)
- [git-latest-tag](#git-latest-tag)
- [github-check-upcoming-runs](#github-check-upcoming-runs)
Expand Down Expand Up @@ -96,12 +96,12 @@ Here follows the list of GitHub Actions topics available in the current document
- [setup-rancher-cli](#setup-rancher-cli)
- [setup-terraform-docs](#setup-terraform-docs)
- [setup-updatebot](#setup-updatebot)
- [slack-file-upload](#slack-file-upload)
- [update-deployment-runtime-versions](#update-deployment-runtime-versions)
- [update-pom-to-next-pre-release](#update-pom-to-next-pre-release)
- [update-project-base-tag](#update-project-base-tag)
- [validate-maven-versions](#validate-maven-versions)
- [veracode](#veracode)
- [github cache cleanup](#github-cache-cleanup)
- [Reusable workflows provided by us](#reusable-workflows-provided-by-us)
- [helm-publish-new-package-version.yml](#helm-publish-new-package-versionyml)
- [terraform](#terraform)
Expand Down Expand Up @@ -598,6 +598,26 @@ Loads the name of the branch on which the action was called into `BRANCH_NAME` e
- uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@ref
```

### gh-cache-cleanup-on-merge

Performs the cleanup of all cache entries related with already closed PR

```yaml
name: Cleanup caches for work branch
on:
pull_request:
types:
- closed
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- uses: Alfresco/alfresco-build-tools/.github/actions/gh-cache-cleanup-on-merge@ref
with:
token: ${{ secrets.GH_TOKEN }}
```

### git-check-existing-tag

Checks if a tag with the given name already exists for this remote repository. Returns the output named `exists` with value `'true'` or `'false'`.
Expand All @@ -621,16 +641,6 @@ This action requires a checkout with fetch-depth option as follow:
- uses: Alfresco/alfresco-build-tools/.github/actions/get-commit-message@ref
```

### gh-cache-cleanup-on-merge

Performs the cleanup of cache entries related with already closed PR:

```yaml
- uses: Alfresco/alfresco-build-tools/.github/actions/gh-cache-cleanup-on-merge@ref
with:
token: ${{ secrets.BOT_GITHUB_TOKEN }}
```

### git-commit-changes

Commits local changes after configuring git user and showing the status of what is going be committed.
Expand Down Expand Up @@ -1617,6 +1627,19 @@ Install the updatebot binary from GitHub Releases and add it to the PATH.
version: "1.1.60"
```

### slack-file-upload

Uploads a file to a Slack channel.

```yaml
- uses: Alfresco/alfresco-build-tools/.github/actions/slack-file-upload@ref
with:
slack-token: ${{ secrets.SLACK_BOT_TOKEN }}
slack-channel-id: 'channel-id' # not the channel name
file-path: 'path/to/file'
file-title: 'file description' # optional
```

### update-deployment-runtime-versions

For more information, see [update-deployment-runtime-versions](../.github/actions/update-deployment-runtime-versions/action.yml).
Expand Down Expand Up @@ -1671,26 +1694,6 @@ This way, the agent-based scan results will be added in the latest promoted scan
srcclr-project-ext: '' # optional, allows for directing scan results to Veracode project named: <default project name>/<srcclr-project-ext>
```

### github cache cleanup

Performs the cleanup of all cache entries related with already closed PR

```yaml
name: Cleanup caches for work branch
on:
pull_request:
types:
- closed
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- uses: Alfresco/alfresco-build-tools/.github/actions/gh-cache-cleanup-on-merge@ref
with:
token: ${{ secrets.GH_TOKEN }}
```

## Reusable workflows provided by us

### helm-publish-new-package-version.yml
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v8.2.0
v8.3.0

0 comments on commit 59a39f5

Please sign in to comment.