@effected/github 0.28.0 → 0.30.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/GitCommit.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { GitHubClient } from "./GitHubClient.js";
2
+ import { GitHubError } from "./GitHubError.js";
2
3
  import { unstubbed } from "./internal/unstubbed.js";
3
4
  import { Repo } from "./Repo.js";
4
5
  import { Context, Effect, Layer, Schema } from "effect";
@@ -59,6 +60,22 @@ var CommitRef = class extends Schema.Class("CommitRef")({
59
60
  parents: Schema.Array(Schema.String)
60
61
  }) {};
61
62
  /**
63
+ * A commit just created through the Git Database API: its sha and the URL
64
+ * GitHub reports for viewing it.
65
+ *
66
+ * @remarks
67
+ * `htmlUrl` is the `html_url` of the creation reply, so it carries the right
68
+ * host on GitHub Enterprise Server with no host arithmetic.
69
+ *
70
+ * @public
71
+ */
72
+ var CreatedCommit = class extends Schema.Class("CreatedCommit")({
73
+ /** The new commit's sha. */
74
+ sha: Schema.String,
75
+ /** The commit's web URL, as GitHub reported it. */
76
+ htmlUrl: Schema.String
77
+ }) {};
78
+ /**
62
79
  * Read commits and build trees and commits through GitHub's Git Database API,
63
80
  * including a one-call "commit these files onto a branch".
64
81
  *
@@ -92,7 +109,9 @@ var GitCommit = class GitCommit extends Context.Service()("@effected/github/GitC
92
109
  get: overrides.get ?? (() => dies("get")),
93
110
  createTree: overrides.createTree ?? (() => dies("createTree")),
94
111
  createCommit: overrides.createCommit ?? (() => dies("createCommit")),
95
- commitFiles: overrides.commitFiles ?? (() => dies("commitFiles"))
112
+ createCommitWithUrl: overrides.createCommitWithUrl ?? (() => dies("createCommitWithUrl")),
113
+ commitFiles: overrides.commitFiles ?? (() => dies("commitFiles")),
114
+ commitFilesWithUrl: overrides.commitFilesWithUrl ?? (() => dies("commitFilesWithUrl"))
96
115
  });
97
116
  /** {@link GitCommit.makeTest} behind a `Layer`. */
98
117
  static layerTest = (overrides = {}) => Layer.succeed(GitCommit, GitCommit.makeTest(overrides));
@@ -143,59 +162,74 @@ const make = (client) => {
143
162
  ...options.baseTree !== void 0 ? { base_tree: options.baseTree } : {}
144
163
  })).sha;
145
164
  });
