mirror of
https://github.com/gradle/actions.git
synced 2025-08-24 19:01:27 +08:00
Upload dependency graph on submission failure (#291)
This commit is contained in:
parent
c3acd19a4a
commit
ff865cb801
@ -60,44 +60,19 @@ export async function complete(config: DependencyGraphConfig): Promise<void> {
|
|||||||
return
|
return
|
||||||
case DependencyGraphOption.GenerateAndSubmit:
|
case DependencyGraphOption.GenerateAndSubmit:
|
||||||
case DependencyGraphOption.Clear: // Submit the empty dependency graph
|
case DependencyGraphOption.Clear: // Submit the empty dependency graph
|
||||||
await submitDependencyGraphs(await findDependencyGraphFiles())
|
await findAndSubmitDependencyGraphs(config)
|
||||||
return
|
return
|
||||||
case DependencyGraphOption.GenerateAndUpload:
|
case DependencyGraphOption.GenerateAndUpload:
|
||||||
await uploadDependencyGraphs(await findDependencyGraphFiles(), config)
|
await findAndUploadDependencyGraphs(config)
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
warnOrFail(config, option, e)
|
warnOrFail(config, option, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadDependencyGraphs(dependencyGraphFiles: string[], config: DependencyGraphConfig): Promise<void> {
|
|
||||||
if (dependencyGraphFiles.length === 0) {
|
|
||||||
core.info('No dependency graph files found to upload.')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isRunningInActEnvironment()) {
|
|
||||||
core.info('Dependency graph upload not supported in the ACT environment.')
|
|
||||||
core.info(`Would upload: ${dependencyGraphFiles.join(', ')}`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const workspaceDirectory = getWorkspaceDirectory()
|
|
||||||
|
|
||||||
const artifactClient = new DefaultArtifactClient()
|
|
||||||
for (const dependencyGraphFile of dependencyGraphFiles) {
|
|
||||||
const relativePath = getRelativePathFromWorkspace(dependencyGraphFile)
|
|
||||||
core.info(`Uploading dependency graph file: ${relativePath}`)
|
|
||||||
const artifactName = `${DEPENDENCY_GRAPH_PREFIX}${path.basename(dependencyGraphFile)}`
|
|
||||||
await artifactClient.uploadArtifact(artifactName, [dependencyGraphFile], workspaceDirectory, {
|
|
||||||
retentionDays: config.getArtifactRetentionDays()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function downloadAndSubmitDependencyGraphs(config: DependencyGraphConfig): Promise<void> {
|
async function downloadAndSubmitDependencyGraphs(config: DependencyGraphConfig): Promise<void> {
|
||||||
if (isRunningInActEnvironment()) {
|
if (isRunningInActEnvironment()) {
|
||||||
core.info('Dependency graph download and submit not supported in the ACT environment.')
|
core.info('Dependency graph not supported in the ACT environment.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,53 +83,32 @@ async function downloadAndSubmitDependencyGraphs(config: DependencyGraphConfig):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function submitDependencyGraphs(dependencyGraphFiles: string[]): Promise<void> {
|
async function findAndSubmitDependencyGraphs(config: DependencyGraphConfig): Promise<void> {
|
||||||
if (dependencyGraphFiles.length === 0) {
|
|
||||||
core.info('No dependency graph files found to submit.')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isRunningInActEnvironment()) {
|
if (isRunningInActEnvironment()) {
|
||||||
core.info('Dependency graph submit not supported in the ACT environment.')
|
core.info('Dependency graph not supported in the ACT environment.')
|
||||||
core.info(`Would submit: ${dependencyGraphFiles.join(', ')}`)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const dependencyGraphFile of dependencyGraphFiles) {
|
const dependencyGraphFiles = await findDependencyGraphFiles()
|
||||||
|
try {
|
||||||
|
await submitDependencyGraphs(dependencyGraphFiles)
|
||||||
|
} catch (e) {
|
||||||
try {
|
try {
|
||||||
await submitDependencyGraphFile(dependencyGraphFile)
|
await uploadDependencyGraphs(dependencyGraphFiles, config)
|
||||||
} catch (error) {
|
} catch (uploadError) {
|
||||||
if (error instanceof RequestError) {
|
core.info(String(uploadError))
|
||||||
error.message = translateErrorMessage(dependencyGraphFile, error)
|
|
||||||
}
|
|
||||||
throw error
|
|
||||||
}
|
}
|
||||||
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function translateErrorMessage(jsonFile: string, error: RequestError): string {
|
async function findAndUploadDependencyGraphs(config: DependencyGraphConfig): Promise<void> {
|
||||||
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
|
if (isRunningInActEnvironment()) {
|
||||||
const mainWarning = `Dependency submission failed for ${relativeJsonFile}.\n${error.message}`
|
core.info('Dependency graph not supported in the ACT environment.')
|
||||||
if (error.message === 'Resource not accessible by integration') {
|
return
|
||||||
return `${mainWarning}
|
|
||||||
Please ensure that the 'contents: write' permission is available for the workflow job.
|
|
||||||
Note that this permission is never available for a 'pull_request' trigger from a repository fork.
|
|
||||||
`
|
|
||||||
}
|
}
|
||||||
return mainWarning
|
|
||||||
}
|
|
||||||
|
|
||||||
async function submitDependencyGraphFile(jsonFile: string): Promise<void> {
|
await uploadDependencyGraphs(await findDependencyGraphFiles(), config)
|
||||||
const octokit = getOctokit()
|
|
||||||
const jsonContent = fs.readFileSync(jsonFile, 'utf8')
|
|
||||||
|
|
||||||
const jsonObject = JSON.parse(jsonContent)
|
|
||||||
jsonObject.owner = github.context.repo.owner
|
|
||||||
jsonObject.repo = github.context.repo.repo
|
|
||||||
const response = await octokit.request('POST /repos/{owner}/{repo}/dependency-graph/snapshots', jsonObject)
|
|
||||||
|
|
||||||
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
|
|
||||||
core.notice(`Submitted ${relativeJsonFile}: ${response.data.message}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function downloadDependencyGraphs(): Promise<string[]> {
|
async function downloadDependencyGraphs(): Promise<string[]> {
|
||||||
@ -195,6 +149,67 @@ async function findDependencyGraphFiles(): Promise<string[]> {
|
|||||||
return unprocessedFiles
|
return unprocessedFiles
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function uploadDependencyGraphs(dependencyGraphFiles: string[], config: DependencyGraphConfig): Promise<void> {
|
||||||
|
if (dependencyGraphFiles.length === 0) {
|
||||||
|
core.info('No dependency graph files found to upload.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspaceDirectory = getWorkspaceDirectory()
|
||||||
|
|
||||||
|
const artifactClient = new DefaultArtifactClient()
|
||||||
|
for (const dependencyGraphFile of dependencyGraphFiles) {
|
||||||
|
const relativePath = getRelativePathFromWorkspace(dependencyGraphFile)
|
||||||
|
core.info(`Uploading dependency graph file: ${relativePath}`)
|
||||||
|
const artifactName = `${DEPENDENCY_GRAPH_PREFIX}${path.basename(dependencyGraphFile)}`
|
||||||
|
await artifactClient.uploadArtifact(artifactName, [dependencyGraphFile], workspaceDirectory, {
|
||||||
|
retentionDays: config.getArtifactRetentionDays()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function submitDependencyGraphs(dependencyGraphFiles: string[]): Promise<void> {
|
||||||
|
if (dependencyGraphFiles.length === 0) {
|
||||||
|
core.info('No dependency graph files found to submit.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const dependencyGraphFile of dependencyGraphFiles) {
|
||||||
|
try {
|
||||||
|
await submitDependencyGraphFile(dependencyGraphFile)
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof RequestError) {
|
||||||
|
error.message = translateErrorMessage(dependencyGraphFile, error)
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function translateErrorMessage(jsonFile: string, error: RequestError): string {
|
||||||
|
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
|
||||||
|
const mainWarning = `Dependency submission failed for ${relativeJsonFile}.\n${error.message}`
|
||||||
|
if (error.message === 'Resource not accessible by integration') {
|
||||||
|
return `${mainWarning}
|
||||||
|
Please ensure that the 'contents: write' permission is available for the workflow job.
|
||||||
|
Note that this permission is never available for a 'pull_request' trigger from a repository fork.
|
||||||
|
`
|
||||||
|
}
|
||||||
|
return mainWarning
|
||||||
|
}
|
||||||
|
|
||||||
|
async function submitDependencyGraphFile(jsonFile: string): Promise<void> {
|
||||||
|
const octokit = getOctokit()
|
||||||
|
const jsonContent = fs.readFileSync(jsonFile, 'utf8')
|
||||||
|
|
||||||
|
const jsonObject = JSON.parse(jsonContent)
|
||||||
|
jsonObject.owner = github.context.repo.owner
|
||||||
|
jsonObject.repo = github.context.repo.repo
|
||||||
|
const response = await octokit.request('POST /repos/{owner}/{repo}/dependency-graph/snapshots', jsonObject)
|
||||||
|
|
||||||
|
const relativeJsonFile = getRelativePathFromWorkspace(jsonFile)
|
||||||
|
core.notice(`Submitted ${relativeJsonFile}: ${response.data.message}`)
|
||||||
|
}
|
||||||
function getReportDirectory(): string {
|
function getReportDirectory(): string {
|
||||||
return process.env.DEPENDENCY_GRAPH_REPORT_DIR!
|
return process.env.DEPENDENCY_GRAPH_REPORT_DIR!
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user