-
Notifications
You must be signed in to change notification settings - Fork 52
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
allow branches #34
base: master
Are you sure you want to change the base?
allow branches #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ on: | |
- push | ||
|
||
env: | ||
GO_VERSION: 1.18 | ||
GO_VERSION: 1.19 | ||
|
||
jobs: | ||
build: | ||
|
@@ -14,12 +14,12 @@ jobs: | |
steps: | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
uses: actions/checkout@v3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just updated these as older actions will stop working eventually (some nodejs thing that GitHub deprecated). |
||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v2 | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ env.GO_VERSION }} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
module github.com/sosedoff/gitkit | ||
|
||
go 1.16 | ||
go 1.19 | ||
|
||
require ( | ||
github.com/gofrs/uuid v4.0.0+incompatible | ||
github.com/stretchr/testify v1.7.0 | ||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a | ||
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
golang.org/x/sys v0.1.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,15 @@ import ( | |
"strings" | ||
|
||
"github.com/gofrs/uuid" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
const ZeroSHA = "0000000000000000000000000000000000000000" | ||
|
||
type Receiver struct { | ||
Debug bool | ||
MasterOnly bool | ||
AllowedRefs []string | ||
TmpDir string | ||
HandlerFunc func(*HookInfo, string) error | ||
} | ||
|
@@ -45,14 +47,30 @@ func IsForcePush(hook *HookInfo) (bool, error) { | |
return base != hook.OldRev, nil | ||
} | ||
|
||
func (r *Receiver) CheckAllowedBranch(hook *HookInfo) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made this public so I could add a test. Otherwise, the code in |
||
if r.MasterOnly { // for BC | ||
r.AllowedRefs = append(r.AllowedRefs, "refs/heads/master") | ||
} | ||
|
||
if len(r.AllowedRefs) == 0 { | ||
return nil | ||
} | ||
|
||
if !slices.Contains(r.AllowedRefs, hook.Ref) { | ||
return fmt.Errorf("cannot push branch, allowed branches: %s", strings.Join(r.AllowedRefs, ", ")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *Receiver) Handle(reader io.Reader) error { | ||
hook, err := ReadHookInput(reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if r.MasterOnly && hook.Ref != "refs/heads/master" { | ||
return fmt.Errorf("cant push to non-master branch") | ||
if err = r.CheckAllowedBranch(hook); err != nil { | ||
return err | ||
} | ||
|
||
id, err := uuid.NewV4() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package gitkit_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/sosedoff/gitkit" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type gitReceiveMock struct { | ||
name string | ||
masterOnly bool | ||
allowedBranches []string | ||
ref string | ||
err error | ||
} | ||
|
||
func TestMasterOnly(t *testing.T) { | ||
testCases := []gitReceiveMock{ | ||
{ | ||
name: "push to master, no error", | ||
masterOnly: true, | ||
ref: "refs/heads/master", | ||
err: nil, | ||
}, | ||
{ | ||
name: "push to a branch, should trigger error", | ||
masterOnly: true, | ||
ref: "refs/heads/branch", | ||
err: fmt.Errorf("cannot push branch, allowed branches: refs/heads/master"), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
r := &gitkit.Receiver{ | ||
MasterOnly: tc.masterOnly, | ||
} | ||
|
||
err := r.CheckAllowedBranch(&gitkit.HookInfo{ | ||
Ref: tc.ref, | ||
}) | ||
|
||
assert.Equal(t, tc.err, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestAllowedBranches(t *testing.T) { | ||
testCases := []gitReceiveMock{ | ||
{ | ||
name: "push to master, no error", | ||
allowedBranches: []string{"refs/heads/master"}, | ||
ref: "refs/heads/master", | ||
err: nil, | ||
}, | ||
{ | ||
name: "push to a branch, should trigger error", | ||
allowedBranches: []string{"refs/heads/master"}, | ||
ref: "refs/heads/some-branch", | ||
err: fmt.Errorf("cannot push branch, allowed branches: refs/heads/master"), | ||
}, | ||
{ | ||
name: "push to another-branch", | ||
allowedBranches: []string{"refs/heads/another-branch"}, | ||
ref: "refs/heads/another-branch", | ||
err: nil, | ||
}, | ||
{ | ||
name: "push to main and only allow main", | ||
allowedBranches: []string{"refs/heads/main"}, | ||
ref: "refs/heads/main", | ||
err: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
r := &gitkit.Receiver{ | ||
AllowedRefs: tc.allowedBranches, | ||
} | ||
|
||
err := r.CheckAllowedBranch(&gitkit.HookInfo{ | ||
Ref: tc.ref, | ||
}) | ||
|
||
assert.Equal(t, tc.err, err) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly updated to be able to use some functions and also, newer is better. :D