-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
76 lines (70 loc) · 2.93 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
name: Parse Git+SSH URL
description: "A composite action that parses a Git+SSH URL into its required components."
author: Dr. Kibitz (@drkibitz)
inputs:
url:
description: "The Git+SSH URL to parse (e.g., `git@hostname:user/repo.git`, `ssh://user@hostname:port/repo.git`)."
required: true
default-port:
description: "The default SSH port when not specified in the URL (default: `22` if not specified)."
required: false
default: "22"
default-username:
description: "The default SSH username when not specified in the URL (default: `git` if not specified)."
required: false
default: "git"
outputs:
username:
description: "The `username` component of the parsed URL."
value: ${{ steps.parse-url.outputs.username }}
hostname:
description: "The `hostname` component of the parsed URL."
value: ${{ steps.parse-url.outputs.hostname }}
port:
description: "The `port` component of the parsed URL or value of inputs.default-port."
value: ${{ steps.parse-url.outputs.port }}
pathname:
description: "The `pathname` (repository path) component of the parsed URL."
value: ${{ steps.parse-url.outputs.pathname }}
runs:
using: composite
steps:
- name: Parse Git+SSH URL
id: parse-url
run: |
from urllib.parse import urlparse
import re
import os
import sys
def parse_git_ssh_url(url):
parsed = urlparse(url)
if parsed.hostname:
if parsed.scheme != 'ssh' or not parsed.path:
raise ValueError(f"Invalid Git+SSH URL: '{url}'")
return {
'username': parsed.username or '${{ inputs.default-username }}',
'hostname': parsed.hostname,
'port': int(parsed.port) if parsed.port else ${{ inputs.default-port }},
'pathname': f"/{parsed.path.lstrip('/')}",
}
scp = re.match(r"^(?P<username>[^@]+)@(?P<hostname>[^:/]+)(?::(?P<pathname>.*))?$", url)
if scp:
pathname = scp.group('pathname') or "/~/"
pathname = f"/~/{pathname}" if not (pathname.startswith('/') or pathname.startswith('~')) else f"/{pathname.lstrip('/')}"
return {
'username': scp.group('username'),
'hostname': scp.group('hostname'),
'port': ${{ inputs.default-port }},
'pathname': pathname,
}
raise ValueError(f"Invalid Git+SSH URL: '{url}'")
try:
outputs = parse_git_ssh_url("${{ inputs.url }}")
for value in outputs.values():
print(f"::add-mask::{value}")
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
f.writelines(f"{key}={value}\n" for key, value in outputs.items())
except Exception as e:
print(f"::error::{e}", file=sys.stderr)
exit(1)
shell: python