Store/update entity sha after importing from GitHub (#23935)

* Refactor WebApiManager
* Store/update entity sha after importing

GitOrigin-RevId: 604bf3b8010140355c21afca01a54237a301eb92
This commit is contained in:
Alf Eaton
2025-03-11 09:05:42 +00:00
committed by Copybot
parent fe4f41501f
commit eedf5367fc
4 changed files with 118 additions and 50 deletions
@@ -138,36 +138,50 @@ async function updateFolder(req, res) {
// .gitignore, etc because people are generally more explicit with the files
// they want in git.
async function updateProjectContents(req, res, next) {
async function updateProjectContents(req, res) {
const projectId = req.params.project_id
const path = `/${req.params[0]}` // UpdateMerger expects leading slash
const source = req.headers['x-update-source'] || 'unknown'
try {
await UpdateMerger.promises.mergeUpdate(null, projectId, path, req, source)
const metadata = await UpdateMerger.promises.mergeUpdate(
null,
projectId,
path,
req,
source
)
res.json({
entityId: metadata.entityId.toString(),
rev: metadata.rev,
})
} catch (error) {
if (
error instanceof Errors.InvalidNameError ||
error instanceof Errors.DuplicateNameError
) {
return res.sendStatus(422)
res.sendStatus(422)
} else {
throw error
}
}
res.sendStatus(200)
}
async function deleteProjectContents(req, res, next) {
async function deleteProjectContents(req, res) {
const projectId = req.params.project_id
const path = `/${req.params[0]}` // UpdateMerger expects leading slash
const source = req.headers['x-update-source'] || 'unknown'
await UpdateMerger.promises.deleteUpdate(null, projectId, path, source)
res.sendStatus(200)
const entityId = await UpdateMerger.promises.deleteUpdate(
null,
projectId,
path,
source
)
res.json({ entityId })
}
async function getQueues(req, res, next) {
async function getQueues(req, res) {
const userId = SessionManager.getLoggedInUserId(req.session)
res.json(await TpdsQueueManager.promises.getQueues(userId))
}
@@ -206,6 +220,11 @@ export default {
deleteProjectContents: expressify(deleteProjectContents),
getQueues: expressify(getQueues),
promises: {
deleteProjectContents,
updateProjectContents,
},
// for tests only
parseParams,
}
@@ -12,15 +12,15 @@ const { pipeline } = require('stream/promises')
async function mergeUpdate(userId, projectId, path, updateRequest, source) {
const fsPath = await writeUpdateToDisk(projectId, updateRequest)
try {
const metadata = await _mergeUpdate(userId, projectId, path, fsPath, source)
return metadata
// note: important to await here so file reading finishes before cleaning up below
return await _mergeUpdate(userId, projectId, path, fsPath, source)
} finally {
try {
await fsPromises.unlink(fsPath)
} catch (err) {
// note: not awaited or thrown
fsPromises.unlink(fsPath).catch(err => {
logger.err({ err, projectId, fsPath }, 'error deleting file')
}
})
}
}
@@ -128,7 +128,7 @@ async function _mergeUpdate(userId, projectId, path, fsPath, source) {
async function deleteUpdate(userId, projectId, path, source) {
try {
await EditorController.promises.deleteEntityWithPath(
return await EditorController.promises.deleteEntityWithPath(
projectId,
path,
source,