Skip to content

Commit 9f92b42

Browse files
authored
Merge branch 'konveyor:main' into main
2 parents e08e76a + 8d1d186 commit 9f92b42

File tree

7 files changed

+1377
-45
lines changed

7 files changed

+1377
-45
lines changed

.github/workflows/koncur-test.yaml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: Koncur Integration Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_call:
7+
inputs:
8+
koncur_ref:
9+
type: string
10+
required: false
11+
default: 'main'
12+
description: Koncur branch/tag/ref to test against
13+
workflow_dispatch:
14+
inputs:
15+
koncur_ref:
16+
type: string
17+
required: false
18+
default: 'main'
19+
description: Koncur branch/tag/ref to test against
20+
21+
jobs:
22+
koncur-test:
23+
name: Set up koncur tests
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Checkout kantra
27+
uses: actions/checkout@v4
28+
with:
29+
fetch-depth: 0
30+
31+
- name: Set up Go
32+
uses: actions/setup-go@v5
33+
with:
34+
go-version: '1.21'
35+
36+
- name: Extract pull request number from inputs or PR description
37+
run: |
38+
echo "${{ github.event.pull_request.body }}"
39+
PULL_REQUEST_NUMBER=$(echo "${{ github.event.pull_request.body }}" | grep -oP 'Analyzer PR: \K\d+' || true)
40+
if [ -z "$PULL_REQUEST_NUMBER" ]; then
41+
echo "ANALYZER_REF=main" >>$GITHUB_ENV
42+
else
43+
echo "ANALYZER_REF=refs/pull/$PULL_REQUEST_NUMBER/merge" >>$GITHUB_ENV
44+
fi
45+
46+
- name: Checkout analyzer-lsp
47+
uses: actions/checkout@v4
48+
with:
49+
repository: konveyor/analyzer-lsp
50+
ref: ${{ env.ANALYZER_REF }}
51+
path: analyzer-lsp
52+
53+
- name: Build analyzer image
54+
working-directory: analyzer-lsp
55+
run: |
56+
podman build -t quay.io/konveyor/analyzer-lsp:latest .
57+
58+
- name: Build kantra image and binary
59+
run: |
60+
podman build -t localhost/kantra:latest -f Dockerfile .
61+
go build -o kantra main.go
62+
63+
- name: Checkout koncur repository
64+
uses: actions/checkout@v4
65+
with:
66+
repository: konveyor/koncur
67+
ref: ${{ inputs.koncur_ref || 'main' }}
68+
path: koncur
69+
70+
- name: Create kantra target configuration
71+
run: |
72+
cat > koncur/target-kantra.yaml << 'EOF'
73+
type: kantra
74+
kantra:
75+
binaryPath: ${{ github.workspace }}/kantra
76+
EOF
77+
78+
- name: Run koncur tests
79+
env:
80+
RUNNER_IMG: localhost/kantra:latest
81+
run: |
82+
cd koncur
83+
TESTS=(
84+
# "tomcat-legacy"
85+
"book-server-deps"
86+
# "another-test"
87+
)
88+
89+
FAILED=0
90+
for test_name in "${TESTS[@]}"; do
91+
echo "=========================================="
92+
echo "Running test: $test_name"
93+
echo "=========================================="
94+
set +e
95+
OUTPUT=$(go run cmd/koncur/main.go run "tests/$test_name/test.yaml" --target-config target-kantra.yaml 2>&1)
96+
EXIT_CODE=$?
97+
set -e
98+
echo "$OUTPUT"
99+
if [ $EXIT_CODE -ne 0 ] || echo "$OUTPUT" | grep -qE "(FAILED|Exit code mismatch)"; then
100+
echo "Koncur $test_name test failed (exit code: $EXIT_CODE)"
101+
FAILED=1
102+
else
103+
echo "Koncur $test_name test passed"
104+
fi
105+
done
106+
exit $FAILED
107+
108+
- name: Upload test artifacts
109+
if: always()
110+
uses: actions/upload-artifact@v4
111+
with:
112+
name: koncur-test-logs
113+
path: koncur/.koncur/output/
114+
retention-days: 7

cmd/analyze-bin.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"regexp"
13-
"runtime"
1413
"sort"
1514
"strconv"
1615
"strings"
@@ -508,49 +507,6 @@ func (a *analyzeCommand) walkRuleFilesForLabelsContainerless(label string) ([]st
508507
return labelsSlice, nil
509508
}
510509

