-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
102 lines (93 loc) · 2.38 KB
/
common.ts
File metadata and controls
102 lines (93 loc) · 2.38 KB
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
import path from 'node:path'
import { exec } from 'node:child_process'
import { promisify } from 'node:util'
export const isTestEnvironment = process.env.NODE_ENV === 'test'
const execAsync = promisify(exec)
export type EventMeta = {
/**
* Full name of repository: tangibleinc/example-plugin
*/
repoFullName: string
eventType: 'branch' | 'tag'
/**
* refs/heads/<branch_name>
* refs/tags/<tag_name>
* refs/pull/<pr_number>/merge
*/
gitRef: string
/**
* Branch or tag name
*/
gitRefName: string
/**
* Branch name, even in context of "tag" event type
*/
branchName: string
}
export async function getEventMeta(): Promise<EventMeta> {
/**
* [GitHub default environment variables](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables)
*/
const {
GITHUB_REPOSITORY: repoFullName = '',
GITHUB_REF_TYPE: eventType = 'branch',
GITHUB_REF: gitRef = '',
GITHUB_REF_NAME: gitRefName = '',
} = process.env
/**
* Necessary to get branch name using `git` because GitHub Actions does not provide
* branch name via env variable on event type `tag`
*/
const branchName =
eventType === 'tag' ? await getBranchNameFromTag(gitRefName) : gitRefName
return {
repoFullName,
eventType: eventType as EventMeta['eventType'],
gitRef,
gitRefName,
branchName,
}
}
export async function getBranchNameFromTag(tag: string): Promise<string> {
let branchName = tag
try {
let result = (
(
await execAsync(
`git branch -r --contains ${tag} | head -n 1 | sed 's/^[[:space:]]*//'`
)
).stdout || ''
)
.replace(/^\* /, '')
.replace(/^origin\//, '')
.trim()
if (result) {
branchName = result
}
} catch (e) {
console.error(`Failed to get branch name from tag ${branchName}`)
}
return branchName
}
export async function getProjectConfig({ projectPath }): Promise<
| {
archive?: {
/**
* Destination zip file name
*/
dest: string
/**
* Root folder in the archive, usually the plugin name
*/
root?: string
}
}
| undefined
> {
const configPath = path.join(projectPath, 'tangible.config.js')
try {
return (await import(configPath)).default
} catch (e) {
// Not found
}
}