Skip to content

Commit ff865cb

Browse files
authored
Upload dependency graph on submission failure (#291)
1 parent c3acd19 commit ff865cb

File tree

1 file changed

+79
-64
lines changed

1 file changed

+79
-64
lines changed

sources/src/dependency-graph.ts

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -60,44 +60,19 @@ export async function complete(config: DependencyGraphConfig): Promise<void> {
6060
return
6161
case DependencyGraphOption.GenerateAndSubmit:
6262
case DependencyGraphOption.Clear: // Submit the empty dependency graph
63-
await submitDependencyGraphs(await findDependencyGraphFiles())
63+
await findAndSubmitDependencyGraphs(config)
6464
return
6565
case DependencyGraphOption.GenerateAndUpload:
66-
await uploadDependencyGraphs(await findDependencyGraphFiles(), config)
66+
await findAndUploadDependencyGraphs(config)
6767
}
6868
} catch (e) {
6969
warnOrFail(config, option, e)
7070
}
7171
}
7272

73-
async function uploadDependencyGraphs(dependencyGraphFiles: string[], config: DependencyGraphConfig): Promise<void> {
74-
if (dependencyGraphFiles.length === 0) {
75-
core.info('No dependency graph files found to upload.')
76-
return
77-
}
78-
79-
if (isRunningInActEnvironment()) {
80-
core.info('Dependency graph upload not supported in the ACT environment.')
81-
core.info(`Would upload: ${dependencyGraphFiles.join(', ')}`)
82-
return
83-
}
84-
85-
const workspaceDirectory = getWorkspaceDirectory()
86-
87-
const artifactClient = new DefaultArtifactClient()
88-
for (const dependencyGraphFile of dependencyGraphFiles) {
89-
const relativePath = getRelativePathFromWorkspace(dependencyGraphFile)
90-
core.info(`Uploading dependency graph file: ${relativePath}`)
91-
const artifactName = `${DEPENDENCY_GRAPH_PREFIX}${path.basename(dependencyGraphFile)}`
92-
await artifactClient.uploadArtifact(artifactName, [dependencyGraphFile], workspaceDirectory, {
93-
retentionDays: config.getArtifactRetentionDays()
94-
})
95-
}
96-
}
97-
9873
async function downloadAndSubmitDependencyGraphs(config: DependencyGraphConfig): Promise<void> {
9974
if (isRunningInActEnvironment()) {
100-
core.info('Dependency graph download and submit not supported in the ACT environment.')
75+
core.info('Dependency graph not supported in the ACT environment.')
10176
return
10277
}
10378

@@ -108,53 +83,32 @@ async function downloadAndSubmitDependencyGraphs(config: DependencyGraphConfig):
10883
}
10984
}
11085

