-
Notifications
You must be signed in to change notification settings - Fork 13
/
checks.go
110 lines (98 loc) · 3.8 KB
/
checks.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// Copyright 2014, Sander van Harmelen
//
// 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 main
import (
"fmt"
"net/http"
"os"
"os/exec"
"strings"
"syscall"
)
func (cg *ChefGuard) executeChecks() (int, error) {
if cfg.Tests.Foodcritic != "" {
if errCode, err := runFoodcritic(cg.ChefOrg, cg.CookbookPath); err != nil {
if errCode == http.StatusInternalServerError || !cg.continueAfterFailedCheck("foodcritic") {
return errCode, err
}
}
}
if cfg.Tests.Rubocop != "" {
if errCode, err := runRubocop(cg.CookbookPath); err != nil {
if errCode == http.StatusInternalServerError || !cg.continueAfterFailedCheck("rubocop") {
return errCode, err
}
}
}
return 0, nil
}
func (cg *ChefGuard) continueAfterFailedCheck(check string) bool {
WARNING.Printf("%s errors when uploading cookbook '%s' for '%s'\n", strings.Title(check), cg.Cookbook.Name, cg.User)
if getEffectiveConfig("Mode", cg.ChefOrg).(string) == "permissive" && cg.ForcedUpload {
return true
}
return false
}
func runFoodcritic(org, cookbookPath string) (int, error) {
args := getFoodcriticArgs(org, cookbookPath)
cmd := exec.Command(cfg.Tests.Foodcritic, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "RUBY_THREAD_VM_STACK_SIZE=2097152")
output, err := cmd.CombinedOutput()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 3 {
errText := strings.TrimSpace(strings.Replace(string(output), fmt.Sprintf("%s/", cookbookPath), "", -1))
return http.StatusPreconditionFailed, fmt.Errorf("\n=== Foodcritic errors found ===\n%s\n===============================\n", errText)
}
}
return http.StatusInternalServerError, fmt.Errorf("Failed to execute \"foodcritic %s\": %s - %s", strings.Join(cmd.Args, " "), output, err)
}
// This is still needed for Foodcritic > v9.x.x
if strings.TrimSpace(string(output)) != "" {
errText := strings.TrimSpace(strings.Replace(string(output), fmt.Sprintf("%s/", cookbookPath), "", -1))
return http.StatusPreconditionFailed, fmt.Errorf("\n=== Foodcritic errors found ===\n%s\n===============================\n", errText)
}
return 0, nil
}
func getFoodcriticArgs(org, cookbookPath string) []string {
excludes := cfg.Default.ExcludeFCs
custExcludes := getEffectiveConfig("ExcludeFCs", org)
if excludes != custExcludes {
excludes = fmt.Sprintf("%s,%s", excludes, custExcludes)
}
args := []string{}
for _, exclude := range strings.Split(excludes, ",") {
args = append(args, "--tags", "~"+exclude)
}
if cfg.Default.IncludeFCs != "" {
args = append(args, "--include", cfg.Default.IncludeFCs)
}
return append(args, "--no-progress", "--cookbook-path", cookbookPath)
}
func runRubocop(cookbookPath string) (int, error) {
cmd := exec.Command(cfg.Tests.Rubocop, cookbookPath)
cmd.Env = []string{"HOME=" + cfg.Default.Tempdir}
output, err := cmd.CombinedOutput()
if err != nil {
if strings.Contains(string(output), "offense") {
errText := strings.TrimSpace(strings.Replace(string(output), fmt.Sprintf("%s/", cookbookPath), "", -1))
return http.StatusPreconditionFailed, fmt.Errorf("\n=== Rubocop errors found ===\n%s\n============================\n", errText)
}
return http.StatusInternalServerError, fmt.Errorf("Failed to execute \"rubocop %s\": %s - %s", cookbookPath, output, err)
}
return 0, nil
}