forked from actions/go-dependency-submission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
96 lines (86 loc) · 2.72 KB
/
index.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
import path from 'path'
import fs from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {
Snapshot,
Manifest,
submitSnapshot
} from '@github/dependency-submission-toolkit'
import {
processGoGraph,
processGoDirectDependencies,
processGoIndirectDependencies
} from './process'
async function main () {
const goModPath = path.normalize(
core.getInput('go-mod-path', { required: true })
)
if (path.basename(goModPath) !== 'go.mod' || !fs.existsSync(goModPath)) {
throw new Error(`${goModPath} is not a go.mod file or does not exist!`)
}
const goModDir = path.dirname(goModPath)
let goBuildTarget = core.getInput('go-build-target')
if (goBuildTarget !== 'all' && goBuildTarget !== './...') {
if (!fs.existsSync(goBuildTarget)) {
throw new Error(`The build target '${goBuildTarget}' does not exist`)
}
if (goModDir !== '.') {
if (goBuildTarget.startsWith(goModDir)) {
goBuildTarget = goBuildTarget.replace(goModDir, '')
goBuildTarget = goBuildTarget.startsWith('/')
? goBuildTarget.substring(1)
: goBuildTarget
} else {
throw new Error(
`The build target ${goBuildTarget} is not a sub-directory of ${goModDir}`
)
}
}
}
const directDeps = await processGoDirectDependencies(goModDir, goBuildTarget)
const indirectDeps = await processGoIndirectDependencies(
goModDir,
goBuildTarget
)
const packageCache = await processGoGraph(goModDir, directDeps, indirectDeps)
// if using the pseudotargets "all" or "./...", use the path to go.mod as filepath
const filepath =
goBuildTarget === 'all' || goBuildTarget === './...'
? goModPath
: path.join(goModDir, goBuildTarget)
const manifest = new Manifest(goBuildTarget, filepath)
directDeps.forEach((pkgUrl) => {
const dep = packageCache.lookupPackage(pkgUrl)
if (!dep) {
throw new Error(
'assertion failed: expected all direct dependencies to have entries in PackageCache'
)
}
manifest.addDirectDependency(dep)
})
indirectDeps.forEach((pkgUrl) => {
const dep = packageCache.lookupPackage(pkgUrl)
if (!dep) {
throw new Error(
'assertion failed: expected all indirect dependencies to have entries in PackageCache'
)
}
manifest.addIndirectDependency(dep)
})
const snapshot = new Snapshot(
{
name: 'actions/go-dependency-submission',
url: 'https://github.com/actions/go-dependency-submission',
version: '0.0.1'
},
github.context,
{
correlator: `${github.context.job}-${goBuildTarget}`,
id: github.context.runId.toString()
}
)
snapshot.addManifest(manifest)
submitSnapshot(snapshot)
}
main()