gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0a46bf7d0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre0a46bf7d/components/settings.hl

17.2 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. appName = ''
  62. appHook = ''
  63. appError = ''
  64. hasShown = false
  65. shownName = ''
  66. shownKey = ''
  67. hasShownKey = false
  68. shownSecret = ''
  69. shownText = ''
  70. // REQUIRED BEFORE MERGE (gitoria#46): the contexts the owner requires to be success on a pull request's head
  71. required = isOwner ? requiredList(repo.id) : []
  72. noRequired = isOwner && required.length == 0
  73. requiredError = ''
  74. View {
  75. repoPage {
  76. if (hasRepo) {
  77. repoSettings { id = "settings"
  78. section { id = "visibility"
  79. h2 { "Visibility" }
  80. p { id = "privtext" privText }
  81. if (isOwner) { button { id = "togglepriv" type = "button" privButton on click(e) { emit togglePriv() } } }
  82. if (isOwner) {
  83. h2 { "Members" }
  84. 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." }
  85. if (noMembers) { p { id = "nomembers" class = "empty" "No members yet." } }
  86. ul { id = "members"
  87. for (m of members) {
  88. li { span { class = "slug" m.name } button { class = "quiet" type = "button" "Remove" on click(e) { emit removeMember(m.name) } } }
  89. }
  90. }
  91. form { id = "memberform"
  92. on submit(e) {
  93. e.preventDefault()
  94. emit addMember(e)
  95. }
  96. label { "Display name"
  97. input { id = "membername" name = "membername" required = "required" maxlength = "60" autocomplete = "off" }
  98. }
  99. button { id = "memberadd" type = "submit" "Add member" }
  100. p { id = "membererror" class = "message" memberError }
  101. }
  102. }
  103. }
  104. section { id = "apps"
  105. h2 { "Apps" }
  106. if (parkedSignedOut) { p { id = "appsignin" class = "message" "Sign in with the selector at the top to connect " + parkedApp + " to this repository." } }
  107. if (parkedNotOwner) { p { id = "appnotowner" class = "message" "Only the owner of this repository can connect " + parkedApp + "." } }
  108. if (parkedOwner) {
  109. form { id = "connectapp" method = "post" action = confirmAction
  110. p { id = "connectask" "Connect " + parkedApp + " to " + slug + "?" }
  111. 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 + "." }
  112. input { name = "nonce" type = "hidden" value = parkedNonce }
  113. button { id = "connectyes" type = "submit" name = "decision" value = "yes" "Yes, connect" }
  114. button { id = "connectno" class = "quiet" type = "submit" name = "decision" value = "no" "No" }
  115. }
  116. }
  117. if (hasShown) {
  118. div { id = "appshown" class = "message"
  119. p { shownText }
  120. if (hasShownKey) { p { "Key: " code { id = "shownkey" shownKey } } }
  121. p { "Secret: " code { id = "shownsecret" shownSecret } }
  122. }
  123. }
  124. if (isOwner) {
  125. 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." }
  126. if (noApps) { p { id = "noapps" class = "empty" "No apps connected." } }
  127. ul { id = "applist"
  128. for (a of apps) {
  129. li {
  130. span { class = "slug" a.name }
  131. span { class = "muted" a.host + " · added " + a.created + " · " + a.status }
  132. button { class = "quiet" type = "button" "Send ping" on click(e) { emit pingApp(a.id) } }
  133. button { class = "quiet" type = "button" "Rotate secret" on click(e) { emit rotateApp(a.id) } }
  134. button { class = "quiet" type = "button" "Remove" on click(e) { emit removeAppRow(a.id) } }
  135. }
  136. }
  137. }
  138. form { id = "appform"
  139. on submit(e) {
  140. e.preventDefault()
  141. emit addAppRow(e)
  142. }
  143. label { "App name"
  144. input { id = "appname" name = "appname" required = "required" maxlength = "60" autocomplete = "off" }
  145. }
  146. label { "Receiver address"
  147. input { id = "apphook" name = "apphook" type = "url" required = "required" maxlength = "500" autocomplete = "off" placeholder = "https://…" }
  148. }
  149. button { id = "appadd" type = "submit" "Add app" }
  150. p { id = "apperror" class = "message" appError }
  151. }
  152. }
  153. }
  154. section { id = "required"
  155. if (isOwner) {
  156. h2 { "Required before merge" }
  157. 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." }
  158. if (noRequired) { p { id = "norequired" class = "empty" "Nothing is required: every pull request can be merged." } }
  159. ul { id = "requiredlist"
  160. for (r of required) {
  161. li {
  162. span { class = "slug" r.name }
  163. button { class = "quiet" type = "button" "Remove" on click(e) { emit dropRequiredRow(r.id) } }
  164. }
  165. }
  166. }
  167. form { id = "requiredform"
  168. on submit(e) {
  169. e.preventDefault()
  170. emit addRequiredRow(e)
  171. }
  172. label { "Status context"
  173. input { id = "requiredcontext" name = "requiredcontext" required = "required" maxlength = "100" autocomplete = "off" }
  174. }
  175. button { id = "requiredadd" type = "submit" "Require it" }
  176. p { id = "requirederror" class = "message" requiredError }
  177. }
  178. }
  179. }
  180. section { id = "ticketsettings"
  181. h2 { "Tickets" }
  182. if (hasConnectNote) { p { id = "connectnote" class = "message" connectNote } }
  183. if (connected) {
  184. p { id = "connectedto" "Connected to the tickets project " a { id = "projectlink" href = projectHref projectTitle } "." }
  185. }
  186. if (notConnected) { p { id = "notconnected" class = "muted" "This repository is not connected to a tickets project yet." } }
  187. if (isOwner) {
  188. p {
  189. a { id = "connecttickets" class = "button" href = connectHref "Tickets: connect" }
  190. }
  191. button { id = "disconnecttickets" class = forgetClass type = "button" "Forget the connection" on click(e) { emit disconnect() } }
  192. p { class = "muted" "Connecting asks tickets which project (you must be its admin there). Tickets can also disconnect gitoria from its side." }
  193. }
  194. if (notOwnerHint) { p { id = "ownerhint" class = "muted" "Only the owner of this repository can connect it." } }
  195. p { id = "settingserror" class = "message" settingsError }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. on togglePriv() {
  202. memberError = ''
  203. r = emit server gitoriaSetPrivate(slug, !isPrivate)
  204. if (r == null || r.error != null) {
  205. memberError = r != null ? r.error : 'could not change the visibility'
  206. return null
  207. }
  208. isPrivate = r.repo.isPrivate
  209. privText = isPrivate ? 'This repository is private: only its owner and its members can see it.' : 'This repository is public: everybody can read it.'
  210. privButton = isPrivate ? 'Make public' : 'Make private'
  211. }
  212. on addMember(e) {
  213. memberError = ''
  214. r = emit server gitoriaChangeMember(slug, e.target.elements.membername.value, true)
  215. if (r == null || r.error != null) {
  216. memberError = r != null ? r.error : 'could not add the member'
  217. return null
  218. }
  219. members = r.members
  220. noMembers = members.length == 0
  221. e.target.elements.membername.value = ''
  222. }
  223. on removeMember(name) {
  224. memberError = ''
  225. r = emit server gitoriaChangeMember(slug, name, false)
  226. if (r == null || r.error != null) {
  227. memberError = r != null ? r.error : 'could not remove the member'
  228. return null
  229. }
  230. members = r.members
  231. noMembers = members.length == 0
  232. }
  233. on server gitoriaSetPrivate(slugOf, priv, session) {
  234. return setRepoPriv(userIdOfSession(session), slugOf, priv)
  235. }
  236. on addAppRow(e) {
  237. appError = ''
  238. r = emit server gitoriaAppAdd(slug, e.target.elements.appname.value, e.target.elements.apphook.value)
  239. if (r == null || r.error != null) {
  240. appError = r != null ? r.error : 'could not add the app'
  241. return null
  242. }
  243. apps = r.rows
  244. noApps = apps.length == 0
  245. hasShown = true
  246. shownText = 'Connected ' + r.name + '. Copy the key and the secret now: they are not shown again.'
  247. shownKey = r.key
  248. hasShownKey = true
  249. shownSecret = r.secret
  250. e.target.elements.appname.value = ''
  251. e.target.elements.apphook.value = ''
  252. }
  253. on removeAppRow(id) {
  254. appError = ''
  255. r = emit server gitoriaAppRemove(slug, id)
  256. if (r == null || r.error != null) {
  257. appError = r != null ? r.error : 'could not remove the app'
  258. return null
  259. }
  260. apps = r.rows
  261. noApps = apps.length == 0
  262. hasShown = false
  263. }
  264. on pingApp(id) {
  265. appError = ''
  266. r = emit server gitoriaAppPing(slug, id)
  267. if (r == null || r.error != null) {
  268. appError = r != null ? r.error : 'could not send the ping'
  269. return null
  270. }
  271. appError = 'Ping to ' + r.name + ' is on its way. The status of the app shows the answer.'
  272. }
  273. on rotateApp(id) {
  274. appError = ''
  275. r = emit server gitoriaAppRotate(slug, id)
  276. if (r == null || r.error != null) {
  277. appError = r != null ? r.error : 'could not rotate the secret'
  278. return null
  279. }
  280. apps = r.rows
  281. hasShown = true
  282. shownText = 'New secret for ' + r.name + '. Copy it now: it is not shown again. The old secret no longer signs anything.'
  283. shownKey = ''
  284. hasShownKey = false
  285. shownSecret = r.secret
  286. }
  287. on addRequiredRow(e) {
  288. requiredError = ''
  289. r = emit server gitoriaRequiredAdd(slug, e.target.elements.requiredcontext.value)
  290. if (r == null || r.error != null) {
  291. requiredError = r != null ? r.error : 'could not save the setting'
  292. return null
  293. }
  294. required = r.rows
  295. noRequired = required.length == 0
  296. e.target.elements.requiredcontext.value = ''
  297. }
  298. on dropRequiredRow(id) {
  299. requiredError = ''
  300. r = emit server gitoriaRequiredRemove(slug, id)
  301. if (r == null || r.error != null) {
  302. requiredError = r != null ? r.error : 'could not save the setting'
  303. return null
  304. }
  305. required = r.rows
  306. noRequired = required.length == 0
  307. }
  308. on server gitoriaRequiredAdd(slugOf, ctxName, session) {
  309. return requireContext(userIdOfSession(session), session, slugOf, ctxName)
  310. }
  311. on server gitoriaRequiredRemove(slugOf, id, session) {
  312. return dropRequired(userIdOfSession(session), session, slugOf, id)
  313. }
  314. on server gitoriaAppAdd(slugOf, name, hook, session) {
  315. return addApp(userIdOfSession(session), session, slugOf, name, hook)
  316. }
  317. on server gitoriaAppRemove(slugOf, id, session) {
  318. return removeApp(userIdOfSession(session), session, slugOf, id)
  319. }
  320. on server gitoriaAppPing(slugOf, id, session) {
  321. return sendPing(userIdOfSession(session), session, slugOf, id)
  322. }
  323. on server gitoriaAppRotate(slugOf, id, session) {
  324. return rotateSecret(userIdOfSession(session), session, slugOf, id)
  325. }
  326. on server gitoriaChangeMember(slugOf, name, add, session) {
  327. return changeMember(userIdOfSession(session), 'r', slugOf, name, add)
  328. }
  329. on disconnect() {
  330. settingsError = ''
  331. r = emit server gitoriaDisconnectTickets(slug)
  332. if (r == null || r.error != null) {
  333. settingsError = r != null ? r.error : 'could not disconnect'
  334. return null
  335. }
  336. connected = false
  337. notConnected = true
  338. forgetClass = 'nomerge'
  339. projectTitle = ''
  340. projectHref = ''
  341. }
  342. on server gitoriaDisconnectTickets(slugOf, session) {
  343. if (session == null || repoAt(slugOf, session) == null) { return { error = 'could not disconnect' } }
  344. if (userOfSession(session) == null || !ownsRepo(slugOf, session)) { return { error = 'only the owner can change this' } }
  345. r = setTicketsConnection(slugOf, '', '', '', '')
  346. if (r.error != null) { return { error = r.error } }
  347. return { ok = true }
  348. }
  349. Style {
  350. repoSettings {
  351. display = 'grid'
  352. gap = '1rem'
  353. minWidth = '0'
  354. '> *' {
  355. margin = '0'
  356. }
  357. }
  358. #ticketsettings {
  359. display = 'grid'
  360. gap = '.75rem'
  361. justifyItems = 'start'
  362. '> *' {
  363. margin = '0'
  364. }
  365. }
  366. '#disconnecttickets.nomerge' {
  367. display = 'none'
  368. }
  369. #connecttickets {
  370. color = 'var(--color-accent-text)'
  371. background = 'var(--color-accent)'
  372. }
  373. #members {
  374. listStyle = 'none'
  375. margin = '0'
  376. padding = '0'
  377. display = 'grid'
  378. gap = '.25rem'
  379. maxWidth = '32rem'
  380. li {
  381. display = 'flex'
  382. alignItems = 'center'
  383. justifyContent = 'space-between'
  384. gap = '.75rem'
  385. minHeight = '2.75rem'
  386. padding = '0 .5rem 0 1rem'
  387. background = 'var(--color-surface)'
  388. borderRadius = 'var(--radius-large)'
  389. }
  390. }
  391. #memberform {
  392. display = 'grid'
  393. gap = '.75rem'
  394. margin = '.75rem 0 1.5rem'
  395. maxWidth = '32rem'
  396. }
  397. #apps {
  398. display = 'grid'
  399. gap = '.75rem'
  400. margin = '0 0 1.5rem'
  401. justifyItems = 'start'
  402. '> *' {
  403. margin = '0'
  404. }
  405. }
  406. #connectapp {
  407. display = 'grid'
  408. gap = '.75rem'
  409. maxWidth = '32rem'
  410. padding = '1rem'
  411. background = 'var(--color-surface)'
  412. borderRadius = 'var(--radius-large)'
  413. }
  414. #appshown {
  415. display = 'grid'
  416. gap = '.5rem'
  417. maxWidth = '100%'
  418. code {
  419. overflowWrap = 'anywhere'
  420. }
  421. }
  422. #applist {
  423. listStyle = 'none'
  424. margin = '0'
  425. padding = '0'
  426. display = 'grid'
  427. gap = '.25rem'
  428. maxWidth = '40rem'
  429. width = '100%'
  430. li {
  431. display = 'flex'
  432. flexWrap = 'wrap'
  433. alignItems = 'center'
  434. gap = '.5rem .75rem'
  435. minHeight = '2.75rem'
  436. padding = '.25rem .5rem .25rem 1rem'
  437. background = 'var(--color-surface)'
  438. borderRadius = 'var(--radius-large)'
  439. }
  440. }
  441. #required {
  442. display = 'grid'
  443. gap = '.75rem'
  444. margin = '0 0 1.5rem'
  445. justifyItems = 'start'
  446. '> *' {
  447. margin = '0'
  448. }
  449. }
  450. #requiredlist {
  451. listStyle = 'none'
  452. margin = '0'
  453. padding = '0'
  454. display = 'grid'
  455. gap = '.25rem'
  456. maxWidth = '40rem'
  457. width = '100%'
  458. li {
  459. display = 'flex'
  460. flexWrap = 'wrap'
  461. alignItems = 'center'
  462. justifyContent = 'space-between'
  463. gap = '.5rem .75rem'
  464. minHeight = '2.75rem'
  465. padding = '.25rem .5rem .25rem 1rem'
  466. background = 'var(--color-surface)'
  467. borderRadius = 'var(--radius-large)'
  468. }
  469. }
  470. #requiredform {
  471. display = 'grid'
  472. gap = '.75rem'
  473. maxWidth = '32rem'
  474. }
  475. #appform {
  476. display = 'grid'
  477. gap = '.75rem'
  478. maxWidth = '32rem'
  479. }
  480. #togglepriv {
  481. justifySelf = 'start'
  482. }
  483. #visibility {
  484. display = 'grid'
  485. gap = '.75rem'
  486. margin = '0 0 1.5rem'
  487. }
  488. }

Branches

Latest commits

  • 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
  • c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
  • e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
  • 26a140e3gitoria#44: reviews, inline threads, replies, conversation for apps (merge w44-reviews)mre
  • 888dbf87gitoria#44: unknown API paths answer the contract's JSON 404 and sit above the page routesmre
  • ea587d90gitoria#44: reviews with inline threads, replies, resolve and the PR conversation for apps (lib/reviews.hl, tests/reviews.mjs)mre
  • 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
  • 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
  • 15e01e94gitoria#43: tests/events.mjs and its probemre
  • 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre