From 9ae9874491941bb8327c93febde95b3206cae6fd Mon Sep 17 00:00:00 2001 From: Ching Date: Wed, 20 Mar 2024 17:47:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=20discord=20?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=96=87=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 4b58402..4bdb53b 100644 --- a/app.py +++ b/app.py @@ -13,19 +13,31 @@ DISCORD_WEBHOOK_TOKEN = DISCORD_WEBHOOK_URL.split('/')[-1] 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')) + 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) + logger.info('Received issue webhook: %s' % data) + # {'action': 'update', 'actor': {'id': '38c20f6d-8088-461c-9ea3-9f36e185cb62', 'name': 'Ching'}, 'createdAt': '2024-03-20T09:23:00.785Z', 'data': {'id': '3f0d5021-eda8-486a-93b8-a2bd9ec461a3', 'createdAt': '2024-03-20T07:11:44.535Z', 'updatedAt': '2024-03-20T09:23:00.785Z', 'number': 21, 'title': 'etsttse', 'priority': 0, 'boardOrder': 0, 'sortOrder': -7967.13, 'completedAt': '2024-03-20T09:23:00.773Z', 'labelIds': [], 'teamId': '1f28d52c-c91a-4c48-8ca8-96425dfd6516', 'previousIdentifiers': [], 'creatorId': '38c20f6d-8088-461c-9ea3-9f36e185cb62', 'assigneeId': '38c20f6d-8088-461c-9ea3-9f36e185cb62', 'stateId': '5dbc5296-8275-4271-a595-bae6465f17c9', 'priorityLabel': 'No priority', 'botActor': {'id': '5c07d33f-5e8f-484b-8100-67908589ec45', 'type': 'workflow', 'name': 'Linear', 'avatarUrl': 'https://static.linear.app/assets/pwa/icon_maskable_512.png'}, 'identifier': 'TUN-21', 'url': 'https://linear.app/tunpok/issue/TUN-21/etsttse', 'assignee': {'id': '38c20f6d-8088-461c-9ea3-9f36e185cb62', 'name': 'Ching'}, 'state': {'id': '5dbc5296-8275-4271-a595-bae6465f17c9', 'color': '#5e6ad2', 'name': 'Done', 'type': 'completed'}, 'team': {'id': '1f28d52c-c91a-4c48-8ca8-96425dfd6516', 'key': 'TUN', 'name': 'Dev'}, 'subscriberIds': ['38c20f6d-8088-461c-9ea3-9f36e185cb62'], 'labels': [], 'description': 'sdgsgesg', 'descriptionData': '{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"sdgsgesg"}]}]}'}, 'updatedFrom': {'updatedAt': '2024-03-20T09:21:05.589Z', 'sortOrder': -62.54, 'completedAt': None, 'stateId': '4f02237e-e233-410a-a4f2-3c5ff75e6927', 'canceledAt': '2024-03-20T09:21:05.572Z'}, 'url': 'https://linear.app/tunpok/issue/TUN-21/etsttse', 'type': 'Issue', 'organizationId': '60b84a77-cde4-47dd-8f56-df41efc3a899', 'webhookTimestamp': 1710926580871, 'webhookId': '76f3898f-8fb2-4d79-8c42-4ef926434fff'} if data['action'] != 'update': - logger.warning('Ignoring issue action: %s', data['action']) + logger.warning('Ignoring issue action: %s' % data['action']) return jsonify({'message': 'Ignoring issue action'}), 200 # Send Discord message + if not data['updateFrom'].get('stateId'): + logger.warning('Ignoring issue changes') + return jsonify({'message': 'Ignoring issue changes'}), 200 apobj = apprise.Apprise() - apobj.add(f'discord://{DISCORD_WEBHOOK_ID}/{DISCORD_WEBHOOK_TOKEN}/?avatar=No&format=markdown') + apobj.add(f'discord://{DISCORD_WEBHOOK_ID}/{DISCORD_WEBHOOK_TOKEN}/?avatar=No&format=markdown&url={data["data"]["url"]}') apobj.asset.app_id = None - apobj.notify(body='Issue status changed') + + title = data['data']['identifier'] + ' - ' + data['data']['title'] + + body = f"状态变更。\n#Status\n{data['data']['state']['name']}" + notify_type = apprise.NotifyType.INFO + if data['data']['state']['type'] == 'completed': + notify_type = apprise.NotifyType.SUCCESS + apobj.notify(body=body, title=title, notify_type=notify_type) + return jsonify({'message': 'Ok'}), 200 # if data['updatedFrom'].get('statusId') != data['issue']['statusId']: # logger.info('Issue status changed: %s', data['issue']['status']['name'])