All files / owid-grapher/adminSiteServer gitDataExport.ts

13.49% Statements 17/126
100% Branches 0/0
0% Functions 0/3
13.49% Lines 17/126

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1511x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x                                                           1x                                                                                                                                                                                                      
import * as path from "path"
import * as fs from "fs-extra"
import { Dataset } from "../db/model/Dataset"
import { Source } from "../db/model/Source"
import { GIT_DATASETS_DIR, TMP_DIR } from "../settings/serverSettings"
import {
    GIT_DEFAULT_USERNAME,
    GIT_DEFAULT_EMAIL,
} from "../settings/serverSettings"
import * as db from "../db/db"
import filenamify from "filenamify"
import { execFormatted } from "../db/execWrapper"
import { JsonError } from "../clientUtils/owidTypes"
 
const datasetToReadme = async (dataset: Dataset): Promise<string> => {
    const source = await Source.findOne({ datasetId: dataset.id })
    return `# ${dataset.name}\n\n${
        (source && source.description && source.description.additionalInfo) ||
        ""
    }`
}
 
export async function removeDatasetFromGitRepo(
    datasetName: string,
    namespace: string,
    options: { commitName?: string; commitEmail?: string } = {}
) {
    const { commitName, commitEmail } = options

    const repoDir = path.join(GIT_DATASETS_DIR, namespace)

    if (!fs.existsSync(path.join(repoDir, ".git"))) {
        return
    }

    if (!fs.existsSync(`${repoDir}/datasets/${datasetName}`)) {
        return
    }

    await execFormatted(
        `cd %s && rm -rf %s && git add -A %s && (git diff-index --quiet HEAD || (git commit -m %s --quiet --author="${
            commitName || GIT_DEFAULT_USERNAME
        } <${commitEmail || GIT_DEFAULT_EMAIL}>" && git push))`,
        [
            repoDir,
            `${repoDir}/datasets/${datasetName}`,
            `${repoDir}/datasets/${datasetName}`,
            `Removing ${datasetName}`,
        ]
    )
}
 
export async function syncDatasetToGitRepo(
    datasetId: number,
    options: {
        transaction?: db.TransactionContext
        oldDatasetName?: string
        commitName?: string
        commitEmail?: string
        commitOnly?: boolean
    } = {}
) {
    const { oldDatasetName, commitName, commitEmail, commitOnly } = options

    const oldDatasetFilename = oldDatasetName
        ? filenamify(oldDatasetName)
        : undefined

    const datasetRepo = options.transaction
        ? options.transaction.manager.getRepository(Dataset)
        : Dataset.getRepository()

    const dataset = await datasetRepo.findOne({ id: datasetId })
    if (!dataset) throw new JsonError(`No such dataset ${datasetId}`, 404)

    if (dataset.isPrivate || dataset.nonRedistributable)
        // Private dataset doesn't go in git repo
        return removeDatasetFromGitRepo(
            oldDatasetName || dataset.name,
            dataset.namespace,
            options
        )

    // Not doing bulk imports for now
    if (dataset.namespace !== "owid") return

    // Base repository directory for this dataspace
    const repoDir = path.join(GIT_DATASETS_DIR, dataset.namespace)

    if (!fs.existsSync(path.join(repoDir, ".git"))) {
        await fs.mkdirp(repoDir)
        await execFormatted(
            `cd %s && git init && git config user.name %s && git config user.email %s`,
            [repoDir, GIT_DEFAULT_USERNAME, GIT_DEFAULT_EMAIL]
        )
    }

    // Output dataset to temporary directory
    const tmpDatasetDir = path.join(TMP_DIR, dataset.filename)
    await fs.mkdirp(tmpDatasetDir)

    await Promise.all([
        fs.writeFile(
            path.join(tmpDatasetDir, `${dataset.filename}.csv`),
            await dataset.toCSV()
        ),
        fs.writeFile(
            path.join(tmpDatasetDir, `datapackage.json`),
            JSON.stringify(await dataset.toDatapackage(), null, 2)
        ),
        fs.writeFile(
            path.join(tmpDatasetDir, `README.md`),
            await datasetToReadme(dataset)
        ),
    ])

    const datasetsDir = path.join(repoDir, "datasets")
    await fs.mkdirp(datasetsDir)

    const finalDatasetDir = path.join(datasetsDir, dataset.filename)
    const isNew = !fs.existsSync(finalDatasetDir)
    await execFormatted(`cd %s && rm -rf %s && mv %s %s && git add -A %s`, [
        repoDir,
        finalDatasetDir,
        tmpDatasetDir,
        finalDatasetDir,
        finalDatasetDir,
    ])
 
    if (oldDatasetFilename && oldDatasetFilename !== dataset.filename) {
        const oldDatasetDir = path.join(datasetsDir, oldDatasetFilename)
        await execFormatted(`cd %s && rm -rf %s && git add -A %s`, [
            repoDir,
            oldDatasetDir,
            oldDatasetDir,
        ])
    }
 
    const commitMsg = isNew
        ? `Adding ${dataset.filename}`
        : `Updating ${dataset.filename}`
    await execFormatted(
        `cd %s && (git diff-index --quiet HEAD || (git commit -m %s --quiet --author="${
            commitName || GIT_DEFAULT_USERNAME
        } <${commitEmail || GIT_DEFAULT_EMAIL}>"${
            commitOnly ? "" : " && git push))"
        }`,
        [repoDir, commitMsg]
    )
}