Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AWS CodeCommit HTTP (GRC) #321

Merged
merged 1 commit into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -72,7 +73,11 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error {

switch {
case newPath:
logger.Log("clone", fmt.Sprintf("%s -> %s", remoteURL, fpath))
if remoteURL.Scheme == "codecommit" {
logger.Log("clone", fmt.Sprintf("%s -> %s", remoteURL.Opaque, fpath))
} else {
logger.Log("clone", fmt.Sprintf("%s -> %s", remoteURL, fpath))
}
var (
localRepoRoot = fpath
repoURL = remoteURL
Expand All @@ -88,6 +93,9 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error {
localRepoRoot = filepath.Join(local.RootPath, remoteURL.Hostname(), l)
}

if remoteURL.Scheme == "codecommit" {
repoURL, _ = url.Parse(remoteURL.Opaque)
}
if getRepoLock(localRepoRoot) {
return vcs.Clone(&vcsGetOption{
url: repoURL,
Expand Down
7 changes: 5 additions & 2 deletions local_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ func LocalRepositoryFromURL(remoteURL *url.URL) (*LocalRepository, error) {
if localRepository != nil {
return localRepository, nil
}

prim, err := getRoot(remoteURL.String())
var remoteURLStr = remoteURL.String()
if remoteURL.Scheme == "codecommit" {
remoteURLStr = remoteURL.Opaque
}
prim, err := getRoot(remoteURLStr)
if err != nil {
return nil, err
}
Expand Down
33 changes: 29 additions & 4 deletions remote_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ func (repo *DarksHubRepository) VCS() (*VCSBackend, *url.URL, error) {
return DarcsBackend, repo.URL(), nil
}

// A CodeCommitRepository represents a CodeCommit repository. Implements RemoteRepository.
type CodeCommitRepository struct {
url *url.URL
}

// URL reutrns URL of the repository
func (repo *CodeCommitRepository) URL() *url.URL {
return repo.url
}

// IsValid determine if the repository is valid or not
func (repo *CodeCommitRepository) IsValid() bool {
return true
}

// VCS returns VCSBackend of the repository
func (repo *CodeCommitRepository) VCS() (*VCSBackend, *url.URL, error) {
u := *repo.url
return GitBackend, &u, nil
}

// OtherRepository represents other repository
type OtherRepository struct {
url *url.URL
Expand All @@ -109,11 +130,12 @@ func (repo *OtherRepository) IsValid() bool {
}

var (
vcsSchemeReg = regexp.MustCompile(`^(git|svn|bzr)(?:\+|$)`)
vcsSchemeReg = regexp.MustCompile(`^(git|svn|bzr|codecommit)(?:\+|$)`)
scheme2vcs = map[string]*VCSBackend{
"git": GitBackend,
"svn": SubversionBackend,
"bzr": BazaarBackend,
"git": GitBackend,
"codecommit": GitBackend,
"svn": SubversionBackend,
"bzr": BazaarBackend,
}
)

Expand Down Expand Up @@ -165,6 +187,9 @@ func (repo *OtherRepository) VCS() (*VCSBackend, *url.URL, error) {
// NewRemoteRepository returns new RemoteRepository object from URL
func NewRemoteRepository(u *url.URL) (RemoteRepository, error) {
repo := func() RemoteRepository {
if u.Scheme == "codecommit" {
return &CodeCommitRepository{u}
}
switch u.Host {
case "github.com":
return &GitHubRepository{u}
Expand Down
55 changes: 55 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"bytes"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
Expand All @@ -20,6 +22,7 @@ var (
hasSchemePattern = regexp.MustCompile("^[^:]+://")
scpLikeURLPattern = regexp.MustCompile("^([^@]+@)?([^:]+):(/?.+)$")
looksLikeAuthorityPattern = regexp.MustCompile(`[A-Za-z0-9]\.[A-Za-z]+(?::\d{1,5})?$`)
codecommitLikeURLPattern = regexp.MustCompile(`^(codecommit):(?::([a-z][a-z0-9-]+):)?//(?:([^]]+)@)?([\w\.-]+)$`)
)

func newURL(ref string, ssh, forceMe bool) (*url.URL, error) {
Expand Down Expand Up @@ -51,6 +54,58 @@ func newURL(ref string, ssh, forceMe bool) (*url.URL, error) {
}
}

if codecommitLikeURLPattern.MatchString(ref) {
// SEE ALSO:
// https://github.com/aws/git-remote-codecommit/blob/master/git_remote_codecommit/__init__.py#L68
matched := codecommitLikeURLPattern.FindStringSubmatch(ref)
region := matched[2]

if matched[2] == "" {
// Region detection priority:
// 1. Explicit specification (codecommit::region://...)
// 2. Environment variables
// a. AWS_REGION (implicit priority)
// b. AWS_DEFAULT_REGION
// 3. AWS CLI profiles
// SEE ALSO:
// https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-precedence
var exists bool
region, exists = os.LookupEnv("AWS_REGION")
if !exists {
region, exists = os.LookupEnv("AWS_DEFAULT_REGION")
}

if !exists {
var stdout bytes.Buffer
var stderr bytes.Buffer

cmd := exec.Command("aws", "configure", "get", "region")
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Run()
if err != nil {
if stderr.String() == "" {
fmt.Fprintln(os.Stderr, "You must specify a region. You can also configure your region by running \"aws configure\".")
} else {
fmt.Fprint(os.Stderr, stderr.String())
}
os.Exit(1)
}

region = strings.TrimSpace(stdout.String())
}
}

return &url.URL{
Scheme: matched[1],
Host: region,
User: url.User(matched[3]),
Path: matched[4],
Opaque: ref,
}, nil
}

if !hasSchemePattern.MatchString(ref) {
if scpLikeURLPattern.MatchString(ref) {
matched := scpLikeURLPattern.FindStringSubmatch(ref)
Expand Down
1 change: 1 addition & 0 deletions vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ var BazaarBackend = &VCSBackend{
var vcsRegistry = map[string]*VCSBackend{
"git": GitBackend,
"github": GitBackend,
"codecommit": GitBackend,
"svn": SubversionBackend,
"subversion": SubversionBackend,
"git-svn": GitsvnBackend,
Expand Down