511-
func (a *analyzeCommand) setKantraDir() error {
512-
var dir string
513-
var err error
514-
set := true
515-
reqs := []string{
516-
RulesetsLocation,
517-
"jdtls",
518-
"static-report",
519-
}
520-
// check current dir first for reqs
521-
dir, err = os.Getwd()
522-
if err != nil {
523-
return err
524-
}
525-
for _, v := range reqs {
526-
_, err := os.Stat(filepath.Join(dir, v))
527-
if err != nil {
528-
set = false
529-
a.log.V(7).Info("requirement not found in current dir. Checking $HOME/.kantra")
530-
break
531-
}
532-
}
533-
// all reqs found here
534-
if set {
535-
a.kantraDir = dir
536-
return nil
537-
}
538-
// fall back to $HOME/.kantra
539-
ops := runtime.GOOS
540-
if ops == "linux" {
541-
dir, set = os.LookupEnv("XDG_CONFIG_HOME")
542-
}
543-
if ops != "linux" || dir == "" || !set {
544-
// on Unix, including macOS, this returns the $HOME environment variable. On Windows, it returns %USERPROFILE%
545-
dir, err = os.UserHomeDir()
546-
if err != nil {
547-
return err
548-
}
549-
}
550-
a.kantraDir = filepath.Join(dir, ".kantra")
551-
return nil
552-
}
553-
554510
func (a *analyzeCommand) setBinMapContainerless() error {
555511
a.reqMap["bundle"] = filepath.Join(a.kantraDir, JavaBundlesLocation)
556512
a.reqMap["jdtls"] = filepath.Join(a.kantraDir, JDTLSBinLocation)

cmd/analyze.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
115115
cmd.MarkFlagRequired("input")
116116
}
117117
if analyzeCmd.runLocal {
118-
err := analyzeCmd.setKantraDir()
118+
kantraDir, err := util.GetKantraDir()
119119
if err != nil {
120120
analyzeCmd.log.Error(err, "unable to get analyze reqs")
121121
return err
122122
}
123+
analyzeCmd.kantraDir = kantraDir
124+
analyzeCmd.log.Info("found kantra dir", "dir", kantraDir)
123125
}
124126
err := analyzeCmd.Validate(cmd.Context(), cmd)
125127
if err != nil {

cmd/dump-rules.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package cmd
2+
3+
import (
4+
"archive/zip"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"io/fs"
9+
"os"
10+
"path/filepath"
11+
12+
"github.com/go-logr/logr"
13+
"github.com/konveyor-ecosystem/kantra/pkg/util"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
type dumpRulesCommand struct {
18+
output string
19+
overwrite bool
20+
log logr.Logger
21+
}
22+
23+
func NewDumpRulesCommand(log logr.Logger) *cobra.Command {
24+
dumpRulesCmd := &dumpRulesCommand{
25+
log: log,
26+
}
27+
28+
dumpRulesCommand := &cobra.Command{
29+
Use: "dump-rules",
30+
Short: "Dump builtin rulesets",
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
dumpRulesCmd.output = filepath.Join(dumpRulesCmd.output, "default-rulesets.zip")
33+
err := dumpRulesCmd.handleOutputFile()
34+
if err != nil {
35+
return err
36+
}
37+
38+
kantraDir, err := util.GetKantraDir()
39+
if err != nil {
40+
log.Error(err, "unable to get kantra dir")
41+
return err
42+
}
43+
44+
rulesPath := filepath.Join(kantraDir, RulesetsLocation)
45+
if _, err := os.Stat(rulesPath); os.IsNotExist(err) {
46+
log.Error(err, "cannot open rulesets path")
47+
return fmt.Errorf("rulesets directory not found at %s", rulesPath)
48+
}
49+
50+
log.Info("rulesets dir found", "dir", rulesPath)
51+
log.Info("dumping rules")
52+
53+
file, err := os.Create(dumpRulesCmd.output)
54+
if err != nil {
55+
log.Error(err, "error while creating output file")
56+
return err
57+
}
58+
defer func(file *os.File) {
59+
err := file.Close()
60+
if err != nil {
61+
log.Error(err, "an error occurred while closing the output file")
62+
}
63+
}(file)
64+
65+
w := zip.NewWriter(file)
66+
defer func(w *zip.Writer) {
67+
err := w.Close()
68+
if err != nil {
69+
log.Error(err, "an error occurred while closing the walker")
70+
}
71+
}(w)
72+
73+
walker := func(path string, entry fs.DirEntry, err error) error {
74+
if err != nil {
75+
return err
76+
}
77+
if entry.IsDir() {
78+
return nil
79+
}
80+
file, err := os.Open(path)
81+
if err != nil {
82+
return err
83+
}
84+
85+
defer func(file *os.File) {
86+
err := file.Close()
87+
if err != nil {
88+
log.Error(err, "an error occurred while closing a file")
89+
return
90+
}
91+
}(file)
92+
93+
relPath, err := filepath.Rel(rulesPath, path)
94+
if err != nil {
95+
return err
96+
}
97+
98+
f, err := w.Create(relPath)
99+
if err != nil {
100+
return err
101+
}
102+
103+
_, err = io.Copy(f, file)
104+
if err != nil {
105+
return err
106+
}
107+
108+
return nil
109+
}
110+
err = filepath.WalkDir(rulesPath, walker)
111+
if err != nil {
112+
log.Error(err, "error while exporting rules")
113+
return err
114+
}
115+
log.Info("rulesets exported successfully ", "file", dumpRulesCmd.output)
116+
return nil
117+
},
118+
}
119+
dumpRulesCommand.Flags().BoolVar(&dumpRulesCmd.overwrite, "overwrite", false, "overwrite output directory")
120+
dumpRulesCommand.Flags().StringVarP(&dumpRulesCmd.output, "output", "o", "", "path to the directory for rulesets output")
121+
err := dumpRulesCommand.MarkFlagRequired("output")
122+
if err != nil {
123+
return nil
124+
}
125+
126+
return dumpRulesCommand
127+
}
128+
129+
func (d *dumpRulesCommand) handleOutputFile() error {
130+
stat, err := os.Stat(d.output)
131+
if err != nil {
132+
if !errors.Is(err, os.ErrNotExist) {
133+
return err
134+
}
135+
}
136+
137+
if !d.overwrite && stat != nil {
138+
return fmt.Errorf("output file %v already exists and --overwrite not set", d.output)
139+
}
140+
141+
if d.overwrite && stat != nil {
142+
err := os.RemoveAll(d.output)
143+
if err != nil {
144+
return err
145+
}
146+
}
147+
return nil
148+
}

0 commit comments

Comments
 (0)