MockProjectHistoryApi.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.projectVersions = {}
  59. return (this.labels = {})
  60. },
  61. run() {
  62. app.post('/project', (req, res, next) => {
  63. return res.json({ project: { id: 1 } })
  64. })
  65. app.get(
  66. '/project/:project_id/version/:version/:pathname',
  67. (req, res, next) => {
  68. const { project_id, version, pathname } = req.params
  69. const key = `${project_id}:${version}:${pathname}`
  70. if (this.oldFiles[key] != null) {
  71. return res.send(this.oldFiles[key])
  72. } else {
  73. return res.send(404)
  74. }
  75. }
  76. )
  77. app.get('/project/:project_id/version/:version', (req, res, next) => {
  78. const { project_id, version } = req.params
  79. const key = `${project_id}:${version}`
  80. if (this.projectSnapshots[key] != null) {
  81. return res.json(this.projectSnapshots[key])
  82. } else {
  83. return res.sendStatus(404)
  84. }
  85. })
  86. app.get('/project/:project_id/version', (req, res, next) => {
  87. const { project_id } = req.params
  88. if (this.projectVersions[project_id] != null) {
  89. return res.json(this.projectVersions[project_id])
  90. } else {
  91. return res.send(404)
  92. }
  93. })
  94. app.get('/project/:project_id/labels', (req, res, next) => {
  95. const { project_id } = req.params
  96. const labels = this.getLabels(project_id)
  97. if (labels != null) {
  98. return res.json(labels)
  99. } else {
  100. return res.send(404)
  101. }
  102. })
  103. app.post(
  104. '/project/:project_id/user/:user_id/labels',
  105. bodyParser.json(),
  106. (req, res, next) => {
  107. const { project_id } = req.params
  108. const { comment, version } = req.body
  109. const label_id = new ObjectId().toString()
  110. this.addLabel(project_id, { id: label_id, comment, version })
  111. return res.json({ label_id, comment, version })
  112. }
  113. )
  114. app.delete(
  115. '/project/:project_id/user/:user_id/labels/:label_id',
  116. (req, res, next) => {
  117. const { project_id, label_id } = req.params
  118. const label =
  119. this.labels[project_id] != null
  120. ? this.labels[project_id][label_id]
  121. : undefined
  122. if (label != null) {
  123. this.deleteLabel(project_id, label_id)
  124. return res.send(204)
  125. } else {
  126. return res.send(404)
  127. }
  128. }
  129. )
  130. app.post('/project/:project_id/flush', (req, res, next) => {
  131. return res.sendStatus(200)
  132. })
  133. return app
  134. .listen(3054, function(error) {
  135. if (error != null) {
  136. throw error
  137. }
  138. })
  139. .on('error', function(error) {
  140. console.error('error starting MockProjectHistoryApi:', error.message)
  141. return process.exit(1)
  142. })
  143. }
  144. }
  145. MockProjectHistoryApi.run()