Skip to content

Commit

Permalink
default repoURL using git remote origin url config
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Dec 17, 2023
1 parent 96575dc commit 5cd7f86
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
20 changes: 19 additions & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -74,7 +75,7 @@ gh jira-changelog generate --config="<path-to-config-file>.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"))
Expand Down Expand Up @@ -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
}
32 changes: 32 additions & 0 deletions pkg/jira_changelog/git/commands.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 5 additions & 10 deletions pkg/jira_changelog/messages/pull_request_populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

0 comments on commit 5cd7f86

Please sign in to comment.