diff --git a/cmd/generate.go b/cmd/generate.go index 6650dbd..339a59b 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -8,6 +8,7 @@ import ( "time" "github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog" + "github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/git" "github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/jira" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -74,7 +75,7 @@ gh jira-changelog generate --config=".yaml" --from="v0.1.0" usePR, fromRef, toRef, - viper.GetString("repo_url"), + repoUrl(ctx), ) slog.Info("Generating changelog", "From", fromRef, "To", toRef, "repoURL", viper.GetString("repo_url")) @@ -111,3 +112,20 @@ func init() { rootCmd.AddCommand(generateCmd) } + +func repoUrl(ctx context.Context) string { + url := viper.GetString("repo_url") + if url != "" { + return url + } + + url, err := git.CurrentRepoURL(ctx) + if err != nil { + slog.Error("failed to fetch current repo url", err) + return "" + } + + slog.Debug("Current repo URL", "url", url) + + return url +} diff --git a/pkg/jira_changelog/git/commands.go b/pkg/jira_changelog/git/commands.go new file mode 100644 index 0000000..9e377ef --- /dev/null +++ b/pkg/jira_changelog/git/commands.go @@ -0,0 +1,32 @@ +package git + +import ( + "context" + "fmt" + "os/exec" + "strings" + + giturls "github.com/whilp/git-urls" + "golang.org/x/exp/slog" +) + +func CurrentRepoURL(ctx context.Context) (string, error) { + cmd := exec.CommandContext(ctx, "git", "config", "--get", "remote.origin.url") + stdout, err := cmd.Output() + if err != nil { + return "", fmt.Errorf("failed to get remote origin url: %w", err) + } + + result := strings.TrimSpace(string(stdout)) + url, err := giturls.Parse(result) + if err != nil { + slog.Error("failed to parse git url.", "error", err) + return "", fmt.Errorf("failed to parse git url: %w", err) + } + + host := url.Host + user := strings.Split(url.Path, "/")[0] + repoName := strings.TrimSuffix(strings.Split(url.Path, "/")[1], ".git") + + return fmt.Sprintf("https://%s/%s/%s", host, user, repoName), nil +} diff --git a/pkg/jira_changelog/messages/pull_request_populator.go b/pkg/jira_changelog/messages/pull_request_populator.go index 22d626b..567a049 100644 --- a/pkg/jira_changelog/messages/pull_request_populator.go +++ b/pkg/jira_changelog/messages/pull_request_populator.go @@ -5,12 +5,12 @@ import ( "context" "encoding/json" "fmt" + "net/url" "regexp" "strings" "github.com/cli/go-gh/v2/pkg/api" "github.com/samber/lo" - giturls "github.com/whilp/git-urls" "golang.org/x/exp/slog" ) @@ -151,7 +151,7 @@ func repoOwner(repoURL string) (string, error) { return "", err } - return path[len(path)-2], nil + return path[0], nil } func repoName(repoURL string) (string, error) { @@ -160,19 +160,14 @@ func repoName(repoURL string) (string, error) { return "", err } - return path[len(path)-1], nil + return path[1], nil } func repoPath(repoURL string) ([]string, error) { - url, err := giturls.Parse(repoURL) + url, err := url.Parse(repoURL) if err != nil { return []string{}, fmt.Errorf("error parsing repo url: %w", err) } - path := strings.Split(url.Path, "/") - if len(path) < 2 { - return []string{}, fmt.Errorf("invalid repo url: %s", repoURL) - } - - return path, nil + return strings.Split(url.Path, "/"), nil }