-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
129 lines (105 loc) · 3.93 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import * as os from 'os'
import * as path from 'path'
import * as core from '@actions/core'
import { getOctokit, context } from '@actions/github'
import { DefaultArtifactClient } from '@actions/artifact'
export async function run(): Promise<void> {
try {
if (!context.payload.pull_request) {
core.info('Not a pull request. Skipping action.')
return
}
const inputs = {
name: core.getInput('name', { required: true }),
description: core.getInput('description', { required: false }),
path: core.getInput('path', { required: false }),
token: core.getInput('github-token', { required: false }),
repository: core.getInput('repository', { required: false }),
runID: parseInt(core.getInput('run-id', { required: false }))
}
if (!inputs.path) {
inputs.path = process.env['GITHUB_WORKSPACE'] || process.cwd()
}
if (inputs.path.startsWith(`~`)) {
inputs.path = inputs.path.replace('~', os.homedir())
}
const resolvedPath = path.resolve(inputs.path)
core.debug(`Resolved path is ${resolvedPath}`)
const workflowName: string = context.workflow
const workflowRunId: number = context.runId
const [repositoryOwner, repositoryName] = inputs.repository.split('/')
if (!repositoryOwner || !repositoryName) {
throw new Error(
`Invalid repository: '${inputs.repository}'. Must be in format owner/repo`
)
}
const token = process.env['GITHUB_TOKEN'] || inputs.token
if (!token) {
throw new Error('GitHub token is required')
}
const findBy = {
token,
workflowRunId,
repositoryOwner,
repositoryName
}
core.info(
`Owner: ${repositoryOwner}, Repo: ${repositoryName}, Run ID: ${inputs.runID}`
)
const artifact = new DefaultArtifactClient()
await artifact.getArtifact(inputs.name, { findBy })
const { artifact: targetArtifact } = await artifact.getArtifact(
inputs.name,
{ findBy }
)
if (!targetArtifact) {
throw new Error(`Artifact '${inputs.name}' not found`)
}
core.debug(
`Found named artifact '${inputs.name}' (ID: ${targetArtifact.id}, Size: ${targetArtifact.size})`
)
// update PR description
const messageSeperatorStart = `\n\n<!-- download-section ${workflowName} ${inputs.name} start -->\n`
const link = `https://nightly.link/${repositoryOwner}/${repositoryName}/actions/artifacts/${targetArtifact.id}.zip`
const bodyMessage = `[${inputs.description}](${link})\n`
const messageSeperatorEnd = `\n<!-- download-section ${workflowName} ${inputs.name} end -->`
// Get the current pull request number
const pullRequestNumber = context.issue.number
// Initialize octokit
const octokit = getOctokit(inputs.token)
// Fetch the current body of the pull request
const { data: pullRequest } = await octokit.rest.pulls.get({
owner: repositoryOwner,
repo: repositoryName,
pull_number: pullRequestNumber
})
const oldBody: string = pullRequest.body || ''
let newBody = ''
const startIndex = oldBody.indexOf(messageSeperatorStart)
const endIndex = oldBody.indexOf(messageSeperatorEnd)
if (startIndex === -1) {
// First time updating this description
newBody =
oldBody + messageSeperatorStart + bodyMessage + messageSeperatorEnd
} else {
// Replace existing section
newBody =
oldBody.substring(0, startIndex) +
messageSeperatorStart +
bodyMessage +
messageSeperatorEnd +
oldBody.substring(endIndex + messageSeperatorEnd.length)
}
core.debug(`New body: ${newBody}`)
// Update the PR body with newBody
await octokit.rest.pulls.update({
owner: repositoryOwner,
repo: repositoryName,
pull_number: pullRequestNumber,
body: newBody
})
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
}
}