Skip to content

Commit 3410b78

Browse files
authored
Merge pull request #4944 from wled/copilot/fix-4943
Add GitHub repository information to build and API response
2 parents 762d443 + e69bf4e commit 3410b78

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
.pioenvs
88
.piolibdeps
99
.vscode
10+
compile_commands.json
11+
__pycache__/
1012

1113
esp01-update.sh
1214
platformio_override.ini

pio-scripts/set_repo.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Import('env')
2+
import subprocess
3+
import re
4+
5+
def get_github_repo():
6+
"""Extract GitHub repository name from git remote URL.
7+
8+
Returns:
9+
str: Repository name in 'owner/repo' format for GitHub repos,
10+
'unknown' for non-GitHub repos, missing git CLI, or any errors.
11+
"""
12+
try:
13+
# Get the remote URL for origin
14+
result = subprocess.run(['git', 'remote', 'get-url', 'origin'],
15+
capture_output=True, text=True, check=True)
16+
remote_url = result.stdout.strip()
17+
18+
# Check if it's a GitHub URL
19+
if 'github.com' not in remote_url.lower():
20+
return 'unknown'
21+
22+
# Parse GitHub URL patterns:
23+
# https://github.com/owner/repo.git
24+
# [email protected]:owner/repo.git
25+
# https://github.com/owner/repo
26+
27+
# Remove .git suffix if present
28+
if remote_url.endswith('.git'):
29+
remote_url = remote_url[:-4]
30+
31+
# Handle HTTPS URLs
32+
https_match = re.search(r'github\.com/([^/]+/[^/]+)', remote_url, re.IGNORECASE)
33+
if https_match:
34+
return https_match.group(1)
35+
36+
# Handle SSH URLs
37+
ssh_match = re.search(r'github\.com:([^/]+/[^/]+)', remote_url, re.IGNORECASE)
38+
if ssh_match:
39+
return ssh_match.group(1)
40+
41+
return 'unknown'
42+
43+
except FileNotFoundError:
44+
# Git CLI is not installed or not in PATH
45+
return 'unknown'
46+
except subprocess.CalledProcessError:
47+
# Git command failed (e.g., not a git repo, no remote, etc.)
48+
return 'unknown'
49+
except Exception:
50+
# Any other unexpected error
51+
return 'unknown'
52+
53+
repo = get_github_repo()
54+
env.Append(BUILD_FLAGS=[f'-DWLED_REPO=\\"{repo}\\"'])

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ ldscript_4m1m = eagle.flash.4m1m.ld
111111
[scripts_defaults]
112112
extra_scripts =
113113
pre:pio-scripts/set_version.py
114+
pre:pio-scripts/set_repo.py
114115
post:pio-scripts/output_bins.py
115116
post:pio-scripts/strip-floats.py
116117
pre:pio-scripts/user_config_copy.py

wled00/json.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ void serializeInfo(JsonObject root)
695695
root[F("vid")] = VERSION;
696696
root[F("cn")] = F(WLED_CODENAME);
697697
root[F("release")] = releaseString;
698+
root[F("repo")] = repoString;
698699

699700
JsonObject leds = root.createNestedObject(F("leds"));
700701
leds[F("count")] = strip.getLengthTotal();

wled00/wled.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,14 @@ using PSRAMDynamicJsonDocument = BasicJsonDocument<PSRAM_Allocator>;
276276
#ifndef WLED_RELEASE_NAME
277277
#define WLED_RELEASE_NAME "Custom"
278278
#endif
279+
#ifndef WLED_REPO
280+
#define WLED_REPO "unknown"
281+
#endif
279282

280283
// Global Variable definitions
281284
WLED_GLOBAL char versionString[] _INIT(TOSTRING(WLED_VERSION));
282285
WLED_GLOBAL char releaseString[] _INIT(WLED_RELEASE_NAME); // must include the quotes when defining, e.g -D WLED_RELEASE_NAME=\"ESP32_MULTI_USREMODS\"
286+
WLED_GLOBAL char repoString[] _INIT(WLED_REPO);
283287
#define WLED_CODENAME "Niji"
284288

285289
// AP and OTA default passwords (for maximum security change them!)

0 commit comments

Comments
 (0)