Skip to content

Commit

Permalink
Add git branch, add ddev-generated
Browse files Browse the repository at this point in the history
  • Loading branch information
bserem committed Sep 23, 2024
1 parent d90748d commit 146c081
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 60 deletions.
97 changes: 42 additions & 55 deletions commands/host/branch
Original file line number Diff line number Diff line change
@@ -1,84 +1,71 @@
#!/usr/bin/env python3
#ddev-generated

## Description: Create a git branch from a trello url
## Description: Create a git branch from a Teamwork url
## Usage: branch
## Example: ddev branch

# Given a Trello url, and optional name, creates a new branch
# Given a Teamwork url, and optional name, creates a new branch
#
# Prompts for a Trello url, and then for an optional branch name. Given the
# Prompts for a Teamwork url, and then for an optional branch name. Given the
# following url and string:
#
# - https://trello.com/c/xxK4l2eP/31-region-footer
# - https://projects.YOURCOMPANY.com/app/tasks/17360561
# - qa_rework
#
# the script will create a git branch with the following name:
#
# 2019_w37_31_region_footer_xxK4l2eP__qa_rework
# 17360561_2024_w37__qa_rework
#
# Where:
#
# - 2019 is the year
# - 17360561 is the Teamwork card identifier
# - 2024 is the year
# - w37 is the current week
# - 31_region_footer_xxK4l2eP is the Trello card identifier
# - qa_rework is the branch name provided
#

import datetime, re, subprocess, sys, getopt
from urllib.parse import urlparse
import datetime
import subprocess

# Function to extract the ID from the URL
def extract_last_part(url):
path = urlparse(url).path
last_part = path.split('/')[-1]
return last_part

def main(argv):
trello = ''
name = ''
# Get current year and month
current_year = datetime.datetime.now().year
current_month = datetime.datetime.now().month

if len(argv) == 0:
trello = input("Please enter the Trello url: ")
name = input("Please enter a branch name (optional): ")
suffix = ""
elif len(argv) == 1 and ('trello' in argv[0]):
trello = argv[0]
else:
try:
opts, args = getopt.getopt(argv, "u:b:", ['url=', 'branch='])
except getopt.GetoptError as err:
print(err)
print("fin branch -u TRELLO_URL [-b BRANCH_NAME]")
sys.exit(2)
for opt, arg in opts:
if opt in ("-u", "--url"):
trello = arg
elif opt in ("-b", "--branch"):
name = arg
try:
""" Make sure this looks like part of a Trello path. """
path = trelloPath(trello)
# Main script
def main():
url = input("Enter the URL: ").strip()
custom_text = input("Enter custom text: ").strip()

# Replace spaces with hyphens and make it Git-friendly
custom_text = custom_text.replace(" ", "_")

""" If we got a name, ensure it's composed of acceptable characters. """
if name != None:
suffix = re.fullmatch(r'[a-zA-Z0-9_-]+', name)
# Extract the last part (ID) from the URL
last_part = extract_last_part(url)

""" Proceed if we we got a match. """
if path != None:
parts = path.group(1).split("/")
today = datetime.date.today()
name = re.sub(r'-', '_', parts[1])
hash = parts[0]
branch = "{}_w{}_{}_{}".format(
today.year, today.isocalendar()[1], name, hash)
# Create the branch name in the format YEAR/MONTH/ID-description
branch_name = f"{current_year}/{current_month:02}/T-{last_part}__{custom_text}"

""" If we were able to match a suffix, append it now. """
if suffix != None:
branch = "{}__{}".format(branch, suffix.group(0))
# Ensure only valid characters are in the branch name
branch_name = "".join(c for c in branch_name if c.isalnum() or c in ['-', '_', '.', '/'])

""" Check out a branch with the new name. """
subprocess.run(["git", "checkout", "-b", branch])
else:
raise ValueError
# Print the branch name
print(f"Creating new Git branch: {branch_name}")

except ValueError:
print("The argument did not match the expected pattern.")
# Execute git command to create and switch to the new branch
try:
subprocess.run(["git", "checkout", "-b", branch_name], check=True)
print(f"Successfully created and switched to branch '{branch_name}'")
except subprocess.CalledProcessError as e:
print(f"Error creating the branch: {e}")

