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) # {'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']) 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&url={data["data"]["url"]}') apobj.asset.app_id = None 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']) # # 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)