import pMap from 'p-map' ; import { GetPkgReleasesConfig , getPkgReleases } from '../../../datasource' ; import { TerraformProviderDatasource } from '../../../datasource/terraform-provider' ; import { logger } from '../../../logger' ; import { get as getVersioning } from '../../../versioning' ; import type { UpdateArtifact , UpdateArtifactsResult } from '../../types' ; import { massageProviderLookupName } from '../util' ; import { TerraformProviderHash } from './hash' ; import type { ProviderLock , ProviderLockUpdate } from './types' ; import { extractLocks , findLockFile , isPinnedVersion , readLockFile , writeLockUpdates , } from './util' ; async function updateAllLocks ( locks ) { const updates = await pMap ( locks , async ( lock ) => { const updateConfig = { versioning : 'hashicorp' , datasource : 'terraform-provider' , depName : lock . lookupName , } ; const { releases } = await getPkgReleases ( updateConfig ) ; const versioning = getVersioning ( updateConfig . versioning ) ; const versionsList = releases . map ( ( release ) => release . version ) ; const newVersion = versioning . getSatisfyingVersion ( versionsList , lock . constraints ) ; if ( newVersion === lock . version ) { return null ; } const update = { newVersion , newConstraint : lock . constraints , newHashes : await TerraformProviderHash . createHashes ( lock . registryUrl , lock . lookupName , newVersion ) , ... lock , } ; return update ; } , { concurrency : 4 } ) ; return updates . filter ( Boolean ) ; } export async function updateArtifacts ( { packageFileName , updatedDeps , config , } ) { logger . debug ( `terraform.updateArtifacts(${ packageFileName })` ) ; const lockFilePath = findLockFile ( packageFileName ) ; try { const lockFileContent = await readLockFile ( lockFilePath ) ; if ( ! lockFileContent ) { logger . debug ( 'No .terraform.lock.hcl found' ) ; return null ; } const locks = extractLocks ( lockFileContent ) ; if ( ! locks ) { logger . debug ( 'No Locks in .terraform.lock.hcl found' ) ; return null ; } const updates = [ ] ; if ( config . updateType === 'lockFileMaintenance' ) { const maintenanceUpdates = await updateAllLocks ( locks ) ; updates . push ( ... maintenanceUpdates ) ; } else { const providerDeps = updatedDeps . filter ( ( dep ) => [ 'provider' , 'required_provider' ] . includes ( dep . depType ) ) ; for ( const dep of providerDeps ) { massageProviderLookupName ( dep ) ; const { registryUrls , newVersion , newValue , lookupName } = dep ; const registryUrl = registryUrls ? registryUrls [ 0 ] : TerraformProviderDatasource . defaultRegistryUrls [ 0 ] ; const newConstraint = isPinnedVersion ( newValue ) ? newVersion : newValue ; const updateLock = locks . find ( ( value ) => value . lookupName === lookupName ) ; const update = { newVersion , newConstraint , newHashes : await TerraformProviderHash . createHashes ( registryUrl , lookupName , newVersion ) , ... updateLock , } ; updates . push ( update ) ; } } if ( updates . length === 0 || updates . some ( ( value ) => ! value . newHashes ) ) { return null ; } const res = writeLockUpdates ( updates , lockFilePath , lockFileContent ) ; return res ? [ res ] : null ; } catch ( err ) { return [ { artifactError : { lockFile : lockFilePath , stderr : err . message , } , } , ] ; } }