Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPSEXP-2656 Add slack file upload composite action #808

Merged
merged 12 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/actions/slack-file-upload/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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'
runs:
using: composite
steps:
- name: Setup Python
uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
with:
python-version: "3.9"

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

- uses: actions/cache@v4
gionn marked this conversation as resolved.
Show resolved Hide resolved
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements-slack.txt') }}

- name: Install requirements via pip
shell: bash
run: 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: python3 ${{ 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
56 changes: 35 additions & 21 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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)
- [git-commit-changes](#git-commit-changes)
Expand Down Expand Up @@ -83,10 +84,10 @@ Here follows the list of GitHub Actions topics available in the current document
- [setup-github-release-binary](#setup-github-release-binary)
- [setup-java-build](#setup-java-build)
- [setup-kind](#setup-kind)
- [slack-file-upload](#slack-file-upload)
- [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 @@ -567,6 +568,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 Down Expand Up @@ -1483,6 +1504,19 @@ Spin up a local kubernetes cluster with nginx ingress exposing http/https ports.
helm install acs ./helm/chart
```

### 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-project-base-tag

Used to update a base tag in the release descriptor. It will add or update the
Expand Down Expand Up @@ -1529,26 +1563,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
Loading