Skip to content

Commit

Permalink
feat(sonarqube): init plugin (apache#4197)
Browse files Browse the repository at this point in the history
* feat(sonarqube): init

* fix(sonarqube): fix conflicts

* feat(sonarqube): modify connection

* fix(sonarqube): resolve conflicts

* fix(project): fix partial lint

* fix(framework): fix for golangci lint
  • Loading branch information
warren830 authored Jan 12, 2023
1 parent 243cc8a commit c050d72
Show file tree
Hide file tree
Showing 49 changed files with 839 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
uses: actions/cache@v3
with:
path: golangci-lint
key: ${{ runner.os }}-golangci-lint
key: ${{ runner.os }}-backend-golangci-lint
- name: generate mock
run: |
cd backend
Expand Down
4 changes: 2 additions & 2 deletions backend/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ linters-settings:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#bare-return
- name: bare-return
severity: warning
disabled: false
disabled: true
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#blank-imports
- name: blank-imports
severity: warning
Expand Down Expand Up @@ -167,7 +167,7 @@ linters-settings:
# # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#exported
- name: exported
severity: warning
disabled: false
disabled: true
arguments:
- "disableStutteringCheck"
# - "checkPrivateReceivers"
Expand Down
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ RUN if [ "$(arch)" != "x86_64" ] ; then \
apt-get install -y gcc-x86-64-linux-gnu binutils-x86-64-linux-gnu ; \
fi

RUN go install github.com/vektra/mockery/v2@v2.12.3
RUN go install github.com/vektra/mockery/v2@v2.14.0
RUN go install github.com/swaggo/swag/cmd/[email protected]

COPY --from=debian-amd64 /usr/include /rootfs-amd64/usr/include
Expand Down
1 change: 1 addition & 0 deletions backend/core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package config

import "time"

// ConfigReader defined a serial of interfaces
type ConfigReader interface {
Get(key string) interface{}
GetBool(name string) bool
Expand Down
2 changes: 1 addition & 1 deletion backend/core/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Tabler interface {
TableName() string
}

// Default Table is working for the Tabler interface witch only need TableName
// DefaultTabler is working for the Tabler interface witch only need TableName
type DefaultTabler struct {
Name string
}
Expand Down
8 changes: 4 additions & 4 deletions backend/core/plugin/plugin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"net/url"
)

// Contains api request information
// ApiResourceInput Contains api request information
type ApiResourceInput struct {
Params map[string]string // path variables
Query url.Values // query string
Expand All @@ -37,7 +37,7 @@ type OutputFile struct {
Data []byte
}

// Describe response data of a api
// ApiResourceOutput Describe response data of a api
type ApiResourceOutput struct {
Body interface{} // response body
Status int
Expand All @@ -47,10 +47,10 @@ type ApiResourceOutput struct {

type ApiResourceHandler func(input *ApiResourceInput) (*ApiResourceOutput, errors.Error)

// Implement this interface if plugin offered API
// PluginApi: Implement this interface if plugin offered API
// Code sample to register a api on `sources/:connectionId`:
//
// func (plugin Jira) ApiResources() map[string]map[string]plugin.ApiResourceHandler {
// func (p Jira) ApiResources() map[string]map[string]plugin.ApiResourceHandler {
// return map[string]map[string]plugin.ApiResourceHandler{
// "connections/:connectionId": {
// "PUT": api.PutConnection,
Expand Down
16 changes: 8 additions & 8 deletions backend/core/plugin/plugin_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type RunningProgress struct {
Total int
SubTaskName string
SubTaskNumber int
}
} // nolint

// ExecContext This interface define all resources that needed for task/subtask execution
type ExecContext interface {
Expand Down Expand Up @@ -67,24 +67,24 @@ type TaskContext interface {
type SubTask interface {
// Execute FIXME ...
Execute() errors.Error
}
} // nolint

// SubTaskEntryPoint All subtasks from plugins should comply to this prototype, so they could be orchestrated by framework
type SubTaskEntryPoint func(c SubTaskContext) errors.Error

const DOMAIN_TYPE_CODE = "CODE"
const DOMAIN_TYPE_TICKET = "TICKET"
const DOMAIN_TYPE_CODE_REVIEW = "CODEREVIEW"
const DOMAIN_TYPE_CROSS = "CROSS"
const DOMAIN_TYPE_CICD = "CICD"
const DOMAIN_TYPE_CODE = "CODE" //nolint
const DOMAIN_TYPE_TICKET = "TICKET" //nolint
const DOMAIN_TYPE_CODE_REVIEW = "CODEREVIEW" //nolint
const DOMAIN_TYPE_CROSS = "CROSS" //nolint
const DOMAIN_TYPE_CICD = "CICD" //nolint

var DOMAIN_TYPES = []string{
DOMAIN_TYPE_CODE,
DOMAIN_TYPE_TICKET,
DOMAIN_TYPE_CODE_REVIEW,
DOMAIN_TYPE_CROSS,
DOMAIN_TYPE_CICD,
}
} //nolint

// SubTaskMeta Metadata of a subtask
type SubTaskMeta struct {
Expand Down
10 changes: 5 additions & 5 deletions backend/core/plugin/plugin_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ func Decrypt(encKey, encryptedText string) (string, errors.Error) {
return "", errors.Default.New("invalid encKey")
}

// PKCS7 padding
// PKCS7Padding PKCS7 padding
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}

// PKCS7 unPadding
// PKCS7UnPadding PKCS7 unPadding
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
if length == 0 {
Expand All @@ -102,7 +102,7 @@ func PKCS7UnPadding(origData []byte) []byte {
return origData[:(length - unpadding)]
}

// AES encryption, CBC
// AesEncrypt AES encryption, CBC
func AesEncrypt(origData, key []byte) ([]byte, errors.Error) {
// data alignment fill and encryption
sha256Key := sha256.Sum256(key)
Expand All @@ -120,7 +120,7 @@ func AesEncrypt(origData, key []byte) ([]byte, errors.Error) {
return crypted, nil
}

// AES decryption
// AesDecrypt AES decryption
func AesDecrypt(crypted, key []byte) ([]byte, errors.Error) {
// Uniformly use sha256 to process as 32-bit Byte (256-bit bit)
sha256Key := sha256.Sum256(key)
Expand All @@ -143,7 +143,7 @@ func AesDecrypt(crypted, key []byte) ([]byte, errors.Error) {
return origData, nil
}

// A random string of length len uppercase characters
// RandomCapsStr A random string of length len uppercase characters
func RandomCapsStr(len int) string {
r := rand.New(rand.NewSource(time.Now().Unix()))
randomBytes := make([]byte, len)
Expand Down
14 changes: 7 additions & 7 deletions backend/core/runner/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ import (
)

// NewGormDb creates a new *gorm.DB and set it up properly
func NewGormDb(config config.ConfigReader, logger log.Logger) (*gorm.DB, errors.Error) {
return NewGormDbEx(config, logger, &dal.SessionConfig{
func NewGormDb(configReader config.ConfigReader, logger log.Logger) (*gorm.DB, errors.Error) {
return NewGormDbEx(configReader, logger, &dal.SessionConfig{
PrepareStmt: true,
SkipDefaultTransaction: true,
})
}

// NewGormDbEx acts like NewGormDb but accept extra sessionConfig
func NewGormDbEx(config config.ConfigReader, logger log.Logger, sessionConfig *dal.SessionConfig) (*gorm.DB, errors.Error) {
func NewGormDbEx(configReader config.ConfigReader, logger log.Logger, sessionConfig *dal.SessionConfig) (*gorm.DB, errors.Error) {
dbLoggingLevel := gormLogger.Error
switch strings.ToLower(config.GetString("DB_LOGGING_LEVEL")) {
switch strings.ToLower(configReader.GetString("DB_LOGGING_LEVEL")) {
case "silent":
dbLoggingLevel = gormLogger.Silent
case "warn":
Expand All @@ -52,11 +52,11 @@ func NewGormDbEx(config config.ConfigReader, logger log.Logger, sessionConfig *d
dbLoggingLevel = gormLogger.Info
}

idleConns := config.GetInt("DB_IDLE_CONNS")
idleConns := configReader.GetInt("DB_IDLE_CONNS")
if idleConns <= 0 {
idleConns = 10
}
dbMaxOpenConns := config.GetInt("DB_MAX_CONNS")
dbMaxOpenConns := configReader.GetInt("DB_MAX_CONNS")
if dbMaxOpenConns <= 0 {
dbMaxOpenConns = 100
}
Expand All @@ -74,7 +74,7 @@ func NewGormDbEx(config config.ConfigReader, logger log.Logger, sessionConfig *d
PrepareStmt: sessionConfig.PrepareStmt,
SkipDefaultTransaction: sessionConfig.SkipDefaultTransaction,
}
dbUrl := config.GetString("DB_URL")
dbUrl := configReader.GetString("DB_URL")
if dbUrl == "" {
return nil, errors.BadInput.New("DB_URL is required")
}
Expand Down
6 changes: 3 additions & 3 deletions backend/core/runner/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func GetMigrator() plugin.Migrator {

/*
// RegisterMigrationScripts FIXME ...
func RegisterMigrationScripts(scripts []migration.Script, comment string, config plugin.ConfigGetter, logger plugin.Logger) {
func RegisterMigrationScripts(scripts []plugin.MigrationScript, comment string, config core.ConfigGetter, logger core.Logger) {
for _, script := range scripts {
if s, ok := script.(plugin.InjectConfigGetter); ok {
if s, ok := script.(core.InjectConfigGetter); ok {
s.SetConfigGetter(config)
}
if s, ok := script.(plugin.InjectLogger); ok {
if s, ok := script.(core.InjectLogger); ok {
s.SetLogger(logger)
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/generator/cmd/init_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Type in which plugin do you want init migrations in, then generator will create
util.WriteTemplates(migrationPath, templates)
if modifyExistCode {
println("Last Step: add some code in plugin to implement Migratable like this:\n" +
"func (plugin " + strcase.ToCamel(pluginName) + ") MigrationScripts() []migration.Script {\n\treturn migrationscripts.All()\n}")
"func (p " + strcase.ToCamel(pluginName) + ") MigrationScripts() []plugin.MigrationScript {\n\treturn migrationscripts.All()\n}")
}
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

type addInitTables struct {}

func (*{{ .Purpose }}) Up(basicRes core.BasicRes) errors.Error {
func (*addInitTables) Up(basicRes context.BasicRes) errors.Error {
return migrationhelper.AutoMigrateTables(
basicRes,
// TO Add models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

type {{ .Purpose }} struct{}

func (*{{ .Purpose }}) Up(basicRes core.BasicRes) errors.Error {
func (*{{ .Purpose }}) Up(basicRes context.BasicRes) errors.Error {
return migrationhelper.AutoMigrateTables(
basicRes,
// TO Add models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (u *{{ .Purpose }}) SetConfigGetter(config core.ConfigGetter) {
u.config = config
}

func (*{{ .Purpose }}) Up(basicRes core.BasicRes) errors.Error {
func (*{{ .Purpose }}) Up(basicRes context.BasicRes) errors.Error {
return migrationhelper.AutoMigrateTables(
basicRes,
// TO Add models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ limitations under the License.

package migrationscripts

import "github.com/apache/incubator-devlake/migration"
import "github.com/apache/incubator-devlake/plugins/core"

// All return all the migration scripts
func All() []migration.Script {
return []migration.Script{
func All() []plugin.MigrationScript {
return []plugin.MigrationScript{
new(addInitTables),
}
}
4 changes: 2 additions & 2 deletions backend/generator/template/plugin/api/blueprint.go-template
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"github.com/apache/incubator-devlake/plugins/{{ .plugin_name }}/tasks"
)

func MakePipelinePlan(subtaskMetas []core.SubTaskMeta, connectionId uint64, scope []*core.BlueprintScopeV100) (core.PipelinePlan, errors.Error) {
func MakePipelinePlan(subtaskMetas []plugin.SubTaskMeta, connectionId uint64, scope []*plugin.BlueprintScopeV100) (plugin.PipelinePlan, errors.Error) {
var err error
plan := make(core.PipelinePlan, len(scope))
plan := make(plugin.PipelinePlan, len(scope))
for i, scopeElem := range scope {
taskOptions := make(map[string]interface{})
err = json.Unmarshal(scopeElem.Options, &taskOptions)
Expand Down
24 changes: 12 additions & 12 deletions backend/generator/template/plugin/api/connection.go-template
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ import (
)

//TODO Please modify the following code to fit your needs
func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
func TestConnection(input *core.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
// decode
var err errors.Error
var connection models.TestConnectionRequest
if err = helper.Decode(input.Body, &connection, vld); err != nil {
return nil, err
}
// test connection
apiClient, err := helper.NewApiClient(
apiClient, err := api.NewApiClient(
context.TODO(),
connection.Endpoint,
map[string]string{
Expand Down Expand Up @@ -78,14 +78,14 @@ POST /plugins/{{ .PluginName }}/connections
"password": "{{ .PluginName }} api access token"
}
*/
func PostConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
func PostConnections(input *core.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
// update from request and save to database
connection := &models.{{ .PluginName }}Connection{}
err := connectionHelper.Create(connection, input)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil
return &plugin.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil
}

//TODO Please modify the folowing code to adapt to your plugin
Expand All @@ -98,38 +98,38 @@ PATCH /plugins/{{ .PluginName }}/connections/:connectionId
"password": "{{ .PluginName }} api access token"
}
*/
func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
func PatchConnection(input *core.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection := &models.{{ .PluginName }}Connection{}
err := connectionHelper.Patch(connection, input)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connection}, nil
return &plugin.ApiResourceOutput{Body: connection}, nil
}

/*
DELETE /plugins/{{ .PluginName }}/connections/:connectionId
*/
func DeleteConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
func DeleteConnection(input *core.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection := &models.{{ .PluginName }}Connection{}
err := connectionHelper.First(connection, input.Params)
if err != nil {
return nil, err
}
err = connectionHelper.Delete(connection)
return &core.ApiResourceOutput{Body: connection}, err
return &plugin.ApiResourceOutput{Body: connection}, err
}

/*
GET /plugins/{{ .PluginName }}/connections
*/
func ListConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
func ListConnections(input *core.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
var connections []models.{{ .PluginName }}Connection
err := connectionHelper.List(&connections)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connections, Status: http.StatusOK}, nil
return &plugin.ApiResourceOutput{Body: connections, Status: http.StatusOK}, nil
}

//TODO Please modify the folowing code to adapt to your plugin
Expand All @@ -142,8 +142,8 @@ GET /plugins/{{ .PluginName }}/connections/:connectionId
"password": "{{ .PluginName }} api access token"
}
*/
func GetConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
func GetConnection(input *core.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection := &models.{{ .PluginName }}Connection{}
err := connectionHelper.First(connection, input.Params)
return &core.ApiResourceOutput{Body: connection}, err
return &plugin.ApiResourceOutput{Body: connection}, err
}
Loading

0 comments on commit c050d72

Please sign in to comment.