1
0

taiga_sync_fixer.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import requests, urllib3
  2. urllib3.disable_warnings()
  3. # Configuration – adjust as needed
  4. TAIGA_URL = 'https://192.168.130.161/taiga/api/v1'
  5. USERNAME = 'lanfr1904@outlook.com'
  6. PASSWORD = 'BTSai123'
  7. PROJECT_ID = 21
  8. DEFAULT_TASK_SUBJECT = 'Auto‑generated task (define details)'
  9. DEFAULT_POINTS = 1
  10. # Authenticate and obtain token
  11. auth_resp = requests.post(
  12. f'{TAIGA_URL}/auth',
  13. json={'type': 'normal', 'username': USERNAME, 'password': PASSWORD},
  14. verify=False
  15. ).json()
  16. token = auth_resp.get('auth_token')
  17. if not token:
  18. raise RuntimeError('Authentication to Taiga failed')
  19. headers = {'Authorization': f'Bearer {token}'}
  20. # Helper functions
  21. def get_user_stories():
  22. resp = requests.get(
  23. f'{TAIGA_URL}/userstories?project={PROJECT_ID}',
  24. headers=headers,
  25. verify=False
  26. )
  27. resp.raise_for_status()
  28. return resp.json()
  29. def get_tasks_for_us(us_id):
  30. resp = requests.get(
  31. f'{TAIGA_URL}/tasks?user_story={us_id}',
  32. headers=headers,
  33. verify=False
  34. )
  35. resp.raise_for_status()
  36. return resp.json()
  37. def create_task(us_id):
  38. payload = {
  39. 'subject': DEFAULT_TASK_SUBJECT,
  40. 'user_story': us_id,
  41. 'project': PROJECT_ID,
  42. 'status': 101 # Status 101 = "New" for project 21
  43. }
  44. resp = requests.post(
  45. f'{TAIGA_URL}/tasks',
  46. json=payload,
  47. headers=headers,
  48. verify=False
  49. )
  50. if not resp.ok:
  51. print("Error creating task:", resp.text)
  52. resp.raise_for_status()
  53. return resp.json()
  54. def set_points(us_id, points, version):
  55. payload = {
  56. 'total_points': points,
  57. 'version': version
  58. }
  59. resp = requests.patch(
  60. f'{TAIGA_URL}/userstories/{us_id}',
  61. json=payload,
  62. headers=headers,
  63. verify=False
  64. )
  65. if not resp.ok:
  66. print("Error setting points:", resp.text)
  67. resp.raise_for_status()
  68. return resp.json()
  69. def main():
  70. us_list = get_user_stories()
  71. for us in us_list:
  72. # 1️⃣ Ensure at least one task exists
  73. tasks = get_tasks_for_us(us['id'])
  74. if not tasks:
  75. print(f"US #{us['ref']} missing tasks – creating default task")
  76. create_task(us['id'])
  77. # 2️⃣ Ensure story has points
  78. if not us.get('total_points'):
  79. print(f"US #{us['ref']} missing points – setting to {DEFAULT_POINTS}")
  80. set_points(us['id'], DEFAULT_POINTS, us['version'])
  81. if __name__ == '__main__':
  82. main()