pisha-server 0.0.0-stage → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -2
  3. package/accounts/accounts.js +754 -0
  4. package/accounts/index.js +1 -0
  5. package/accounts/passwords.js +41 -0
  6. package/accounts/totp.js +54 -0
  7. package/boards/boards.js +1079 -0
  8. package/boards/github.js +212 -0
  9. package/boards/index.js +1 -0
  10. package/boards/sync.js +980 -0
  11. package/companies.js +294 -0
  12. package/company-export.js +101 -0
  13. package/company.js +79 -0
  14. package/dashboard/dashboard.js +407 -0
  15. package/dashboard/index.js +1 -0
  16. package/data-upgrade.js +69 -0
  17. package/install/linux.sh +181 -0
  18. package/install/macos.sh +121 -0
  19. package/install/windows.bat +54 -0
  20. package/machines/index.js +3 -0
  21. package/machines/machines.js +910 -0
  22. package/messaging/inbox.js +252 -0
  23. package/messaging/index.js +3 -0
  24. package/messaging/messaging.js +551 -0
  25. package/messaging/plans.js +67 -0
  26. package/observation/index.js +1 -0
  27. package/observation/observation.js +224 -0
  28. package/organization/index.js +1 -0
  29. package/organization/organization.js +218 -0
  30. package/package.json +54 -4
  31. package/permissions/index.js +1 -0
  32. package/permissions/permissions.js +211 -0
  33. package/pisha-host.mjs +3297 -0
  34. package/pisha-host.mjs.sig +1 -0
  35. package/platform/acme.js +207 -0
  36. package/platform/basics.js +14 -0
  37. package/platform/config.js +154 -0
  38. package/platform/control.js +93 -0
  39. package/platform/credentials.js +151 -0
  40. package/platform/database.js +251 -0
  41. package/platform/errors.js +13 -0
  42. package/platform/files.js +50 -0
  43. package/platform/http.js +207 -0
  44. package/platform/index.js +58 -0
  45. package/platform/limits.js +122 -0
  46. package/platform/listen.js +183 -0
  47. package/platform/roles.js +6 -0
  48. package/platform/stream.js +19 -0
  49. package/platform/text.js +83 -0
  50. package/platform/ws.js +186 -0
  51. package/routes.js +405 -0
  52. package/server.js +65 -0
  53. package/sessions/index.js +1 -0
  54. package/sessions/sessions.js +533 -0
  55. package/ui/LICENSE-affectui.txt +26 -0
  56. package/ui/addmachine.js +151 -0
  57. package/ui/api.js +20 -0
  58. package/ui/app.js +143 -0
  59. package/ui/attention.js +78 -0
  60. package/ui/back.js +166 -0
  61. package/ui/boards.js +958 -0
  62. package/ui/chat.js +388 -0
  63. package/ui/content.js +157 -0
  64. package/ui/dialogs.js +135 -0
  65. package/ui/doing.js +112 -0
  66. package/ui/dom.js +6 -0
  67. package/ui/ds-base.css +146 -0
  68. package/ui/ds-components.css +229 -0
  69. package/ui/fonts/LICENSE-space-grotesk.txt +93 -0
  70. package/ui/fonts/space-grotesk-latin-700-normal.woff2 +0 -0
  71. package/ui/fonts/space-grotesk-latin-ext-700-normal.woff2 +0 -0
  72. package/ui/format.js +25 -0
  73. package/ui/i18n.js +2093 -0
  74. package/ui/icons/apple-touch-icon.png +0 -0
  75. package/ui/icons/icon-192.png +0 -0
  76. package/ui/icons/icon-512.png +0 -0
  77. package/ui/icons/maskable-512.png +0 -0
  78. package/ui/icons.js +40 -0
  79. package/ui/inbox.js +535 -0
  80. package/ui/index.html +103 -0
  81. package/ui/machines.js +261 -0
  82. package/ui/manifest.webmanifest +16 -0
  83. package/ui/mosaic.js +189 -0
  84. package/ui/nav.js +141 -0
  85. package/ui/newteam.js +223 -0
  86. package/ui/org.js +653 -0
  87. package/ui/overlays.css +126 -0
  88. package/ui/palette.js +179 -0
  89. package/ui/people.js +370 -0
  90. package/ui/permissions.js +202 -0
  91. package/ui/qr.js +158 -0
  92. package/ui/router.js +171 -0
  93. package/ui/routes.js +46 -0
  94. package/ui/screen-boards.css +241 -0
  95. package/ui/screen-inbox.css +201 -0
  96. package/ui/screen-machines.css +76 -0
  97. package/ui/screen-mosaic.css +26 -0
  98. package/ui/screen-newteam.css +90 -0
  99. package/ui/screen-org.css +113 -0
  100. package/ui/screen-session.css +99 -0
  101. package/ui/screen-signin.css +28 -0
  102. package/ui/screen-team.css +80 -0
  103. package/ui/screen-teams.css +41 -0
  104. package/ui/session.js +42 -0
  105. package/ui/shell.css +122 -0
  106. package/ui/signin.js +120 -0
  107. package/ui/squads.js +370 -0
  108. package/ui/state.js +59 -0
  109. package/ui/stream.js +268 -0
  110. package/ui/styles.css +343 -0
  111. package/ui/team.js +115 -0
  112. package/ui/teams.js +58 -0
  113. package/ui/terminal.js +261 -0
  114. package/ui/theme.js +32 -0
  115. package/ui/timeline.js +163 -0
  116. package/ui/topbar.js +205 -0
  117. package/ui/voice-parse.js +98 -0
  118. package/ui/voice.js +320 -0
  119. package/vendor/InterVariable.woff2 +0 -0
  120. package/vendor/LICENSE-inter +92 -0
  121. package/vendor/LICENSE-xterm +21 -0
  122. package/vendor/README.md +8 -0
  123. package/vendor/addon-fit.js +2 -0
  124. package/vendor/xterm.css +218 -0
  125. package/vendor/xterm.js +2 -0
