Skip to content

Commit

Permalink
fix: PR Populator to form URL correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Dec 29, 2023
1 parent 5cd7f86 commit 60a1b20
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The go binary will be installed in `$GOPATH/bin`

`$ gh-jira-changelog version`
```
v0.3.0
v0.3.2
```

### Usage
Expand Down
7 changes: 4 additions & 3 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0"
ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancel()

repoURL := repoURL(ctx)
changelog := jira_changelog.NewGenerator(
jira.NewClient(jira.NewClientOptions(jira.Options{
jira.BaseURL: viper.GetString("base_url"),
Expand All @@ -75,10 +76,10 @@ gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0"
usePR,
fromRef,
toRef,
repoUrl(ctx),
repoURL,
)

slog.Info("Generating changelog", "From", fromRef, "To", toRef, "repoURL", viper.GetString("repo_url"))
slog.Info("Generating changelog", "From", fromRef, "To", toRef, "repoURL", repoURL)
changelog.Generate(ctx).Render(writer(writeTo))

return nil
Expand Down Expand Up @@ -113,7 +114,7 @@ func init() {
rootCmd.AddCommand(generateCmd)
}

func repoUrl(ctx context.Context) string {
func repoURL(ctx context.Context) string {
url := viper.GetString("repo_url")
if url != "" {
return url
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

const version = "dev"
const version = "v0.3.2"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
28 changes: 6 additions & 22 deletions pkg/jira_changelog/git/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,15 @@ 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) {
func ConfigRemoteOriginCommand(ctx context.Context) ([]byte, 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 cmd.Output()
}

return fmt.Sprintf("https://%s/%s/%s", host, user, repoName), nil
func GitLogCommand(ctx context.Context, fromRef, toRef string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "git", "log", "--decorate-refs-exclude=refs/*", "--pretty=(%ct) {%h} %d %s", "--no-merges", fromRef+".."+toRef)
return cmd.Output()
}
57 changes: 57 additions & 0 deletions pkg/jira_changelog/git/repo.go
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
}
22 changes: 22 additions & 0 deletions pkg/jira_changelog/git/repo_test.go
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)
}
7 changes: 3 additions & 4 deletions pkg/jira_changelog/messages/git_commits_populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package messages
import (
"context"
"fmt"
"os/exec"
"time"

"github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/git"
"github.com/samber/lo"
)

Expand Down Expand Up @@ -51,12 +51,11 @@ func (cpw *commitPopulator) Populate(ctx context.Context) ([]Messager, error) {
}

func execGitLog(ctx context.Context, fromRef, toRef string) (GitOutput, error) {
cmd := exec.CommandContext(ctx, "git", "log", "--decorate-refs-exclude=refs/*", "--pretty=(%ct) {%h} %d %s", "--no-merges", fromRef+".."+toRef)
stdout, err := cmd.Output()
resultBytes, err := git.GitLogCommand(ctx, fromRef, toRef)
if err != nil {
return "", fmt.Errorf("failed to execute git command: %v", err)
}

result := string(stdout)
result := string(resultBytes)
return GitOutput(result), nil
}
13 changes: 11 additions & 2 deletions pkg/jira_changelog/messages/pull_request_populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ func (p *pullRequestPopulator) PullRequests(ctx context.Context) ([]PullRequest,

slog.Debug("fetching changelog from github")

err = p.apiClient.Post(fmt.Sprintf("repos/%s/%s/releases/generate-notes", p.repoOwner, p.repoName), bytes.NewBuffer(requestBody), &response)
requestUrl, err := url.JoinPath("repos", p.repoOwner, p.repoName, "releases", "generate-notes")
if err != nil {
slog.Error("failed to create requrest url", "error", err)
return []PullRequest{}, err
}

err = p.apiClient.Post(requestUrl, bytes.NewBuffer(requestBody), &response)
if err != nil {
return []PullRequest{}, err
}
Expand Down Expand Up @@ -169,5 +175,8 @@ func repoPath(repoURL string) ([]string, error) {
return []string{}, fmt.Errorf("error parsing repo url: %w", err)
}

return strings.Split(url.Path, "/"), nil
paths := strings.Split(url.Path, "/")
paths = lo.Filter(paths, func(path string, i int) bool { return path != "" })

return paths, nil
}
22 changes: 22 additions & 0 deletions pkg/jira_changelog/messages/pull_request_populator_test.go
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)
}

0 comments on commit 60a1b20

Please sign in to comment.