From 8e100f71706cc43ceebe091ba9933434be7e843f Mon Sep 17 00:00:00 2001 From: Ching Date: Wed, 20 Mar 2024 17:05:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20linear=20webhook?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E5=92=8C=20discord=20=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone.yml | 73 ++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 14 +++++++++ app.py | 43 +++++++++++++++++++++++++++ docker-compose.yml | 10 +++++++ 4 files changed, 140 insertions(+) create mode 100644 .drone.yml create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 docker-compose.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..70ae7e9 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,73 @@ +kind: pipeline +type: docker +name: default + +steps: + - name: build-and-push + image: plugins/docker + settings: + username: + from_secret: docker_username + password: + from_secret: docker_password + repo: git.tunpok.com/ching/linear-discord-webhook + registry: git.tunpok.com + tags: latest + + # - name: notify + # image: plugins/webhook + # settings: + # urls: http://bark.tunpok.com/UZ6zC82bKRjQaXiVkosVWh/ + # content_type: application/json + # template: | + # { + # "title": "{{#success build.status}}🟢{{else}}🔴{{/success}} Drone Build #{{ build.number }} {{ build.status }} {{#success build.status}}🟢{{else}}🔴{{/success}}", + # "body": "Project: {{ repo.name }}\nBranch: {{ build.branch }}\nCommit: {{ truncate build.commit 8 }}", + # "group": "drone", + # "url": "{{ build.link }}", + # "icon": "https://static-00.iconduck.com/assets.00/drone-icon-2048x2048-6zua2vkz.png" + # } + # when: + # status: [success, failure] + # https://discord.com/api/webhooks/1213409068559900683/szl0AICZwA1V82dg8Vrc_xqOCl1WwnxktlQdf4cdILswR-xZBI5-JOdqGSD8dVUNcUlH + - name: deploy + image: appleboy/drone-ssh + settings: + host: + - 148.135.109.242 + username: root + key: + from_secret: ssh_key + passphrase: + from_secret: ssh_passphrase + port: 22 + command_timeout: 2m + script: + - echo "Pull new image" + - docker pull git.tunpok.com/ching/linear-discord-webhook:latest + - echo "Go to the project directory" + - cd /root/develop/linear-discord-webhook + - echo "Restart the container" + - docker-compose down && docker-compose up -d + + - name: discord notification + image: appleboy/drone-discord + settings: + webhook_id: + from_secret: discord_webhook_id + webhook_token: + from_secret: discord_webhook_token + # message: | + # Drone Build #${DRONE_BUILD_NUMBER} ${DRONE_BUILD_STATUS} + # Project: ${DRONE_REPO_NAME} + # Branch: ${DRONE_BRANCH} + # Commit: ${DRONE_COMMIT_SHA:0:8} + # [Build Log](${DRONE_BUILD_LINK}) + when: + status: [success, failure] + + +volumes: + - name: dockersock + host: + path: /var/run/docker.sock diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b2d25b2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# 使用自定义 Docker Registry 中的官方 Python 镜像作为基础镜像 +FROM git.tunpok.com/ching/python-env:latest + +# 设置工作目录 +WORKDIR /app + +# 将当前目录下的所有文件复制到容器中 +COPY . . + +# port number +EXPOSE 5000 + +# run flask app +CMD ["python", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..4b58402 --- /dev/null +++ b/app.py @@ -0,0 +1,43 @@ +from flask import Flask, request, jsonify +import apprise +import os +from loguru import logger + +app = Flask(__name__) + +DISCORD_WEBHOOK_URL = os.environ.get('DISCORD_WEBHOOK_URL') +DISCORD_WEBHOOK_ID = DISCORD_WEBHOOK_URL.split('/')[-2] +DISCORD_WEBHOOK_TOKEN = DISCORD_WEBHOOK_URL.split('/')[-1] + +@app.route('/linear/issue', methods=['POST']) +def linear_issue(): + """ https://developers.linear.app/docs/graphql/webhooks#the-webhook-payload + """ + if request.headers.get('Linear-Event') != 'issue': + logger.error('Invalid event type: %s', request.headers.get('Linear-Event')) + return jsonify({'message': 'Invalid event type'}), 400 + data = request.json + logger.info('Received issue webhook: %s', data) + if data['action'] != 'update': + logger.warning('Ignoring issue action: %s', data['action']) + return jsonify({'message': 'Ignoring issue action'}), 200 + # Send Discord message + apobj = apprise.Apprise() + apobj.add(f'discord://{DISCORD_WEBHOOK_ID}/{DISCORD_WEBHOOK_TOKEN}/?avatar=No&format=markdown') + apobj.asset.app_id = None + apobj.notify(body='Issue status changed') + + # if data['updatedFrom'].get('statusId') != data['issue']['statusId']: + # logger.info('Issue status changed: %s', data['issue']['status']['name']) + # # Send Discord message with embeded content + # # set apprise format to markdown + # apobj.notify( + # body=f'Issue status changed: **{data["issue"]["status"]["name"]}**', + # title=data['issue']['title'], + # body_format=apprise.AppriseFormat.MARKDOWN + # ) + + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=5000) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7aeb993 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: '3' +services: + barcode-helper: + image: git.tunpok.com/ching/linear-discord-webhook:latest + container_name: linear-discord-webhook + restart: always + environment: + - DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/1234567890/ABCDEFGHIJKLMN + ports: + - 9289:5000