Skip to content

Commit 057672f

Browse files
committed
Add pre-commit workflow and update structure.yaml; enhance caching in filters
1 parent f76431d commit 057672f

7 files changed

Lines changed: 68 additions & 3 deletions

File tree

example/structure.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ folders:
3333
struct:
3434
- docker-files
3535
- go-project
36+
- github/workflows/pre-commit
3637
variables:
3738
- project_name:
3839
description: 'The name of the project.'

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ colorlog
99
boto3
1010
google-cloud
1111
google-api-core
12+
cachetools

struct_module/content_fetcher.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ def fetch_content(self, content_location):
5353

5454
for prefix, method in protocol_map.items():
5555
if content_location.startswith(prefix):
56-
return method(content_location[len(prefix):])
56+
if content_location.startswith("http"):
57+
return method(content_location)
58+
else:
59+
return method(content_location[len(prefix):])
5760

5861
raise ValueError(f"Unsupported content location: {content_location}")
5962

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
structure:
2+
- .github/workflows/pre-commit.yaml:
3+
content: |
4+
name: pre-commit
5+
6+
on:
7+
pull_request:
8+
push:
9+
branches: [{{@ current_repo() | default_branch @}}]
10+
11+
jobs:
12+
pre-commit:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@{{@ "actions/checkout" | latest_release @}}
16+
- uses: actions/setup-python@{{@ "actions/setup-python" | latest_release @}}
17+
- uses: pre-commit/action@{{@ "pre-commit/action" | latest_release @}}
18+
- .pre-commit-config.yaml:
19+
content: |
20+
repos:
21+
- repo: https://github.com/pre-commit/pre-commit-hooks
22+
rev: {{@ "pre-commit/pre-commit-hooks" | latest_release @}}
23+
hooks:
24+
- id: check-yaml
25+
- id: end-of-file-fixer
26+
- id: trailing-whitespace

struct_module/filters.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import os
22
import re
33
from github import Github
4+
from cachetools import TTLCache, cached
45

6+
cache = TTLCache(maxsize=100, ttl=600)
7+
8+
@cached(cache)
59
def get_latest_release(repo_name):
610
token = os.getenv('GITHUB_TOKEN')
711

@@ -25,6 +29,7 @@ def get_latest_release(repo_name):
2529
except Exception as e:
2630
return "LATEST_RELEASE_ERROR"
2731

32+
@cached(cache)
2833
def get_default_branch(repo_name):
2934
token = os.getenv('GITHUB_TOKEN')
3035

struct_module/template_renderer.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from jinja2 import Environment, meta
44
from struct_module.filters import get_latest_release, slugify, get_default_branch
55
from struct_module.input_store import InputStore
6+
from struct_module.utils import get_current_repo
67

78
class TemplateRenderer:
89
def __init__(self, config_variables, input_store):
@@ -17,13 +18,20 @@ def __init__(self, config_variables, input_store):
1718
comment_end_string='@#}'
1819
)
1920

21+
self.logger = logging.getLogger(__name__)
22+
2023
custom_filters = {
2124
'latest_release': get_latest_release,
2225
'slugify': slugify,
2326
'default_branch': get_default_branch
2427
}
28+
29+
globals = {
30+
'current_repo': get_current_repo
31+
}
32+
33+
self.env.globals.update(globals)
2534
self.env.filters.update(custom_filters)
26-
self.logger = logging.getLogger(__name__)
2735
self.input_store = InputStore(input_store)
2836
self.input_store.load()
2937
self.input_data = self.input_store.get_data()

struct_module/utils.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
import yaml
22
import os
3+
import subprocess
34

45
def read_config_file(file_path):
56
with open(file_path, 'r') as f:
67
return yaml.safe_load(f)
78

8-
99
def merge_configs(file_config, args):
1010
args_dict = vars(args)
1111
for key, value in file_config.items():
1212
if key in args_dict and args_dict[key] is None:
1313
args_dict[key] = value
1414
return args_dict
1515

16+
def get_current_repo():
17+
try:
18+
# Get the remote URL
19+
remote_url = subprocess.check_output(['git', 'config', '--get', 'remote.origin.url'], text=True).strip()
20+
21+
# Handle different remote URL formats (HTTPS and SSH)
22+
if remote_url.startswith("https://"):
23+
# Extract "owner/repository" from HTTPS URL
24+
owner_repo = remote_url.split("github.com/")[1].replace(".git", "")
25+
elif remote_url.startswith("git@"):
26+
# Extract "owner/repository" from SSH URL
27+
owner_repo = remote_url.split(":")[1].replace(".git", "")
28+
else:
29+
return "Error: Not a GitHub repository"
30+
31+
return owner_repo
32+
except subprocess.CalledProcessError:
33+
return "Error: Not a Git repository or no remote URL set"
34+
35+
1636
project_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
37+

0 commit comments

Comments
 (0)