Skip to content

Commit

Permalink
feat: add support to dissect a CodeCommit URL into component parts (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
ga-paul-t committed Feb 2, 2022
1 parent 9adf2f2 commit 07a9e71
Show file tree
Hide file tree
Showing 23 changed files with 314 additions and 172 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 Gemba Advantage
# Copyright (c) 2022 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 Gemba Advantage
# Copyright (c) 2022 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 7 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 Gemba Advantage
# Copyright (c) 2022 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,20 +37,22 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Install Task
uses: arduino/setup-task@v1
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
skip-go-installation: true
- name: Test
run: make test
run: task test
- name: Code Coverage
uses: codecov/codecov-action@v2.0.3
- name: Build
run: make build
run: task build
- name: Tag
if: github.ref == 'refs/heads/main'
uses: gembaadvantage/uplift-action@v1
uses: gembaadvantage/uplift-action@v2.0.1
with:
version: v0.7.0
args: release
env:
GITHUB_TOKEN: ${{ secrets.GH_UPLIFT }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 Gemba Advantage
# Copyright (c) 2022 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 4 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 Gemba Advantage
# Copyright (c) 2022 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -40,7 +40,8 @@ linters:
- varcheck

linters-settings:
gofmt:
simplify: true
gofumpt:
lang-version: "1.17"
extra-rules: true
dupl:
threshold: 400
14 changes: 13 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 Gemba Advantage
# Copyright (c) 2022 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -45,6 +45,7 @@ builds:
ignore:
- goos: darwin
goarch: 386

archives:
- id: codecommit-sign-archive
format: tar.gz
Expand All @@ -61,8 +62,10 @@ archives:
files:
- README.md
- LICENSE

signs:
- artifacts: checksum

brews:
- name: codecommit-sign
tap:
Expand All @@ -86,10 +89,19 @@ brews:
test: |
installed_version = shell_output("#{bin}/codecommit-sign version --short 2>&1")
assert_match "v#{version}", installed_version
scoop:
bucket:
owner: gembaadvantage
name: scoops
homepage: "https://github.com/gembaadvantage/codecommit-sign"
description: "Generate a signed AWS V4 CodeCommit URL directly from an IAM role. No dedicated CodeCommit credentials needed"
license: MIT

rigs:
- rig:
owner: gembaadvantage
name: fish-food
homepage: "https://github.com/gembaadvantage/codecommit-sign"
description: "Generate a signed AWS V4 CodeCommit URL directly from an IAM role. No dedicated CodeCommit credentials needed"
license: MIT
2 changes: 1 addition & 1 deletion .uplift.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 Gemba Advantage
# Copyright (c) 2022 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
67 changes: 0 additions & 67 deletions Makefile

This file was deleted.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ brew install codecommit-sign
To use [Fish](https://gofi.sh/):

```sh
gofish install codecommit-sign
gofish rig add https://github.com/gembaadvantage/fish-food
gofish install github.com/gembaadvantage/fish-food/codecommit-sign

```

### Scoop
Expand Down
65 changes: 65 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (c) 2022 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# in the Software without restriction, including without limitation the rights
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

version: "3"

vars:
BINDIR: bin
BINNAME: codecommit-sign
GIT_COMMIT:
sh: git rev-parse HEAD
GIT_SHA:
sh: git rev-parse --short HEAD
GIT_BRANCH:
sh: git branch --show-current
LDFLAGS: >
-X github.com/gembaadvantage/codecommit-sign/internal/version.version=dev-{{.GIT_SHA}}
-X github.com/gembaadvantage/codecommit-sign/internal/version.gitCommit={{.GIT_COMMIT}}
-X github.com/gembaadvantage/codecommit-sign/internal/version.gitBranch={{.GIT_BRANCH}}
-X github.com/gembaadvantage/codecommit-sign/internal/version.buildDate={{now | date "2006-01-02T15:04:05Z07:00"}}
tasks:
default:
desc: Runs all of the default tasks
cmds:
- task: clean
- task: lint
- task: test
- task: build

build:
desc: Build the uplift binary
cmds:
- go build -ldflags '-s -w {{.LDFLAGS}}' -o '{{.BINDIR}}/{{.BINNAME}}' ./cmd/codecommitsign

test:
desc: Run the tests
cmds:
- go test -race -vet=off -p 1 -covermode=atomic -coverprofile=coverage.out ./...

lint:
desc: Lint the code using golangci
cmds:
- golangci-lint run --timeout 5m0s

clean:
desc: Remove any built artifacts
cmds:
- rm -rf {{.BINDIR}}
2 changes: 1 addition & 1 deletion cmd/codecommitsign/completion.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
2 changes: 1 addition & 1 deletion cmd/codecommitsign/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions cmd/codecommitsign/root.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -104,7 +104,7 @@ func (o signOptions) Run(out io.Writer) error {
// Detect if a GRC URL has been provided and translate
if strings.HasPrefix(o.CloneURL, "codecommit::") {
var terr error
o.CloneURL, terr = translate.FromGrc(o.CloneURL)
o.CloneURL, terr = translate.FromGRC(o.CloneURL)
if terr != nil {
return terr
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/codecommitsign/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion internal/version/build.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion pkg/awsv4/signer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion pkg/awsv4/signer_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
39 changes: 11 additions & 28 deletions pkg/translate/grc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021 Gemba Advantage
Copyright (c) 2022 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,44 +23,27 @@ SOFTWARE.
package translate

import (
"errors"
"fmt"
"regexp"
"strings"
)

var (
urlRgx = regexp.MustCompile(`^https://(.+@)?git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
grcRgx = regexp.MustCompile(`^codecommit::(.+)://(.+)$`)
)

// ToGrc translates a CodeCommit HTTPS URL to a compatible CodeCommit (git-remote-codecommit)
// GRC based URL that can be used to fetch and push changes to a CodeCommit repository
func ToGrc(url string) (string, error) {
m := urlRgx.FindStringSubmatch(url)
if len(m) < 4 {
return "", errors.New("malformed codecommit HTTPS URL")
func ToGRC(url string) (string, error) {
rem, err := RemoteHTTPS(url)
if err != nil {
return "", err
}

region := m[2]
repo := m[len(m)-1]

return fmt.Sprintf("codecommit::%s://%s", region, repo), nil
return fmt.Sprintf("codecommit::%s://%s", rem.Region, rem.Repository), nil
}

// FromGrc translates a CodeCommit (git-remote-codecommit) GRC URL to a compatible HTTPS URL
// that can be used to fetch and push changes to a CodeCommit repository
func FromGrc(url string) (string, error) {
m := grcRgx.FindStringSubmatch(url)
if len(m) < 3 {
return "", errors.New("malformed codecommit grc URL")
}

region := m[1]
repo := m[len(m)-1]
if strings.Contains(repo, "@") {
repo = strings.Split(repo, "@")[1]
func FromGRC(url string) (string, error) {
rem, err := RemoteGRC(url)
if err != nil {
return "", err
}

return fmt.Sprintf("https://git-codecommit.%s.amazonaws.com/v1/repos/%s", region, repo), nil
return fmt.Sprintf("https://git-codecommit.%s.amazonaws.com/v1/repos/%s", rem.Region, rem.Repository), nil
}

0 comments on commit 07a9e71

Please sign in to comment.