kiosko-plugin-github 0.2.3 → 0.3.0

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.
package/README.md CHANGED
@@ -78,7 +78,7 @@ Uses: `github/activity`.
78
78
 
79
79
  ### GitHub activity — `github/activity`
80
80
 
81
- Actions runs of your repos, your open pull requests and the ones you are asked to review, through a token or the gh CLI. Read every 2 min while a widget uses it. Options are set once in Kiosko → Settings → Data sources.
81
+ Actions runs of your repos, your open pull requests and the ones you are asked to review, through a token or the gh CLI. Read every 2 min while a widget uses it. Options are set in Kiosko → Settings → Data sources.
82
82
 
83
83
  | Option | Type | Default | |
84
84
  |---|---|---|---|
@@ -91,6 +91,12 @@ Actions runs of your repos, your open pull requests and the ones you are asked t
91
91
  | `maxMentions` | number | `10` | Mentions read. Open issues and pull requests that mention you, at most. 0 = don't look for mentions. |
92
92
  | `bin` | string | `"gh"` | gh command |
93
93
 
94
+ ### Several accounts
95
+
96
+ With Kiosko 0.2.12 and later the source can have **profiles**: named configurations, each with its own options, say «Personal» with your own token, «Work» with the company's token and `query` set to `org:acme`, and «Enterprise» with a GitHub Enterprise Server host. A profile without a token asks gh for its host's login (`gh auth login --hostname github.example.com`); two accounts on the same host need a token each, since `gh auth token` gives the active one only.
97
+
98
+ Each profile is read on its own and keeps its own ETags, notifications count, last data and rate limit: one account being rate-limited doesn't stop the others. Each tile picks its profile in the inspector (**Source profile**; **New profile…** makes one from the tile), and the **Repositories** filter lists the repos of that profile. Settings → Data sources shows the profiles as tabs. A tile with no profile reads the source's own options, as before; with older Kiosko versions there is one account.
99
+
94
100
  ## Development
95
101
 
