forked from salsaflow/salsaflow
/
code_review_tool.go
63 lines (51 loc) · 1.62 KB
/
code_review_tool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package modules
import (
// Stdlib
"fmt"
// Internal
"github.com/salsaflow/salsaflow/errs"
"github.com/salsaflow/salsaflow/modules/common"
// Internal: modules
github "github.com/salsaflow/salsaflow/modules/github/codereview"
"github.com/salsaflow/salsaflow/modules/reviewboard"
)
// CodeReviewTool instantiation ------------------------------------------------
type CodeReviewToolFactory func() (common.CodeReviewTool, error)
var codeReviewToolFactories = map[string]CodeReviewToolFactory{
github.Id: github.Factory,
reviewboard.Id: reviewboard.Factory,
}
func AvailableCodeReviewToolKeys() []string {
keys := make([]string, 0, len(codeReviewToolFactories))
for key := range codeReviewToolFactories {
keys = append(keys, key)
}
return keys
}
func GetCodeReviewTool() (common.CodeReviewTool, error) {
// Load configuration.
config, err := common.LoadConfig()
if err != nil && config == nil {
return nil, err
}
// Choose the code review tool based on the configuration.
var task = "Instantiate the selected code review plugin"
id := config.CodeReviewToolId()
factory, ok := codeReviewToolFactories[id]
if !ok {
// Collect the available code review tool ids.
ids := make([]string, 0, len(codeReviewToolFactories))
for id := range codeReviewToolFactories {
ids = append(ids, id)
}
hint := fmt.Sprintf("\nAvailable code review tools: %v\n\n", ids)
return nil, errs.NewErrorWithHint(
task, fmt.Errorf("unknown code review tool: '%v'", id), hint)
}
// Try to instantiate the code review tool.
tool, err := factory()
if err != nil {
return nil, errs.NewError(task, err)
}
return tool, nil
}