|
1 | 1 | #!/usr/bin/env python3
|
| 2 | +#ddev-generated |
2 | 3 |
|
3 |
| -## Description: Create a git branch from a trello url |
| 4 | +## Description: Create a git branch from a Teamwork url |
4 | 5 | ## Usage: branch
|
5 | 6 | ## Example: ddev branch
|
6 | 7 |
|
7 |
| -# Given a Trello url, and optional name, creates a new branch |
| 8 | +# Given a Teamwork url, and optional name, creates a new branch |
8 | 9 | #
|
9 |
| -# Prompts for a Trello url, and then for an optional branch name. Given the |
| 10 | +# Prompts for a Teamwork url, and then for an optional branch name. Given the |
10 | 11 | # following url and string:
|
11 | 12 | #
|
12 |
| -# - https://trello.com/c/xxK4l2eP/31-region-footer |
| 13 | +# - https://projects.YOURCOMPANY.com/app/tasks/17360561 |
13 | 14 | # - qa_rework
|
14 | 15 | #
|
15 | 16 | # the script will create a git branch with the following name:
|
16 | 17 | #
|
17 |
| -# 2019_w37_31_region_footer_xxK4l2eP__qa_rework |
| 18 | +# 17360561_2024_w37__qa_rework |
18 | 19 | #
|
19 | 20 | # Where:
|
20 | 21 | #
|
21 |
| -# - 2019 is the year |
| 22 | +# - 17360561 is the Teamwork card identifier |
| 23 | +# - 2024 is the year |
22 | 24 | # - w37 is the current week
|
23 |
| -# - 31_region_footer_xxK4l2eP is the Trello card identifier |
24 | 25 | # - qa_rework is the branch name provided
|
25 | 26 | #
|
26 | 27 |
|
27 |
| -import datetime, re, subprocess, sys, getopt |
| 28 | +from urllib.parse import urlparse |
| 29 | +import datetime |
| 30 | +import subprocess |
28 | 31 |
|
| 32 | +# Function to extract the ID from the URL |
| 33 | +def extract_last_part(url): |
| 34 | + path = urlparse(url).path |
| 35 | + last_part = path.split('/')[-1] |
| 36 | + return last_part |
29 | 37 |
|
30 |
| -def main(argv): |
31 |
| - trello = '' |
32 |
| - name = '' |
| 38 | +# Get current year and month |
| 39 | +current_year = datetime.datetime.now().year |
| 40 | +current_month = datetime.datetime.now().month |
33 | 41 |
|
34 |
| - if len(argv) == 0: |
35 |
| - trello = input("Please enter the Trello url: ") |
36 |
| - name = input("Please enter a branch name (optional): ") |
37 |
| - suffix = "" |
38 |
| - elif len(argv) == 1 and ('trello' in argv[0]): |
39 |
| - trello = argv[0] |
40 |
| - else: |
41 |
| - try: |
42 |
| - opts, args = getopt.getopt(argv, "u:b:", ['url=', 'branch=']) |
43 |
| - except getopt.GetoptError as err: |
44 |
| - print(err) |
45 |
| - print("fin branch -u TRELLO_URL [-b BRANCH_NAME]") |
46 |
| - sys.exit(2) |
47 |
| - for opt, arg in opts: |
48 |
| - if opt in ("-u", "--url"): |
49 |
| - trello = arg |
50 |
| - elif opt in ("-b", "--branch"): |
51 |
| - name = arg |
52 |
| - try: |
53 |
| - """ Make sure this looks like part of a Trello path. """ |
54 |
| - path = trelloPath(trello) |
| 42 | +# Main script |
| 43 | +def main(): |
| 44 | + url = input("Enter the URL: ").strip() |
| 45 | + custom_text = input("Enter custom text: ").strip() |
| 46 | + |
| 47 | + # Replace spaces with hyphens and make it Git-friendly |
| 48 | + custom_text = custom_text.replace(" ", "_") |
55 | 49 |
|
56 |
| - """ If we got a name, ensure it's composed of acceptable characters. """ |
57 |
| - if name != None: |
58 |
| - suffix = re.fullmatch(r'[a-zA-Z0-9_-]+', name) |
| 50 | + # Extract the last part (ID) from the URL |
| 51 | + last_part = extract_last_part(url) |
59 | 52 |
|
60 |
| - """ Proceed if we we got a match. """ |
61 |
| - if path != None: |
62 |
| - parts = path.group(1).split("/") |
63 |
| - today = datetime.date.today() |
64 |
| - name = re.sub(r'-', '_', parts[1]) |
65 |
| - hash = parts[0] |
66 |
| - branch = "{}_w{}_{}_{}".format( |
67 |
| - today.year, today.isocalendar()[1], name, hash) |
| 53 | + # Create the branch name in the format YEAR/MONTH/ID-description |
| 54 | + branch_name = f"{current_year}/{current_month:02}/T-{last_part}__{custom_text}" |
68 | 55 |
|
69 |
| - """ If we were able to match a suffix, append it now. """ |
70 |
| - if suffix != None: |
71 |
| - branch = "{}__{}".format(branch, suffix.group(0)) |
| 56 | + # Ensure only valid characters are in the branch name |
| 57 | + branch_name = "".join(c for c in branch_name if c.isalnum() or c in ['-', '_', '.', '/']) |
72 | 58 |
|
73 |
| - """ Check out a branch with the new name. """ |
74 |
| - subprocess.run(["git", "checkout", "-b", branch]) |
75 |
| - else: |
76 |
| - raise ValueError |
| 59 | + # Print the branch name |
| 60 | + print(f"Creating new Git branch: {branch_name}") |
77 | 61 |
|
78 |
| - except ValueError: |
79 |
| - print("The argument did not match the expected pattern.") |
| 62 | + # Execute git command to create and switch to the new branch |
| 63 | + try: |
| 64 | + subprocess.run(["git", "checkout", "-b", branch_name], check=True) |
| 65 | + print(f"Successfully created and switched to branch '{branch_name}'") |
| 66 | + except subprocess.CalledProcessError as e: |
| 67 | + print(f"Error creating the branch: {e}") |
80 | 68 |
|
81 |
| -def trelloPath(test_url): |
82 |
| - return re.fullmatch(r'https:\/\/trello.com\/[a-zA-Z]{1}\/([0-9A-Za-z-]+\/[0-9A-Za-z-]+)', test_url) |
| 69 | +if __name__ == "__main__": |
| 70 | + main() |
83 | 71 |
|
84 |
| -main(sys.argv[1:]) |
0 commit comments