gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit6947cd586947cd58logmre6947cd58/components/settings.hl

17.7 KB

  1. // components/settings.hl — THE SETTINGS of a repo (gitoria#18): `/settings` on <slug>.<domain>. For now one section,
  2. // "Tickets": connect the repo to a tickets.worldapi.org project (the owner clicks "Tickets: connect", tickets asks which
  3. // project, and brings them back — lib/api.hl /settings/connect and /settings/connected; lib/tickets.hl). Anyone sees
  4. // whether the repo is connected; only the owner sees the buttons. Rendered on the server from the request's host.
  5. // "Apps" (gitoria#41, lib/apps.hl): the owner connects outside apps to the repo — the confirm card of connect flow A (an app sent the browser
  6. // here; lib/api.hl appConnectStart parked the request in the session), the list (Remove, Rotate secret) and Add (the manual way). Key and secret
  7. // are shown ONCE, in the page state of the click that made them.
  8. // (The main branch is switched on the code page.)
  9. parent './repo.hl'
  10. import { siteName } from '../project.hl'
  11. import { slugOfRequest } from '../lib/orgs.hl'
  12. import { userOfSession, userIdOfSession } from '../lib/users.hl'
  13. import { titleSlug, repoAt, ownsRepo, setTicketsConnection, setRepoPriv, membersOf, changeMember } from '../lib/repos.hl'
  14. import { px } from '../lib/util.hl'
  15. import { pageTitle } from '../lib/repos-helpers.hl'
  16. import { ticketsUrl } from '../lib/tickets-helpers.hl'
  17. import { appRows, addApp, removeApp, rotateSecret, sendPing } from '../lib/apps.hl'
  18. import { hostOfUrl } from '../lib/apps-helpers.hl'
  19. import { requiredList, requireContext, dropRequired } from '../lib/statuses.hl'
  20. host = null
  21. orepo = ''
  22. session = null
  23. slug = slugOfRequest(host, orepo)
  24. repo = repoAt(slug, session)
  25. hasRepo = repo != null
  26. pre = px(slug)
  27. connectHref = pre + '/settings/connect'
  28. isOwner = repo != null && ownsRepo(slug, session)
  29. connected = repo != null && repo.ticketsOn
  30. notConnected = repo != null && !repo.ticketsOn
  31. projectTitle = repo != null ? repo.ticketsTitle : ''
  32. projectHref = repo != null && repo.ticketsOn ? ticketsUrl + '/projects/' + repo.ticketsProject : ''
  33. connectNote = session != null && session.data != null && session.data.connectNote != null ? session.data.connectNote : ''
  34. hasConnectNote = connectNote != ''
  35. notOwnerHint = repo != null && !isOwner
  36. settingsError = ''
  37. forgetClass = repo != null && repo.ticketsOn ? 'quiet' : 'nomerge'
  38. __title = pageTitle(siteName, titleSlug(host, orepo, session), 'Settings')
  39. // VISIBILITY AND MEMBERS (GO34): the owner makes the repo private / public and keeps the member list (people who see a private repo and may push)
  40. isPrivate = repo != null && repo.isPrivate
  41. ownPriv = repo != null && repo.isPrivate && !repo.inOrg
  42. privText = repo == null ? '' : (repo.isPrivate ? 'This repository is private: only its owner and its members can see it.' : 'This repository is public: everybody can read it.')
  43. privButton = repo != null && repo.isPrivate ? 'Make public' : 'Make private'
  44. listed = isOwner ? membersOf('r', slug, userIdOfSession(session)) : null
  45. members = listed != null && listed.members != null ? listed.members : []
  46. noMembers = isOwner && members.length == 0
  47. memberName = ''
  48. memberError = ''
  49. // APPS (gitoria#41): the owner's list; the connect request an app parked in this session (for this repo only); what was just made (shown once)
  50. apps = isOwner ? appRows(repo.id) : []
  51. noApps = isOwner && apps.length == 0
  52. parked = session != null && session.data != null && session.data.appConnect != null && repo != null && session.data.appConnect.slug == slug ? session.data.appConnect : null
  53. hasParked = parked != null
  54. parkedApp = parked != null ? parked.app : ''
  55. parkedHost = parked != null ? hostOfUrl(parked.ret) : ''
  56. parkedNonce = parked != null ? parked.nonce : ''
  57. confirmAction = pre + '/settings/apps/confirm'
  58. parkedSignedOut = hasParked && session != null && userIdOfSession(session) == null
  59. parkedNotOwner = hasParked && userIdOfSession(session) != null && !isOwner
  60. parkedOwner = hasParked && isOwner
  61. // the Apps section is the owner's; a signed-in non-owner who came to connect an app still gets its message (gitoria#45)
  62. showApps = isOwner || hasParked
  63. appName = ''
  64. appHook = ''
  65. appError = ''
  66. hasShown = false
  67. shownName = ''
  68. shownKey = ''
  69. hasShownKey = false
  70. shownSecret = ''
  71. shownText = ''
  72. // REQUIRED BEFORE MERGE (gitoria#46): the contexts the owner requires to be success on a pull request's head
  73. required = isOwner ? requiredList(repo.id) : []
  74. noRequired = isOwner && required.length == 0
  75. requiredError = ''
  76. View {
  77. repoPage {
  78. if (hasRepo) {
  79. repoSettings { id = "settings"
  80. section { id = "visibility"
  81. h2 { "Visibility" }
  82. p { id = "privtext" privText }
  83. if (isOwner) { button { id = "togglepriv" type = "button" privButton on click(e) { emit togglePriv() } } }
  84. if (isOwner) {
  85. h2 { "Members" }
  86. p { class = "muted" "Members see this repository even when it is private, and may push to it. Add somebody by the display name they chose." }
  87. if (noMembers) { p { id = "nomembers" class = "empty" "No members yet." } }
  88. ul { id = "members"
  89. for (m of members) {
  90. li { span { class = "slug" m.name } button { class = "quiet" type = "button" "Remove" on click(e) { emit removeMember(m.name) } } }
  91. }
  92. }
  93. form { id = "memberform"
  94. on submit(e) {
  95. e.preventDefault()
  96. emit addMember(e)
  97. }
  98. label { "Display name"
  99. input { id = "membername" name = "membername" required = "required" maxlength = "60" autocomplete = "off" }
  100. }
  101. button { id = "memberadd" type = "submit" "Add member" }
  102. p { id = "membererror" class = "message" memberError }
  103. }
  104. }
  105. }
  106. if (showApps) {
  107. section { id = "apps"
  108. h2 { "Apps" }
  109. if (parkedSignedOut) { p { id = "appsignin" class = "message" "Sign in with the selector at the top to connect " + parkedApp + " to this repository." } }
  110. if (parkedNotOwner) { p { id = "appnotowner" class = "message" "Only the owner of this repository can connect " + parkedApp + "." } }
  111. if (parkedOwner) {
  112. form { id = "connectapp" method = "post" action = confirmAction
  113. p { id = "connectask" "Connect " + parkedApp + " to " + slug + "?" }
  114. p { class = "muted" "It will get read access to this repository and may review its pull requests. When you say Yes you go back to " + parkedHost + "." }
  115. input { name = "nonce" type = "hidden" value = parkedNonce }
  116. button { id = "connectyes" type = "submit" name = "decision" value = "yes" "Yes, connect" }
  117. button { id = "connectno" class = "quiet" type = "submit" name = "decision" value = "no" "No" }
  118. }
  119. }
  120. if (hasShown) {
  121. div { id = "appshown" class = "message"
  122. p { shownText }
  123. if (hasShownKey) { p { "Key: " code { id = "shownkey" shownKey } } }
  124. p { "Secret: " code { id = "shownsecret" shownSecret } }
  125. }
  126. }
  127. if (isOwner) {
  128. p { class = "muted" "An app is an outside program (for example a code reviewer) that works on this repository under its own name. Its key and secret are shown once." }
  129. if (noApps) { p { id = "noapps" class = "empty" "No apps connected." } }
  130. ul { id = "applist"
  131. for (a of apps) {
  132. li {
  133. span { class = "slug" a.name }
  134. span { class = "muted" a.host + " · added " + a.created + " · " + a.status }
  135. button { class = "quiet" type = "button" "Send ping" on click(e) { emit pingApp(a.id) } }
  136. button { class = "quiet" type = "button" "Rotate secret" on click(e) { emit rotateApp(a.id) } }
  137. button { class = "quiet" type = "button" "Remove" on click(e) { emit removeAppRow(a.id) } }
  138. }
  139. }
  140. }
  141. form { id = "appform"
  142. on submit(e) {
  143. e.preventDefault()
  144. emit addAppRow(e)
  145. }
  146. label { "App name"
  147. input { id = "appname" name = "appname" required = "required" maxlength = "60" autocomplete = "off" }
  148. }
  149. label { "Receiver address"
  150. input { id = "apphook" name = "apphook" type = "url" required = "required" maxlength = "500" autocomplete = "off" placeholder = "https://…" }
  151. }
  152. button { id = "appadd" type = "submit" "Add app" }
  153. p { id = "apperror" class = "message" appError }
  154. }
  155. }
  156. }
  157. }
  158. section { id = "required"
  159. if (isOwner) {
  160. h2 { "Required before merge" }
  161. p { class = "muted" "Pick a connected app and the status context it must set to success on a pull request's current head before it can be merged. Another app's success with the same context name does not count. A push gives the pull request a new head, so the check starts again." }
  162. if (noRequired) { p { id = "norequired" class = "empty" "Nothing is required: every pull request can be merged." } }
  163. ul { id = "requiredlist"
  164. for (r of required) {
  165. li {
  166. span { class = "slug" r.name }
  167. button { class = "quiet" type = "button" "Remove" on click(e) { emit dropRequiredRow(r.id) } }
  168. }
  169. }
  170. }
  171. form { id = "requiredform"
  172. on submit(e) {
  173. e.preventDefault()
  174. emit addRequiredRow(e)
  175. }
  176. label { "App"
  177. select { id = "requiredapp" name = "requiredapp"
  178. for (a of apps) { option { value = a.id a.name } }
  179. }
  180. }
  181. label { "Status context the app sets"
  182. input { id = "requiredcontext" name = "requiredcontext" required = "required" maxlength = "100" autocomplete = "off" }
  183. }
  184. button { id = "requiredadd" type = "submit" "Require it" }
  185. p { id = "requirederror" class = "message" requiredError }
  186. }
  187. }
  188. }
  189. section { id = "ticketsettings"
  190. h2 { "Tickets" }
  191. if (hasConnectNote) { p { id = "connectnote" class = "message" connectNote } }
  192. if (connected) {
  193. p { id = "connectedto" "Connected to the tickets project " a { id = "projectlink" href = projectHref projectTitle } "." }
  194. }
  195. if (notConnected) { p { id = "notconnected" class = "muted" "This repository is not connected to a tickets project yet." } }
  196. if (isOwner) {
  197. p {
  198. a { id = "connecttickets" class = "button" href = connectHref "Tickets: connect" }
  199. }
  200. button { id = "disconnecttickets" class = forgetClass type = "button" "Forget the connection" on click(e) { emit disconnect() } }
  201. p { class = "muted" "Connecting asks tickets which project (you must be its admin there). Tickets can also disconnect gitoria from its side." }
  202. }
  203. if (notOwnerHint) { p { id = "ownerhint" class = "muted" "Only the owner of this repository can connect it." } }
  204. p { id = "settingserror" class = "message" settingsError }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. on togglePriv() {
  211. memberError = ''
  212. r = emit server gitoriaSetPrivate(slug, !isPrivate)
  213. if (r == null || r.error != null) {
  214. memberError = r != null ? r.error : 'could not change the visibility'
  215. return null
  216. }
  217. isPrivate = r.repo.isPrivate
  218. privText = isPrivate ? 'This repository is private: only its owner and its members can see it.' : 'This repository is public: everybody can read it.'
  219. privButton = isPrivate ? 'Make public' : 'Make private'
  220. }
  221. on addMember(e) {
  222. memberError = ''
  223. r = emit server gitoriaChangeMember(slug, e.target.elements.membername.value, true)
  224. if (r == null || r.error != null) {
  225. memberError = r != null ? r.error : 'could not add the member'
  226. return null
  227. }
  228. members = r.members
  229. noMembers = members.length == 0
  230. e.target.elements.membername.value = ''
  231. }
  232. on removeMember(name) {
  233. memberError = ''
  234. r = emit server gitoriaChangeMember(slug, name, false)
  235. if (r == null || r.error != null) {
  236. memberError = r != null ? r.error : 'could not remove the member'
  237. return null
  238. }
  239. members = r.members
  240. noMembers = members.length == 0
  241. }
  242. on server gitoriaSetPrivate(slugOf, priv, session) {
  243. return setRepoPriv(userIdOfSession(session), slugOf, priv)
  244. }
  245. on addAppRow(e) {
  246. appError = ''
  247. r = emit server gitoriaAppAdd(slug, e.target.elements.appname.value, e.target.elements.apphook.value)
  248. if (r == null || r.error != null) {
  249. appError = r != null ? r.error : 'could not add the app'
  250. return null
  251. }
  252. apps = r.rows
  253. noApps = apps.length == 0
  254. hasShown = true
  255. shownText = 'Connected ' + r.name + '. Copy the key and the secret now: they are not shown again.'
  256. shownKey = r.key
  257. hasShownKey = true
  258. shownSecret = r.secret
  259. e.target.elements.appname.value = ''
  260. e.target.elements.apphook.value = ''
  261. }
  262. on removeAppRow(id) {
  263. appError = ''
  264. r = emit server gitoriaAppRemove(slug, id)
  265. if (r == null || r.error != null) {
  266. appError = r != null ? r.error : 'could not remove the app'
  267. return null
  268. }
  269. apps = r.rows
  270. noApps = apps.length == 0
  271. hasShown = false
  272. }
  273. on pingApp(id) {
  274. appError = ''
  275. r = emit server gitoriaAppPing(slug, id)
  276. if (r == null || r.error != null) {
  277. appError = r != null ? r.error : 'could not send the ping'
  278. return null
  279. }
  280. appError = 'Ping to ' + r.name + ' is on its way. The status of the app shows the answer.'
  281. }
  282. on rotateApp(id) {
  283. appError = ''
  284. r = emit server gitoriaAppRotate(slug, id)
  285. if (r == null || r.error != null) {
  286. appError = r != null ? r.error : 'could not rotate the secret'
  287. return null
  288. }
  289. apps = r.rows
  290. hasShown = true
  291. shownText = 'New secret for ' + r.name + '. Copy it now: it is not shown again. The old secret no longer signs anything.'
  292. shownKey = ''
  293. hasShownKey = false
  294. shownSecret = r.secret
  295. }
  296. on addRequiredRow(e) {
  297. requiredError = ''
  298. r = emit server gitoriaRequiredAdd(slug, e.target.elements.requiredapp.value, e.target.elements.requiredcontext.value)
  299. if (r == null || r.error != null) {
  300. requiredError = r != null ? r.error : 'could not save the setting'
  301. return null
  302. }
  303. required = r.rows
  304. noRequired = required.length == 0
  305. e.target.elements.requiredcontext.value = ''
  306. }
  307. on dropRequiredRow(id) {
  308. requiredError = ''
  309. r = emit server gitoriaRequiredRemove(slug, id)
  310. if (r == null || r.error != null) {
  311. requiredError = r != null ? r.error : 'could not save the setting'
  312. return null
  313. }
  314. required = r.rows
  315. noRequired = required.length == 0
  316. }
  317. on server gitoriaRequiredAdd(slugOf, appId, ctxName, session) {
  318. return requireContext(userIdOfSession(session), session, slugOf, appId, ctxName)
  319. }
  320. on server gitoriaRequiredRemove(slugOf, id, session) {
  321. return dropRequired(userIdOfSession(session), session, slugOf, id)
  322. }
  323. on server gitoriaAppAdd(slugOf, name, hook, session) {
  324. return addApp(userIdOfSession(session), session, slugOf, name, hook)
  325. }
  326. on server gitoriaAppRemove(slugOf, id, session) {
  327. return removeApp(userIdOfSession(session), session, slugOf, id)
  328. }
  329. on server gitoriaAppPing(slugOf, id, session) {
  330. return sendPing(userIdOfSession(session), session, slugOf, id)
  331. }
  332. on server gitoriaAppRotate(slugOf, id, session) {
  333. return rotateSecret(userIdOfSession(session), session, slugOf, id)
  334. }
  335. on server gitoriaChangeMember(slugOf, name, add, session) {
  336. return changeMember(userIdOfSession(session), 'r', slugOf, name, add)
  337. }
  338. on disconnect() {
  339. settingsError = ''
  340. r = emit server gitoriaDisconnectTickets(slug)
  341. if (r == null || r.error != null) {
  342. settingsError = r != null ? r.error : 'could not disconnect'
  343. return null
  344. }
  345. connected = false
  346. notConnected = true
  347. forgetClass = 'nomerge'
  348. projectTitle = ''
  349. projectHref = ''
  350. }
  351. on server gitoriaDisconnectTickets(slugOf, session) {
  352. if (session == null || repoAt(slugOf, session) == null) { return { error = 'could not disconnect' } }
  353. if (userOfSession(session) == null || !ownsRepo(slugOf, session)) { return { error = 'only the owner can change this' } }
  354. r = setTicketsConnection(slugOf, '', '', '', '')
  355. if (r.error != null) { return { error = r.error } }
  356. return { ok = true }
  357. }
  358. Style {
  359. repoSettings {
  360. display = 'grid'
  361. gap = '1rem'
  362. minWidth = '0'
  363. '> *' {
  364. margin = '0'
  365. }
  366. }
  367. #ticketsettings {
  368. display = 'grid'
  369. gap = '.75rem'
  370. justifyItems = 'start'
  371. '> *' {
  372. margin = '0'
  373. }
  374. }
  375. '#disconnecttickets.nomerge' {
  376. display = 'none'
  377. }
  378. #connecttickets {
  379. color = 'var(--color-accent-text)'
  380. background = 'var(--color-accent)'
  381. }
  382. #members {
  383. listStyle = 'none'
  384. margin = '0'
  385. padding = '0'
  386. display = 'grid'
  387. gap = '.25rem'
  388. maxWidth = '32rem'
  389. li {
  390. display = 'flex'
  391. alignItems = 'center'
  392. justifyContent = 'space-between'
  393. gap = '.75rem'
  394. minHeight = '2.75rem'
  395. padding = '0 .5rem 0 1rem'
  396. background = 'var(--color-surface)'
  397. borderRadius = 'var(--radius-large)'
  398. }
  399. }
  400. #memberform {
  401. display = 'grid'
  402. gap = '.75rem'
  403. margin = '.75rem 0 1.5rem'
  404. maxWidth = '32rem'
  405. }
  406. #apps {
  407. display = 'grid'
  408. gap = '.75rem'
  409. margin = '0 0 1.5rem'
  410. justifyItems = 'start'
  411. '> *' {
  412. margin = '0'
  413. }
  414. }
  415. #connectapp {
  416. display = 'grid'
  417. gap = '.75rem'
  418. maxWidth = '32rem'
  419. padding = '1rem'
  420. background = 'var(--color-surface)'
  421. borderRadius = 'var(--radius-large)'
  422. }
  423. #appshown {
  424. display = 'grid'
  425. gap = '.5rem'
  426. maxWidth = '100%'
  427. code {
  428. overflowWrap = 'anywhere'
  429. }
  430. }
  431. #applist {
  432. listStyle = 'none'
  433. margin = '0'
  434. padding = '0'
  435. display = 'grid'
  436. gap = '.25rem'
  437. maxWidth = '40rem'
  438. width = '100%'
  439. li {
  440. display = 'flex'
  441. flexWrap = 'wrap'
  442. alignItems = 'center'
  443. gap = '.5rem .75rem'
  444. minHeight = '2.75rem'
  445. padding = '.25rem .5rem .25rem 1rem'
  446. background = 'var(--color-surface)'
  447. borderRadius = 'var(--radius-large)'
  448. }
  449. }
  450. #required {
  451. display = 'grid'
  452. gap = '.75rem'
  453. margin = '0 0 1.5rem'
  454. justifyItems = 'start'
  455. '> *' {
  456. margin = '0'
  457. }
  458. }
  459. #requiredlist {
  460. listStyle = 'none'
  461. margin = '0'
  462. padding = '0'
  463. display = 'grid'
  464. gap = '.25rem'
  465. maxWidth = '40rem'
  466. width = '100%'
  467. li {
  468. display = 'flex'
  469. flexWrap = 'wrap'
  470. alignItems = 'center'
  471. justifyContent = 'space-between'
  472. gap = '.5rem .75rem'
  473. minHeight = '2.75rem'
  474. padding = '.25rem .5rem .25rem 1rem'
  475. background = 'var(--color-surface)'
  476. borderRadius = 'var(--radius-large)'
  477. }
  478. }
  479. #requiredform {
  480. display = 'grid'
  481. gap = '.75rem'
  482. maxWidth = '32rem'
  483. }
  484. #appform {
  485. display = 'grid'
  486. gap = '.75rem'
  487. maxWidth = '32rem'
  488. }
  489. #togglepriv {
  490. justifySelf = 'start'
  491. }
  492. #visibility {
  493. display = 'grid'
  494. gap = '.75rem'
  495. margin = '0 0 1.5rem'
  496. }
  497. }

