-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: PR Populator to form URL correctly
- Loading branch information
1 parent
5cd7f86
commit 60a1b20
Showing
9 changed files
with
127 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package git | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
giturls "github.com/whilp/git-urls" | ||
"golang.org/x/exp/slog" | ||
) | ||
|
||
func CurrentRepoURL(ctx context.Context) (string, error) { | ||
resultBytes, err := ConfigRemoteOriginCommand(ctx) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get remote origin url: %w", err) | ||
} | ||
|
||
u, err := gitURLtoHttpURL(string(resultBytes)) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to convert git URL to HTTP URL: %w", err) | ||
} | ||
|
||
return u, nil | ||
} | ||
|
||
func gitURLtoHttpURL(gitURL string) (string, error) { | ||
guRaw := strings.TrimSpace(gitURL) | ||
gu, err := giturls.Parse(guRaw) | ||
if err != nil { | ||
slog.Error("failed to parse git url.", "error", err) | ||
return "", fmt.Errorf("failed to parse git url: %w", err) | ||
} | ||
|
||
paths := strings.Split(gu.Path, "/") | ||
|
||
if len(paths) < 2 { | ||
return "", fmt.Errorf("invalid git url provided: %w", err) | ||
} | ||
|
||
host := gu.Host | ||
user := paths[0] | ||
repoName := strings.TrimSuffix(paths[1], ".git") | ||
|
||
repoPath, err := url.JoinPath(user, repoName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
u := url.URL{ | ||
Scheme: "https", | ||
Host: host, | ||
Path: repoPath, | ||
} | ||
|
||
return u.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package git | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGitURLToHttpURL_WhenGitURLIsValid(t *testing.T) { | ||
gitURL := "git@github.com:HandOfGod94/jira_changelog.git" | ||
|
||
result, err := gitURLtoHttpURL(gitURL) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://github.com/HandOfGod94/jira_changelog", result) | ||
} | ||
|
||
func TestGitURLToHttpURL_WhenGitURLIsInvalid(t *testing.T) { | ||
gitURL := "foobar" | ||
|
||
_, err := gitURLtoHttpURL(gitURL) | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
pkg/jira_changelog/messages/pull_request_populator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package messages | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRepoPath_WhenRepoURLIsValid(t *testing.T) { | ||
repoURL := "https://github.com/HandOfGod94/jira_changelog" | ||
result, err := repoPath(repoURL) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []string{"HandOfGod94", "jira_changelog"}, result) | ||
} | ||
|
||
func TestRepoPath_WhenRepoURLIsInvalid(t *testing.T) { | ||
repoURL := "https://google.com/foo?bar=fizz" | ||
result, err := repoPath(repoURL) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []string{"foo"}, result) | ||
} | ||
|