dmitri.shuralyov.com/route/github/...

Initial commit.

Factor out Router from github.com/shurcooL/notifications/githubapi
package, so it can be more easily imported both by notifications/githubapi
and events/githubapi.

Add context parameter to enable some request-scoped flexibility over
routing.
dmitshur committed 6 years ago commit c44386cf891f9c8b6e6f0cccb3b2f066c02c428a
Collapse all
github.go
@@ -0,0 +1,37 @@
// Package github defines a router for targeting GitHub subjects.
package github

import (
	"context"
	"fmt"
)

// Router provides HTML URLs of GitHub subjects.
type Router interface {
	// IssueURL returns the HTML URL of the specified GitHub issue.
	IssueURL(ctx context.Context, owner, repo string, issueID, commentID uint64) string

	// PullRequestURL returns the HTML URL of the specified GitHub pull request.
	PullRequestURL(ctx context.Context, owner, repo string, prID, commentID uint64) string
}

// DotCom provides HTML URLs of GitHub subjects on github.com.
type DotCom struct{}

// IssueURL returns the HTML URL of the specified GitHub issue on github.com.
func (DotCom) IssueURL(_ context.Context, owner, repo string, issueID, commentID uint64) string {
	var fragment string
	if commentID != 0 {
		fragment = fmt.Sprintf("#issuecomment-%d", commentID)
	}
	return fmt.Sprintf("https://github.com/%s/%s/issues/%d%s", owner, repo, issueID, fragment)
}

// PullRequestURL returns the HTML URL of the specified GitHub pull request on github.com.
func (DotCom) PullRequestURL(_ context.Context, owner, repo string, prID, commentID uint64) string {
	var fragment string
	if commentID != 0 {
		fragment = fmt.Sprintf("#issuecomment-%d", commentID)
	}
	return fmt.Sprintf("https://github.com/%s/%s/pull/%d%s", owner, repo, prID, fragment)
}