Branches

Latest commits

  • 6947cd58logmre
  • 2175e48clogmre
  • 40eb3ca1gitoria#38: review package, logmre
  • 06a5a50egitoria#38: deploy b8252ca, logmre
  • b8252ca0gitoria#46: W46b report, logmre
  • 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
  • dd0267edgitoria#46: fix a label in the keyed gatemre
  • b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
  • 2287363egitoria#47: W47 report, logmre
  • 9f502db1gitoria#46: W46b briefmre
  • 9678a8f3gitoria#46: contract v1.4, statuses keyed by app + contextmre
  • bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mre
  • 772c401fgitoria#45: W45 report, logmre
  • b6b793afgitoria#45: PR page with reviews, threads, conversation, Review again (merge w45-pr-page)mre
  • fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemre
  • f7198131gitoria#45: tests/pr-page.mjs gatemre
  • 483141d0gitoria#45: Settings shows the Apps heading only to the ownermre
  • e2eff8f6gitoria#45: the PR page /pulls/N with reviews, threads, verdicts, conversation, comment, reply, resolve and Review againmre
  • 7a602839gitoria#45: raiseEvent can send to one app only; people's comments, replies and resolving in reviews.hlmre
  • 5de7f578gitoria#47: rate limits per app (429, Retry-After), status read route, statuses and required on the pull, one JSON check for every write routemre