OXYGENHUB
# 77998d682d47 oxygenhub-vscode / src / hub.ts
6.7 KiB · last changed 0.1.0: sign in, hub client with generated types, remote provider, proposals and issues views, review threads on atoms, seal and land through oxy, checks status bar 1 month ago source blame history raw
1
// The hub client: typed fetch over /api/v1 (hub spec §12.1) using the
2
// generated types in hubapi.d.ts, plus URL helpers. No vscode imports,
3
// so it is tested with plain Node against a real hub.
4
5
import type * as api from "./hubapi";
6
7
export class HubError extends Error {
8
  constructor(
9
    readonly status: number,
10
    readonly title: string,
11
    readonly detail: string,
12
  ) {
13
    super(`${status} ${title}: ${detail}`);
14
  }
15
}
16
17
/** `https://hub/owner/name` → { base, owner, name }; undefined for anything else. */
18
export function parseRepoUrl(url: string): { base: string; owner: string; name: string } | undefined {
19
  const m = /^(https?:\/\/[^/]+)\/([A-Za-z0-9][A-Za-z0-9._-]*)\/([A-Za-z0-9][A-Za-z0-9._-]*)\/?$/.exec(url.trim());
20
  if (!m || m[2] === "api") {
21
    return undefined;
22
  }
23
  return { base: m[1], owner: m[2], name: m[3] };
24
}
25
26
export interface Session {
27
  base: string;
28
  token: string;
29
  username?: string;
30
}
31
32
export class HubClient {
33
  constructor(
34
    readonly base: string,
35
    private readonly token: string | undefined,
36
    private readonly fetchImpl: typeof fetch = fetch,
37
  ) {}
38
39
  private async request<T>(method: string, path: string, body?: unknown): Promise<T> {
40
    const headers: Record<string, string> = { accept: "application/json" };
41
    if (this.token) {
42
      headers.authorization = `Bearer ${this.token}`;
43
    }
44
    if (body !== undefined) {
45
      headers["content-type"] = "application/json";
46
    }
47
    const res = await this.fetchImpl(`${this.base}/api/v1${path}`, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) });
48
    if (res.status === 204) {
49
      return undefined as T;
50
    }
51
    const text = await res.text();
52
    if (!res.ok) {
53
      try {
54
        const p = JSON.parse(text) as api.Problem;
55
        throw new HubError(p.status, p.title, p.detail);
56
      } catch (e) {
57
        if (e instanceof HubError) {
58
          throw e;
59
        }
60
        throw new HubError(res.status, res.statusText, text);
61
      }
62
    }
63
    return (text ? JSON.parse(text) : undefined) as T;
64
  }
65
66
  me(): Promise<api.User> {
67
    return this.request("GET", "/user");
68
  }
69
70
  repositories(): Promise<api.Repository[]> {
71
    return this.request("GET", "/repos");
72
  }
73
74
  repository(owner: string, name: string): Promise<api.Repository> {
75
    return this.request("GET", `/repos/${owner}/${name}`);
76
  }
77
78
  proposals(owner: string, name: string): Promise<api.Proposal[]> {
79
    return this.request("GET", `/repos/${owner}/${name}/proposals`);
80
  }
81
82
  proposal(owner: string, name: string, number: number): Promise<api.Proposal> {
83
    return this.request("GET", `/repos/${owner}/${name}/proposals/${number}`);
84
  }
85
86
  createProposal(owner: string, name: string, p: api.NewProposal): Promise<api.Proposal> {
87
    return this.request("POST", `/repos/${owner}/${name}/proposals`, p);
88
  }
89
90
  proposalComments(owner: string, name: string, number: number): Promise<api.Comment[]> {
91
    return this.request("GET", `/repos/${owner}/${name}/proposals/${number}/comments`);
92
  }
93
94
  commentProposal(owner: string, name: string, number: number, body: string): Promise<api.Comment> {
95
    return this.request("POST", `/repos/${owner}/${name}/proposals/${number}/comments`, { body });
96
  }
