-
Notifications
You must be signed in to change notification settings - Fork 22
/
gen_mutations.go
45 lines (35 loc) · 1.47 KB
/
gen_mutations.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
package gen
import (
"fmt"
"regexp"
"strings"
)
var enabledMutators = map[string]func(ScrapeResult) ScrapeResult{
"/repos/{owner}/{repo}/contents/{path}": allowExtendedLastParamMutatorHelper(),
"/repos/{owner}/{repo}/git/ref/{ref}": allowExtendedLastParamMutatorHelper(),
"/repos/{owner}/{repo}/git/refs/{ref}": allowExtendedLastParamMutatorHelper(), // thanks for the consistency, GitHub
"/repos/{owner}/{repo}/commits/{ref}": allowExtendedLastParamMutatorHelper(), // thanks for not base64encode the parameter, GitHub
}
// allowExtendedLastParamMutatorHelper mutates the last param of the endpoint pattern
// allowing it to have any characters (including slashes)
func allowExtendedLastParamMutatorHelper() func(ScrapeResult) ScrapeResult {
return func(sr ScrapeResult) ScrapeResult {
endpointSplits := strings.Split(sr.EndpointPattern, "/")
lastParam := endpointSplits[len(endpointSplits)-1]
r := regexp.MustCompile(`[a-z]+`)
lastParamCleaned := r.FindString(lastParam)
endpointSplits[len(endpointSplits)-1] = fmt.Sprintf("{%s:.+}", lastParamCleaned)
sr.EndpointPattern = strings.Join(endpointSplits, "/")
return sr
}
}
// applyMutation applies mutation to the scrape result if necessary.
//
// There are some edge cases due to inconsistencies between GitHub's OpenAPI definition
// compared to the real world.
func applyMutation(sr ScrapeResult) ScrapeResult {
if mutator, found := enabledMutators[sr.EndpointPattern]; found {
return mutator(sr)
}
return sr
}