Skip to content

Commit e164f42

Browse files
authored
Merge pull request #16 from rulasg/Get-NoteLink
feat(Get-NoteLink): implement function to generate GitHub URL from local file path
2 parents 742f329 + 17bcead commit e164f42

4 files changed

Lines changed: 144 additions & 18 deletions

File tree

Test/public/getNoteLink.test.ps1

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function Test_GetNoteLink_ReturnsGitHubUrl{
2+
3+
# Arrange
4+
$repoRoot = $PSScriptRoot | Split-Path -Parent | Split-Path -Parent
5+
$notePath = Join-Path $repoRoot "public/getnoteLink.ps1"
6+
7+
# Act
8+
$result = Get-NoteLink -NotePath $notePath
9+
10+
# Assert
11+
Assert-IsTrue -Condition ([bool]($result -match "^https://github\.com/.+/blob/.+/public/getnoteLink\.ps1$")) -Comment "Should return a valid GitHub URL"
12+
}
13+
14+
function Test_GetNoteLink_NestedPath{
15+
16+
# Arrange
17+
$repoRoot = $PSScriptRoot | Split-Path -Parent | Split-Path -Parent
18+
$notePath = Join-Path $repoRoot "public/notes/templates/getTemplate.ps1"
19+
20+
# Act
21+
$result = Get-NoteLink -NotePath $notePath
22+
23+
# Assert
24+
Assert-IsTrue -Condition ([bool]($result -match "^https://github\.com/.+/blob/.+/public/notes/templates/getTemplate\.ps1$")) -Comment "Should handle nested paths correctly"
25+
}
26+
27+
function Test_GetNoteLink_InvalidPath{
28+
29+
# Arrange
30+
$notePath = "/nonexistent/path/file.ps1"
31+
32+
# Act
33+
$result = Get-NoteLink -NotePath $notePath -ErrorAction SilentlyContinue
34+
35+
# Assert
36+
Assert-IsNull -Object $result -Comment "Should return null for invalid path"
37+
}
38+
39+
function Test_GetNoteLink_NotInGitRepo{
40+
41+
# Arrange
42+
$notePath = "/tmp"
43+
44+
# Act
45+
$result = Get-NoteLink -NotePath $notePath -ErrorAction SilentlyContinue
46+
47+
# Assert
48+
Assert-IsNull -Object $result -Comment "Should return null when path is not in a Git repository"
49+
}
50+
51+
function Test_GetNoteLink_UsesHostnameFromRemote{
52+
53+
# Arrange
54+
$repoRoot = $PSScriptRoot | Split-Path -Parent | Split-Path -Parent
55+
$notePath = Join-Path $repoRoot "README.md"
56+
57+
# Get the expected hostname from the actual remote (HTTPS format only)
58+
$remoteUrl = git -C $repoRoot remote get-url origin 2>$null
59+
if ($remoteUrl -match '^https://([^/]+)/') {
60+
$expectedHost = $matches[1]
61+
}
62+
63+
# Act
64+
$result = Get-NoteLink -NotePath $notePath
65+
66+
# Assert
67+
Assert-IsTrue -Condition ([bool]($result -match "^https://$expectedHost/")) -Comment "Should use hostname from remote URL"
68+
}

