Skip to content

Commit

Permalink
feat: implement sending webhook messages
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Aug 10, 2024
1 parent 4d79f4b commit 98eee7c
Show file tree
Hide file tree
Showing 21 changed files with 1,851 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"plugin:prettier/recommended"
],
"parserOptions": {
"project": "./tsconfig.json"
"project": "./tsconfig.base.json"
},
"rules": {
"n/no-sync": "off",
Expand Down
45 changes: 29 additions & 16 deletions .github/workflows/continuous-delivery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,41 @@ on:
push:
branches:
- main
paths:
- 'src/**'
- 'concourse/**'
- '.github/workflows/continuous-delivery.yml'
- 'README.md'
- 'Dockerfile'

jobs:
Publish:
name: Publish Next
name: Publish image to container registries
runs-on: ubuntu-latest
if: false
steps:
- name: Checkout Project
uses: actions/checkout@v4
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3.6.1
- name: Login to DockerHub
uses: docker/login-action@v3.3.0
with:
fetch-depth: 0
- name: Add TypeScript problem matcher
run: echo "::add-matcher::.github/problemMatchers/tsc.json"
- name: Use Node.js v20
uses: actions/setup-node@v4
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3.3.0
with:
node-version: 20
cache: yarn
registry-url: https://registry.yarnpkg.com/
- name: Install Dependencies
run: yarn --immutable
- name: Bump version
run: yarn bump --preid "next.$(git rev-parse --verify --short HEAD)" --skip-changelog
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v6.5.0
with:
push: true
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: |
linux/amd64
linux/arm64
tags: ghcr.io/favware/concource-discord-webhook-resource:alpha
54 changes: 53 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ on:
default: false

jobs:
PublishPackage:
PublishVersion:
name: Publish @favware/discord-application-emojis-manager
runs-on: ubuntu-latest
if: github.repository_owner == 'favware'
outputs:
new_version: ${{ steps.StoreNewVersion.outputs.new_version }}
steps:
- name: Checkout Project
uses: actions/checkout@v4
Expand Down Expand Up @@ -46,3 +48,53 @@ jobs:
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Store new version
id: StoreNewVersion
run: |
new_version=$(node -e "console.log(require('./package.json').version)")
echo "new_version=${new_version}" >> "$GITHUB_OUTPUT"
PublishImage:
name: Publish image to container registries
runs-on: ubuntu-latest
needs: PublishVersion
steps:
- name: Checkout Project
uses: actions/checkout@v4
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3.6.1
- name: Login to DockerHub
uses: docker/login-action@v3.3.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3.3.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v6.5.0
with:
push: true
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: |
linux/amd64
linux/arm64
tags: |
favware/concource-discord-webhook-resource:${{ needs.PublishVersion.outputs.new_version }}
favware/concource-discord-webhook-resource:latest
ghcr.io/favware/concource-discord-webhook-resource:${{ needs.PublishVersion.outputs.new_version }}
ghcr.io/favware/concource-discord-webhook-resource:latest
- name: Update repo description
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: favware/concource-discord-webhook-resource
enable-url-completion: true
short-description: Extensive Pokemon GraphQL API

5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"source.fixAll": "explicit",
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},
"files.associations": {
"concourse/check": "shellscript",
"concourse/in": "shellscript",
"concourse/out": "shellscript"
}
}
59 changes: 59 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## Base state ##

FROM node:20-alpine3.20 AS base

WORKDIR /usr/src/app

ENV YARN_DISABLE_GIT_HOOKS=1
ENV CI=true

RUN set -ex \
&& apk add --no-cache dumb-init ca-certificates

COPY --chown=node:node package.json .
COPY --chown=node:node yarn.lock .
COPY --chown=node:node .yarnrc.yml .
COPY --chown=node:node .yarn/ .yarn/

COPY --chown=node:node ./concourse/check /assets/check
COPY --chown=node:node ./concourse/in /assets/in
COPY --chown=node:node ./concourse/out /assets/out

RUN yarn install --immutable

ENTRYPOINT ["dumb-init", "--"]

## Builder stage ##

FROM base AS builder

COPY --from=base --chown=node:node /usr/src/app/node_modules/ /usr/src/app/node_modules/

COPY --chown=node:node tsconfig.base.json .
COPY --chown=node:node tsup.config.ts .
COPY --chown=node:node src/ src/

RUN yarn build

## Runtime stage ##

FROM base AS runtime

ENV NODE_ENV="production"
ENV NODE_OPTIONS="--enable-source-maps"

# Create a directory for Concourse scripts
RUN mkdir -p /opt/resource