def trelloPath(test_url):
return re.fullmatch(r'https:\/\/trello.com\/[a-zA-Z]{1}\/([0-9A-Za-z-]+\/[0-9A-Za-z-]+)', test_url)
if __name__ == "__main__":
main()

main(sys.argv[1:])
85 changes: 85 additions & 0 deletions commands/host/branch-trello.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python3
#ddev-generated

## Description: Create a git branch from a trello url
## Usage: branch
## Example: ddev branch

# Given a Trello url, and optional name, creates a new branch
#
# Prompts for a Trello url, and then for an optional branch name. Given the
# following url and string:
#
# - https://trello.com/c/xxK4l2eP/31-region-footer
# - qa_rework
#
# the script will create a git branch with the following name:
#
# 2019_w37_31_region_footer_xxK4l2eP__qa_rework
#
# Where:
#
# - 2019 is the year
# - w37 is the current week
# - 31_region_footer_xxK4l2eP is the Trello card identifier
# - qa_rework is the branch name provided
#

import datetime, re, subprocess, sys, getopt


def main(argv):
trello = ''
name = ''

if len(argv) == 0:
trello = input("Please enter the Trello url: ")
name = input("Please enter a branch name (optional): ")
suffix = ""
elif len(argv) == 1 and ('trello' in argv[0]):
trello = argv[0]
else:
try:
opts, args = getopt.getopt(argv, "u:b:", ['url=', 'branch='])
except getopt.GetoptError as err:
print(err)
print("fin branch -u TRELLO_URL [-b BRANCH_NAME]")
sys.exit(2)
for opt, arg in opts:
if opt in ("-u", "--url"):
trello = arg
elif opt in ("-b", "--branch"):
name = arg
try:
""" Make sure this looks like part of a Trello path. """
path = trelloPath(trello)

""" If we got a name, ensure it's composed of acceptable characters. """
if name != None:
suffix = re.fullmatch(r'[a-zA-Z0-9_-]+', name)

""" Proceed if we we got a match. """
if path != None:
parts = path.group(1).split("/")
today = datetime.date.today()
name = re.sub(r'-', '_', parts[1])
hash = parts[0]
branch = "{}_w{}_{}_{}".format(
today.year, today.isocalendar()[1], name, hash)

""" If we were able to match a suffix, append it now. """
if suffix != None:
branch = "{}__{}".format(branch, suffix.group(0))

""" Check out a branch with the new name. """
subprocess.run(["git", "checkout", "-b", branch])
else:
raise ValueError

except ValueError:
print("The argument did not match the expected pattern.")

def trelloPath(test_url):
return re.fullmatch(r'https:\/\/trello.com\/[a-zA-Z]{1}\/([0-9A-Za-z-]+\/[0-9A-Za-z-]+)', test_url)

main(sys.argv[1:])
3 changes: 2 additions & 1 deletion commands/host/login
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
#!/bin/sh
#ddev-generated

## Description: Launch a browser and login to the current Drupal project.
## Usage: login [--name=USER]
Expand Down
3 changes: 2 additions & 1 deletion commands/web/behat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
#!/bin/sh
#ddev-generated
## Description: Run behat inside the web container
## Usage: behat [flags] [args]
## Example: "ddev behat --tags ~wip"
Expand Down
3 changes: 2 additions & 1 deletion commands/web/robo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
#!/bin/sh
#ddev-generated
## Description: Run robo inside the web container
## Usage: robo [flags] [args]
## Example: "ddev robo init"
Expand Down
Binary file removed images/gh-tmate.jpg
Binary file not shown.
Binary file removed images/template-button.png
Binary file not shown.
5 changes: 3 additions & 2 deletions scripts/git-hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
#!/bin/sh
#ddev-generated

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

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

Expand Down
9 changes: 9 additions & 0 deletions settings.ddev.annertech.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/**
* @file
* This is a Drupal settings.local.php file automatically generated by DDEV basec on the annertech-ddev addon.
*
* DDEV manages this file and may delete or overwrite it unless this
* comment, marked with #ddev-generated, is removed. It is recommended
* that you leave this file alone.
*/

// Simplei settings for local environments. Needs to be overwritten on PSH/Acquia config.
$settings['simple_environment_indicator'] = 'darkgreen LOCAL';

Expand Down

0 comments on commit 146c081

Please sign in to comment.