gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9f502db19f502db1gitoria#46: W46b briefmre9f502db1/components/settings.hl

17.4 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" "Name the status contexts an app must set to success on a pull request's current head before it can be merged. 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 { "Status context"
  177. input { id = "requiredcontext" name = "requiredcontext" required = "required" maxlength = "100" autocomplete = "off" }
  178. }
  179. button { id = "requiredadd" type = "submit" "Require it" }
  180. p { id = "requirederror" class = "message" requiredError }
  181. }
  182. }
  183. }
  184. section { id = "ticketsettings"
  185. h2 { "Tickets" }
  186. if (hasConnectNote) { p { id = "connectnote" class = "message" connectNote } }
  187. if (connected) {
  188. p { id = "connectedto" "Connected to the tickets project " a { id = "projectlink" href = projectHref projectTitle } "." }
  189. }
  190. if (notConnected) { p { id = "notconnected" class = "muted" "This repository is not connected to a tickets project yet." } }
  191. if (isOwner) {
  192. p {
  193. a { id = "connecttickets" class = "button" href = connectHref "Tickets: connect" }
  194. }
  195. button { id = "disconnecttickets" class = forgetClass type = "button" "Forget the connection" on click(e) { emit disconnect() } }
  196. p { class = "muted" "Connecting asks tickets which project (you must be its admin there). Tickets can also disconnect gitoria from its side." }
  197. }
  198. if (notOwnerHint) { p { id = "ownerhint" class = "muted" "Only the owner of this repository can connect it." } }
  199. p { id = "settingserror" class = "message" settingsError }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. on togglePriv() {
  206. memberError = ''
  207. r = emit server gitoriaSetPrivate(slug, !isPrivate)
  208. if (r == null || r.error != null) {
  209. memberError = r != null ? r.error : 'could not change the visibility'
  210. return null
  211. }
  212. isPrivate = r.repo.isPrivate
  213. privText = isPrivate ? 'This repository is private: only its owner and its members can see it.' : 'This repository is public: everybody can read it.'
  214. privButton = isPrivate ? 'Make public' : 'Make private'
  215. }
  216. on addMember(e) {
  217. memberError = ''
  218. r = emit server gitoriaChangeMember(slug, e.target.elements.membername.value, true)
  219. if (r == null || r.error != null) {
  220. memberError = r != null ? r.error : 'could not add the member'
  221. return null
  222. }
  223. members = r.members
  224. noMembers = members.length == 0
  225. e.target.elements.membername.value = ''
  226. }
  227. on removeMember(name) {
  228. memberError = ''
  229. r = emit server gitoriaChangeMember(slug, name, false)
  230. if (r == null || r.error != null) {
  231. memberError = r != null ? r.error : 'could not remove the member'
  232. return null
  233. }
  234. members = r.members
  235. noMembers = members.length == 0
  236. }
  237. on server gitoriaSetPrivate(slugOf, priv, session) {
  238. return setRepoPriv(userIdOfSession(session), slugOf, priv)
  239. }
  240. on addAppRow(e) {
  241. appError = ''
  242. r = emit server gitoriaAppAdd(slug, e.target.elements.appname.value, e.target.elements.apphook.value)
  243. if (r == null || r.error != null) {
  244. appError = r != null ? r.error : 'could not add the app'
  245. return null
  246. }
  247. apps = r.rows
  248. noApps = apps.length == 0
  249. hasShown = true
  250. shownText = 'Connected ' + r.name + '. Copy the key and the secret now: they are not shown again.'
  251. shownKey = r.key
  252. hasShownKey = true
  253. shownSecret = r.secret
  254. e.target.elements.appname.value = ''
  255. e.target.elements.apphook.value = ''
  256. }
  257. on removeAppRow(id) {
  258. appError = ''
  259. r = emit server gitoriaAppRemove(slug, id)
  260. if (r == null || r.error != null) {
  261. appError = r != null ? r.error : 'could not remove the app'
  262. return null
  263. }
  264. apps = r.rows
  265. noApps = apps.length == 0
  266. hasShown = false
  267. }
  268. on pingApp(id) {
  269. appError = ''
  270. r = emit server gitoriaAppPing(slug, id)
  271. if (r == null || r.error != null) {
  272. appError = r != null ? r.error : 'could not send the ping'
  273. return null
  274. }
  275. appError = 'Ping to ' + r.name + ' is on its way. The status of the app shows the answer.'
  276. }
  277. on rotateApp(id) {
  278. appError = ''
  279. r = emit server gitoriaAppRotate(slug, id)
  280. if (r == null || r.error != null) {
  281. appError = r != null ? r.error : 'could not rotate the secret'
  282. return null
  283. }
  284. apps = r.rows
  285. hasShown = true
  286. shownText = 'New secret for ' + r.name + '. Copy it now: it is not shown again. The old secret no longer signs anything.'
  287. shownKey = ''
  288. hasShownKey = false
  289. shownSecret = r.secret
  290. }
  291. on addRequiredRow(e) {
  292. requiredError = ''
  293. r = emit server gitoriaRequiredAdd(slug, e.target.elements.requiredcontext.value)
  294. if (r == null || r.error != null) {
  295. requiredError = r != null ? r.error : 'could not save the setting'
  296. return null
  297. }
  298. required = r.rows
  299. noRequired = required.length == 0
  300. e.target.elements.requiredcontext.value = ''
  301. }
  302. on dropRequiredRow(id) {
  303. requiredError = ''
  304. r = emit server gitoriaRequiredRemove(slug, id)
  305. if (r == null || r.error != null) {
  306. requiredError = r != null ? r.error : 'could not save the setting'
  307. return null
  308. }
  309. required = r.rows
  310. noRequired = required.length == 0
  311. }
  312. on server gitoriaRequiredAdd(slugOf, ctxName, session) {
  313. return requireContext(userIdOfSession(session), session, slugOf, ctxName)
  314. }
  315. on server gitoriaRequiredRemove(slugOf, id, session) {
  316. return dropRequired(userIdOfSession(session), session, slugOf, id)
  317. }
  318. on server gitoriaAppAdd(slugOf, name, hook, session) {
  319. return addApp(userIdOfSession(session), session, slugOf, name, hook)
  320. }
  321. on server gitoriaAppRemove(slugOf, id, session) {
  322. return removeApp(userIdOfSession(session), session, slugOf, id)
  323. }
  324. on server gitoriaAppPing(slugOf, id, session) {
  325. return sendPing(userIdOfSession(session), session, slugOf, id)
  326. }
  327. on server gitoriaAppRotate(slugOf, id, session) {
  328. return rotateSecret(userIdOfSession(session), session, slugOf, id)
  329. }
  330. on server gitoriaChangeMember(slugOf, name, add, session) {
  331. return changeMember(userIdOfSession(session), 'r', slugOf, name, add)
  332. }
  333. on disconnect() {
  334. settingsError = ''
  335. r = emit server gitoriaDisconnectTickets(slug)
  336. if (r == null || r.error != null) {
  337. settingsError = r != null ? r.error : 'could not disconnect'
  338. return null
  339. }
  340. connected = false
  341. notConnected = true
  342. forgetClass = 'nomerge'
  343. projectTitle = ''
  344. projectHref = ''
  345. }
  346. on server gitoriaDisconnectTickets(slugOf, session) {
  347. if (session == null || repoAt(slugOf, session) == null) { return { error = 'could not disconnect' } }
  348. if (userOfSession(session) == null || !ownsRepo(slugOf, session)) { return { error = 'only the owner can change this' } }
  349. r = setTicketsConnection(slugOf, '', '', '', '')
  350. if (r.error != null) { return { error = r.error } }
  351. return { ok = true }
  352. }
  353. Style {
  354. repoSettings {
  355. display = 'grid'
  356. gap = '1rem'
  357. minWidth = '0'
  358. '> *' {
  359. margin = '0'
  360. }
  361. }
  362. #ticketsettings {
  363. display = 'grid'
  364. gap = '.75rem'
  365. justifyItems = 'start'
  366. '> *' {
  367. margin = '0'
  368. }
  369. }
  370. '#disconnecttickets.nomerge' {
  371. display = 'none'
  372. }
  373. #connecttickets {
  374. color = 'var(--color-accent-text)'
  375. background = 'var(--color-accent)'
  376. }
  377. #members {
  378. listStyle = 'none'
  379. margin = '0'
  380. padding = '0'
  381. display = 'grid'
  382. gap = '.25rem'
  383. maxWidth = '32rem'
  384. li {
  385. display = 'flex'
  386. alignItems = 'center'
  387. justifyContent = 'space-between'
  388. gap = '.75rem'
  389. minHeight = '2.75rem'
  390. padding = '0 .5rem 0 1rem'
  391. background = 'var(--color-surface)'
  392. borderRadius = 'var(--radius-large)'
  393. }
  394. }
  395. #memberform {
  396. display = 'grid'
  397. gap = '.75rem'
  398. margin = '.75rem 0 1.5rem'
  399. maxWidth = '32rem'
  400. }
  401. #apps {
  402. display = 'grid'
  403. gap = '.75rem'
  404. margin = '0 0 1.5rem'
  405. justifyItems = 'start'
  406. '> *' {
  407. margin = '0'
  408. }
  409. }
  410. #connectapp {
  411. display = 'grid'
  412. gap = '.75rem'
  413. maxWidth = '32rem'
  414. padding = '1rem'
  415. background = 'var(--color-surface)'
  416. borderRadius = 'var(--radius-large)'
  417. }
  418. #appshown {
  419. display = 'grid'
  420. gap = '.5rem'
  421. maxWidth = '100%'
  422. code {
  423. overflowWrap = 'anywhere'
  424. }
  425. }
  426. #applist {
  427. listStyle = 'none'
  428. margin = '0'
  429. padding = '0'
  430. display = 'grid'
  431. gap = '.25rem'
  432. maxWidth = '40rem'
  433. width = '100%'
  434. li {
  435. display = 'flex'
  436. flexWrap = 'wrap'
  437. alignItems = 'center'
  438. gap = '.5rem .75rem'
  439. minHeight = '2.75rem'
  440. padding = '.25rem .5rem .25rem 1rem'
  441. background = 'var(--color-surface)'
  442. borderRadius = 'var(--radius-large)'
  443. }
  444. }
  445. #required {
  446. display = 'grid'
  447. gap = '.75rem'
  448. margin = '0 0 1.5rem'
  449. justifyItems = 'start'
  450. '> *' {
  451. margin = '0'
  452. }
  453. }
  454. #requiredlist {
  455. listStyle = 'none'
  456. margin = '0'
  457. padding = '0'
  458. display = 'grid'
  459. gap = '.25rem'
  460. maxWidth = '40rem'
  461. width = '100%'
  462. li {
  463. display = 'flex'
  464. flexWrap = 'wrap'
  465. alignItems = 'center'
  466. justifyContent = 'space-between'
  467. gap = '.5rem .75rem'
  468. minHeight = '2.75rem'
  469. padding = '.25rem .5rem .25rem 1rem'
  470. background = 'var(--color-surface)'
  471. borderRadius = 'var(--radius-large)'
  472. }
  473. }
  474. #requiredform {
  475. display = 'grid'
  476. gap = '.75rem'
  477. maxWidth = '32rem'
  478. }
  479. #appform {
  480. display = 'grid'
  481. gap = '.75rem'
  482. maxWidth = '32rem'
  483. }
  484. #togglepriv {
  485. justifySelf = 'start'
  486. }
  487. #visibility {
  488. display = 'grid'
  489. gap = '.75rem'
  490. margin = '0 0 1.5rem'
  491. }
  492. }

Branches

Latest commits

  • 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
  • 19edc1f5gitoria#44: contract v1.3.1, what outdated meansmre
  • 7c2277ccgitoria#38: worker reports W40–W46, logmre
  • 1c3fa842gitoria#45 #47: worker briefsmre
  • 0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre
  • 3373f7c3gitoria#38: push API contract v1.2.1, reviews, worker briefs and logmre
  • 34143d2cgitoria#46: closing brace of appComments lost in the mergemre
  • 54927c9cgitoria#46: PR statuses and required checks before merge (merge w46-status)mre
  • 187e1871gitoria#46: badges without decorative bordersmre
  • 6b022798gitoria#46: tests/status.mjs in GATESmre