Test/public/newNotes.test.ps1

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ function Test_NewNote_Simple_WithOutDate{
6363

6464
$category = "TestClient"
6565
$title = "This is the title of the note"
66+
$date = (Get-Date).ToString("yyMMdd")
6667

67-
$header = "# {category} - {title} (NoDate)"
68+
$header = "# {category} - {title} ({date})"
6869
$header = $header -replace "{category}", $category
6970
$header = $header -replace "{title}", $title
71+
$header = $header -replace "{date}", $date
7072

71-
# Add note with date
72-
$path = New-Note $category $title -NoOpen -Force -Date $date
73+
# Add note with DateToday
74+
$path = New-Note $category $title -NoOpen -Force -DateToday
7375

7476
$content = Get-Content -Path $path -Raw
7577

@@ -185,19 +187,18 @@ function Test_NewNotes_SUCCESS{
185187
New-TestingFolder -Path "./TestNotesRoot/howto"
186188
$today = (Get-Date).ToString("yyMMdd")
187189

188-
# Act
189-
$result = New-Note howto "someting that may be useful" -NoOpen
190+
# Act - With DateToday
191+
$result = New-Note howto "someting that may be useful" -NoOpen -DateToday
190192

191-
# $expectedPath = "./TestNotesRoot/howto/howto-someting_that_may_be_useful/howto-someting_that_may_be_useful.md"
192-
$expectedPath = "./TestNotesRoot/howto/howto-someting_that_may_be_useful.md"
193+
$expectedPath = "./TestNotesRoot/howto/$today-howto-someting_that_may_be_useful.md"
193194

194195
Assert-AreEqualPath -Expected $expectedPath -Presented $result
195196

196-
# With date
197+
# With explicit date
197198

198-
$result = New-Note howto "someting that may be useful" -NoOpen -Date $today
199+
$result = New-Note howto "someting that may be useful 2" -NoOpen -Date $today
199200

200-
$expectedPath = "./TestNotesRoot/howto/$today-howto-someting_that_may_be_useful.md"
201+
$expectedPath = "./TestNotesRoot/howto/$today-howto-someting_that_may_be_useful_2.md"
201202

202203
Assert-AreEqualPath -Expected $expectedPath -Presented $result
203204

@@ -214,19 +215,18 @@ function Test_NewNotes_SUCCESS_WithRootPath{
214215
New-TestingFolder -Path "./$RootFolder/howto"
215216
$today = (Get-Date).ToString("yyMMdd")
216217

217-
# Act
218-
$result = New-Note howto "someting that may be useful" -NoOpen -RootPath $RootFolder
218+
# Act - With DateToday
219+
$result = New-Note howto "someting that may be useful" -NoOpen -RootPath $RootFolder -DateToday
219220

220-
# $expectedPath = "./$RootFolder/howto/howto-someting_that_may_be_useful/howto-someting_that_may_be_useful.md"
221-
$expectedPath = "./$RootFolder/howto/howto-someting_that_may_be_useful.md"
221+
$expectedPath = "./$RootFolder/howto/$today-howto-someting_that_may_be_useful.md"
222222

223223
Assert-AreEqualPath -Expected $expectedPath -Presented $result
224224

225-
# With date
225+
# With explicit date
226226

227-
$result = New-Note howto "someting that may be useful" -NoOpen -Date $today -RootPath $RootFolder
227+
$result = New-Note howto "someting that may be useful 2" -NoOpen -Date $today -RootPath $RootFolder
228228

229-
$expectedPath = "./$RootFolder/howto/$today-howto-someting_that_may_be_useful.md"
229+
$expectedPath = "./$RootFolder/howto/$today-howto-someting_that_may_be_useful_2.md"
230230

231231
Assert-AreEqualPath -Expected $expectedPath -Presented $result
232232

public/getNotes.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Get-Notes () {
2020
[CmdletBinding()]
2121
[Alias("notes")]
2222
param(
23-
[Parameter()][string] $Filter,
23+
[Parameter(Position=0)][string] $Filter,
2424
[Parameter()][int32] $DaysAgo,
2525
#all
2626
[Parameter()][switch] $All

public/getnoteLink.ps1

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function Get-NoteLink {
2+
[CmdletBinding()]
3+
param (
4+
# Local file path to get the GitHub link for
5+
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,Position=0)][Alias("Path")][string] $NotePath
6+
)
7+
8+
# Resolve the full path
9+
$fullPath = Resolve-Path -Path $NotePath -ErrorAction SilentlyContinue
10+
if (-not $fullPath) {
11+
Write-Error "Path not found: $NotePath"
12+
return
13+
}
14+
15+
# Get the directory to run git commands from
16+
$workingDir = if (Test-Path -Path $fullPath -PathType Container) { $fullPath } else { Split-Path -Path $fullPath -Parent }
17+
18+
# Get the git root directory
19+
$gitRoot = git -C $workingDir rev-parse --show-toplevel 2>$null
20+
if (-not $gitRoot) {
21+
Write-Error "Path is not inside a Git repository: $NotePath"
22+
return
23+
}
24+
25+
# Get the remote origin URL
26+
$remoteUrl = git -C $gitRoot remote get-url origin 2>$null
27+
if (-not $remoteUrl) {
28+
Write-Error "No remote 'origin' found for repository"
29+
return
30+
}
31+
32+
# Parse HTTPS URL format
33+
# HTTPS format: https://hostname/owner/repo.git
34+
if ($remoteUrl -match '^https://([^/]+)/(.+?)(?:\.git)?$') {
35+
$hostName = $matches[1]
36+
$repoPath = $matches[2] -replace '\.git$', ''
37+
$baseUrl = "https://$hostName/$repoPath"
38+
}
39+
else {
40+
Write-Error "Unsupported remote URL format: $remoteUrl"
41+
return
42+
}
43+
44+
# Get the current branch
45+
$branch = git -C $gitRoot rev-parse --abbrev-ref HEAD 2>$null
46+
if (-not $branch) {
47+
$branch = "main"
48+
}
49+
50+
# Get the relative path from the git root
51+
$relativePath = $fullPath.Path.Substring($gitRoot.Length).TrimStart('/', '\') -replace '\\', '/'
52+
53+
# Construct the GitHub URL
54+
$githubUrl = "$baseUrl/blob/$branch/$relativePath"
55+
56+
return $githubUrl
57+
} Export-ModuleMember -Function Get-NoteLink
58+

0 commit comments

Comments
 (0)