forked from OpenNeuroOrg/openneuro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft.ts
More file actions
61 lines (56 loc) · 1.55 KB
/
Copy pathdraft.ts
File metadata and controls
61 lines (56 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Manage serving a Draft object based on DataLad working trees
*/
import request from "superagent"
import Dataset from "../models/dataset"
import { getDatasetWorker } from "../libs/datalad-service"
import CacheItem, { CacheType } from "../cache/item"
import { getRedis } from "../libs/redis"
// Draft info resolver
type DraftInfo = {
ref: string
hexsha: string // Duplicate of ref for backwards compatibility
tree: string
message: string
modified: string
}
export const getDraftRevision = async (datasetId): Promise<string> => {
const { hexsha } = await getDraftInfo(datasetId)
return hexsha
}
export const getDraftInfo = async (datasetId) => {
const cache = new CacheItem(
getRedis(),
CacheType.draftRevision,
[datasetId],
10,
)
return cache.get(async (_doNotCache): Promise<DraftInfo> => {
const draftUrl = `http://${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/draft`
const response = await fetch(draftUrl)
return await response.json()
})
}
export const updateDatasetRevision = (datasetId) => {
/**
* Update the revision modified time in a draft on changes
*/
return Dataset.updateOne({ id: datasetId }, { modified: new Date() }).exec()
}
/**
* Run a git reset on the draft
* @param {string} datasetId Accession number
* @param {string} ref Git hexsha
*/
export const resetDraft = (datasetId, ref) => {
const resetUrl = `${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/reset/${ref}`
return request.post(resetUrl).set("Accept", "application/json")
}