-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Graveler pre-commit/merge hooks (#1407)
- Loading branch information
Showing
17 changed files
with
496 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package catalog | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type Action struct { | ||
Name string `yaml:"name"` | ||
Description string `yaml:"description"` | ||
On struct { | ||
PreMerge *ActionOn `yaml:"pre-merge"` | ||
PreCommit *ActionOn `yaml:"pre-commit"` | ||
} `yaml:"on"` | ||
Hooks []ActionHook `yaml:"hooks"` | ||
} | ||
|
||
type ActionOn struct { | ||
Branches []string `yaml:"branches"` | ||
} | ||
|
||
type ActionHook struct { | ||
ID string `yaml:"id"` | ||
Type string `yaml:"type"` | ||
Description string `yaml:"description"` | ||
Properties map[string]string `yaml:"properties"` | ||
} | ||
|
||
var reHookID = regexp.MustCompile(`^[_a-zA-Z][\-_a-zA-Z0-9]{1,255}$`) | ||
|
||
func (a *Action) Validate() error { | ||
if a.On.PreMerge == nil && a.On.PreCommit == nil { | ||
return fmt.Errorf("%w 'on' is required", ErrInvalidAction) | ||
} | ||
ids := make(map[string]struct{}) | ||
for i, hook := range a.Hooks { | ||
if !reHookID.MatchString(hook.ID) { | ||
return fmt.Errorf("hook[%d] missing ID: %w", i, ErrInvalidAction) | ||
} | ||
if _, found := ids[hook.ID]; found { | ||
return fmt.Errorf("hook[%d] duplicate ID '%s': %w", i, hook.ID, ErrInvalidAction) | ||
} | ||
ids[hook.ID] = struct{}{} | ||
if hook.Type != "webhook" { | ||
return fmt.Errorf("hook[%d] '%s' unknown type: %w", i, hook.ID, ErrInvalidAction) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package catalog | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"path" | ||
"testing" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestAction_Validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
filename string | ||
wantErr error | ||
}{ | ||
{name: "full", filename: "action_full.yaml", wantErr: nil}, | ||
{name: "required", filename: "action_required.yaml", wantErr: nil}, | ||
{name: "duplicate id", filename: "action_duplicate_id.yaml", wantErr: ErrInvalidAction}, | ||
{name: "invalid id", filename: "action_invalid_id.yaml", wantErr: ErrInvalidAction}, | ||
{name: "invalid hook type", filename: "action_invalid_type.yaml", wantErr: ErrInvalidAction}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actionData, err := ioutil.ReadFile(path.Join("testdata", tt.filename)) | ||
if err != nil { | ||
t.Fatalf("Failed to load testdata %s, err=%s", tt.filename, err) | ||
} | ||
var act Action | ||
err = yaml.Unmarshal(actionData, &act) | ||
if err != nil { | ||
t.Fatalf("Unmarshal action err=%s", err) | ||
} | ||
err = act.Validate() | ||
if !errors.Is(err, tt.wantErr) { | ||
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
on: | ||
pre-merge: | ||
branches: | ||
- master | ||
hooks: | ||
- id: hook1 | ||
type: webhook | ||
properties: | ||
url: "https://api.lakefs.io/webhook1?t=1za2PbkZK1bd4prMuTDr6BeEQwWYcX2R" | ||
- id: hook1 | ||
type: webhook | ||
properties: | ||
url: "https://api.lakefs.io/webhook1?t=1za2PbkZK1bd4prMuTDr6BeEQwWYcX2R" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Good merge | ||
description: set of checks to verify that branch is good | ||
on: | ||
pre-merge: | ||
branches: | ||
- master | ||
- stage | ||
pre-commit: | ||
branches: | ||
- feature-* | ||
hooks: | ||
- id: no_temp | ||
type: webhook | ||
description: checking no temporary files found | ||
properties: | ||
url: "https://api.lakefs.io/webhook1?t=1za2PbkZK1bd4prMuTDr6BeEQwWYcX2R" | ||
- id: no_freeze | ||
type: webhook | ||
description: check production is not in dev freeze | ||
properties: | ||
url: "https://api.lakefs.io/webhook2?t=1za2PbkZK1bd4prMuTDr6BeEQwWYcX2R" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
on: | ||
pre-merge: | ||
branches: | ||
- master | ||
hooks: | ||
- id: not valid to use space | ||
type: webhook | ||
properties: | ||
url: "https://api.lakefs.io/webhook1?t=1za2PbkZK1bd4prMuTDr6BeEQwWYcX2R" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
on: | ||
pre-merge: | ||
branches: | ||
- master | ||
hooks: | ||
- id: no_temp | ||
type: command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
on: | ||
pre-merge: | ||
branches: | ||
- master | ||
hooks: | ||
- id: no_temp | ||
type: webhook | ||
properties: | ||
url: "https://api.lakefs.io/webhook1?t=1za2PbkZK1bd4prMuTDr6BeEQwWYcX2R" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.