96
102
  ```sh
package/dist/index.js CHANGED
@@ -170,17 +170,17 @@ function parseRuns(json, repo) {
170
170
  updatedAt: r.updated_at ?? r.run_started_at ?? ""
171
171
  }));
172
172
  }
173
- var etags = /* @__PURE__ */ new Map();
174
- var notificationsNotBefore = 0;
175
- var lastNotifications = null;
176
- var rateLimitedUntil = 0;
177
- var lastGood;
173
+ var states = /* @__PURE__ */ new Map();
174
+ function stateOf(profile = "") {
175
+ let st = states.get(profile);
176
+ if (!st) {
177
+ st = { etags: /* @__PURE__ */ new Map(), notificationsNotBefore: 0, lastNotifications: null, rateLimitedUntil: 0, lastGood: void 0 };
178
+ states.set(profile, st);
179
+ }
180
+ return st;
181
+ }
178
182
  function resetState() {
179
- etags.clear();
180
- notificationsNotBefore = 0;
181
- lastNotifications = null;
182
- rateLimitedUntil = 0;
183
- lastGood = void 0;
183
+ states.clear();
184
184
  }
185
185
  function rateLimit(res, now) {
186
186
  if (res.status !== 403 && res.status !== 429) return null;
@@ -192,9 +192,9 @@ function rateLimit(res, now) {
192
192
  }
193
193
  return null;
194
194
  }
195
- async function getJson(deps, token, url, signal) {
195
+ async function getJson(deps, st, token, url, signal) {
196
196
  const key = `${token.slice(-8)} ${url}`;
197
- const cached = etags.get(key);
197
+ const cached = st.etags.get(key);
198
198
  const res = await deps.fetch(url, {
199
199
  headers: {
200
200
  Accept: "application/vnd.github+json",
@@ -212,7 +212,7 @@ async function getJson(deps, token, url, signal) {
212
212
  const link = res.headers.get("link");
213
213
  if (res.ok) {
214
214
  const etag = res.headers.get("etag");
215
- if (etag) etags.set(key, { etag, body, link });
215
+ if (etag) st.etags.set(key, { etag, body, link });
216
216
  }
217
217
  return { status: res.status, body, headers: res.headers, link };
218
218
  }
@@ -224,12 +224,12 @@ function nextLink(link) {
224
224
  return null;
225
225
  }
226
226
  var MAX_NOTIFICATIONS = 100;
227
- async function countNotifications(deps, token, rest, signal) {
227
+ async function countNotifications(deps, st, token, rest, signal) {
228
228
  let url = `${rest}/notifications?per_page=50`;
229
229
  let count = 0;
230
230
  let poll = 0;
231
231
  for (let page = 0; url && page * 50 < MAX_NOTIFICATIONS; page++) {
232
- const r = await getJson(deps, token, url, signal);
232
+ const r = await getJson(deps, st, token, url, signal);
233
233
  if (page === 0) poll = Number(r.headers.get("x-poll-interval"));
234
234
  if (r.status !== 200) return { count: page === 0 ? null : count, poll };
235
235
  count += JSON.parse(r.body).length;
@@ -246,9 +246,10 @@ async function resolveToken(c, deps, signal) {
246
246
  }
247
247
  }
248
248
  var empty = (state) => ({ state, me: "", runs: [], authored: [], toReview: [], mentions: [], notifications: null });
249
- async function readGitHub(c, signal, deps = defaultDeps) {
249
+ async function readGitHub(c, signal, deps = defaultDeps, profile) {
250
+ const st = stateOf(profile);
250
251
  const now = deps.now();
251
- if (now < rateLimitedUntil) return { ...lastGood ?? empty("ok"), rateLimitedUntil };
252
+ if (now < st.rateLimitedUntil) return { ...st.lastGood ?? empty("ok"), rateLimitedUntil: st.rateLimitedUntil };
252
253
  const token = await resolveToken(c, deps, signal);
253
254
  if (!token) return empty("unconfigured");
254
255
  const api = endpoints(c.host);
@@ -276,33 +277,33 @@ async function readGitHub(c, signal, deps = defaultDeps) {
276
277
  const problems = [];
277
278
  const perPage = Math.max(1, Math.min(30, Math.round(c.runsPerRepo) || 10));
278
279
  const runLists = await Promise.all((repos.length ? repos : viewer.recentRepos).map(async (repo) => {
279
- const r = await getJson(deps, token, `${api.rest}/repos/${repo}/actions/runs?per_page=${perPage}&exclude_pull_requests=true`, signal);
280
+ const r = await getJson(deps, st, token, `${api.rest}/repos/${repo}/actions/runs?per_page=${perPage}&exclude_pull_requests=true`, signal);
280
281
  if (r.status !== 200) {
281
282
  problems.push(`${repo}: HTTP ${r.status}`);
282
283
  return [];
283
284
  }
284
285
  return parseRuns(r.body, repo);
285
286
  }));
286
- if (now >= notificationsNotBefore) {
287
- const { count, poll } = await countNotifications(deps, token, api.rest, signal);
288
- notificationsNotBefore = now + (poll > 0 ? poll * 1e3 : 6e4);
289
- lastNotifications = count;
287
+ if (now >= st.notificationsNotBefore) {
288
+ const { count, poll } = await countNotifications(deps, st, token, api.rest, signal);
289
+ st.notificationsNotBefore = now + (poll > 0 ? poll * 1e3 : 6e4);
290
+ st.lastNotifications = count;
290
291
  }
291
- lastGood = {
292
+ st.lastGood = {
292
293
  state: "ok",
293
294
  me: viewer.me,
294
295
  runs: runLists.flat(),
295
296
  authored: viewer.authored,
296
297
  toReview: viewer.toReview,
297
298
  mentions: viewer.mentions,
298
- notifications: lastNotifications,
299
+ notifications: st.lastNotifications,
299
300
  ...problems.length ? { problems } : {}
300
301
  };
301
- return lastGood;
302
+ return st.lastGood;
302
303
  } catch (e) {
303
304
  if (e instanceof RateLimited) {
304
- rateLimitedUntil = e.until;
305
- return { ...lastGood ?? empty("ok"), rateLimitedUntil };
305
+ st.rateLimitedUntil = e.until;
306
+ return { ...st.lastGood ?? empty("ok"), rateLimitedUntil: st.rateLimitedUntil };
306
307
  }
307
308
  throw e;
308
309
  }
@@ -508,8 +509,11 @@ var activity = defineSource({
508
509
  // One GraphQL query and a REST call per repo each read: a floor that keeps far from the hourly API limit.
509
510
  minIntervalMs: 6e4,
510
511
  timeoutMs: 3e4,
511
- read: ({ options, signal }) => readGitHub(options, signal),
512
+ // Each profile's rate limit, ETags and last data apart: `ctx.profile` (Kiosko 0.2.12 on; undefined before).
513
+ read: (ctx) => readGitHub(ctx.options, ctx.signal, void 0, ctx.profile),
512
514
  demo: ({ now, locale }) => demoGitHub(now, locale),
515
+ // Several accounts (personal, work, an Enterprise host), each with its token: Kiosko 0.2.12 and later; older ones ignore it.
516
+ ...{ profiles: true },
513
517
  // Every repo it has seen (runs and pull requests), for the widgets' filter.
514
518
  choices: {
515
519
  repos: (d) => {
@@ -724,7 +728,7 @@ var inbox = defineWidget({
724
728
  var index_default = definePlugin({
725
729
  id: "github",
726
730
  name: { en: "GitHub", es: "GitHub", de: "GitHub", "zh-Hant": "GitHub", "zh-Hans": "GitHub" },
727
- version: "0.2.2",
731
+ version: "0.3.0",
728
732
  languages: ["en", "es", "de", "zh-Hant", "zh-Hans"],
729
733
  sdk: "^0.2.0",
730
734
  permissions: { network: ["api.github.com", "user"], exec: ["gh"] },
@@ -9,7 +9,7 @@
9
9
  "zh-Hant": "GitHub",
10
10
  "zh-Hans": "GitHub"
11
11
  },
12
- "version": "0.2.3",
12
+ "version": "0.3.0",
13
13
  "description": {
14
14
  "en": "Your Actions runs, the pull requests waiting for your review and your own.",
15
15
  "es": "Tus ejecuciones de Actions, las pull requests que te toca revisar y las tuyas.",
@@ -663,6 +663,7 @@
663
663
  },
664
664
  "polled": true,
665
665
  "intervalMs": 120000,
666
+ "profiles": true,
666
667
  "options": {
667
668
  "default": {
668
669
  "host": "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kiosko-plugin-github",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "GitHub cards: your Actions workflow runs, the pull requests waiting for your review and your own.",
5
5
  "type": "module",
6
6
  "license": "MIT",