Skip to content

Commit

Permalink
Merge pull request #321 from xeres/feature-remote-codecommit
Browse files Browse the repository at this point in the history
Support AWS CodeCommit HTTP (GRC)
  • Loading branch information
Songmu authored May 22, 2021
2 parents 22ce394 + e6c6bb3 commit c62fd67
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
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

0 comments on commit c62fd67

Please sign in to comment.