111-
async function submitDependencyGraphs(dependencyGraphFiles: string[]): Promise<void> {
112-
if (dependencyGraphFiles.length === 0) {
113-
core.info('No dependency graph files found to submit.')
114-
return
115-
}
116-
86+
async function findAndSubmitDependencyGraphs(config: DependencyGraphConfig): Promise<void> {
11787
if (isRunningInActEnvironment()) {
118-
core.info('Dependency graph submit not supported in the ACT environment.')
119-
core.info(`Would submit: ${dependencyGraphFiles.join(', ')}`)
88+
core.info('Dependency graph not supported in the ACT environment.')
12089
return
12190
}
12291

123-
for (const dependencyGraphFile of dependencyGraphFiles) {
92+
const dependencyGraphFiles = await findDependencyGraphFiles()
93+
try {
94+
await submitDependencyGraphs(dependencyGraphFiles)
95+
} catch (e) {
12496
try {
125-
await submitDependencyGraphFile(dependencyGraphFile)
126-
} catch (error) {
127-
if (error instanceof RequestError) {
128-
error.message = translateErrorMessage(dependencyGraphFile, error)
129-
}
130-
throw error
97+
await uploadDependencyGraphs(dependencyGraphFiles, config)
98+
} catch (uploadError) {
99+
core.info(String(uploadError))
131100
}
101+
throw e
132102
}
133103
}
134104

135-
function translateErrorMessage(jsonFile: string, error: RequestError): string {
136-
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
137-
const mainWarning = `Dependency submission failed for ${relativeJsonFile}.\n${error.message}`
138-
if (error.message === 'Resource not accessible by integration') {
139-
return `${mainWarning}
140-
Please ensure that the 'contents: write' permission is available for the workflow job.
141-
Note that this permission is never available for a 'pull_request' trigger from a repository fork.
142-
`
105+
async function findAndUploadDependencyGraphs(config: DependencyGraphConfig): Promise<void> {
106+
if (isRunningInActEnvironment()) {
107+
core.info('Dependency graph not supported in the ACT environment.')
108+
return
143109
}
144-
return mainWarning
145-
}
146-
147-
async function submitDependencyGraphFile(jsonFile: string): Promise<void> {
148-
const octokit = getOctokit()
149-
const jsonContent = fs.readFileSync(jsonFile, 'utf8')
150110

151-
const jsonObject = JSON.parse(jsonContent)
152-
jsonObject.owner = github.context.repo.owner
153-
jsonObject.repo = github.context.repo.repo
154-
const response = await octokit.request('POST /repos/{owner}/{repo}/dependency-graph/snapshots', jsonObject)
155-
156-
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
157-
core.notice(`Submitted ${relativeJsonFile}: ${response.data.message}`)
111+
await uploadDependencyGraphs(await findDependencyGraphFiles(), config)
158112
}
159113

160114
async function downloadDependencyGraphs(): Promise<string[]> {
@@ -195,6 +149,67 @@ async function findDependencyGraphFiles(): Promise<string[]> {
195149
return unprocessedFiles
196150
}
197151

152+
async function uploadDependencyGraphs(dependencyGraphFiles: string[], config: DependencyGraphConfig): Promise<void> {
153+
if (dependencyGraphFiles.length === 0) {
154+
core.info('No dependency graph files found to upload.')
155+
return
156+
}
157+
158+
const workspaceDirectory = getWorkspaceDirectory()
159+
160+
const artifactClient = new DefaultArtifactClient()
161+
for (const dependencyGraphFile of dependencyGraphFiles) {
162+
const relativePath = getRelativePathFromWorkspace(dependencyGraphFile)
163+
core.info(`Uploading dependency graph file: ${relativePath}`)
164+
const artifactName = `${DEPENDENCY_GRAPH_PREFIX}${path.basename(dependencyGraphFile)}`
165+
await artifactClient.uploadArtifact(artifactName, [dependencyGraphFile], workspaceDirectory, {
166+
retentionDays: config.getArtifactRetentionDays()
167+
})
168+
}
169+
}
170+
171+
async function submitDependencyGraphs(dependencyGraphFiles: string[]): Promise<void> {
172+
if (dependencyGraphFiles.length === 0) {
173+
core.info('No dependency graph files found to submit.')
174+
return
175+
}
176+
177+
for (const dependencyGraphFile of dependencyGraphFiles) {
178+
try {
179+
await submitDependencyGraphFile(dependencyGraphFile)
180+
} catch (error) {
181+
if (error instanceof RequestError) {
182+
error.message = translateErrorMessage(dependencyGraphFile, error)
183+
}
184+
throw error
185+
}
186+
}
187+
}
188+
189+
function translateErrorMessage(jsonFile: string, error: RequestError): string {
190+
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
191+
const mainWarning = `Dependency submission failed for ${relativeJsonFile}.\n${error.message}`
192+
if (error.message === 'Resource not accessible by integration') {
193+
return `${mainWarning}
194+
Please ensure that the 'contents: write' permission is available for the workflow job.
195+
Note that this permission is never available for a 'pull_request' trigger from a repository fork.
196+
`
197+
}
198+
return mainWarning
199+
}
200+
201+
async function submitDependencyGraphFile(jsonFile: string): Promise<void> {
202+
const octokit = getOctokit()
203+
const jsonContent = fs.readFileSync(jsonFile, 'utf8')
204+
205+
const jsonObject = JSON.parse(jsonContent)
206+
jsonObject.owner = github.context.repo.owner
207+
jsonObject.repo = github.context.repo.repo
208+
const response = await octokit.request('POST /repos/{owner}/{repo}/dependency-graph/snapshots', jsonObject)
209+
210+
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
211+
core.notice(`Submitted ${relativeJsonFile}: ${response.data.message}`)
212+
}
198213
function getReportDirectory(): string {
199214
return process.env.DEPENDENCY_GRAPH_REPORT_DIR!
200215
}

0 commit comments

Comments
 (0)