-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.go
81 lines (66 loc) · 1.76 KB
/
helper.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
package main
import (
"errors"
"log"
"strings"
)
func buildMappingKey(keys []string) string {
return strings.Join(keys, "|")
}
func createJobURL(jenkinsURL, job string) string {
return string(jenkinsURL + "/job/" + job + "/build")
}
func removeLastRune(s string) string {
if len(s) <= 1 {
return ""
}
r := []rune(s)
return string(r[:len(r)-1])
}
func appendMappingKeys(keys, files []string, repo, branch, fileprefix string) []string {
for _, file := range files {
keys = append(keys, buildMappingKey([]string{repo, branch, fileprefix + file}))
}
return keys
}
func evalMappingKeys(repo, branch string, files []string, filematch bool, sematicrepo string) []string {
var keys []string
if filematch {
if sematicrepo != "" {
pkgname, err := getSemanticRepoName(repo, sematicrepo)
if err == nil && pkgname != "" {
keys = appendMappingKeys(keys, files, repo, branch, pkgname+"/")
} else {
keys = appendMappingKeys(keys, files, repo, branch, "")
}
} else {
keys = appendMappingKeys(keys, files, repo, branch, "")
}
} else {
key := buildMappingKey([]string{repo, branch})
keys = append(keys, key)
}
return keys
}
func getSemanticRepoName(repo, semanticRepo string) (string, error) {
reponame := strings.ReplaceAll(repo, ".git", "")
reponame = strings.ReplaceAll(reponame, semanticRepo, "")
if strings.ContainsAny(reponame, "/:.") {
return "", errors.New("semantic repo reduction failed")
}
log.Printf("found semantic repo: %s\n", reponame)
return reponame, nil
}
func uniqueNonEmptyElementsOf(s []string) []string {
unique := make(map[string]bool, len(s))
us := make([]string, len(unique))
for _, elem := range s {
if len(elem) != 0 {
if !unique[elem] {
us = append(us, elem)
unique[elem] = true
}
}
}
return us
}