Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(internal): write branches cache
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* istanbul ignore file */ | ||
|
||
import { logger } from '../../logger'; | ||
import { platform } from '../../platform'; | ||
import { | ||
BranchCache, | ||
BranchUpgradeCache, | ||
getCache, | ||
} from '../../util/cache/repository'; | ||
import { getBranchCommit, isBranchModified } from '../../util/git'; | ||
import { BranchConfig, BranchUpgradeConfig } from '../common'; | ||
|
||
function generateBranchUpgradeCache( | ||
upgrade: BranchUpgradeConfig | ||
): BranchUpgradeCache { | ||
const { | ||
datasource, | ||
depName, | ||
lookupName, | ||
fixedVersion, | ||
fromVersion, | ||
toVersion, | ||
currentDigest, | ||
newDigest, | ||
} = upgrade; | ||
return { | ||
datasource, | ||
depName, | ||
lookupName, | ||
fixedVersion, | ||
fromVersion, | ||
toVersion, | ||
currentDigest, | ||
newDigest, | ||
}; | ||
} | ||
|
||
async function generateBranchCache(branch: BranchConfig): Promise<BranchCache> { | ||
const { branchName } = branch; | ||
try { | ||
const sha = getBranchCommit(branchName) || null; | ||
let prNo = null; | ||
if (sha) { | ||
const branchPr = await platform.getBranchPr(branchName); | ||
if (branchPr) { | ||
prNo = branchPr.number; | ||
} | ||
} | ||
const automerge = !!branch.automerge; | ||
const isModified = sha ? await isBranchModified(branchName) : false; | ||
const upgrades: BranchUpgradeCache[] = branch.upgrades | ||
? branch.upgrades.map(generateBranchUpgradeCache) | ||
: []; | ||
return { branchName, sha, prNo, automerge, isModified, upgrades }; | ||
} catch (err) { | ||
logger.error({ err, branchName }, 'Error generating branch cache'); | ||
return null; | ||
} | ||
} | ||
|
||
export async function setBranchCache(branches: BranchConfig[]): Promise<void> { | ||
const branchCache: BranchCache[] = []; | ||
for (const branch of branches) { | ||
branchCache.push(await generateBranchCache(branch)); | ||
} | ||
getCache().branches = branchCache.filter(Boolean); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters