Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
doriable committed May 15, 2024
1 parent 5a43017 commit 8cc3b88
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 76 deletions.
57 changes: 9 additions & 48 deletions private/buf/cmd/buf/command/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ const (
draftFlagName = "draft"
branchFlagName = "branch"

gitOriginRemote = "origin"
githubGitlabRemoteURLFormat = "https://%s%s/commit/%s"
bitBucketRemoteURLFormat = "https://%s%s/commits/%s"
gitOriginRemote = "origin"
)

var (
Expand Down Expand Up @@ -149,15 +147,16 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
fmt.Sprintf(
`Uses the Git source control state to set flag values. If this flag is set, we will use the following values for your flags:
--%s to <git remote URL>/<repository name>/<route>/<checked out commit sha> (e.g. https://github.com/acme/weather/commit/ffac537e6cbbf934b08745a378932722df287a53)
--%s for each Git tag and branch pointing to the currently checked out commit
--%s to the Git default branch (e.g. main) - this is only in effect if --%s is also set
--%s to <git remote URL>/<repository name>/<route>/<checked out commit sha> (e.g. https://github.com/acme/weather/commit/ffac537e6cbbf934b08745a378932722df287a53).
--%s for each Git tag and branch pointing to the currently checked out commit. You can set additional labels using --%s with this flag.
--%s to the Git default branch (e.g. main) - this is only in effect if --%s is also set.
The source control URL and default branch is based on the required Git remote %q.
This flag is only compatible with checkouts of Git source repositories.
This flag does not allow you to set any of the following flags yourself: --%s, --%s.`,
sourceControlURLFlagName,
labelFlagName,
labelFlagName,
createDefaultLabelFlagName,
createFlagName,
gitOriginRemote,
Expand Down Expand Up @@ -462,13 +461,11 @@ func getGitMetadataUploadOptions(
if gitLabelsUploadOption != nil {
gitMetadataUploadOptions = append(gitMetadataUploadOptions, gitLabelsUploadOption)
}
gitSourceControlURLUploadOption, err := getGitMetadataSourceControlURLUploadOption(originRemote, currentGitCommit)
if err != nil {
return nil, err
}
if gitSourceControlURLUploadOption != nil {
gitMetadataUploadOptions = append(gitMetadataUploadOptions, gitSourceControlURLUploadOption)
sourceControlURL := originRemote.SourceControlURL(currentGitCommit)
if sourceControlURL == "" {
return nil, appcmd.NewInvalidArgumentError("unable to determine source control URL for this repository; only GitHub/GitLab/BitBucket are supported")
}
gitMetadataUploadOptions = append(gitMetadataUploadOptions, bufmodule.UploadWithSourceControlURL(sourceControlURL))
if flags.Create {
createModuleVisibility, err := bufmodule.ParseModuleVisibility(flags.CreateVisibility)
if err != nil {
Expand Down Expand Up @@ -499,42 +496,6 @@ func getGitMetadataLabelsUploadOptions(
return bufmodule.UploadWithLabels(refs...), nil
}

// getGitMetadataSourceControlURLUploadOption takes a remote and git commit sha and makes
// the best effort to construct a user-facing URL based on the hostname.
//
// If the remote hostname contains bitbucket (e.g. bitbucket.mycompany.com or bitbucket.org),
// then it uses the route /commits for the git commit sha.
//
// If the remote hostname contains github (e.g. github.mycompany.com or github.com) or gitlab
// (e.g. gitlab.mycompany.com or gitlab.com) then it uses the route /commit for the git
// commit sha.
func getGitMetadataSourceControlURLUploadOption(
remote git.Remote,
gitCommitSha string,
) (bufmodule.UploadOption, error) {
switch remote.Kind() {
case git.RemoteKindBitBucket:
return bufmodule.UploadWithSourceControlURL(
fmt.Sprintf(
bitBucketRemoteURLFormat,
remote.Hostname(),
remote.RepositoryPath(),
gitCommitSha,
),
), nil
case git.RemoteKindGitHub, git.RemoteKindGitLab:
return bufmodule.UploadWithSourceControlURL(
fmt.Sprintf(
githubGitlabRemoteURLFormat,
remote.Hostname(),
remote.RepositoryPath(),
gitCommitSha,
),
), nil
}
return nil, appcmd.NewInvalidArgumentError("unable to determine source control URL for this repository; only GitHub/GitLab/BitBucket are supported")
}

func getLabelUploadOption(flags *flags) bufmodule.UploadOption {
// We do not allow the mixing of flags, so post-validation, we only expect one of the
// flags to be set. And so we return the corresponding bufmodule.UploadOption if any
Expand Down
34 changes: 18 additions & 16 deletions private/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,34 +136,36 @@ type ListFilesAndUnstagedFilesOptions struct {
IgnorePathRegexps []*regexp.Regexp
}

// RemoteKind is the kind of remote based on Git source (e.g. GitHub, GitLab, BitBucket, etc.)
type RemoteKind int

const (
// RemoteKindUnknown is a remote to a unknown Git source.
RemoteKindUnknown RemoteKind = iota + 1
// RemoteKindGitHub is a remote to a GitHub Git source.
RemoteKindGitHub
// RemoteKindGitLab is a remote to a GitLab Git source.
RemoteKindGitLab
// RemoteKindBitBucket is a remote to a BitBucket Git source.
RemoteKindBitBucket
)

// Remote represents a Git remote and provides associated metadata.
type Remote interface {
// Name of the remote (e.g. "origin")
Name() string
// HEADBranch is the name of the HEAD branch of the remote.
HEADBranch() string
// Kind of remote (e.g. GitHub, GitLab, BitBucket, etc.).
Kind() RemoteKind
// Hostname is the host name parsed from the remote URL. If the remote is an unknown
// kind, then this may be an empty string.
Hostname() string
// RepositoryPath is the path to the repository based on the remote URL. If the remote
// is an unknown kind, then this may be an empty string.
RepositoryPath() string
// SourceControlURL makes the best effort to construct a user-facing source control url
// given a commit sha string based on the remoteKind and available hostname and
// repository path information.
//
// If the remote hostname contains bitbucket (e.g. bitbucket.mycompany.com or bitbucket.org),
// then it uses the route /commits for the git commit sha.
//
// If the remote hostname contains github (e.g. github.mycompany.com or github.com) or gitlab
// (e.g. gitlab.mycompany.com or gitlab.com) then it uses the route /commit for the git
// commit sha.
//
// If the remote is unknown and/or no hostname/repository path information is available,
// this will return an empty string.
//
// This does not do any validation against the gitCommitSha provided.
SourceControlURL(gitCommitSha string) string

isRemote()
}

// GetRemote gets the Git remote based on the given remote name.
Expand Down
63 changes: 51 additions & 12 deletions private/pkg/git/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,35 @@ import (
)

const (
bitBucketHostname = "bitbucket"
githubHostname = "github"
gitlabHostname = "gitlab"
gitSuffix = ".git"
bitBucketHostname = "bitbucket"
githubHostname = "github"
gitlabHostname = "gitlab"
gitSuffix = ".git"
githubGitlabRemoteURLFormat = "https://%s%s/commit/%s"
bitBucketRemoteURLFormat = "https://%s%s/commits/%s"
)

var (
ErrRemoteNotFound = errors.New("git remote not found")
)

// remoteKind is the kind of remote based on Git source (e.g. GitHub, GitLab, BitBucket, etc.)
type remoteKind int

const (
// remoteKindUnknown is a remote to a unknown Git source.
remoteKindUnknown remoteKind = iota + 1
// RemoteKindGitHub is a remote to a GitHub Git source.
remoteKindGitHub
// RemoteKindGitLab is a remote to a GitLab Git source.
remoteKindGitLab
// RemoteKindBitBucket is a remote to a BitBucket Git source.
remoteKindBitBucket
)

type remote struct {
name string
kind RemoteKind
kind remoteKind
hostname string
repositoryPath string
headBranch string
Expand All @@ -51,7 +67,7 @@ func (r *remote) Name() string {
return r.name
}

func (r *remote) Kind() RemoteKind {
func (r *remote) Kind() remoteKind {
return r.kind
}

Expand All @@ -67,9 +83,32 @@ func (r *remote) HEADBranch() string {
return r.headBranch
}

func (r *remote) isRemote() {}

func (r *remote) SourceControlURL(gitCommitSha string) string {
switch r.kind {
case remoteKindBitBucket:
return fmt.Sprintf(
bitBucketRemoteURLFormat,
r.Hostname(),
r.RepositoryPath(),
gitCommitSha,
)
case remoteKindGitHub, remoteKindGitLab:
return fmt.Sprintf(
githubGitlabRemoteURLFormat,
r.Hostname(),
r.RepositoryPath(),
gitCommitSha,
)
}
// Unknown remote kind, we return an empty URL.
return ""
}

func newRemote(
name string,
kind RemoteKind,
kind remoteKind,
hostname string,
repositoryPath string,
headBranch string,
Expand Down Expand Up @@ -233,15 +272,15 @@ func getRemoteHEADBranch(
return "", errors.New("no HEAD branch information found")
}

func getRemoteKindFromHostname(hostname string) RemoteKind {
func getRemoteKindFromHostname(hostname string) remoteKind {
if strings.Contains(hostname, bitBucketHostname) {
return RemoteKindBitBucket
return remoteKindBitBucket
}
if strings.Contains(hostname, githubHostname) {
return RemoteKindGitHub
return remoteKindGitHub
}
if strings.Contains(hostname, gitlabHostname) {
return RemoteKindGitLab
return remoteKindGitLab
}
return RemoteKindUnknown
return remoteKindUnknown
}

0 comments on commit 8cc3b88

Please sign in to comment.