146
- const createCommit = Effect.fn("GitCommit.createCommit")(function* (options) {
165
+ const createRaw = Effect.fn("GitCommit.createCommit")(function* (options) {
147
166
  const { owner, repo } = yield* Repo;
148
167
  yield* Effect.annotateCurrentSpan({
149
168
  owner,
150
169
  repo,
151
170
  tree: options.tree
152
171
  });
153
- return (yield* client.request("POST /repos/{owner}/{repo}/git/commits", {
172
+ const commit = yield* client.request("POST /repos/{owner}/{repo}/git/commits", {
154
173
  owner,
155
174
  repo,
156
175
  message: options.message,
157
176
  tree: options.tree,
158
177
  parents: [...options.parents]
159
- })).sha;
178
+ });
179
+ return {
180
+ sha: commit.sha,
181
+ htmlUrl: commit.html_url
182
+ };
183
+ });
184
+ const commitFilesRaw = Effect.fn("GitCommit.commitFiles")(function* (options) {
185
+ const { owner, repo } = yield* Repo;
186
+ const short = options.branch.replace(/^refs\/heads\//, "").replace(/^heads\//, "");
187
+ yield* Effect.annotateCurrentSpan({
188
+ owner,
189
+ repo,
190
+ branch: short,
191
+ changes: options.changes.length
192
+ });
193
+ const ref = yield* client.request("GET /repos/{owner}/{repo}/git/ref/{ref}", {
194
+ owner,
195
+ repo,
196
+ ref: `heads/${short}`
197
+ });
198
+ const head = yield* get(ref.object.sha);
199
+ const tree = yield* createTree({
200
+ changes: options.changes,
201
+ baseTree: head.treeSha
202
+ });
203
+ const commit = yield* createRaw({
204
+ message: options.message,
205
+ tree,
206
+ parents: [head.sha]
207
+ });
208
+ yield* client.request("PATCH /repos/{owner}/{repo}/git/refs/{ref}", {
209
+ owner,
210
+ repo,
211
+ ref: `heads/${short}`,
212
+ sha: commit.sha,
213
+ force: false
214
+ });
215
+ return commit;
160
216
  });
217
+ /**
218
+ * The URL check, run after the work (and for `commitFiles`, the ref move) is
219
+ * done: a reply without `html_url` leaves a real commit, so the failure names it.
220
+ */
221
+ const withUrl = (operation, created) => typeof created.htmlUrl === "string" ? Effect.succeed(CreatedCommit.make({
222
+ sha: created.sha,
223
+ htmlUrl: created.htmlUrl
224
+ })) : Effect.fail(GitHubError.decode(operation, `the commit ${created.sha} was created, but GitHub's reply carried no html_url`));
161
225
  return {
162
226
  get,
163
227
  createTree,
164
- createCommit,
165
- commitFiles: Effect.fn("GitCommit.commitFiles")(function* (options) {
166
- const { owner, repo } = yield* Repo;
167
- const short = options.branch.replace(/^refs\/heads\//, "").replace(/^heads\//, "");
168
- yield* Effect.annotateCurrentSpan({
169
- owner,
170
- repo,
171
- branch: short,
172
- changes: options.changes.length
173
- });
174
- const ref = yield* client.request("GET /repos/{owner}/{repo}/git/ref/{ref}", {
175
- owner,
176
- repo,
177
- ref: `heads/${short}`
178
- });
179
- const head = yield* get(ref.object.sha);
180
- const tree = yield* createTree({
181
- changes: options.changes,
182
- baseTree: head.treeSha
183
- });
184
- const commit = yield* createCommit({
185
- message: options.message,
186
- tree,
187
- parents: [head.sha]
188
- });
189
- yield* client.request("PATCH /repos/{owner}/{repo}/git/refs/{ref}", {
190
- owner,
191
- repo,
192
- ref: `heads/${short}`,
193
- sha: commit,
194
- force: false
195
- });
196
- return commit;
197
- })
228
+ createCommit: (options) => Effect.map(createRaw(options), (commit) => commit.sha),
229
+ createCommitWithUrl: (options) => Effect.flatMap(createRaw(options), (commit) => withUrl("GitCommit.createCommitWithUrl", commit)),
230
+ commitFiles: (options) => Effect.map(commitFilesRaw(options), (commit) => commit.sha),
231
+ commitFilesWithUrl: (options) => Effect.flatMap(commitFilesRaw(options), (commit) => withUrl("GitCommit.commitFilesWithUrl", commit))
198
232
  };
199
233
  };
200
234
 
201
- export { CommitRef, FileChange, FileContent, FileDeletion, FileMode, GitCommit };
235
+ export { CommitRef, CreatedCommit, FileChange, FileContent, FileDeletion, FileMode, GitCommit };
package/GitHubClient.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { GitHubError } from "./GitHubError.js";
2
2
  import { GitHubGraphQLError } from "./GraphQL.js";
3
+ import { API_VERSION_PATTERN, DEFAULT_API_VERSION } from "./internal/apiVersion.js";
3
4
  import { rawRequestRefusal, refusedRequest } from "./internal/confinement.js";
4
- import { DEFAULT_API_VERSION, expandOnDotcom, makeTransport } from "./internal/octokit.js";
5
+ import { expandOnDotcom, makeTransport } from "./internal/octokit.js";
5
6
  import { fromArray, paginate } from "./internal/paginate.js";
6
7
  import { unstubbed } from "./internal/unstubbed.js";
7
8
  import { DOTCOM_API_BASE_URL, originOf, uploadsBaseUrlFor, withUploadChannel } from "./internal/uploads.js";
@@ -233,8 +234,6 @@ var GitHubClient = class GitHubClient extends Context.Service()("@effected/githu
233
234
  */
234
235
  static layerFixture = (fixtures) => Layer.succeed(GitHubClient, confineDouble(makeFixture(fixtures)));
235
236
  };
236
- /** A calendar api-version: `YYYY-MM-DD`. */
237
- const API_VERSION_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
238
237
  /**
239
238
  * The api-version a client pins: the option when it is a calendar version,
240
239
  * the default when it is absent, and a defect naming the option otherwise.