# Copy Concourse scripts
COPY --from=base /assets/check /opt/resource/
COPY --from=base /assets/in /opt/resource/
COPY --from=base /assets/out /opt/resource/

# Copy NodeJS scripts
COPY --from=base --chown=node:node /usr/src/app/node_modules/ /usr/src/app/node_modules/
COPY --from=builder --chown=node:node /usr/src/app/dist/ /usr/src/app/dist/

USER node

CMD echo "This container is not used directly."; exit 1
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ https://discord.com/developers/docs/resources/webhook#execute-webhook.**
- `username` (_string_): Override the default username of the webhook
- `avatar_url` (_string)_: Any text wanted to ultimately appear on the page as
the title of the message.
- `allowed_mentions_roles` (_array of strings_): Roles that are allowed to be
mentioned (default: `[]`).
- `allowed_mentions_users` (_array of strings_): Users that are allowed to be
mentioned (default: `[]`).
- `allowed_mentions_everyone` (_boolean_): Whether `@everyone` and `@here` are
allowed to be mentioned (default: `false`)
- `flags` (_integer_):
- `allowed_mentions` (_object_): Allowed mentions for the message.
- `.roles` (_array of strings_): Roles that are allowed to be mentioned
(default: `[]`).
- `.users` (_array of strings_): Users that are allowed to be mentioned
(default: `[]`).
- `flags` (_object_): An object of the allowed values from
[message flags](https://discord.com/developers/docs/resources/message#message-object-message-flags)
combined as a [bitfield](https://en.wikipedia.org/wiki/Bit_field) (only
`SUPPRESS_EMBEDS` and `SUPPRESS_NOTIFICATIONS` can be set can be set)
- `.suppress_embeds` (_boolean_): Do not include any embeds when serializing
this message (default: `false`).
- `.suppress_notifications` (_boolean_): This message will not trigger push
and desktop notifications (default: `false`).
- `verbose` (_boolean_): Whether to print verbose messages during the process (useful for debugging) (default: `false`).

## Usage Examples

Expand All @@ -132,6 +134,34 @@ jobs:
http://my.concourse.url/builds/${BUILD_ID}
```

**With all parameters**

```yaml
jobs:
- name: discord-send
plan:
- put: discord-notification
params:
content: |
The build had a result. Check it out at:
http://my.concourse.url/teams/${BUILD_TEAM_NAME}/pipelines/${BUILD_PIPELINE_NAME}/jobs/${BUILD_JOB_NAME}/builds/${BUILD_NAME}
or at:
http://my.concourse.url/builds/${BUILD_ID}
username: ConcourseCI
avatar_url: https://github.com/cloudfoundry-community.png
allowed_mentions:
roles:
- 123456789012345678
- 234567890123456789
users:
- 345678901234567890
- 456789012345678901
flags:
suppress_embeds: false
suppress_notifications: false
verbose: true
```

## Buy us some doughnuts

Favware projects are and always will be open source, even if we don't get
Expand Down
25 changes: 25 additions & 0 deletions concourse/check
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

# The MIT License (MIT)
# Copyright (c) 2017-2020 James Hunt
# Copyright (c) 2021-present Benjamin Gandon, Gstack
# Retrieved from: https://github.com/cloudfoundry-community/slack-notification-resource/blob/62db24f60f5b442b60d7160edec533662d26c23a/check

set -e

exec 3>&1 # make stdout available as fd 3 for the result
exec 1>&2 # redirect all output to stderr for logging

PAYLOAD=$(mktemp /tmp/resource-check.XXXXXX)

cat > "$PAYLOAD" <&0

TS=$(jq '.version.timestamp // empty' < "$PAYLOAD")

if [ -z "$TS" ]; then
echo '[]' >&3
else
jq -n "[
{ timestamp: $TS }
]" >&3
fi
24 changes: 24 additions & 0 deletions concourse/in
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

# The MIT License (MIT)
# Copyright (c) 2017-2020 James Hunt
# Copyright (c) 2021-present Benjamin Gandon, Gstack
# Retrieved from: https://github.com/cloudfoundry-community/slack-notification-resource/blob/62db24f60f5b442b60d7160edec533662d26c23a/in

set -e

exec 3>&1 # make stdout available as fd 3 for the result
exec 1>&2 # redirect all output to stderr for logging

PAYLOAD=$(mktemp /tmp/resource-in.XXXXXX)

cat > "$PAYLOAD" <&0

TS=$(jq '.version.timestamp // empty' < "$PAYLOAD")
[ -z "$TS" ] && TS='"none"'

jq -n "{
version: {
timestamp: $TS
}
}" >&3
Loading

0 comments on commit 98eee7c

Please sign in to comment.