-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathutils.ts
100 lines (85 loc) · 3.2 KB
/
utils.ts
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
import wcMatch from "wildcard-match";
import {Analytics} from "analytics";
import {Probot} from "probot";
const googleAnalytics = require('@analytics/google-analytics').default
// Regexp below is a stricter implementation of https://git-scm.com/docs/git-check-ref-format
const GIT_SAFE_REGEXP = /([.~^:?*[\]@{}<>|\\"'()`,]|\s)+/g
export function makePrefixGitSafe(s: string, {replaceChars = '', replacementChar = '_'} = {}) {
let result = trim(s, ' ').replace(GIT_SAFE_REGEXP, replacementChar)
if (replaceChars.length > 0) {
result = result.replace(new RegExp(`[${replaceChars}]`, 'g'), replacementChar)
}
return result
}
export function makeGitSafe(s: string, {replaceChars = '', replacementChar = '_'} = {}) {
let result = makePrefixGitSafe(s, {replaceChars: replaceChars, replacementChar: replacementChar})
result = trim(result, replacementChar)
result = result.replace(/[/]+$/, '')
return result
}
function trim(str: string, ch: string) {
let start = 0
let end = str.length
while (start < end && str[start] === ch) ++start
while (end > start && str[end - 1] === ch) --end
return (start > 0 || end < str.length) ? str.substring(start, end) : str
}
export function wildcardMatch(pattern: string, s: string) {
const isMatch = wcMatch(pattern, false)
return isMatch(s)
}
export function isProduction() {
return process.env.NODE_ENV === 'production'
}
const analytics = Analytics({
app: 'create-issue-branch', //
plugins: [googleAnalytics({
trackingId: 'UA-207350952-1'
})]
})
export function pushMetric(app: Probot, owner: string) {
analytics.identify(owner, () => {
analytics.track('branch_created', {category: 'Branches'}, () => app.log.info('Pushed metric to Google Analytics'))
.catch(err => app.log.error('Could not push metric to Google Analytics: ' + err))
}).catch(err => app.log.error('Could not identify user: ' + err))
}
export function isRunningInGitHubActions() {
return process.env.GITHUB_ACTIONS === 'true';
}
export function isRunningInTestEnvironment() {
return process.env.NODE_ENV === 'test';
}
export function getStringLengthInBytes(str: string) {
return (new TextEncoder().encode(str)).length
}
export function trimStringToByteLength(str: string, length: number) {
if (getStringLengthInBytes(str) <= length) {
return str
} else {
let result = str.substring(0, length)
while (getStringLengthInBytes(result) > length) {
result = Array.from(str).slice(0, [...result].length - 1).join('')
}
return result
}
}
export function logMemoryUsage(app: Probot) {
const usage = Math.round(process.memoryUsage().rss / 1024 / 1024)
if (usage >= 150) {
app.log.info(`Total memory: ${usage} Mb`)
} else {
app.log.debug(`Total memory: ${usage} Mb`)
}
}
export function formatAsExpandingMarkdown(title: string, content: string) {
let result = ''
result += '<details>\n'
result += `<summary><b>${title}</b></summary>\n`
result += '\n'
result += `${content}\n`
result += '</details>\n'
return result
}
export async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}