Last active
July 31, 2022 04:48
-
-
Save pineoc/1d3696a9a83419821b42cb6100a03901 to your computer and use it in GitHub Desktop.
Perforce trigger script for update description add jira link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import subprocess | |
import re | |
JIRA_URL = 'https://jira.pineoc.com/browse/' | |
def get_desc(change): | |
try: | |
cmd = ['p4', 'describe', change] | |
desc = subprocess.check_output(cmd).decode() | |
# get stream from affected files list first item | |
desc_splited = desc.split('Affected files ...') | |
desc_msg = desc_splited[0] | |
# remove cl, user, '\t', \r, last newline | |
split_msg = desc_msg.replace(' on ', ' by ').split(' by ') | |
pure_desc = str(split_msg[2]).split('\t', 1)[1].replace('\t', '').replace('\r', '').rstrip() | |
return pure_desc | |
except Exception as e: | |
print(e) | |
return | |
def create_jira_url(desc): | |
try: | |
# regex jira issue key | |
match = re.findall(r'PIN-\d+', desc) | |
match = list(dict.fromkeys(match)) # remove dups | |
urls = list(map(lambda key: JIRA_URL+key, match)) | |
urls.sort() | |
# print(match, urls) | |
jira_urls_result = '*JIRA*\n' | |
for url in urls: | |
jira_urls_result += '- {}\n'.format(url) | |
# print(jira_urls_result) | |
return jira_urls_result | |
except Exception as e: | |
print('jira_url:', e) | |
return | |
def edit_description(desc, change): | |
try: | |
edit_desc = desc | |
jira_url_desc = create_jira_url(desc) | |
edit_desc = 'Description={}'.format(desc + '\n' + jira_url_desc) | |
if desc.find('*JIRA*') != -1: | |
return | |
# p4 --field Description=desc1234 change -o changelist | p4 change -if | |
cmd = ['p4', '--field', edit_desc, 'change', '-o', change] | |
p4_edit_field = subprocess.run(cmd, check=True, capture_output=True) | |
# print('field:', p4_edit_field) | |
p4_excute = subprocess.run(['p4', 'change', '-if'], input=p4_edit_field.stdout.decode('cp949').encode('utf-8'), capture_output=True) | |
if p4_excute.stderr: | |
print('[Jira link error] IGNORE THIS ERROR\n', p4_excute) | |
exit(1) | |
except Exception as e: | |
print('(Error) Edit_desc:', str(e)) | |
def main(change): | |
try: | |
desc = get_desc(change) | |
edit_description(desc, change) | |
except Exception as e: | |
print(e) | |
exit(1) | |
return | |
if __name__ == '__main__': | |
main(sys.argv[1]) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment