generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathgithub-utils.ts
More file actions
115 lines (101 loc) · 3.43 KB
/
github-utils.ts
File metadata and controls
115 lines (101 loc) · 3.43 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
103
104
105
106
107
108
109
110
111
112
113
114
115
import {createWriteStream} from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {GitHub} from '@actions/github/lib/utils'
import type {PullRequest, WorkflowRunEvent} from '@octokit/webhooks-types'
import {IncomingMessage} from 'http'
import * as stream from 'stream'
import {promisify} from 'util'
import got, {Progress} from 'got'
const asyncStream = promisify(stream.pipeline)
export function getCheckRunContext(): {sha: string; runId: number} {
if (github.context.eventName === 'workflow_run') {
core.info('Action was triggered by workflow_run: using SHA and RUN_ID from triggering workflow')
const event = github.context.payload as WorkflowRunEvent
if (!event.workflow_run) {
throw new Error("Event of type 'workflow_run' is missing 'workflow_run' field")
}
return {
sha: event.workflow_run.head_commit.id,
runId: event.workflow_run.id
}
}
const runId = github.context.runId
if (github.context.payload.pull_request) {
core.info(`Action was triggered by ${github.context.eventName}: using SHA from head of source branch`)
const pr = github.context.payload.pull_request as PullRequest
return {sha: pr.head.sha, runId}
}
return {sha: github.context.sha, runId}
}
export async function downloadArtifact(
octokit: InstanceType<typeof GitHub>,
artifactId: number,
fileName: string,
token: string
): Promise<void> {
core.startGroup(`Downloading artifact ${fileName}`)
try {
core.info(`Artifact ID: ${artifactId}`)
const req = octokit.rest.actions.downloadArtifact.endpoint({
...github.context.repo,
artifact_id: artifactId,
archive_format: 'zip'
})
const headers = {
Authorization: `Bearer ${token}`
}
const downloadStream = got.stream(req.url, {headers})
const fileWriterStream = createWriteStream(fileName)
downloadStream.on('redirect', (response: IncomingMessage) => {
core.info(`Downloading ${response.headers.location}`)
})
downloadStream.on('downloadProgress', (progress: Progress) => {
core.info(`Progress: ${progress.transferred} B`)
})
await asyncStream(downloadStream, fileWriterStream)
} finally {
core.endGroup()
}
}
export async function listFiles(octokit: InstanceType<typeof GitHub>, sha: string): Promise<string[]> {
core.startGroup('Fetching list of tracked files from GitHub')
try {
const commit = await octokit.rest.git.getCommit({
commit_sha: sha,
...github.context.repo
})
const files = await listGitTree(octokit, commit.data.tree.sha, '')
return files
} finally {
core.endGroup()
}
}
async function listGitTree(octokit: InstanceType<typeof GitHub>, sha: string, path: string): Promise<string[]> {
const pathLog = path ? ` at ${path}` : ''
core.info(`Fetching tree ${sha}${pathLog}`)
let truncated = false
let tree = await octokit.rest.git.getTree({
recursive: 'true',
tree_sha: sha,
...github.context.repo
})
if (tree.data.truncated) {
truncated = true
tree = await octokit.rest.git.getTree({
tree_sha: sha,
...github.context.repo
})
}
const result: string[] = []
for (const tr of tree.data.tree) {
const file = `${path}${tr.path}`
if (tr.type === 'blob') {
result.push(file)
} else if (tr.type === 'tree' && truncated) {
const files = await listGitTree(octokit, tr.sha as string, `${file}/`)
result.push(...files)
}
}
return result
}