1
0
Fork 0

implement feedback

pull/1487/head
Bethany 2023-08-09 17:42:14 -07:00
parent 2f42c127c7
commit 188abfc20b
5 changed files with 123 additions and 13 deletions

View File

@ -0,0 +1,59 @@
import {Timestamp} from '../src/generated'
import * as retention from '../src/internal/upload/retention'
describe('retention', () => {
beforeEach(() => {
delete process.env['GITHUB_RETENTION_DAYS']
})
it('should return the inputted retention days if it is less than the max retention days', () => {
// setup
const mockDate = new Date('2020-01-01')
jest.useFakeTimers().setSystemTime(mockDate)
process.env['GITHUB_RETENTION_DAYS'] = '90'
const exp = retention.getExpiration(30)
expect(exp).toBeDefined()
const expDate = Timestamp.toDate(exp!) // we check whether exp is defined above
const expected = new Date()
expected.setDate(expected.getDate() + 30)
expect(expDate).toEqual(expected)
})
it('should return the max retention days if the inputted retention days is greater than the max retention days', () => {
// setup
const mockDate = new Date('2020-01-01')
jest.useFakeTimers().setSystemTime(mockDate)
process.env['GITHUB_RETENTION_DAYS'] = '90'
const exp = retention.getExpiration(120)
expect(exp).toBeDefined()
const expDate = Timestamp.toDate(exp!) // we check whether exp is defined above
const expected = new Date()
expected.setDate(expected.getDate() + 90)
expect(expDate).toEqual(expected)
})
it('should return undefined if the inputted retention days is undefined', () => {
const exp = retention.getExpiration()
expect(exp).toBeUndefined()
})
it('should return the inputted retention days if there is no max retention days', () => {
// setup
const mockDate = new Date('2020-01-01')
jest.useFakeTimers().setSystemTime(mockDate)
const exp = retention.getExpiration(30)
expect(exp).toBeDefined()
const expDate = Timestamp.toDate(exp!) // we check whether exp is defined above
const expected = new Date()
expected.setDate(expected.getDate() + 30)
expect(expDate).toEqual(expected)
})
})

View File

@ -2,6 +2,7 @@ import {UploadOptions} from './upload/upload-options'
import {UploadResponse} from './upload/upload-response' import {UploadResponse} from './upload/upload-response'
import {uploadArtifact} from './upload/upload-artifact' import {uploadArtifact} from './upload/upload-artifact'
import {warning} from '@actions/core' import {warning} from '@actions/core'
import {isGhes} from './shared/config'
export interface ArtifactClient { export interface ArtifactClient {
/** /**
@ -40,10 +41,25 @@ export class Client implements ArtifactClient {
rootDirectory: string, rootDirectory: string,
options?: UploadOptions | undefined options?: UploadOptions | undefined
): Promise<UploadResponse> { ): Promise<UploadResponse> {
if (isGhes()) {
warning(
`@actions/artifact v2 and upload-artifact v4 are not currently supported on GHES.`
)
return {
success: false
}
}
try { try {
return uploadArtifact(name, files, rootDirectory, options) return uploadArtifact(name, files, rootDirectory, options)
} catch (error) { } catch (error) {
warning(`Failed to upload artifact: ${error}`) warning(
`Artifact upload failed with error: ${error}.
Errors can be temporary, so please try again and optionally run the action with debug enabled for more information.
If the error persists, please check whether Actions is running normally at [https://githubstatus.com](https://www.githubstatus.com).`
)
return { return {
success: false success: false
} }

View File

@ -13,3 +13,10 @@ export function getResultsServiceUrl(): string {
} }
return resultsUrl return resultsUrl
} }
export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
)
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'
}

View File

@ -0,0 +1,34 @@
import {Timestamp} from '../../generated'
import * as core from '@actions/core'
export function getExpiration(retentionDays?: number): Timestamp | undefined {
if (!retentionDays) {
return undefined
}
const maxRetentionDays = getRetentionDays()
if (maxRetentionDays && maxRetentionDays < retentionDays) {
core.warning(
`Retention days cannot be greater than the maximum allowed retention set within the repository. Using ${maxRetentionDays} instead.`
)
retentionDays = maxRetentionDays
}
const expirationDate = new Date()
expirationDate.setDate(expirationDate.getDate() + retentionDays)
return Timestamp.fromDate(expirationDate)
}
function getRetentionDays(): number | undefined {
const retentionDays = process.env['GITHUB_RETENTION_DAYS']
if (!retentionDays) {
return undefined
}
const days = parseInt(retentionDays)
if (isNaN(days)) {
return undefined
}
return days
}

View File

@ -1,6 +1,7 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {UploadOptions} from './upload-options' import {UploadOptions} from './upload-options'
import {UploadResponse} from './upload-response' import {UploadResponse} from './upload-response'
import {getExpiration} from './util'
import {validateArtifactName} from './path-and-artifact-name-validation' import {validateArtifactName} from './path-and-artifact-name-validation'
import {createArtifactTwirpClient} from '../shared/artifact-twirp-client' import {createArtifactTwirpClient} from '../shared/artifact-twirp-client'
import { import {
@ -9,7 +10,7 @@ import {
validateRootDirectory validateRootDirectory
} from './upload-zip-specification' } from './upload-zip-specification'
import {getBackendIdsFromToken} from '../shared/util' import {getBackendIdsFromToken} from '../shared/util'
import {CreateArtifactRequest, Timestamp} from 'src/generated' import {CreateArtifactRequest} from 'src/generated'
export async function uploadArtifact( export async function uploadArtifact(
name: string, name: string,
@ -39,6 +40,10 @@ export async function uploadArtifact(
success: false success: false
} }
} }
core.debug(`Workflow Run Backend ID: ${backendIds.workflowRunBackendId}`)
core.debug(
`Workflow Job Run Backend ID: ${backendIds.workflowJobRunBackendId}`
)
// create the artifact client // create the artifact client
const artifactClient = createArtifactTwirpClient('upload') const artifactClient = createArtifactTwirpClient('upload')
@ -91,14 +96,3 @@ export async function uploadArtifact(
return uploadResponse return uploadResponse
} }
function getExpiration(retentionDays?: number): Timestamp | undefined {
if (!retentionDays) {
return undefined
}
const expirationDate = new Date()
expirationDate.setDate(expirationDate.getDate() + retentionDays)
return Timestamp.fromDate(expirationDate)
}