@noldova/git.hosting.protocol 0.0.0-stage → 0.0.1

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.
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { JsonReader } from "@noldova/context.json";
9
+ import { SignInFailure } from "../enums/sign-in-failure.js";
10
+ import { SignInState } from "../enums/sign-in-state.js";
11
+ import { Resources } from "../resources.js";
12
+ import { DeviceCode } from "./device-code.js";
13
+ import { FieldCheck } from "./field-check.js";
14
+ import { HostAccount } from "./host-account.js";
15
+ export class HostSignIn {
16
+ static FIELDS = [Resources.stateField, Resources.deviceCodeField, Resources.accountField, Resources.failureField];
17
+ static STATES = Object.values(SignInState);
18
+ static FAILURES = Object.values(SignInFailure);
19
+ state;
20
+ deviceCode;
21
+ account;
22
+ failure;
23
+ constructor(state, deviceCode, account, failure, reader) {
24
+ if ((state === SignInState.Waiting) !== (deviceCode !== null))
25
+ FieldCheck.fail(Resources.deviceCodeField, Resources.deviceCodeExpected, reader);
26
+ if ((state === SignInState.SignedIn) !== (account !== null))
27
+ FieldCheck.fail(Resources.accountField, Resources.accountExpected, reader);
28
+ if (state !== SignInState.SignedOut && failure !== null)
29
+ FieldCheck.fail(Resources.failureField, Resources.failureExpected, reader);
30
+ this.state = state;
31
+ this.deviceCode = deviceCode;
32
+ this.account = account;
33
+ this.failure = failure;
34
+ }
35
+ static signedOut(failure) {
36
+ return new HostSignIn(SignInState.SignedOut, null, null, failure ?? null);
37
+ }
38
+ static waiting(deviceCode) {
39
+ return new HostSignIn(SignInState.Waiting, deviceCode, null, null);
40
+ }
41
+ static signedIn(account) {
42
+ return new HostSignIn(SignInState.SignedIn, null, account, null);
43
+ }
44
+ static fromJson(value, path) {
45
+ const reader = JsonReader.fromValue(value, path);
46
+ FieldCheck.refuseUnknownFields(reader, HostSignIn.FIELDS);
47
+ return new HostSignIn(reader.readOneOf(Resources.stateField, HostSignIn.STATES), FieldCheck.readNullableModel(reader, Resources.deviceCodeField, DeviceCode.fromJson), FieldCheck.readNullableModel(reader, Resources.accountField, HostAccount.fromJson), FieldCheck.readNullableMember(reader, Resources.failureField, HostSignIn.FAILURES), reader);
48
+ }
49
+ toJson() {
50
+ return {
51
+ [Resources.stateField]: this.state,
52
+ [Resources.deviceCodeField]: this.deviceCode?.toJson() ?? null,
53
+ [Resources.accountField]: this.account?.toJson() ?? null,
54
+ [Resources.failureField]: this.failure
55
+ };
56
+ }
57
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { Resources } from "../resources.js";
9
+ export class HostingNames {
10
+ static readSignInMethod = "readSignIn";
11
+ static startSignInMethod = "startSignIn";
12
+ static cancelSignInMethod = "cancelSignIn";
13
+ static signOutMethod = "signOut";
14
+ static signInChangedEvent = "signInChanged";
15
+ static readReferenceMethod = "readReference";
16
+ static readBranchPullRequestMethod = "readBranchPullRequest";
17
+ static createPullRequestMethod = "createPullRequest";
18
+ static nameOf(moduleId, member) {
19
+ return Resources.formatMemberName(moduleId, member);
20
+ }
21
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import { JsonReader } from "@noldova/context.json";
9
+ import { Resources } from "../resources.js";
10
+ import { FieldCheck } from "./field-check.js";
11
+ export class PullRequestDraft {
12
+ static FIELDS = [Resources.hostField, Resources.pathField, Resources.headBranchField, Resources.baseBranchField,
13
+ Resources.titleField, Resources.bodyField, Resources.isDraftField];
14
+ host;
15
+ path;
16
+ headBranch;
17
+ baseBranch;
18
+ title;
19
+ body;
20
+ isDraft;
21
+ constructor(host, path, headBranch, baseBranch, title, body, isDraft, reader) {
22
+ FieldCheck.requireText(host, Resources.hostField, reader);
23
+ FieldCheck.requirePath(path, Resources.pathField, reader);
24
+ FieldCheck.requireText(headBranch, Resources.headBranchField, reader);
25
+ FieldCheck.requireText(baseBranch, Resources.baseBranchField, reader);
26
+ FieldCheck.requireText(title, Resources.titleField, reader);
27
+ if (headBranch === baseBranch)
28
+ FieldCheck.fail(Resources.baseBranchField, Resources.otherBranchExpected, reader);
29
+ this.host = host;
30
+ this.path = path;
31
+ this.headBranch = headBranch;
32
+ this.baseBranch = baseBranch;
33
+ this.title = title;
34
+ this.body = body;
35
+ this.isDraft = isDraft;
36
+ }
37
+ static fromJson(value, path) {
38
+ const reader = JsonReader.fromValue(value, path);
39
+ FieldCheck.refuseUnknownFields(reader, PullRequestDraft.FIELDS);
40
+ return new PullRequestDraft(reader.readString(Resources.hostField), reader.readString(Resources.pathField), reader.readString(Resources.headBranchField), reader.readString(Resources.baseBranchField), reader.readString(Resources.titleField), reader.readString(Resources.bodyField), reader.readBoolean(Resources.isDraftField), reader);
41
+ }
42
+ toJson() {
43
+ return {
44
+ [Resources.hostField]: this.host,
45
+ [Resources.pathField]: this.path,
46
+ [Resources.headBranchField]: this.headBranch,
47
+ [Resources.baseBranchField]: this.baseBranch,
48
+ [Resources.titleField]: this.title,
49
+ [Resources.bodyField]: this.body,
50
+ [Resources.isDraftField]: this.isDraft
51
+ };
52
+ }
53
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ export class Resources {
9
+ static unknownField = "none, since the model doesn't define it";
10
+ static nonBlankTextExpected = "a text that isn't blank";
11
+ static httpsAddressExpected = "an https address with no user name or password";
12
+ static positiveExpected = "a whole number above zero";
13
+ static countExpected = "a whole number, zero or more";
14
+ static nameExpected = "a name of letters, digits, '.', '_' and '-', other than '.' and '..'";
15
+ static pathExpected = "names of letters, digits, '.', '_' and '-' joined by '/', none of them '.' or '..'";
16
+ static namePattern = /^(?!\.\.?$)[A-Za-z0-9._-]+$/;
17
+ static pathPattern = /^(?!\.\.?(?:\/|$))[A-Za-z0-9._-]+(?:\/(?!\.\.?(?:\/|$))[A-Za-z0-9._-]+)*$/;
18
+ static commitExpected = "a full commit hash, 40 or 64 lowercase hexadecimal digits";
19
+ static pullRequestStateExpected = "Open, Draft, Merged or Closed, the states of a pull request";
20
+ static issueStateExpected = "Open, ClosedDone or ClosedNotPlanned, the states of an issue";
21
+ static changesExpected = "changes exactly for a pull request";
22
+ static pullRequestExpected = "a pull request";
23
+ static checksExpected = "no checks, since there is no pull request";
24
+ static otherBranchExpected = "a branch other than the head branch";
25
+ static commitPattern = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
26
+ static avatarExpected = "a PNG, JPEG, GIF or WebP image of at most 64 KiB as a base64 data address";
27
+ static avatarPattern = /^data:image\/(?:png|jpeg|gif|webp);base64,((?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?)$/;
28
+ static maximumAvatarLength = 87_384;
29
+ static closedAtExpected = "a time exactly when the item is merged or closed";
30
+ static deviceCodeExpected = "a device code exactly while the sign-in waits for the person";
31
+ static accountExpected = "an account exactly while signed in";
32
+ static failureExpected = "null, since only a sign-out has a failure";
33
+ static stateField = "state";
34
+ static deviceCodeField = "deviceCode";
35
+ static accountField = "account";
36
+ static failureField = "failure";
37
+ static userCodeField = "userCode";
38
+ static verificationUriField = "verificationUri";
39
+ static expiresAtField = "expiresAt";
40
+ static loginField = "login";
41
+ static nameField = "name";
42
+ static avatarField = "avatar";
43
+ static scopesField = "scopes";
44
+ static httpsProtocol = "https:";
45
+ static hostField = "host";
46
+ static ownerField = "owner";
47
+ static repositoryField = "repository";
48
+ static numberField = "number";
49
+ static referenceField = "reference";
50
+ static kindField = "kind";
51
+ static titleField = "title";
52
+ static urlField = "url";
53
+ static authorField = "author";
54
+ static createdAtField = "createdAt";
55
+ static closedAtField = "closedAt";
56
+ static commentsField = "comments";
57
+ static changesField = "changes";
58
+ static additionsField = "additions";
59
+ static deletionsField = "deletions";
60
+ static changedFilesField = "changedFiles";
61
+ static headBranchField = "headBranch";
62
+ static baseBranchField = "baseBranch";
63
+ static headCommitField = "headCommit";
64
+ static pathField = "path";
65
+ static branchField = "branch";
66
+ static pullRequestField = "pullRequest";
67
+ static checksField = "checks";
68
+ static bodyField = "body";
69
+ static isDraftField = "isDraft";
70
+ static formatFieldPath(parent, field) {
71
+ return `${parent}.${field}`;
72
+ }
73
+ static formatInvalidField(field, expectation) {
74
+ return `The field ${field} is invalid: expected ${expectation}.`;
75
+ }
76
+ static formatInvalidArgument(parameterName, expectation) {
77
+ return `The argument ${parameterName} is invalid: expected ${expectation}.`;
78
+ }
79
+ static formatMemberName(moduleId, member) {
80
+ return `${moduleId}.${member}`;
81
+ }
82
+ static formatReference(owner, repository, number) {
83
+ return `${owner}/${repository}#${number}`;
84
+ }
85
+ }