@@ -0,0 +1,212 @@
1
+ // GitHub's GraphQL API, as the boards use it: a project (Projects v2) with its
2
+ // Status, Agent and Type fields, its items and their issues, sub-issues and
3
+ // comments; and the repository's pull requests. Only what the sync needs; every call is named, so a test can
4
+ // answer it (PISHA_GITHUB_API points elsewhere).
5
+
6
+ const API = () => process.env.PISHA_GITHUB_API || 'https://api.github.com/graphql'
7
+
8
+ export class GithubError extends Error {
9
+ constructor(message, { status = 0, auth = false, notFound = false, transient = false } = {}) { super(message); Object.assign(this, { status, auth, notFound, transient }) }
10
+ }
11
+
12
+ // "https://github.com/orgs/acme/projects/3", "github.com/users/ana/projects/1/views/2"
13
+ export function parseProjectUrl(url) {
14
+ const m = /^(?:https?:\/\/)?github\.com\/(orgs|users)\/([A-Za-z0-9-]{1,39})\/projects\/(\d{1,9})(?:[/?#].*)?$/.exec(String(url ?? '').trim())
15
+ return m ? { ownerKind: m[1] === 'orgs' ? 'org' : 'user', owner: m[2], number: Number(m[3]) } : null
16
+ }
17
+ export function parseRepo(text) {
18
+ const m = /^(?:https?:\/\/github\.com\/)?([A-Za-z0-9-]{1,39})\/([A-Za-z0-9._-]{1,100})(?:\.git)?\/?$/.exec(String(text ?? '').trim())
19
+ return m ? { owner: m[1], name: m[2] } : null
20
+ }
21
+
22
+ const FIELDS = `fields(first: 50) { nodes {
23
+ ... on ProjectV2FieldCommon { id name dataType }
24
+ ... on ProjectV2SingleSelectField { options { id name color } } } }`
25
+
26
+ // (The whole board is read without comments: a task's come when it is opened.)
27
+ const ISSUE = `id number title body state stateReason updatedAt url
28
+ repository { nameWithOwner }
29
+ labels(first: 30) { nodes { id name } }
30
+ parent { id }
31
+ assignees(first: 10) { nodes { id login } }`
32
+ const ASSIGNEES = 'assignees(first: 10) { nodes { id login } }'
33
+ const logins = issue => (issue?.assignees?.nodes ?? []).filter(Boolean).map(n => n.login)
34
+ const COMMENTS = 'comments(last: 50) { totalCount nodes { id body createdAt author { login } } }'
35
+
36
+ const ITEM = (withComments = false) => `id isArchived updatedAt type
37
+ fieldValues(first: 30) { nodes { ... on ProjectV2ItemFieldSingleSelectValue { optionId field { ... on ProjectV2FieldCommon { id } } } } }
38
+ content { __typename ... on Issue { ${ISSUE} ${withComments ? COMMENTS : ''} } ... on DraftIssue { id title body updatedAt } }`
39
+
40
+ // (What links one to a task: its title, branch and text, and the issues it closes.)
41
+ const PR = `id number title url state isDraft merged mergedAt updatedAt headRefName body reviewDecision
42
+ author { login }
43
+ closingIssuesReferences(first: 10) { nodes { id } }`
44
+
45
+ const ITEMS = `items(first: 100, after: $after) { pageInfo { hasNextPage endCursor } nodes { ${ITEM()} } }`
46
+
47
+ const PROJECT = kind => `query Project($owner: String!, $number: Int!) {
48
+ ${kind === 'org' ? 'organization' : 'user'}(login: $owner) { projectV2(number: $number) { id title url ${FIELDS} } } }`
49
+
50
+ export function githubClient(token) {
51
+ async function gql(name, query, variables = {}) {
52
+ let r
53
+ try {
54
+ r = await fetch(API(), {
55
+ method: 'POST',
56
+ headers: { Authorization: `bearer ${token}`, 'Content-Type': 'application/json', 'User-Agent': 'Pisha', 'GraphQL-Features': 'sub_issues' },
57
+ body: JSON.stringify({ operationName: name, query, variables }),
58
+ signal: AbortSignal.timeout(30_000),
59
+ })
60
+ } catch (err) { throw new GithubError(`GitHub could not be reached: ${err.message}`, { transient: true }) }
61
+ if (r.status === 401) throw new GithubError('GitHub refused the token (401): it is wrong, expired or revoked', { status: 401, auth: true })
62
+ const j = await r.json().catch(() => null)
63
+ if (r.status === 403 || r.status === 429) throw new GithubError(`GitHub said no (${r.status}): ${j?.message ?? 'rate limited or not allowed'}`, { status: r.status, auth: r.status === 403 && r.headers.get('x-ratelimit-remaining') !== '0', transient: r.status === 429 || r.headers.get('x-ratelimit-remaining') === '0' })
64
+ if (!r.ok || !j) throw new GithubError(`GitHub answered ${r.status}`, { status: r.status, transient: r.status >= 500 })
65
+ if (j.errors?.length) {
66
+ const msg = j.errors.map(e => e.message).join('; ')
67
+ throw new GithubError(`GitHub: ${msg}`, { auth: j.errors.some(e => ['FORBIDDEN', 'INSUFFICIENT_SCOPES'].includes(e.type)), notFound: j.errors.every(e => e.type === 'NOT_FOUND') })
68
+ }
69
+ return j.data
70
+ }
71
+
72
+ return {
73
+ async project({ ownerKind, owner, number }) {
74
+ const d = await gql('Project', PROJECT(ownerKind), { owner, number })
75
+ const p = (ownerKind === 'org' ? d.organization : d.user)?.projectV2
76
+ if (!p) throw new GithubError(`no project ${number} of ${owner} that this token can see`)
77
+ return { id: p.id, title: p.title, url: p.url, fields: p.fields.nodes.filter(f => f?.id) }
78
+ },
79
+ async repository({ owner, name }) {
80
+ const d = await gql('Repository', `query Repository($owner: String!, $name: String!) {
81
+ repository(owner: $owner, name: $name) { id nameWithOwner labels(first: 100) { nodes { id name } } } }`, { owner, name })
82
+ if (!d.repository) throw new GithubError(`no repository ${owner}/${name} that this token can see`)
83
+ return { id: d.repository.id, name: d.repository.nameWithOwner, labels: d.repository.labels.nodes }
84
+ },
85
+ // Every item of the project, page by page.
86
+ async items(projectId) {
87
+ const out = []
88
+ let after = null
89
+ for (let page = 0; page < 60; page++) {
90
+ const d = await gql('Items', `query Items($id: ID!, $after: String) { node(id: $id) { ... on ProjectV2 { ${ITEMS} } } }`, { id: projectId, after })
91
+ const items = d.node?.items
92
+ if (!items) throw new GithubError('the project is gone, or this token no longer sees it')
93
+ out.push(...items.nodes.filter(Boolean))
94
+ if (!items.pageInfo.hasNextPage) return out
95
+ after = items.pageInfo.endCursor
96
+ }
97
+ return out
98
+ },
99
+ // One item, with its issue's comments; null if it is no longer in the project.
100
+ async item(itemId) {
101
+ try {
102
+ const d = await gql('Item', `query Item($id: ID!) { node(id: $id) { ... on ProjectV2Item { ${ITEM(true)} } } }`, { id: itemId })
103
+ return d.node?.id ? d.node : null
104
+ } catch (err) { if (err.notFound) return null; throw err }
105
+ },
106
+ async createField(projectId, name, options) {
107
+ const d = await gql('CreateField', `mutation CreateField($input: CreateProjectV2FieldInput!) {
108
+ createProjectV2Field(input: $input) { projectV2Field { ... on ProjectV2SingleSelectField { id name options { id name color } } } } }`,
109
+ { input: { projectId, dataType: 'SINGLE_SELECT', name, singleSelectOptions: options } })
110
+ return d.createProjectV2Field.projectV2Field
111
+ },
112
+ // (GitHub replaces the options, with new ids: what the items had is put back by the caller.)
113
+ async setOptions(fieldId, options) {
114
+ const d = await gql('SetOptions', `mutation SetOptions($input: UpdateProjectV2FieldInput!) {
115
+ updateProjectV2Field(input: $input) { projectV2Field { ... on ProjectV2SingleSelectField { id name options { id name color } } } } }`,
116
+ { input: { fieldId, singleSelectOptions: options } })
117
+ return d.updateProjectV2Field.projectV2Field
118
+ },
119
+ async setSelect(projectId, itemId, fieldId, optionId) {
120
+ if (optionId) {
121
+ await gql('SetSelect', `mutation SetSelect($input: UpdateProjectV2ItemFieldValueInput!) { updateProjectV2ItemFieldValue(input: $input) { projectV2Item { id } } }`,
122
+ { input: { projectId, itemId, fieldId, value: { singleSelectOptionId: optionId } } })
123
+ } else {
124
+ await gql('ClearSelect', `mutation ClearSelect($input: ClearProjectV2ItemFieldValueInput!) { clearProjectV2ItemFieldValue(input: $input) { projectV2Item { id } } }`,
125
+ { input: { projectId, itemId, fieldId } })
126
+ }
127
+ },
128
+ async createIssue({ repositoryId, title, body, labelIds, parentIssueId, assigneeIds = [] }) {
129
+ const d = await gql('CreateIssue', `mutation CreateIssue($input: CreateIssueInput!) { createIssue(input: $input) { issue { id number url updatedAt ${ASSIGNEES} } } }`,
130
+ { input: { repositoryId, title, body, labelIds, ...(parentIssueId ? { parentIssueId } : {}), ...(assigneeIds.length ? { assigneeIds } : {}) } })
131
+ return { ...d.createIssue.issue, assignees: logins(d.createIssue.issue) }
132
+ },
133
+ // A GitHub user's id, by their login (null: no such user).
134
+ async userId(login) {
135
+ const d = await gql('UserId', 'query UserId($login: String!) { user(login: $login) { id } }', { login })
136
+ return d.user?.id ?? null
137
+ },
138
+ // (Both answer who the issue has afterwards: GitHub leaves out, saying nothing, whom it can't assign.)
139
+ async addAssignees(assignableId, assigneeIds) {
140
+ const d = await gql('AddAssignees', `mutation AddAssignees($input: AddAssigneesToAssignableInput!) { addAssigneesToAssignable(input: $input) { assignable { ... on Issue { ${ASSIGNEES} } } } }`, { input: { assignableId, assigneeIds } })
141
+ return logins(d.addAssigneesToAssignable.assignable)
142
+ },
143
+ async removeAssignees(assignableId, assigneeIds) {
144
+ const d = await gql('RemoveAssignees', `mutation RemoveAssignees($input: RemoveAssigneesFromAssignableInput!) { removeAssigneesFromAssignable(input: $input) { assignable { ... on Issue { ${ASSIGNEES} } } } }`, { input: { assignableId, assigneeIds } })
145
+ return logins(d.removeAssigneesFromAssignable.assignable)
146
+ },
147
+ // (Already in it, as GitHub puts a sub-issue in its parent's project by itself: the item it has.)
148
+ async addItem(projectId, contentId) {
149
+ try {
150
+ const d = await gql('AddItem', `mutation AddItem($input: AddProjectV2ItemByIdInput!) { addProjectV2ItemById(input: $input) { item { id } } }`, { input: { projectId, contentId } })
151
+ return d.addProjectV2ItemById.item.id
152
+ } catch (err) {
153
+ if (!/already exists/i.test(err.message)) throw err
154
+ const d = await gql('ItemOf', `query ItemOf($id: ID!) { node(id: $id) { ... on Issue { projectItems(first: 50) { nodes { id project { id } } } } } }`, { id: contentId })
155
+ const item = d.node?.projectItems?.nodes?.find(n => n?.project?.id === projectId)
156
+ if (!item) throw err
157
+ return item.id
158
+ }
159
+ },
160
+ async removeItem(projectId, itemId) {
161
+ await gql('RemoveItem', `mutation RemoveItem($input: DeleteProjectV2ItemInput!) { deleteProjectV2Item(input: $input) { deletedItemId } }`, { input: { projectId, itemId } })
162
+ },
163
+ async updateIssue(id, patch) {
164
+ await gql('UpdateIssue', `mutation UpdateIssue($input: UpdateIssueInput!) { updateIssue(input: $input) { issue { id } } }`, { input: { id, ...patch } })
165
+ },
166
+ async updateDraft(draftIssueId, patch) {
167
+ await gql('UpdateDraft', `mutation UpdateDraft($input: UpdateProjectV2DraftIssueInput!) { updateProjectV2DraftIssue(input: $input) { draftIssue { id } } }`, { input: { draftIssueId, ...patch } })
168
+ },
169
+ async closeIssue(issueId, stateReason) {
170
+ await gql('CloseIssue', `mutation CloseIssue($input: CloseIssueInput!) { closeIssue(input: $input) { issue { id } } }`, { input: { issueId, stateReason } })
171
+ },
172
+ async reopenIssue(issueId) {
173
+ await gql('ReopenIssue', `mutation ReopenIssue($input: ReopenIssueInput!) { reopenIssue(input: $input) { issue { id } } }`, { input: { issueId } })
174
+ },
175
+ async setParent(issueId, subIssueId) {
176
+ await gql('AddSubIssue', `mutation AddSubIssue($input: AddSubIssueInput!) { addSubIssue(input: $input) { issue { id } } }`, { input: { issueId, subIssueId, replaceParent: true } })
177
+ },
178
+ async removeParent(issueId, subIssueId) {
179
+ await gql('RemoveSubIssue', `mutation RemoveSubIssue($input: RemoveSubIssueInput!) { removeSubIssue(input: $input) { issue { id } } }`, { input: { issueId, subIssueId } })
180
+ },
181
+ async comment(subjectId, body) {
182
+ const d = await gql('AddComment', `mutation AddComment($input: AddCommentInput!) { addComment(input: $input) { commentEdge { node { id } } } }`, { input: { subjectId, body } })
183
+ return d.addComment.commentEdge.node.id
184
+ },
185
+ // The repository's pull requests, the latest changed first: those changed
186
+ // since then (an ISO time), or the latest `first` ones.
187
+ async pullRequests({ owner, name }, since = null, first = 100) {
188
+ const out = []
189
+ let after = null
190
+ for (let page = 0; page < 10; page++) {
191
+ const d = await gql('PullRequests', `query PullRequests($owner: String!, $name: String!, $after: String) {
192
+ repository(owner: $owner, name: $name) { pullRequests(first: 25, after: $after, orderBy: { field: UPDATED_AT, direction: DESC }) {
193
+ pageInfo { hasNextPage endCursor } nodes { ${PR} } } } }`, { owner, name, after })
194
+ if (!d.repository) throw new GithubError(`no repository ${owner}/${name} that this token can see`)
195
+ const prs = d.repository.pullRequests
196
+ for (const pr of prs.nodes) {
197
+ if (!pr) continue
198
+ if (since && Date.parse(pr.updatedAt) < Date.parse(since)) return out
199
+ out.push(pr)
200
+ if (!since && out.length >= first) return out
201
+ }
202
+ if (!prs.pageInfo.hasNextPage) return out
203
+ after = prs.pageInfo.endCursor
204
+ }
205
+ return out
206
+ },
207
+ async createLabel(repositoryId, name, color) {
208
+ const d = await gql('CreateLabel', `mutation CreateLabel($input: CreateLabelInput!) { createLabel(input: $input) { label { id name } } }`, { input: { repositoryId, name, color } })
209
+ return d.createLabel.label
210
+ },
211
+ }
212
+ }
@@ -0,0 +1 @@
1
+ export { createBoards, BOARD_SENDER, CATEGORIES, COLORS } from './boards.js'