MockProjectHistoryApi.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let MockProjectHistoryApi
  15. const _ = require('lodash')
  16. const express = require('express')
  17. const bodyParser = require('body-parser')
  18. const app = express()
  19. const { ObjectId } = require('mongojs')
  20. module.exports = MockProjectHistoryApi = {
  21. docs: {},
  22. oldFiles: {},
  23. projectVersions: {},
  24. labels: {},
  25. projectSnapshots: {},
  26. addOldFile(project_id, version, pathname, content) {
  27. return (this.oldFiles[`${project_id}:${version}:${pathname}`] = content)
  28. },
  29. addProjectSnapshot(project_id, version, snapshot) {
  30. return (this.projectSnapshots[`${project_id}:${version}`] = snapshot)
  31. },
  32. setProjectVersion(project_id, version) {
  33. return (this.projectVersions[project_id] = { version })
  34. },
  35. setProjectVersionInfo(project_id, versionInfo) {
  36. return (this.projectVersions[project_id] = versionInfo)
  37. },
  38. addLabel(project_id, label) {
  39. if (label.id == null) {
  40. label.id = new ObjectId().toString()
  41. }
  42. if (this.labels[project_id] == null) {
  43. this.labels[project_id] = {}
  44. }
  45. return (this.labels[project_id][label.id] = label)
  46. },
  47. deleteLabel(project_id, label_id) {
  48. return delete this.labels[project_id][label_id]
  49. },
  50. getLabels(project_id) {
  51. if (this.labels[project_id] == null) {
  52. return null
  53. }
  54. return _.values(this.labels[project_id])
  55. },
  56. reset() {
  57. this.oldFiles = {}
  58. this.projectHistoryId = 1
  59. this.projectVersions = {}
  60. return (this.labels = {})
  61. },
  62. run() {
  63. this.reset()
  64. app.post('/project', (req, res, next) => {
  65. return res.json({ project: { id: this.projectHistoryId++ } })
  66. })
  67. app.get(
  68. '/project/:project_id/version/:version/:pathname',
  69. (req, res, next) => {
  70. const { project_id, version, pathname } = req.params
  71. const key = `${project_id}:${version}:${pathname}`
  72. if (this.oldFiles[key] != null) {
  73. return res.send(this.oldFiles[key])
  74. } else {
  75. return res.send(404)
  76. }
  77. }
  78. )
  79. app.get('/project/:project_id/version/:version', (req, res, next) => {
  80. const { project_id, version } = req.params
  81. const key = `${project_id}:${version}`
  82. if (this.projectSnapshots[key] != null) {
  83. return res.json(this.projectSnapshots[key])
  84. } else {
  85. return res.sendStatus(404)
  86. }
  87. })
  88. app.get('/project/:project_id/version', (req, res, next) => {
  89. const { project_id } = req.params
  90. if (this.projectVersions[project_id] != null) {
  91. return res.json(this.projectVersions[project_id])
  92. } else {
  93. return res.send(404)
  94. }
  95. })
  96. app.get('/project/:project_id/labels', (req, res, next) => {
  97. const { project_id } = req.params
  98. const labels = this.getLabels(project_id)
  99. if (labels != null) {
  100. return res.json(labels)
  101. } else {
  102. return res.send(404)
  103. }
  104. })
  105. app.post(
  106. '/project/:project_id/user/:user_id/labels',
  107. bodyParser.json(),
  108. (req, res, next) => {
  109. const { project_id } = req.params
  110. const { comment, version } = req.body
  111. const label_id = new ObjectId().toString()
  112. this.addLabel(project_id, { id: label_id, comment, version })
  113. return res.json({ label_id, comment, version })
  114. }
  115. )
  116. app.delete(
  117. '/project/:project_id/user/:user_id/labels/:label_id',
  118. (req, res, next) => {
  119. const { project_id, label_id } = req.params
  120. const label =
  121. this.labels[project_id] != null
  122. ? this.labels[project_id][label_id]
  123. : undefined
  124. if (label != null) {
  125. this.deleteLabel(project_id, label_id)
  126. return res.send(204)
  127. } else {
  128. return res.send(404)
  129. }
  130. }
  131. )
  132. app.post('/project/:project_id/flush', (req, res, next) => {
  133. return res.sendStatus(200)
  134. })
  135. return app
  136. .listen(3054, error => {
  137. if (error != null) {
  138. throw error
  139. }
  140. })
  141. .on('error', error => {
  142. console.error('error starting MockProjectHistoryApi:', error.message)
  143. return process.exit(1)
  144. })
  145. }
  146. }
  147. MockProjectHistoryApi.run()