dmitri.shuralyov.com/route/gerrit

initial commit

It will be used to generate links to Gerrit subjects on the
dmitri.shuralyov.com website.
dmitshur committed 5 years ago commit 370bc52e3c4a5e1d9ec49f2f6738abbb73a27a7b
Showing partial commit. Full Commit
Collapse all
gerrit.go
@@ -0,0 +1,40 @@
// Package gerrit defines a router for targeting Gerrit subjects.
package gerrit

import (
	"context"
	"fmt"
	"strings"
)

// Router provides HTML URLs of Gerrit subjects.
type Router interface {
	// ChangeURL returns the HTML URL of the specified Gerrit change.
	// server is the Gerrit server's hostname, such as "go.googlesource.com".
	ChangeURL(ctx context.Context, server, project string, changeID uint64) string

	// ChangeMessageURL returns the HTML URL of the specified Gerrit change message.
	// server is the Gerrit server's hostname, such as "go.googlesource.com".
	ChangeMessageURL(ctx context.Context, server, project string, changeID uint64, messageID string) string
}

// GoogleSource provides HTML URLs of Gerrit subjects on googlesource.com.
type GoogleSource struct{}

// ChangeURL returns the HTML URL of the specified Gerrit change on googlesource.com.
func (GoogleSource) ChangeURL(_ context.Context, server, project string, changeID uint64) string {
	if !strings.HasSuffix(server, ".googlesource.com") {
		return ""
	}
	sub := server[:len(server)-len(".googlesource.com")]
	return fmt.Sprintf("https://%s-review.googlesource.com/c/%s/+/%d", sub, project, changeID)
}

// ChangeMessageURL returns the HTML URL of the specified Gerrit change message on googlesource.com.
func (GoogleSource) ChangeMessageURL(_ context.Context, server, project string, changeID uint64, messageID string) string {
	if !strings.HasSuffix(server, ".googlesource.com") {
		return ""
	}
	sub := server[:len(server)-len(".googlesource.com")]
	return fmt.Sprintf("https://%s-review.googlesource.com/c/%s/+/%d#message-%s", sub, project, changeID, messageID)
}
gerrit_test.go
@@ -0,0 +1,26 @@
package gerrit_test

import (
	"context"
	"testing"

	"dmitri.shuralyov.com/route/gerrit"
)

func TestGoogleSource(t *testing.T) {
	rtr := gerrit.GoogleSource{}
	t.Run("ChangeURL", func(t *testing.T) {
		got := rtr.ChangeURL(context.Background(), "go.googlesource.com", "tools", 123)
		want := "https://go-review.googlesource.com/c/tools/+/123"
		if got != want {
			t.Errorf("got %q; want %q", got, want)
		}
	})
	t.Run("ChangeMessageURL", func(t *testing.T) {
		got := rtr.ChangeMessageURL(context.Background(), "go.googlesource.com", "tools", 123, "3e1a911dedb249e30454268c6348e4202be73c47")
		want := "https://go-review.googlesource.com/c/tools/+/123#message-3e1a911dedb249e30454268c6348e4202be73c47"
		if got != want {
			t.Errorf("got %q; want %q", got, want)
		}
	})
}
go.mod
@@ -0,0 +1,1 @@
module dmitri.shuralyov.com/route/gerrit