Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions backend/plugins/linker/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package impl
import (
"encoding/json"
"regexp"
"strings"

"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
Expand Down Expand Up @@ -84,13 +85,11 @@ func (p Linker) PrepareTaskData(taskCtx plugin.TaskContext, options map[string]i
taskData := &tasks.LinkerTaskData{
Options: op,
}
if op.PrToIssueRegexp != "" {
re, err := regexp.Compile(op.PrToIssueRegexp)
if err != nil {
return taskData, errors.Convert(err)
}
taskData.PrToIssueRegexp = re
re, compileErr := regexp.Compile(op.PrToIssueRegexp)
if compileErr != nil {
return taskData, errors.Convert(compileErr)
}
taskData.PrToIssueRegexp = re
return taskData, nil
}

Expand All @@ -109,6 +108,10 @@ func (p Linker) MakeMetricPluginPipelinePlanV200(projectName string, options jso
if err != nil {
return nil, errors.Default.WrapRaw(err)
}
op.PrToIssueRegexp = strings.TrimSpace(op.PrToIssueRegexp)
if op.PrToIssueRegexp == "" {
return nil, nil
}
plan := coreModels.PipelinePlan{
{
{
Expand Down
73 changes: 73 additions & 0 deletions backend/plugins/linker/impl/impl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 impl

import (
"encoding/json"
"testing"

coreModels "github.com/apache/incubator-devlake/core/models"
"github.com/stretchr/testify/assert"
)

func TestMakeMetricPluginPipelinePlanV200SkipsEmptyRegexp(t *testing.T) {
var linker Linker

plan, err := linker.MakeMetricPluginPipelinePlanV200("project", json.RawMessage(`{}`))

assert.Nil(t, err)
assert.Nil(t, plan)
}

func TestMakeMetricPluginPipelinePlanV200BuildsPlan(t *testing.T) {
var linker Linker
options, err := json.Marshal(map[string]string{
"prToIssueRegexp": `#(\d+)`,
})
assert.Nil(t, err)

plan, err := linker.MakeMetricPluginPipelinePlanV200("project", options)

assert.Nil(t, err)
assert.Equal(t, coreModels.PipelinePlan{
{
{
Plugin: "linker",
Options: map[string]interface{}{
"projectName": "project",
"prToIssueRegexp": `#(\d+)`,
},
Subtasks: []string{
"LinkPrToIssue",
},
},
},
}, plan)
}

func TestPrepareTaskDataRejectsEmptyRegexp(t *testing.T) {
var linker Linker

taskData, err := linker.PrepareTaskData(nil, map[string]interface{}{
"projectName": "project",
"prToIssueRegexp": "",
})

assert.NotNil(t, err)
assert.Nil(t, taskData)
}
3 changes: 3 additions & 0 deletions backend/plugins/linker/tasks/link_pr_and_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func clearHistoryData(db dal.Dal, data *LinkerTaskData) errors.Error {
func LinkPrToIssue(taskCtx plugin.SubTaskContext) errors.Error {
db := taskCtx.GetDal()
data := taskCtx.GetData().(*LinkerTaskData)
if data.PrToIssueRegexp == nil {
return errors.BadInput.New("prToIssueRegexp is required")
}

if err := clearHistoryData(db, data); err != nil {
return err
Expand Down
8 changes: 7 additions & 1 deletion backend/plugins/linker/tasks/task_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ limitations under the License.
package tasks

import (
"regexp"
"strings"

"github.com/apache/incubator-devlake/core/errors"
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"regexp"
)

type LinkerOptions struct {
Expand All @@ -39,5 +41,9 @@ func DecodeAndValidateTaskOptions(options map[string]interface{}) (*LinkerOption
if err != nil {
return nil, errors.Default.Wrap(err, "error decoding linker task options")
}
op.PrToIssueRegexp = strings.TrimSpace(op.PrToIssueRegexp)
if op.PrToIssueRegexp == "" {
return nil, errors.BadInput.New("prToIssueRegexp is required")
}
return &op, nil
}
43 changes: 43 additions & 0 deletions backend/plugins/linker/tasks/task_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 tasks

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDecodeAndValidateTaskOptionsRequiresPrToIssueRegexp(t *testing.T) {
options, err := DecodeAndValidateTaskOptions(map[string]interface{}{
"projectName": "project",
})

assert.NotNil(t, err)
assert.Nil(t, options)
}

func TestDecodeAndValidateTaskOptionsTrimsPrToIssueRegexp(t *testing.T) {
options, err := DecodeAndValidateTaskOptions(map[string]interface{}{
"projectName": "project",
"prToIssueRegexp": " #(\\d+) ",
})

assert.Nil(t, err)
assert.Equal(t, "#(\\d+)", options.PrToIssueRegexp)
}
Loading