Skip to content

Commit 146c081

Browse files
committed
Add git branch, add ddev-generated
1 parent d90748d commit 146c081

File tree

9 files changed

+145
-60
lines changed

9 files changed

+145
-60
lines changed

commands/host/branch

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,71 @@
11
#!/usr/bin/env python3
2+
#ddev-generated
23

3-
## Description: Create a git branch from a trello url
4+
## Description: Create a git branch from a Teamwork url
45
## Usage: branch
56
## Example: ddev branch
67

7-
# Given a Trello url, and optional name, creates a new branch
8+
# Given a Teamwork url, and optional name, creates a new branch
89
#
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
1011
# following url and string:
1112
#
12-
# - https://trello.com/c/xxK4l2eP/31-region-footer
13+
# - https://projects.YOURCOMPANY.com/app/tasks/17360561
1314
# - qa_rework
1415
#
1516
# the script will create a git branch with the following name:
1617
#
17-
# 2019_w37_31_region_footer_xxK4l2eP__qa_rework
18+
# 17360561_2024_w37__qa_rework
1819
#
1920
# Where:
2021
#
21-
# - 2019 is the year
22+
# - 17360561 is the Teamwork card identifier
23+
# - 2024 is the year
2224
# - w37 is the current week
23-
# - 31_region_footer_xxK4l2eP is the Trello card identifier
2425
# - qa_rework is the branch name provided
2526
#
2627

27-
import datetime, re, subprocess, sys, getopt
28+
from urllib.parse import urlparse
29+
import datetime
30+
import subprocess
2831

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
2937

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
3341

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(" ", "_")
5549

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)
5952

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}"
6855

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 ['-', '_', '.', '/'])
7258

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}")
7761

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}")
8068

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()
8371

84-
main(sys.argv[1:])

commands/host/branch-trello.sample

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
#ddev-generated
3+
4+
## Description: Create a git branch from a trello url
5+
## Usage: branch
6+
## Example: ddev branch
7+
8+
# Given a Trello url, and optional name, creates a new branch
9+
#
10+
# Prompts for a Trello url, and then for an optional branch name. Given the
11+
# following url and string:
12+
#
13+
# - https://trello.com/c/xxK4l2eP/31-region-footer
14+
# - qa_rework
15+
#
16+
# the script will create a git branch with the following name:
17+
#
18+
# 2019_w37_31_region_footer_xxK4l2eP__qa_rework
19+
#
20+
# Where:
21+
#
22+
# - 2019 is the year
23+
# - w37 is the current week
24+
# - 31_region_footer_xxK4l2eP is the Trello card identifier
25+
# - qa_rework is the branch name provided
26+
#
27+
28+
import datetime, re, subprocess, sys, getopt
29+
30+
31+
def main(argv):
32+
trello = ''
33+
name = ''
34+
35+
if len(argv) == 0:
36+
trello = input("Please enter the Trello url: ")
37+
name = input("Please enter a branch name (optional): ")
38+
suffix = ""
39+
elif len(argv) == 1 and ('trello' in argv[0]):
40+
trello = argv[0]
41+
else:
42+
try:
43+
opts, args = getopt.getopt(argv, "u:b:", ['url=', 'branch='])
44+
except getopt.GetoptError as err:
45+
print(err)
46+
print("fin branch -u TRELLO_URL [-b BRANCH_NAME]")
47+
sys.exit(2)
48+
for opt, arg in opts:
49+
if opt in ("-u", "--url"):
50+
trello = arg
51+
elif opt in ("-b", "--branch"):
52+
name = arg
53+
try:
54+
""" Make sure this looks like part of a Trello path. """
55+
path = trelloPath(trello)
56+
57+
""" If we got a name, ensure it's composed of acceptable characters. """
58+
if name != None:
59+
suffix = re.fullmatch(r'[a-zA-Z0-9_-]+', name)
60+
61+
""" Proceed if we we got a match. """
62+
if path != None:
63+
parts = path.group(1).split("/")
64+
today = datetime.date.today()
65+
name = re.sub(r'-', '_', parts[1])
66+
hash = parts[0]
67+
branch = "{}_w{}_{}_{}".format(
68+
today.year, today.isocalendar()[1], name, hash)
69+
70+
""" If we were able to match a suffix, append it now. """
71+
if suffix != None:
72+
branch = "{}__{}".format(branch, suffix.group(0))
73+
74+
""" Check out a branch with the new name. """
75+
subprocess.run(["git", "checkout", "-b", branch])
76+
else:
77+
raise ValueError
78+
79+
except ValueError:
80+
print("The argument did not match the expected pattern.")
81+
82+
def trelloPath(test_url):
83+
return re.fullmatch(r'https:\/\/trello.com\/[a-zA-Z]{1}\/([0-9A-Za-z-]+\/[0-9A-Za-z-]+)', test_url)
84+
85+
main(sys.argv[1:])

commands/host/login

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/bin/bash
1+
#!/bin/sh
2+
#ddev-generated
23

34
## Description: Launch a browser and login to the current Drupal project.
45
## Usage: login [--name=USER]

commands/web/behat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/bin/bash
1+
#!/bin/sh
2+
#ddev-generated
23
## Description: Run behat inside the web container
34
## Usage: behat [flags] [args]
45
## Example: "ddev behat --tags ~wip"

commands/web/robo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/bin/bash
1+
#!/bin/sh
2+
#ddev-generated
23
## Description: Run robo inside the web container
34
## Usage: robo [flags] [args]
45
## Example: "ddev robo init"

images/gh-tmate.jpg

-53.9 KB
Binary file not shown.

images/template-button.png

-20 KB
Binary file not shown.

scripts/git-hooks/commit-msg

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/bin/bash
1+
#!/bin/sh
2+
#ddev-generated
23

34
# Regex pattern for the commit message
45
PATTERN="^T-[0-9]*"
@@ -8,7 +9,7 @@ commit_message=$(cat "$1")
89

910
# Check if the commit message matches the pattern
1011
if [[ ! $commit_message =~ $PATTERN ]]; then
11-
echo "Error: Commit message must start with the pattern 'T-00000000'."
12+
echo "Error: Commit message must start with the pattern 'T-00000000'. Please get the ticket ID from Teamwork URL."
1213
exit 1
1314
fi
1415

settings.ddev.annertech.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/**
4+
* @file
5+
* This is a Drupal settings.local.php file automatically generated by DDEV basec on the annertech-ddev addon.
6+
*
7+
* DDEV manages this file and may delete or overwrite it unless this
8+
* comment, marked with #ddev-generated, is removed. It is recommended
9+
* that you leave this file alone.
10+
*/
11+
312
// Simplei settings for local environments. Needs to be overwritten on PSH/Acquia config.
413
$settings['simple_environment_indicator'] = 'darkgreen LOCAL';
514

0 commit comments

Comments
 (0)