generate_changelog.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import subprocess
  2. import re
  3. import sys
  4. RELEASE=sys.argv[1]
  5. DATE=sys.argv[2]
  6. ORIGIN=sys.argv[3]
  7. START_INSERT=5
  8. TEST_KEYWORDS = ['test', 'script', 'storybook', 'e2e']
  9. BREAKING_KEYWORDS = ['break', 'deprecate']
  10. CHANGED_KEYWORDS = ['update', 'change']
  11. FIXED_KEYWORDS = ['fix', 'repair', 'bug']
  12. def incrementLine():
  13. global START_INSERT
  14. START_INSERT+=1
  15. return START_INSERT
  16. def generateJQLQuery(ticket_list):
  17. last_ticket=len(ticket_list) - 1
  18. jql_query='key in('
  19. for i,ticket in enumerate(ticket_list):
  20. clean_ticket=ticket.strip().replace(':','')
  21. if( i == last_ticket ):
  22. jql_query+=clean_ticket+')'
  23. else:
  24. jql_query+=clean_ticket+','
  25. print('~~~~~~~~TICKETS IN RELEASE~~~~~~~~')
  26. print(jql_query.replace(' ','-'))
  27. print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
  28. def checkKeyWords(list_keywords, commit):
  29. for word in list_keywords:
  30. if(word in commit):
  31. return True
  32. return False
  33. def generateChangeLog(release, date, origin):
  34. git_diff=subprocess.Popen(['git', 'log', '--no-merges', '--oneline', "--pretty=split:'%s'", origin+'/master...HEAD'],
  35. stdout=subprocess.PIPE,
  36. stderr=subprocess.STDOUT)
  37. stdout,stderr = git_diff.communicate()
  38. clean_sdout = str(stdout).replace('b\"', '').replace('\n', '').replace('\\n','').replace("'", '').replace('\"','')
  39. commit_array=clean_sdout.split('split:')
  40. commit_array.pop(0)
  41. breaking=[]
  42. added=[]
  43. changed=[]
  44. fixed=[]
  45. jql_query=[]
  46. for i,commit in enumerate(commit_array):
  47. jira_key_regex=re.match('(''|\s)M3(-|\s)\d{1,5}(-|\s|:)', commit)
  48. if( not (jira_key_regex is None) ):
  49. jira_key=jira_key_regex.group(0)
  50. jql_query.append(jira_key)
  51. commit_array[i]=commit.lstrip(jira_key)
  52. if(checkKeyWords(TEST_KEYWORDS, commit.lower())):
  53. commit_array.pop(i)
  54. continue
  55. if(checkKeyWords(BREAKING_KEYWORDS, commit.lower())):
  56. breaking.append(commit_array[i])
  57. continue
  58. if(checkKeyWords(CHANGED_KEYWORDS, commit.lower())):
  59. changed.append(commit_array[i])
  60. continue
  61. if(checkKeyWords(FIXED_KEYWORDS, commit.lower())):
  62. fixed.append(commit_array[i])
  63. continue
  64. added.append(commit_array[i])
  65. generateJQLQuery(jql_query)
  66. read_change_log=open('CHANGELOG.md', 'r')
  67. change_log_lines=read_change_log.readlines()
  68. read_change_log.close()
  69. change_log_lines.insert(START_INSERT,'\n')
  70. change_log_lines.insert(incrementLine(),'## [%s] - %s\n'%(release,date))
  71. change_log_lines.insert(incrementLine(),'\n')
  72. if( breaking ):
  73. change_log_lines.insert(incrementLine(),'### Breaking:\n')
  74. for commit in breaking:
  75. change_log_lines.insert(incrementLine(),'- %s\n'%(commit))
  76. change_log_lines.insert(incrementLine(),'\n')
  77. if( added ):
  78. change_log_lines.insert(incrementLine(),'### Added:\n')
  79. for commit in added:
  80. change_log_lines.insert(incrementLine(),'- %s\n'%(commit))
  81. change_log_lines.insert(incrementLine(),'\n')
  82. if( changed ):
  83. change_log_lines.insert(incrementLine(),'### Changed:\n')
  84. for commit in changed:
  85. change_log_lines.insert(incrementLine(),'- %s\n'%(commit))
  86. change_log_lines.insert(incrementLine(),'\n')
  87. if( fixed ):
  88. change_log_lines.insert(incrementLine(),'### Fixed:\n')
  89. for commit in fixed:
  90. change_log_lines.insert(incrementLine(),'- %s\n'%(commit))
  91. change_log_lines.insert(incrementLine(),'\n')
  92. write_change_log=open('CHANGELOG.md', 'w')
  93. write_change_log.writelines(change_log_lines)
  94. generateChangeLog(RELEASE, DATE, ORIGIN)