97
98
  reviews(owner: string, name: string, number: number): Promise<api.Review[]> {
99
    return this.request("GET", `/repos/${owner}/${name}/proposals/${number}/reviews`);
100
  }
101
102
  review(owner: string, name: string, number: number, verdict: "comment" | "changes" | "approve", body = ""): Promise<api.Review> {
103
    return this.request("POST", `/repos/${owner}/${name}/proposals/${number}/reviews`, { verdict, body });
104
  }
105
106
  proposalChecks(owner: string, name: string, number: number): Promise<api.Check[]> {
107
    return this.request("GET", `/repos/${owner}/${name}/proposals/${number}/checks`);
108
  }
109
110
  issues(owner: string, name: string, state: "open" | "closed" | "all" = "open"): Promise<api.Issue[]> {
111
    return this.request("GET", `/repos/${owner}/${name}/issues?state=${state}`);
112
  }
113
114
  issue(owner: string, name: string, number: number): Promise<api.Issue> {
115
    return this.request("GET", `/repos/${owner}/${name}/issues/${number}`);
116
  }
117
118
  createIssue(owner: string, name: string, title: string, body = ""): Promise<api.Issue> {
119
    return this.request("POST", `/repos/${owner}/${name}/issues`, { title, body });
120
  }
121
122
  issueComments(owner: string, name: string, number: number): Promise<api.Comment[]> {
123
    return this.request("GET", `/repos/${owner}/${name}/issues/${number}/comments`);
124
  }
125
126
  commentIssue(owner: string, name: string, number: number, body: string): Promise<api.Comment> {
127
    return this.request("POST", `/repos/${owner}/${name}/issues/${number}/comments`, { body });
128
  }
129
130
  setIssueState(owner: string, name: string, number: number, state: "open" | "closed", reason = ""): Promise<api.Issue> {
131
    return this.request("POST", `/repos/${owner}/${name}/issues/${number}`, { state, reason });
132
  }
133
134
  history(owner: string, name: string, reference: string, limit = 50): Promise<api.History> {
135
    return this.request("GET", `/repos/${owner}/${name}/history/${encodeURIComponent(reference)}?limit=${limit}`);
136
  }
137
138
  bookmarks(owner: string, name: string): Promise<api.Bookmark[]> {
139
    return this.request("GET", `/repos/${owner}/${name}/bookmarks`);
140
  }
141
142
  search(q: string): Promise<{ kind: string; repository: string; number: number; title: string }[]> {
143
    return this.request("GET", `/search?q=${encodeURIComponent(q)}`);
144
  }
145
}
146
147
/** Web URLs on the hub for things the remote provider is asked about. */
148
export function hubUrl(base: string, owner: string, name: string, target: { kind: string; [k: string]: unknown }): string {
149
  const repo = `${base}/${owner}/${name}`;
150
  switch (target.kind) {
151
    case "repository":
152
      return repo;
153
    case "bookmark":
154
      return `${repo}/tree/${encodeURIComponent(String(target.name))}`;
155
    case "change":
156
      return `${repo}/change/${target.hash}`;
157
    case "file": {
158
      const line = typeof target.line === "number" ? `#L${target.line}` : "";
159
      return `${repo}/file/${encodeURIComponent(String(target.reference))}/${String(target.path)}${line}`;
160
    }
161
    case "proposals":
162
      return `${repo}/proposals`;
163
    case "issues":
164
      return `${repo}/issues`;
165
    case "proposal":
166
      return `${repo}/proposal/${target.number}`;
167
    case "issue":
168
      return `${repo}/issue/${target.number}`;
169
    default:
170
      return repo;
171
  }
172
}
173
174
/** Which of the proposals need the signed-in user: not theirs, and not yet sealed by them. */
175
export function needsMe(p: import("./hubapi").Proposal, username: string | undefined): boolean {
176
  if (!username || p.author === username) {
177
    return false;
178
  }
179
  if (p.state !== "open") {
180
    return false;
181
  }
182
  const mine = p.approvals.find((a) => a.account === username);
183
  return !mine || !mine.sealed;
184
}