gogmilestone2.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from flask import Flask, request, jsonify
  2. from auth import login, get_csrf_token
  3. from milestone import update_milestone, get_milestone, append_item_to_identifier
  4. from utils import find_username_id, generate_evoluz_link, find_task_type
  5. app = Flask(__name__)
  6. EVOLUZ_URL = "http://192.168.1.33:5050/apps/team/detail?"
  7. @app.route('/webhook', methods=['POST'])
  8. def handle_webhook():
  9. try:
  10. payload = request.get_json()
  11. if not payload:
  12. return 'Invalid JSON payload', 400
  13. if 'pull_request' in payload:
  14. pr = payload.get('pull_request', {})
  15. pr_title = pr.get('title')
  16. pr_username = pr.get('user').get('username')
  17. pr_branch = pr.get('head_branch')
  18. pr_branch_data = find_task_type(pr_branch)
  19. pr_data = {
  20. 'title': pr_title,
  21. 'username': pr_username.capitalize(),
  22. 'branch': pr_branch,
  23. 'task_type': pr_branch_data.get("task_type"),
  24. 'user_id': find_username_id(pr_username),
  25. 'link_evoluz': generate_evoluz_link(find_username_id(pr_username), pr_branch_data.get("task_id"), EVOLUZ_URL)
  26. }
  27. if update_milestone(pr_data):
  28. print("Milestone updated successfully.")
  29. else:
  30. print("Failed to update milestone.")
  31. return jsonify({'status': 'success'}), 200
  32. except Exception as e:
  33. print(f"Error: {e}")
  34. return 'Internal server error', 500
  35. @app.route('/', methods=['GET'])
  36. def index():
  37. return "Webhook server is running."
  38. if __name__ == '__main__':
  39. if login():
  40. print("Initial login successful.")
  41. else:
  42. print("Initial login failed.")
  43. app.run(port=4001)