-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SCM webhook to trigger Pipelines (#597)
* Add scm webhook support * Add scm webhook support * Support to scan multi-branch pipeline via webhook * Add unit test for scm webhook * Support to trigger a regular pipeline via webhook * Add missing license header * Add documentation about the webhook * Fix the unit tests error
- Loading branch information
1 parent
2561eb9
commit 058ff4d
Showing
18 changed files
with
1,018 additions
and
39 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
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,47 @@ | ||
// Copyright 2022 KubeSphere Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package v1alpha3 | ||
|
||
import "encoding/json" | ||
|
||
// +kubebuilder:object:generate=false | ||
|
||
// LastChanges represents a set of last SCM changes | ||
type LastChanges map[string]string | ||
|
||
// GetLastChanges returns the last changes | ||
func GetLastChanges(jsonText string) (lastChange LastChanges, err error) { | ||
lastChange = map[string]string{} | ||
err = json.Unmarshal([]byte(jsonText), &lastChange) | ||
return | ||
} | ||
|
||
// Update updates hash by ref | ||
func (l LastChanges) Update(ref, hash string) LastChanges { | ||
l[ref] = hash | ||
return l | ||
} | ||
|
||
// LastHash return last hash value | ||
func (l LastChanges) LastHash(ref string) (hash string) { | ||
return l[ref] | ||
} | ||
|
||
// String returns the string JSON format | ||
func (l LastChanges) String() string { | ||
data, _ := json.Marshal(l) | ||
return string(data) | ||
} |
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,136 @@ | ||
/* | ||
Copyright 2022 The KubeSphere Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1alpha3 | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGetLastChanges(t *testing.T) { | ||
type args struct { | ||
jsonText string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantLastChange LastChanges | ||
wantErr assert.ErrorAssertionFunc | ||
}{{ | ||
name: "normal JSON data with map format", | ||
args: args{jsonText: `{"master":"1234"}`}, | ||
wantLastChange: map[string]string{ | ||
"master": "1234", | ||
}, | ||
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { | ||
return false | ||
}, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotLastChange, err := GetLastChanges(tt.args.jsonText) | ||
if !tt.wantErr(t, err, fmt.Sprintf("GetLastChanges(%v)", tt.args.jsonText)) { | ||
return | ||
} | ||
assert.Equalf(t, tt.wantLastChange, gotLastChange, "GetLastChanges(%v)", tt.args.jsonText) | ||
}) | ||
} | ||
} | ||
|
||
func TestLastChanges_Update(t *testing.T) { | ||
type args struct { | ||
ref string | ||
hash string | ||
} | ||
tests := []struct { | ||
name string | ||
l LastChanges | ||
args args | ||
want LastChanges | ||
}{{ | ||
name: "update the not existing value", | ||
l: map[string]string{}, | ||
args: args{ | ||
ref: "master", | ||
hash: "2345", | ||
}, | ||
want: map[string]string{ | ||
"master": "2345", | ||
}, | ||
}, { | ||
name: "update the existing value", | ||
l: map[string]string{ | ||
"master": "1234", | ||
}, | ||
args: args{ | ||
ref: "master", | ||
hash: "2345", | ||
}, | ||
want: map[string]string{ | ||
"master": "2345", | ||
}, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, tt.l.Update(tt.args.ref, tt.args.hash), "Update(%v, %v)", tt.args.ref, tt.args.hash) | ||
}) | ||
} | ||
} | ||
|
||
func TestLastChanges_LastHash(t *testing.T) { | ||
type args struct { | ||
ref string | ||
} | ||
tests := []struct { | ||
name string | ||
l LastChanges | ||
args args | ||
wantHash string | ||
}{{ | ||
name: "normal case", | ||
l: map[string]string{ | ||
"master": "1234", | ||
}, | ||
args: args{ref: "master"}, | ||
wantHash: "1234", | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.wantHash, tt.l.LastHash(tt.args.ref), "LastHash(%v)", tt.args.ref) | ||
}) | ||
} | ||
} | ||
|
||
func TestLastChanges_String(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
l LastChanges | ||
want string | ||
}{{ | ||
name: "normal case", | ||
l: map[string]string{ | ||
"master": "1234", | ||
}, | ||
want: `{"master":"1234"}`, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, tt.l.String(), "String()") | ||
}) | ||
} | ||
} |
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.