gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchf2-api-bugs26117255|||PR apps API: review comments on files with spaces in their namemref2-api-bugs/lib/tickets-helpers.hl

4.3 KB

  1. // lib/tickets-helpers.hl — what lib/tickets.hl looks up: where tickets lives, the shapes of its answers, and the
  2. // `#12` ticket references in commit subjects and pull request titles (links both ways). Statics only, no fetch here.
  3. // Config (environment, or `.env` beside project.hl — never printed): GITORIA_TICKETS_URL, default https://tickets.worldapi.org
  4. import { envOr, isDigit } from './util.hl'
  5. static ticketsRaw = envOr('GITORIA_TICKETS_URL', 'https://tickets.worldapi.org')
  6. static ticketsUrl = ticketsRaw.endsWith('/') ? ticketsRaw.slice(0, ticketsRaw.length - 1) : ticketsRaw
  7. static maxRows = 200
  8. static agent = 'gitoria.worldapi.org (tickets)'
  9. // a code tickets sent back: letters, digits, `-` and `_`
  10. static isCode = (s) => {
  11. if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > 200) { return false }
  12. let i = 0
  13. while (i < s.length) {
  14. c = s.charCodeAt(i)
  15. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122) || (c >= 65 && c <= 90) || c == 45 || c == 95)) { return false }
  16. i = i + 1
  17. }
  18. return true
  19. }
  20. static ticketHref = (project, n) => { return ticketsUrl + '/projects/' + project + '/' + n }
  21. // one row for the page (a state's words are shown as tickets writes them)
  22. static viewOf = (t) => {
  23. return { @id = t.number number = t.number subject = t.subject state = t.state stateSlug = t.stateSlug created = t.created updated = t.updated url = ticketsUrl + t.href }
  24. }
  25. // ---- a write as a person: its headers, the reason of a refusal ----------------------------------------
  26. static writeHeaders = (c, identity) => {
  27. return { 'user-agent' = agent 'authorization' = 'Bearer ' + c.key 'x-tickets-identity' = identity }
  28. }
  29. static whyOf = (r) => {
  30. j = r.json()
  31. return j != null && j.error != null ? ': ' + j.error : ''
  32. }
  33. // ---- "#12" in a text ---------------------------------------------------------------------------------
  34. static isWordCode = (c) => { return (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c == 95 }
  35. // every `#N` of a text (N = 1–9 digits, not glued to a word before it): [{ at, len, n }]
  36. static refsOf = (text) => {
  37. out = []
  38. if (text == null || hlTypeName(text) != 'String') { return out }
  39. let i = 0
  40. while (i < text.length) {
  41. if (text[i] == '#' && (i == 0 || !isWordCode(text.charCodeAt(i - 1)))) {
  42. let j = i + 1
  43. while (j < text.length && j - i - 1 < 9 && isDigit(text.charCodeAt(j))) { j = j + 1 }
  44. stop = j >= text.length || !isWordCode(text.charCodeAt(j))
  45. if (j > i + 1 && stop && text.slice(i + 1, i + 2) != '0') { out.push({ at = i len = j - i n = toNumber(text.slice(i + 1, j)) }) }
  46. i = j
  47. } else {
  48. i = i + 1
  49. }
  50. }
  51. return out
  52. }
  53. // a text as pieces for a View: [{ text, href, isLink, isText }] — `#N` becomes a link when the repo is connected
  54. static refParts = (text, project) => {
  55. out = []
  56. t = text == null ? '' : '' + text
  57. refs = project == null || project == '' ? [] : refsOf(t)
  58. let pos = 0
  59. for (x of refs) {
  60. if (x.at > pos) { out.push({ text = t.slice(pos, x.at) href = '' isLink = false isText = true }) }
  61. out.push({ text = t.slice(x.at, x.at + x.len) href = ticketHref(project, x.n) isLink = true isText = false })
  62. pos = x.at + x.len
  63. }
  64. if (pos < t.length || out.length == 0) { out.push({ text = t.slice(pos) href = '' isLink = false isText = true }) }
  65. return out
  66. }
  67. // the numbers a text says it fixes: "fixes #3", "closes #4, #5"? — only the word directly before `#N` counts
  68. static fixedBy = (text) => {
  69. out = []
  70. t = text == null ? '' : ('' + text).toLowerCase()
  71. for (x of refsOf(t)) {
  72. before = t.slice(0, x.at).trimEnd()
  73. let ok = false
  74. for (w of ['fix', 'fixes', 'fixed', 'close', 'closes', 'closed', 'resolve', 'resolves', 'resolved']) {
  75. if (before.endsWith(w) && (before.length == w.length || !isWordCode(before.charCodeAt(before.length - w.length - 1)))) { ok = true }
  76. }
  77. if (ok && !out.includes(x.n)) { out.push(x.n) }
  78. }
  79. return out
  80. }
  81. // the commit list of a code page with pieces for the subjects (the View draws links from `parts`)
  82. static withRefParts = (data, project) => {
  83. if (data == null || data.isMessage) { return data }
  84. d = data
  85. d.subjectParts = refParts(d.subject, project)
  86. cs = []
  87. for (c of d.commits) {
  88. q = c
  89. q.parts = refParts(c.subject, project)
  90. cs.push(q)
  91. }
  92. d.commits = cs
  93. return d
  94. }
  95. static withPullParts = (list, project) => {
  96. out = []
  97. for (p of list) {
  98. q = p
  99. q.parts = refParts(p.title, project)
  100. out.push(q)
  101. }
  102. return out
  103. }

Branches

Latest commits

  • 26117255|||PR apps API: review comments on files with spaces in their namemre
  • b8796cecMerge branch 'f1-broken-json' into mainGitoria
  • 2f2959c5|||PR apps API: broken JSON body gives 400 invalid_json, not 500mre
  • f7c6ca07Merge branch 'ident-chooser-selector' into mainGitoria
  • 691abb8e|||PR tests: find ident identity choice without the removed .choose classmre
  • 16a3530fMerge branch 'heap-revert' into mainGitoria
  • 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
  • 13f0c342Merge branch 'hl25-records' into mainGitoria
  • a002ecb7|||PR records: HL25 report, heap helper, logmre
  • 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
  • fefaedd2log: tracker PR-only, HL25 startedmre
  • 21aa57calog: parkmre
  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre
  • b88e39f1log: privacy cleanup donemre
  • bfd7826ddocs: neutral example organisation namemre