@opensoftware/contracts 0.0.0-stage → 1.0.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/LICENSE +202 -0
- package/README.md +105 -2
- package/dist/breaking.d.ts +27 -0
- package/dist/breaking.js +156 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +68 -0
- package/dist/formats.d.ts +10 -0
- package/dist/formats.js +18 -0
- package/dist/generated/types.d.ts +543 -0
- package/dist/generated/types.js +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +73 -0
- package/dist/roundtrip.d.ts +3 -0
- package/dist/roundtrip.js +89 -0
- package/events/asyncapi.yaml +144 -0
- package/events/gate-decided/gate-decided.schema.json +122 -0
- package/events/gate-requested/gate-requested.schema.json +135 -0
- package/events/pr-opened/pr-opened.schema.json +138 -0
- package/events/run-completed/run-completed.schema.json +163 -0
- package/events/score-recorded/score-recorded.schema.json +163 -0
- package/events/work-received/work-received.schema.json +182 -0
- package/evidence-bundle/evidence-bundle.schema.json +180 -0
- package/factory-definition/factory-definition.schema.json +371 -0
- package/openapi/openapi.json +1553 -0
- package/package.json +50 -3
- package/run-record/run-record.schema.json +179 -0
- package/score/score.schema.json +110 -0
- package/typespec/common.tsp +62 -0
- package/typespec/events.tsp +134 -0
- package/typespec/evidence-bundle.tsp +99 -0
- package/typespec/factory-definition.tsp +193 -0
- package/typespec/main.tsp +10 -0
- package/typespec/run-record.tsp +88 -0
- package/typespec/score.tsp +68 -0
- package/typespec/work-item.tsp +65 -0
- package/work-item/work-item.schema.json +129 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,106 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
covers: [contracts/typespec/**, contracts/src/**, contracts/scripts/**, contracts/events/asyncapi.yaml]
|
|
3
|
+
---
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
# contracts
|
|
6
|
+
|
|
7
|
+
The formats that modules share, from one TypeSpec source: JSON Schemas for every format, the AsyncAPI document of
|
|
8
|
+
the events, and the TypeScript package `@opensoftware/contracts`. Decisions:
|
|
9
|
+
[ADR 0006](../docs/decisions/0006-contracts-typespec.md), [ADR 0012](../docs/decisions/0012-contracts-toolchain.md).
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
This quick start is scenario `SCN-CTR-001` and is tested on every change.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npx @opensoftware/contracts validate factory-definition factory.yaml
|
|
17
|
+
# factory.yaml: valid factory-definition
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
From TypeScript:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { type FactoryDefinition, validate } from "@opensoftware/contracts";
|
|
24
|
+
|
|
25
|
+
const problems = validate("factory-definition", document); // [] when valid, else { path, message } per problem
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The schema files are exported at the paths in the table below, for example
|
|
29
|
+
`@opensoftware/contracts/work-item/work-item.schema.json`, along with `events/asyncapi.yaml`, `openapi/*` and `typespec/*`.
|
|
30
|
+
|
|
31
|
+
## Formats
|
|
32
|
+
|
|
33
|
+
Each format has one schema, generated from `typespec/`, and fixtures that CONTRACT checks on every change.
|
|
34
|
+
|
|
35
|
+
| Format | Schema | Model | Notes |
|
|
36
|
+
| --- | --- | --- | --- |
|
|
37
|
+
| factory definition | `factory-definition/factory-definition.schema.json` | `FactoryDefinition` | `factory.yaml`. Closed objects: an unknown field is an error. |
|
|
38
|
+
| work item | `work-item/work-item.schema.json` | `WorkItem` | The `data` of `work.received@v1`. |
|
|
39
|
+
| run record | `run-record/run-record.schema.json` | `RunRecord` | OTel resource and root-span attributes; pins in `typespec/run-record.tsp`. |
|
|
40
|
+
| evidence bundle | `evidence-bundle/evidence-bundle.schema.json` | `EvidenceManifest` | `evidence.json` at the bundle root. |
|
|
41
|
+
| score | `score/score.schema.json` | `Score` | Subset of Langfuse's create-score request; factory fields in `metadata`. |
|
|
42
|
+
| events | `events/<event>/<event>.schema.json`, `events/asyncapi.yaml` | `WorkReceived` … `PrOpened` | CloudEvents 1.0, structured JSON. |
|
|
43
|
+
|
|
44
|
+
Wire formats (everything but the factory definition) are open: readers ignore fields they do not know, so a minor
|
|
45
|
+
version may add optional fields. Read `factory.yaml` with a YAML 1.2 parser: YAML 1.1 parsers turn the key `on`
|
|
46
|
+
into `true`.
|
|
47
|
+
|
|
48
|
+
## Commands
|
|
49
|
+
|
|
50
|
+
| Command | What it does |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| `osf-contracts validate <format> <file>` | Check a YAML or JSON document. Prints `<file>: valid <format>`, or one `<file> <JSON Pointer>: <problem>` line per problem and exits 1. |
|
|
53
|
+
| `osf-contracts breaking [--base <ref>]` | Compare every schema with the last `contracts-v*` tag (or `<ref>`). A change that makes released documents invalid fails unless `contracts/package.json` has a new major version. |
|
|
54
|
+
| `osf-contracts roundtrip --client typescript [dir]` | Load every valid fixture under `dir` (default `contracts`) with the generated TypeScript types and compare what they write back. TypeScript is the only client (ADR 0012). |
|
|
55
|
+
|
|
56
|
+
## Changing a format
|
|
57
|
+
|
|
58
|
+
1. Change `typespec/`. Run `pnpm build` in `contracts/`; it regenerates the schemas, `openapi/openapi.json`,
|
|
59
|
+
`src/generated/types.ts` and `dist/`. `pnpm lint` fails when a committed generated file is out of date.
|
|
60
|
+
2. Add or update fixtures in `<format>/fixtures/{valid,invalid}/`.
|
|
61
|
+
3. A breaking change (removed or newly required field, changed type, removed enum value, closed object, tighter
|
|
62
|
+
bound or pattern) needs a new major version in `package.json`. Prefer a new optional field, or for events a new
|
|
63
|
+
type `name@v2` next to the old one.
|
|
64
|
+
|
|
65
|
+
## Releases
|
|
66
|
+
|
|
67
|
+
release-please opens a release pull request from the Conventional Commits under `contracts/`. Merging it creates
|
|
68
|
+
the tag `contracts-v<version>`. The workflow `.github/workflows/publish-contracts.yml` then builds the package and
|
|
69
|
+
publishes it to npm as `@opensoftware/contracts`, with provenance. To publish a tag again, run the workflow by
|
|
70
|
+
hand with the tag. The CONTRACT check and `osf-contracts breaking` compare with the newest `contracts-v*` tag.
|
|
71
|
+
|
|
72
|
+
Only the TypeScript package exists (ADR 0012). A client for another language can be generated from
|
|
73
|
+
`openapi/openapi.json` with any OpenAPI generator when someone needs one.
|
|
74
|
+
|
|
75
|
+
One-time setup (no credential is ever stored in the repository):
|
|
76
|
+
|
|
77
|
+
1. npm: the organization `opensoftware` owns the scope `@opensoftware`, under which every package of this
|
|
78
|
+
repository is published.
|
|
79
|
+
2. GitHub: the publish job runs in the environment `npm` (Settings → Environments; GitHub creates it on the first
|
|
80
|
+
run).
|
|
81
|
+
3. First version only: npm configures trusted publishing per existing package, so `1.0.0` needs a token. Add a
|
|
82
|
+
granular npm access token with read and write access to the `opensoftware` organization's packages as the
|
|
83
|
+
secret `NPM_TOKEN` (a repository secret or a secret of the environment `npm`), or publish `1.0.0` by hand.
|
|
84
|
+
4. After the first version: on npmjs.com, package settings → Trusted publishing, add GitHub Actions with the
|
|
85
|
+
repository `sesigl/open-software-factory`, the workflow `publish-contracts.yml` and the environment `npm`.
|
|
86
|
+
Then delete the secret `NPM_TOKEN` and revoke the token. Later versions publish through OIDC only.
|
|
87
|
+
|
|
88
|
+
## Configuration
|
|
89
|
+
|
|
90
|
+
None. The CLI reads no environment variables.
|
|
91
|
+
|
|
92
|
+
## Scenarios
|
|
93
|
+
|
|
94
|
+
<!-- generated by make docs from features/: do not edit -->
|
|
95
|
+
|
|
96
|
+
| ID | Status | Scenario |
|
|
97
|
+
| --- | --- | --- |
|
|
98
|
+
| SCN-CTR-001 | implemented | A valid factory definition validates |
|
|
99
|
+
| SCN-CTR-002 | implemented | A factory definition with an unknown agent field is rejected with its path |
|
|
100
|
+
| SCN-CTR-003 | implemented | A run record that names its transcript, artifacts and pull request validates |
|
|
101
|
+
| SCN-CTR-004 | implemented | A run record without a requester is rejected with its path |
|
|
102
|
+
| SCN-CTR-005 | implemented | A breaking change to a released schema fails the compatibility check |
|
|
103
|
+
| SCN-CTR-006 | implemented | The generated TypeScript types round-trip every valid fixture |
|
|
104
|
+
| SCN-CTR-007 | deprecated | The generated Python client round-trips every valid fixture |
|
|
105
|
+
| SCN-CTR-008 | deprecated | The generated Java client round-trips every valid fixture |
|
|
106
|
+
| SCN-CTR-009 | implemented | A tighter form of a "exactly one of" rule fails the compatibility check |
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface Schema {
|
|
2
|
+
type?: string | string[];
|
|
3
|
+
properties?: Record<string, Schema>;
|
|
4
|
+
required?: string[];
|
|
5
|
+
enum?: unknown[];
|
|
6
|
+
const?: unknown;
|
|
7
|
+
items?: Schema;
|
|
8
|
+
anyOf?: Schema[];
|
|
9
|
+
oneOf?: Schema[];
|
|
10
|
+
additionalProperties?: boolean | Schema;
|
|
11
|
+
unevaluatedProperties?: boolean | Schema;
|
|
12
|
+
pattern?: string;
|
|
13
|
+
format?: string;
|
|
14
|
+
minLength?: number;
|
|
15
|
+
maxLength?: number;
|
|
16
|
+
minimum?: number;
|
|
17
|
+
maximum?: number;
|
|
18
|
+
exclusiveMinimum?: number;
|
|
19
|
+
exclusiveMaximum?: number;
|
|
20
|
+
minItems?: number;
|
|
21
|
+
maxItems?: number;
|
|
22
|
+
minProperties?: number;
|
|
23
|
+
maxProperties?: number;
|
|
24
|
+
}
|
|
25
|
+
export declare function breaking(baseRef: string | undefined): number;
|
|
26
|
+
/** Changes that make data valid under `before` invalid under `after`. */
|
|
27
|
+
export declare function diff(before: Schema, after: Schema, path?: string): string[];
|
package/dist/breaking.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// `osf-contracts breaking`: schema changes since the last release that make documents valid under the released
|
|
2
|
+
// schema invalid, unless the version in contracts/package.json has a new major number.
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
5
|
+
import { join, relative } from "node:path";
|
|
6
|
+
const RELEASE_TAGS = "contracts-v*";
|
|
7
|
+
export function breaking(baseRef) {
|
|
8
|
+
const root = git(["rev-parse", "--show-toplevel"], process.cwd());
|
|
9
|
+
if (root === undefined)
|
|
10
|
+
return fail("osf-contracts breaking runs inside a Git repository");
|
|
11
|
+
const base = baseRef ?? git(["tag", "--list", RELEASE_TAGS, "--sort=-v:refname"], root)?.split("\n")[0];
|
|
12
|
+
if (!base) {
|
|
13
|
+
process.stdout.write(`no ${RELEASE_TAGS} release tag: nothing to compare\n`);
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
if (git(["rev-parse", "--verify", "--quiet", `${base}^{commit}`], root) === undefined) {
|
|
17
|
+
return fail(`${base} is not a commit, tag or branch`);
|
|
18
|
+
}
|
|
19
|
+
const version = (at) => {
|
|
20
|
+
const text = at ? git(["show", `${at}:contracts/package.json`], root) : read(join(root, "contracts/package.json"));
|
|
21
|
+
return text ? JSON.parse(text).version : undefined;
|
|
22
|
+
};
|
|
23
|
+
const before = version(base);
|
|
24
|
+
const after = version();
|
|
25
|
+
const major = (value) => Number(value?.split(".")[0] ?? Number.NaN);
|
|
26
|
+
const allowed = major(after) > major(before);
|
|
27
|
+
const released = (git(["ls-tree", "-r", "--name-only", base, "--", "contracts"], root) ?? "")
|
|
28
|
+
.split("\n")
|
|
29
|
+
.filter(isSchema);
|
|
30
|
+
const current = new Set(schemaFiles(join(root, "contracts")).map((file) => relative(root, file)));
|
|
31
|
+
let failures = 0;
|
|
32
|
+
for (const file of released) {
|
|
33
|
+
const changes = current.has(file)
|
|
34
|
+
? diff(JSON.parse(git(["show", `${base}:${file}`], root) ?? "{}"), JSON.parse(readFileSync(join(root, file), "utf8")))
|
|
35
|
+
: ["removed the format"];
|
|
36
|
+
if (changes.length === 0)
|
|
37
|
+
continue;
|
|
38
|
+
if (allowed) {
|
|
39
|
+
process.stdout.write(`${file}: breaking change allowed by the major version ${before} → ${after}: ${changes.join("; ")}\n`);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
failures++;
|
|
43
|
+
process.stdout.write(`${file}: breaking change since ${base} without a major version (${before ?? "?"} → ${after ?? "?"}): ${changes.join("; ")}\n`);
|
|
44
|
+
}
|
|
45
|
+
if (failures === 0)
|
|
46
|
+
process.stdout.write(`no breaking change since ${base}\n`);
|
|
47
|
+
return failures === 0 ? 0 : 1;
|
|
48
|
+
}
|
|
49
|
+
/** Changes that make data valid under `before` invalid under `after`. */
|
|
50
|
+
export function diff(before, after, path = "") {
|
|
51
|
+
const changes = [];
|
|
52
|
+
const at = path || "the root";
|
|
53
|
+
const name = (key) => (path ? `${path}.${key}` : key);
|
|
54
|
+
const types = (schema) => [schema.type ?? []].flat().sort().join("|");
|
|
55
|
+
if (before.type !== undefined && types(before) !== types(after)) {
|
|
56
|
+
const widened = types(before) === "integer" && types(after) === "number";
|
|
57
|
+
if (!widened)
|
|
58
|
+
changes.push(`changed the type of ${at} from ${types(before)} to ${types(after) || "any"}`);
|
|
59
|
+
}
|
|
60
|
+
for (const key of Object.keys(before.properties ?? {})) {
|
|
61
|
+
const next = after.properties?.[key];
|
|
62
|
+
if (!next)
|
|
63
|
+
changes.push(`removed property ${name(key)}`);
|
|
64
|
+
else
|
|
65
|
+
changes.push(...diff(before.properties?.[key], next, name(key)));
|
|
66
|
+
}
|
|
67
|
+
for (const key of after.required ?? []) {
|
|
68
|
+
if (!(before.required ?? []).includes(key))
|
|
69
|
+
changes.push(`made ${name(key)} required`);
|
|
70
|
+
}
|
|
71
|
+
if (after.enum) {
|
|
72
|
+
for (const value of before.enum ?? []) {
|
|
73
|
+
if (!after.enum.some((candidate) => same(candidate, value))) {
|
|
74
|
+
changes.push(`removed the value ${JSON.stringify(value)} from ${at}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if ("const" in after && !("const" in before && same(before.const, after.const))) {
|
|
79
|
+
changes.push(`fixed ${at} to ${JSON.stringify(after.const)}`);
|
|
80
|
+
}
|
|
81
|
+
for (const keyword of ["additionalProperties", "unevaluatedProperties"]) {
|
|
82
|
+
const old = before[keyword];
|
|
83
|
+
const next = after[keyword];
|
|
84
|
+
if (old !== false && next === false)
|
|
85
|
+
changes.push(`closed ${at} to unknown properties`);
|
|
86
|
+
if (typeof old === "object" && typeof next === "object")
|
|
87
|
+
changes.push(...diff(old, next, name("*")));
|
|
88
|
+
}
|
|
89
|
+
if (before.items && after.items)
|
|
90
|
+
changes.push(...diff(before.items, after.items, `${path}[]`));
|
|
91
|
+
for (const keyword of ["anyOf", "oneOf"]) {
|
|
92
|
+
const old = before[keyword];
|
|
93
|
+
const next = after[keyword];
|
|
94
|
+
if (!old || !next)
|
|
95
|
+
continue;
|
|
96
|
+
if (next.length < old.length)
|
|
97
|
+
changes.push(`removed a form of ${at}`);
|
|
98
|
+
old.forEach((form, index) => {
|
|
99
|
+
const replaced = next[index];
|
|
100
|
+
if (replaced)
|
|
101
|
+
changes.push(...diff(form, replaced, `${path}|${index + 1}`));
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
changes.push(...constraints(before, after, at));
|
|
105
|
+
return changes;
|
|
106
|
+
}
|
|
107
|
+
/** Tighter bounds, a new or changed pattern, a new format. */
|
|
108
|
+
function constraints(before, after, at) {
|
|
109
|
+
const changes = [];
|
|
110
|
+
const lower = ["minLength", "minimum", "exclusiveMinimum", "minItems", "minProperties"];
|
|
111
|
+
const upper = ["maxLength", "maximum", "exclusiveMaximum", "maxItems", "maxProperties"];
|
|
112
|
+
for (const key of lower) {
|
|
113
|
+
const next = after[key];
|
|
114
|
+
if (next !== undefined && next > (before[key] ?? Number.NEGATIVE_INFINITY))
|
|
115
|
+
changes.push(`raised ${key} of ${at} to ${next}`);
|
|
116
|
+
}
|
|
117
|
+
for (const key of upper) {
|
|
118
|
+
const next = after[key];
|
|
119
|
+
if (next !== undefined && next < (before[key] ?? Number.POSITIVE_INFINITY))
|
|
120
|
+
changes.push(`lowered ${key} of ${at} to ${next}`);
|
|
121
|
+
}
|
|
122
|
+
if (after.pattern !== undefined && after.pattern !== before.pattern)
|
|
123
|
+
changes.push(`changed the pattern of ${at}`);
|
|
124
|
+
if (after.format !== undefined && after.format !== before.format)
|
|
125
|
+
changes.push(`required the format ${after.format} for ${at}`);
|
|
126
|
+
return changes;
|
|
127
|
+
}
|
|
128
|
+
function same(a, b) {
|
|
129
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
130
|
+
}
|
|
131
|
+
function isSchema(file) {
|
|
132
|
+
return file.endsWith(".schema.json") && !file.includes("/node_modules/") && !file.includes("/fixtures/");
|
|
133
|
+
}
|
|
134
|
+
function schemaFiles(dir) {
|
|
135
|
+
if (!existsSync(dir))
|
|
136
|
+
return [];
|
|
137
|
+
return readdirSync(dir).flatMap((entry) => {
|
|
138
|
+
if (entry === "node_modules" || entry === "fixtures" || entry.startsWith("."))
|
|
139
|
+
return [];
|
|
140
|
+
const full = join(dir, entry);
|
|
141
|
+
if (statSync(full).isDirectory())
|
|
142
|
+
return schemaFiles(full);
|
|
143
|
+
return isSchema(full) ? [full] : [];
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function read(file) {
|
|
147
|
+
return existsSync(file) ? readFileSync(file, "utf8") : undefined;
|
|
148
|
+
}
|
|
149
|
+
function git(args, cwd) {
|
|
150
|
+
const result = spawnSync("git", args, { cwd, encoding: "utf8" });
|
|
151
|
+
return result.status === 0 ? result.stdout.trim() : undefined;
|
|
152
|
+
}
|
|
153
|
+
function fail(message) {
|
|
154
|
+
process.stderr.write(`${message}\n`);
|
|
155
|
+
return 2;
|
|
156
|
+
}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// osf-contracts: validate documents against the shared formats, check schema compatibility with the last release,
|
|
3
|
+
// and round-trip the fixtures through the generated clients.
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { parse } from "yaml";
|
|
6
|
+
import { breaking } from "./breaking.js";
|
|
7
|
+
import { FORMATS, formatNamed } from "./formats.js";
|
|
8
|
+
import { validate } from "./index.js";
|
|
9
|
+
import { CLIENTS, roundtrip } from "./roundtrip.js";
|
|
10
|
+
const USAGE = `Usage: osf-contracts <command>
|
|
11
|
+
|
|
12
|
+
validate <format> <file> check a YAML or JSON document; formats: ${FORMATS.map((format) => format.name).join(", ")}
|
|
13
|
+
breaking [--base <ref>] breaking schema changes since the last contracts-v* tag (or <ref>) without a major version
|
|
14
|
+
roundtrip --client <client> [dir]
|
|
15
|
+
load every valid fixture under dir (default contracts) with a generated client and
|
|
16
|
+
compare what it writes back; clients: ${CLIENTS.join(", ")}
|
|
17
|
+
`;
|
|
18
|
+
function main(argv) {
|
|
19
|
+
const [command, ...args] = argv;
|
|
20
|
+
switch (command) {
|
|
21
|
+
case "validate":
|
|
22
|
+
return validateCommand(args);
|
|
23
|
+
case "breaking": {
|
|
24
|
+
const at = args.indexOf("--base");
|
|
25
|
+
return breaking(at === -1 ? undefined : args[at + 1]);
|
|
26
|
+
}
|
|
27
|
+
case "roundtrip": {
|
|
28
|
+
const at = args.indexOf("--client");
|
|
29
|
+
const client = at === -1 ? undefined : args[at + 1];
|
|
30
|
+
const dir = args.filter((_, index) => index !== at && index !== at + 1)[0] ?? "contracts";
|
|
31
|
+
if (!client || !CLIENTS.includes(client))
|
|
32
|
+
return usage(`Give a client: ${CLIENTS.join(", ")}`);
|
|
33
|
+
return roundtrip(client, dir);
|
|
34
|
+
}
|
|
35
|
+
case undefined:
|
|
36
|
+
case "help":
|
|
37
|
+
case "--help":
|
|
38
|
+
process.stdout.write(USAGE);
|
|
39
|
+
return command === undefined ? 2 : 0;
|
|
40
|
+
default:
|
|
41
|
+
return usage(`Unknown command: ${command}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function validateCommand([name, file]) {
|
|
45
|
+
const format = name ? formatNamed(name) : undefined;
|
|
46
|
+
if (!format || !file)
|
|
47
|
+
return usage(name && !format ? `Unknown format: ${name}` : "Give a format and a file");
|
|
48
|
+
let document;
|
|
49
|
+
try {
|
|
50
|
+
document = parse(readFileSync(file, "utf8"));
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
process.stdout.write(`${file}: not readable as YAML or JSON: ${error.message}\n`);
|
|
54
|
+
return 1;
|
|
55
|
+
}
|
|
56
|
+
const problems = validate(format, document);
|
|
57
|
+
for (const problem of problems)
|
|
58
|
+
process.stdout.write(`${file} ${problem.path}: ${problem.message}\n`);
|
|
59
|
+
if (problems.length > 0)
|
|
60
|
+
return 1;
|
|
61
|
+
process.stdout.write(`${file}: valid ${format.name}\n`);
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
function usage(message) {
|
|
65
|
+
process.stderr.write(`${message}\n\n${USAGE}`);
|
|
66
|
+
return 2;
|
|
67
|
+
}
|
|
68
|
+
process.exitCode = main(process.argv.slice(2));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Format {
|
|
2
|
+
/** CLI name and file name: `<path>/<name>.schema.json`. */
|
|
3
|
+
name: string;
|
|
4
|
+
/** Folder under contracts/. */
|
|
5
|
+
path: string;
|
|
6
|
+
/** The TypeSpec model, also the name of the generated type and class. */
|
|
7
|
+
model: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const FORMATS: readonly Format[];
|
|
10
|
+
export declare function formatNamed(name: string): Format | undefined;
|
package/dist/formats.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// The formats of the contracts: the name used on the CLI, the folder of the schema and the TypeSpec model.
|
|
2
|
+
const format = (path, model) => ({ name: path.split("/").pop(), path, model });
|
|
3
|
+
export const FORMATS = [
|
|
4
|
+
format("factory-definition", "FactoryDefinition"),
|
|
5
|
+
format("work-item", "WorkItem"),
|
|
6
|
+
format("run-record", "RunRecord"),
|
|
7
|
+
format("evidence-bundle", "EvidenceManifest"),
|
|
8
|
+
format("score", "Score"),
|
|
9
|
+
format("events/work-received", "WorkReceived"),
|
|
10
|
+
format("events/run-completed", "RunCompleted"),
|
|
11
|
+
format("events/gate-requested", "GateRequested"),
|
|
12
|
+
format("events/gate-decided", "GateDecided"),
|
|
13
|
+
format("events/score-recorded", "ScoreRecorded"),
|
|
14
|
+
format("events/pr-opened", "PrOpened"),
|
|
15
|
+
];
|
|
16
|
+
export function formatNamed(name) {
|
|
17
|
+
return FORMATS.find((candidate) => candidate.name === name);
|
|
18
|
+
}
|