@dependabit/monitor 0.1.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.
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/checkers/github-repo.d.ts +17 -0
- package/dist/checkers/github-repo.d.ts.map +1 -0
- package/dist/checkers/github-repo.js +115 -0
- package/dist/checkers/github-repo.js.map +1 -0
- package/dist/checkers/index.d.ts +7 -0
- package/dist/checkers/index.d.ts.map +1 -0
- package/dist/checkers/index.js +7 -0
- package/dist/checkers/index.js.map +1 -0
- package/dist/checkers/openapi.d.ts +24 -0
- package/dist/checkers/openapi.d.ts.map +1 -0
- package/dist/checkers/openapi.js +221 -0
- package/dist/checkers/openapi.js.map +1 -0
- package/dist/checkers/url-content.d.ts +16 -0
- package/dist/checkers/url-content.d.ts.map +1 -0
- package/dist/checkers/url-content.js +66 -0
- package/dist/checkers/url-content.js.map +1 -0
- package/dist/comparator.d.ts +16 -0
- package/dist/comparator.d.ts.map +1 -0
- package/dist/comparator.js +53 -0
- package/dist/comparator.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/monitor.d.ts +43 -0
- package/dist/monitor.d.ts.map +1 -0
- package/dist/monitor.js +85 -0
- package/dist/monitor.js.map +1 -0
- package/dist/normalizer.d.ts +24 -0
- package/dist/normalizer.d.ts.map +1 -0
- package/dist/normalizer.js +97 -0
- package/dist/normalizer.js.map +1 -0
- package/dist/scheduler.d.ts +64 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +132 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/severity.d.ts +22 -0
- package/dist/severity.d.ts.map +1 -0
- package/dist/severity.js +87 -0
- package/dist/severity.js.map +1 -0
- package/dist/types.d.ts +36 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +39 -0
- package/src/checkers/github-repo.ts +150 -0
- package/src/checkers/index.ts +7 -0
- package/src/checkers/openapi.ts +310 -0
- package/src/checkers/url-content.ts +78 -0
- package/src/comparator.ts +68 -0
- package/src/index.ts +20 -0
- package/src/monitor.ts +120 -0
- package/src/normalizer.ts +122 -0
- package/src/scheduler.ts +175 -0
- package/src/severity.ts +112 -0
- package/src/types.ts +40 -0
- package/test/checkers/github-repo.test.ts +124 -0
- package/test/checkers/openapi.test.ts +352 -0
- package/test/checkers/url-content.test.ts +99 -0
- package/test/comparator.test.ts +108 -0
- package/test/monitor.test.ts +177 -0
- package/test/normalizer.test.ts +66 -0
- package/test/scheduler.test.ts +674 -0
- package/test/severity.test.ts +122 -0
- package/tsconfig.json +10 -0
- package/tsconfig.tsbuildinfo +1 -0
package/dist/severity.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Severity Classifier
|
|
3
|
+
* Classifies dependency changes into breaking, major, or minor severity levels
|
|
4
|
+
*/
|
|
5
|
+
export class SeverityClassifier {
|
|
6
|
+
/**
|
|
7
|
+
* Classifies the severity of a change based on version changes and change types
|
|
8
|
+
*/
|
|
9
|
+
classify(changes) {
|
|
10
|
+
if (!changes.hasChanged) {
|
|
11
|
+
return 'minor';
|
|
12
|
+
}
|
|
13
|
+
// Check for version-based classification
|
|
14
|
+
if (changes.oldVersion && changes.newVersion) {
|
|
15
|
+
const severity = this.classifyVersionChange(changes.oldVersion, changes.newVersion);
|
|
16
|
+
if (severity) {
|
|
17
|
+
return severity;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Check for OpenAPI/schema changes
|
|
21
|
+
if (changes.diff && typeof changes.diff === 'object') {
|
|
22
|
+
const diff = changes.diff;
|
|
23
|
+
// Breaking: Removed endpoints or incompatible schema changes
|
|
24
|
+
if (diff.removedEndpoints && diff.removedEndpoints.length > 0) {
|
|
25
|
+
return 'breaking';
|
|
26
|
+
}
|
|
27
|
+
if (diff.schemaChanges && diff.schemaChanges.length > 0) {
|
|
28
|
+
return 'breaking';
|
|
29
|
+
}
|
|
30
|
+
// Major: New endpoints or features
|
|
31
|
+
if (diff.addedEndpoints && diff.addedEndpoints.length > 0) {
|
|
32
|
+
return 'major';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Default to minor for content changes
|
|
36
|
+
return 'minor';
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Classifies severity based on semantic versioning
|
|
40
|
+
* @returns Severity level or undefined if version format not recognized
|
|
41
|
+
*/
|
|
42
|
+
classifyVersionChange(oldVersion, newVersion) {
|
|
43
|
+
// Parse semver-like versions
|
|
44
|
+
const oldParts = this.parseVersion(oldVersion);
|
|
45
|
+
const newParts = this.parseVersion(newVersion);
|
|
46
|
+
if (!oldParts || !newParts) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const [oldMajor, oldMinor, oldPatch] = oldParts;
|
|
50
|
+
const [newMajor, newMinor, newPatch] = newParts;
|
|
51
|
+
// Breaking: Major version increase
|
|
52
|
+
if (newMajor > oldMajor) {
|
|
53
|
+
return 'breaking';
|
|
54
|
+
}
|
|
55
|
+
// Major: Minor version increase
|
|
56
|
+
if (newMajor === oldMajor && newMinor > oldMinor) {
|
|
57
|
+
return 'major';
|
|
58
|
+
}
|
|
59
|
+
// Minor: Patch version increase or same version
|
|
60
|
+
if (newMajor === oldMajor && newMinor === oldMinor && newPatch >= oldPatch) {
|
|
61
|
+
return 'minor';
|
|
62
|
+
}
|
|
63
|
+
// Default for other cases
|
|
64
|
+
return 'major';
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Parses a version string into [major, minor, patch]
|
|
68
|
+
*/
|
|
69
|
+
parseVersion(version) {
|
|
70
|
+
if (!version) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
// Remove 'v' prefix if present
|
|
74
|
+
const cleaned = version.replace(/^v/, '');
|
|
75
|
+
// Match semver pattern
|
|
76
|
+
const match = cleaned.match(/^(\d+)\.(\d+)\.(\d+)/);
|
|
77
|
+
if (!match) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return [
|
|
81
|
+
parseInt(match[1] || '0', 10),
|
|
82
|
+
parseInt(match[2] || '0', 10),
|
|
83
|
+
parseInt(match[3] || '0', 10)
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=severity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"severity.js","sourceRoot":"","sources":["../src/severity.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,MAAM,OAAO,kBAAkB;IAC7B;;OAEG;IACH,QAAQ,CAAC,OAAwB,EAAY;QAC3C,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;YACxB,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,yCAAyC;QACzC,IAAI,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YAC7C,MAAM,QAAQ,GAAG,IAAI,CAAC,qBAAqB,CAAC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;YACpF,IAAI,QAAQ,EAAE,CAAC;gBACb,OAAO,QAAQ,CAAC;YAClB,CAAC;QACH,CAAC;QAED,mCAAmC;QACnC,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACrD,MAAM,IAAI,GAAG,OAAO,CAAC,IAIpB,CAAC;YAEF,6DAA6D;YAC7D,IAAI,IAAI,CAAC,gBAAgB,IAAI,IAAI,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC9D,OAAO,UAAU,CAAC;YACpB,CAAC;YAED,IAAI,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACxD,OAAO,UAAU,CAAC;YACpB,CAAC;YAED,mCAAmC;YACnC,IAAI,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC1D,OAAO,OAAO,CAAC;YACjB,CAAC;QACH,CAAC;QAED,uCAAuC;QACvC,OAAO,OAAO,CAAC;IAAA,CAChB;IAED;;;OAGG;IACK,qBAAqB,CAAC,UAAkB,EAAE,UAAkB,EAAwB;QAC1F,6BAA6B;QAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;QAC/C,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;QAE/C,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC3B,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC;QAChD,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC;QAEhD,mCAAmC;QACnC,IAAI,QAAQ,GAAG,QAAQ,EAAE,CAAC;YACxB,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,gCAAgC;QAChC,IAAI,QAAQ,KAAK,QAAQ,IAAI,QAAQ,GAAG,QAAQ,EAAE,CAAC;YACjD,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,gDAAgD;QAChD,IAAI,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,QAAQ,IAAI,QAAQ,IAAI,QAAQ,EAAE,CAAC;YAC3E,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,0BAA0B;QAC1B,OAAO,OAAO,CAAC;IAAA,CAChB;IAED;;OAEG;IACK,YAAY,CAAC,OAAe,EAAmC;QACrE,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,OAAO,IAAI,CAAC;QACd,CAAC;QAED,+BAA+B;QAC/B,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAE1C,uBAAuB;QACvB,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAC;QACpD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO;YACL,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,EAAE,CAAC;YAC7B,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,EAAE,CAAC;YAC7B,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,EAAE,CAAC;SAC9B,CAAC;IAAA,CACH;CACF"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common types for dependency checkers
|
|
3
|
+
*/
|
|
4
|
+
export interface DependencySnapshot {
|
|
5
|
+
version?: string | undefined;
|
|
6
|
+
stateHash: string;
|
|
7
|
+
fetchedAt: Date;
|
|
8
|
+
metadata?: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
export interface ChangeDetection {
|
|
11
|
+
hasChanged: boolean;
|
|
12
|
+
changes: string[];
|
|
13
|
+
oldVersion?: string | undefined;
|
|
14
|
+
newVersion?: string | undefined;
|
|
15
|
+
diff?: unknown;
|
|
16
|
+
severity?: 'breaking' | 'major' | 'minor';
|
|
17
|
+
}
|
|
18
|
+
export interface AccessConfig {
|
|
19
|
+
url: string;
|
|
20
|
+
accessMethod: 'github-api' | 'http' | 'openapi' | 'context7' | 'arxiv';
|
|
21
|
+
auth?: {
|
|
22
|
+
type: 'token' | 'basic' | 'oauth' | 'none';
|
|
23
|
+
secret?: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface Checker {
|
|
27
|
+
/**
|
|
28
|
+
* Fetches the current state of a dependency
|
|
29
|
+
*/
|
|
30
|
+
fetch(config: AccessConfig): Promise<DependencySnapshot>;
|
|
31
|
+
/**
|
|
32
|
+
* Compares two snapshots to detect changes
|
|
33
|
+
*/
|
|
34
|
+
compare(prev: DependencySnapshot, curr: DependencySnapshot): Promise<ChangeDetection>;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,kBAAkB;IACjC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,IAAI,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,UAAU,GAAG,OAAO,GAAG,OAAO,CAAC;CAC3C;AAED,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,YAAY,GAAG,MAAM,GAAG,SAAS,GAAG,UAAU,GAAG,OAAO,CAAC;IACvE,IAAI,CAAC,EAAE;QACL,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAC;QAC3C,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,CAAC;CACH;AAED,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,KAAK,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;IAEzD;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,kBAAkB,EAAE,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CACvF"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dependabit/monitor",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Change detection and monitoring logic for external dependencies",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"yaml": "^2.8.2",
|
|
16
|
+
"zod": "^4.3.6",
|
|
17
|
+
"@dependabit/manifest": "0.1.1"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^25.2.2",
|
|
21
|
+
"tsx": "^4.21.0",
|
|
22
|
+
"typescript": "^5.9.3",
|
|
23
|
+
"vitest": "^4.0.18"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"monitor",
|
|
27
|
+
"change-detection",
|
|
28
|
+
"dependency"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsgo -p tsconfig.json",
|
|
33
|
+
"clean": "rm -rf dist",
|
|
34
|
+
"dev": "tsx watch src/index.ts",
|
|
35
|
+
"type-check": "tsgo --noEmit -p tsconfig.json",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:watch": "vitest"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub Repository Checker
|
|
3
|
+
* Monitors GitHub repositories for new releases and changes
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Checker, DependencySnapshot, ChangeDetection, AccessConfig } from '../types.js';
|
|
7
|
+
import crypto from 'node:crypto';
|
|
8
|
+
|
|
9
|
+
export class GitHubRepoChecker implements Checker {
|
|
10
|
+
/**
|
|
11
|
+
* Fetches latest release information from a GitHub repository
|
|
12
|
+
*/
|
|
13
|
+
async fetch(config: AccessConfig): Promise<DependencySnapshot> {
|
|
14
|
+
const { url } = config;
|
|
15
|
+
|
|
16
|
+
// Extract owner and repo from GitHub URL
|
|
17
|
+
const match = url.match(/github\.com\/([^/]+)\/([^/]+)/);
|
|
18
|
+
if (!match || !match[1] || !match[2]) {
|
|
19
|
+
throw new Error(`Invalid GitHub URL: ${url}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const owner = match[1];
|
|
23
|
+
const repo = match[2];
|
|
24
|
+
const cleanRepo = repo.replace(/\.git$/, '');
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
// Fetch latest release from GitHub API
|
|
28
|
+
const apiUrl = `https://api.github.com/repos/${owner}/${cleanRepo}/releases/latest`;
|
|
29
|
+
const response = await fetch(apiUrl, {
|
|
30
|
+
headers: {
|
|
31
|
+
Accept: 'application/vnd.github+json',
|
|
32
|
+
'User-Agent': 'dependabit',
|
|
33
|
+
...(config.auth?.secret && { Authorization: `Bearer ${config.auth.secret}` })
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (response.status === 404) {
|
|
38
|
+
// No releases found, fall back to latest commit
|
|
39
|
+
return this.fetchLatestCommit(owner, cleanRepo, config.auth?.secret);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const release = (await response.json()) as {
|
|
47
|
+
tag_name: string;
|
|
48
|
+
name: string;
|
|
49
|
+
published_at: string;
|
|
50
|
+
body: string;
|
|
51
|
+
html_url: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Create state hash from release info
|
|
55
|
+
const stateContent = JSON.stringify({
|
|
56
|
+
tagName: release.tag_name,
|
|
57
|
+
name: release.name,
|
|
58
|
+
publishedAt: release.published_at
|
|
59
|
+
});
|
|
60
|
+
const stateHash = crypto.createHash('sha256').update(stateContent).digest('hex');
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
version: release.tag_name,
|
|
64
|
+
stateHash,
|
|
65
|
+
fetchedAt: new Date(),
|
|
66
|
+
metadata: {
|
|
67
|
+
name: release.name,
|
|
68
|
+
publishedAt: release.published_at,
|
|
69
|
+
body: release.body,
|
|
70
|
+
htmlUrl: release.html_url
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw new Error(`Failed to fetch GitHub release: ${(error as Error).message}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Fallback: Fetch latest commit when no releases exist
|
|
80
|
+
*/
|
|
81
|
+
private async fetchLatestCommit(
|
|
82
|
+
owner: string,
|
|
83
|
+
repo: string,
|
|
84
|
+
token?: string
|
|
85
|
+
): Promise<DependencySnapshot> {
|
|
86
|
+
const apiUrl = `https://api.github.com/repos/${owner}/${repo}/commits?per_page=1`;
|
|
87
|
+
const response = await fetch(apiUrl, {
|
|
88
|
+
headers: {
|
|
89
|
+
Accept: 'application/vnd.github+json',
|
|
90
|
+
'User-Agent': 'dependabit',
|
|
91
|
+
...(token && { Authorization: `Bearer ${token}` })
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const commits = (await response.json()) as Array<{
|
|
100
|
+
sha: string;
|
|
101
|
+
commit: {
|
|
102
|
+
message: string;
|
|
103
|
+
author: { date: string };
|
|
104
|
+
};
|
|
105
|
+
}>;
|
|
106
|
+
|
|
107
|
+
if (commits.length === 0 || !commits[0]) {
|
|
108
|
+
throw new Error('No commits found in repository');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const latestCommit = commits[0];
|
|
112
|
+
const stateHash = latestCommit.sha;
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
stateHash,
|
|
116
|
+
fetchedAt: new Date(),
|
|
117
|
+
metadata: {
|
|
118
|
+
sha: latestCommit.sha,
|
|
119
|
+
message: latestCommit.commit.message,
|
|
120
|
+
date: latestCommit.commit.author.date
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Compares two snapshots to detect version/state changes
|
|
127
|
+
*/
|
|
128
|
+
async compare(prev: DependencySnapshot, curr: DependencySnapshot): Promise<ChangeDetection> {
|
|
129
|
+
const changes: string[] = [];
|
|
130
|
+
|
|
131
|
+
// Check version change
|
|
132
|
+
if (prev.version !== curr.version) {
|
|
133
|
+
if (prev.version && curr.version) {
|
|
134
|
+
changes.push('version');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Check state hash change
|
|
139
|
+
if (prev.stateHash !== curr.stateHash) {
|
|
140
|
+
changes.push('stateHash');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
hasChanged: changes.length > 0,
|
|
145
|
+
changes,
|
|
146
|
+
oldVersion: prev.version,
|
|
147
|
+
newVersion: curr.version
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI Spec Checker
|
|
3
|
+
* Monitors OpenAPI/Swagger specifications for changes with semantic diffing
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Checker, DependencySnapshot, ChangeDetection, AccessConfig } from '../types.js';
|
|
7
|
+
import crypto from 'node:crypto';
|
|
8
|
+
import { parse as parseYAML } from 'yaml';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* OpenAPI document structure (partial, for what we need)
|
|
12
|
+
*/
|
|
13
|
+
interface OpenAPIDocument {
|
|
14
|
+
openapi?: string;
|
|
15
|
+
swagger?: string;
|
|
16
|
+
info?: {
|
|
17
|
+
title?: string;
|
|
18
|
+
version?: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
};
|
|
21
|
+
paths?: Record<string, PathItem>;
|
|
22
|
+
components?: {
|
|
23
|
+
schemas?: Record<string, unknown>;
|
|
24
|
+
securitySchemes?: Record<string, unknown>;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PathItem {
|
|
29
|
+
get?: Operation;
|
|
30
|
+
post?: Operation;
|
|
31
|
+
put?: Operation;
|
|
32
|
+
patch?: Operation;
|
|
33
|
+
delete?: Operation;
|
|
34
|
+
options?: Operation;
|
|
35
|
+
head?: Operation;
|
|
36
|
+
trace?: Operation;
|
|
37
|
+
parameters?: unknown[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Operation {
|
|
41
|
+
operationId?: string;
|
|
42
|
+
summary?: string;
|
|
43
|
+
description?: string;
|
|
44
|
+
parameters?: unknown[];
|
|
45
|
+
requestBody?: unknown;
|
|
46
|
+
responses?: Record<string, unknown>;
|
|
47
|
+
deprecated?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Semantic diff result for OpenAPI specs
|
|
52
|
+
*/
|
|
53
|
+
interface OpenAPIDiff {
|
|
54
|
+
addedEndpoints: string[];
|
|
55
|
+
removedEndpoints: string[];
|
|
56
|
+
modifiedEndpoints: string[];
|
|
57
|
+
addedSchemas: string[];
|
|
58
|
+
removedSchemas: string[];
|
|
59
|
+
modifiedSchemas: string[];
|
|
60
|
+
versionChanged: boolean;
|
|
61
|
+
oldVersion?: string;
|
|
62
|
+
newVersion?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class OpenAPIChecker implements Checker {
|
|
66
|
+
/**
|
|
67
|
+
* Fetches and parses OpenAPI specification
|
|
68
|
+
*/
|
|
69
|
+
async fetch(config: AccessConfig): Promise<DependencySnapshot> {
|
|
70
|
+
const { url } = config;
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const headers: Record<string, string> = {
|
|
74
|
+
Accept: 'application/json, application/yaml, text/yaml, */*',
|
|
75
|
+
'User-Agent': 'dependabit-monitor/1.0'
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
if (config.auth?.secret) {
|
|
79
|
+
if (config.auth.type === 'token' || config.auth.type === 'oauth') {
|
|
80
|
+
headers['Authorization'] = `Bearer ${config.auth.secret}`;
|
|
81
|
+
} else if (config.auth.type === 'basic') {
|
|
82
|
+
headers['Authorization'] = `Basic ${config.auth.secret}`;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const response = await fetch(url, {
|
|
87
|
+
headers
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
throw new Error(`HTTP error: ${response.status} ${response.statusText}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const contentType = response.headers.get('content-type') || '';
|
|
95
|
+
const content = await response.text();
|
|
96
|
+
|
|
97
|
+
// Parse the OpenAPI spec
|
|
98
|
+
let spec: OpenAPIDocument;
|
|
99
|
+
if (contentType.includes('yaml') || url.endsWith('.yaml') || url.endsWith('.yml')) {
|
|
100
|
+
// Parse YAML using standard yaml library
|
|
101
|
+
spec = parseYAML(content) as OpenAPIDocument;
|
|
102
|
+
} else {
|
|
103
|
+
spec = JSON.parse(content) as OpenAPIDocument;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Extract key information for semantic comparison
|
|
107
|
+
const endpoints = this.extractEndpoints(spec);
|
|
108
|
+
const schemas = this.extractSchemas(spec);
|
|
109
|
+
const version = spec.info?.version;
|
|
110
|
+
|
|
111
|
+
// Create a deterministic hash from the semantic content
|
|
112
|
+
const semanticContent = JSON.stringify({
|
|
113
|
+
version,
|
|
114
|
+
endpoints: Object.keys(endpoints).sort(),
|
|
115
|
+
schemas: Object.keys(schemas).sort(),
|
|
116
|
+
endpointDetails: endpoints,
|
|
117
|
+
schemaDetails: schemas
|
|
118
|
+
});
|
|
119
|
+
const stateHash = crypto.createHash('sha256').update(semanticContent).digest('hex');
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
version,
|
|
123
|
+
stateHash,
|
|
124
|
+
fetchedAt: new Date(),
|
|
125
|
+
metadata: {
|
|
126
|
+
title: spec.info?.title,
|
|
127
|
+
description: spec.info?.description,
|
|
128
|
+
specVersion: spec.openapi || spec.swagger,
|
|
129
|
+
endpointCount: Object.keys(endpoints).length,
|
|
130
|
+
schemaCount: Object.keys(schemas).length,
|
|
131
|
+
endpoints,
|
|
132
|
+
schemas
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
} catch (error) {
|
|
136
|
+
throw new Error(`Failed to fetch OpenAPI spec: ${(error as Error).message}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Extract endpoints from OpenAPI spec
|
|
142
|
+
*/
|
|
143
|
+
private extractEndpoints(spec: OpenAPIDocument): Record<string, string[]> {
|
|
144
|
+
const endpoints: Record<string, string[]> = {};
|
|
145
|
+
|
|
146
|
+
if (!spec.paths) return endpoints;
|
|
147
|
+
|
|
148
|
+
for (const [path, pathItem] of Object.entries(spec.paths)) {
|
|
149
|
+
const methods: string[] = [];
|
|
150
|
+
const httpMethods = [
|
|
151
|
+
'get',
|
|
152
|
+
'post',
|
|
153
|
+
'put',
|
|
154
|
+
'patch',
|
|
155
|
+
'delete',
|
|
156
|
+
'options',
|
|
157
|
+
'head',
|
|
158
|
+
'trace'
|
|
159
|
+
] as const;
|
|
160
|
+
|
|
161
|
+
for (const method of httpMethods) {
|
|
162
|
+
if (pathItem[method]) {
|
|
163
|
+
methods.push(method.toUpperCase());
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (methods.length > 0) {
|
|
168
|
+
endpoints[path] = methods;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return endpoints;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Extract schemas from OpenAPI spec
|
|
177
|
+
*/
|
|
178
|
+
private extractSchemas(spec: OpenAPIDocument): Record<string, unknown> {
|
|
179
|
+
return spec.components?.schemas || {};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compares two OpenAPI snapshots with semantic diffing
|
|
184
|
+
*/
|
|
185
|
+
async compare(prev: DependencySnapshot, curr: DependencySnapshot): Promise<ChangeDetection> {
|
|
186
|
+
const changes: string[] = [];
|
|
187
|
+
let severity: 'breaking' | 'major' | 'minor' = 'minor';
|
|
188
|
+
|
|
189
|
+
// Extract endpoints and schemas from metadata
|
|
190
|
+
let prevEndpoints = (prev.metadata?.['endpoints'] as Record<string, string[]>) || {};
|
|
191
|
+
let currEndpoints = (curr.metadata?.['endpoints'] as Record<string, string[]>) || {};
|
|
192
|
+
let prevSchemas = (prev.metadata?.['schemas'] as Record<string, unknown>) || {};
|
|
193
|
+
let currSchemas = (curr.metadata?.['schemas'] as Record<string, unknown>) || {};
|
|
194
|
+
|
|
195
|
+
// If the state hashes match but previous metadata is missing, avoid spurious diffs
|
|
196
|
+
if (
|
|
197
|
+
prev.stateHash !== undefined &&
|
|
198
|
+
curr.stateHash !== undefined &&
|
|
199
|
+
prev.stateHash === curr.stateHash
|
|
200
|
+
) {
|
|
201
|
+
prevEndpoints = currEndpoints;
|
|
202
|
+
prevSchemas = currSchemas;
|
|
203
|
+
}
|
|
204
|
+
const diff: OpenAPIDiff = {
|
|
205
|
+
addedEndpoints: [],
|
|
206
|
+
removedEndpoints: [],
|
|
207
|
+
modifiedEndpoints: [],
|
|
208
|
+
addedSchemas: [],
|
|
209
|
+
removedSchemas: [],
|
|
210
|
+
modifiedSchemas: [],
|
|
211
|
+
versionChanged: prev.version !== curr.version
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
if (prev.version !== undefined) {
|
|
215
|
+
diff.oldVersion = prev.version;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (curr.version !== undefined) {
|
|
219
|
+
diff.newVersion = curr.version;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Compare endpoints
|
|
223
|
+
const prevEndpointKeys = Object.keys(prevEndpoints);
|
|
224
|
+
const currEndpointKeys = Object.keys(currEndpoints);
|
|
225
|
+
|
|
226
|
+
for (const path of currEndpointKeys) {
|
|
227
|
+
if (!prevEndpointKeys.includes(path)) {
|
|
228
|
+
diff.addedEndpoints.push(path);
|
|
229
|
+
} else if (JSON.stringify(prevEndpoints[path]) !== JSON.stringify(currEndpoints[path])) {
|
|
230
|
+
diff.modifiedEndpoints.push(path);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
for (const path of prevEndpointKeys) {
|
|
235
|
+
if (!currEndpointKeys.includes(path)) {
|
|
236
|
+
diff.removedEndpoints.push(path);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Compare schemas
|
|
241
|
+
const prevSchemaKeys = Object.keys(prevSchemas);
|
|
242
|
+
const currSchemaKeys = Object.keys(currSchemas);
|
|
243
|
+
|
|
244
|
+
for (const schema of currSchemaKeys) {
|
|
245
|
+
if (!prevSchemaKeys.includes(schema)) {
|
|
246
|
+
diff.addedSchemas.push(schema);
|
|
247
|
+
} else if (JSON.stringify(prevSchemas[schema]) !== JSON.stringify(currSchemas[schema])) {
|
|
248
|
+
diff.modifiedSchemas.push(schema);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
for (const schema of prevSchemaKeys) {
|
|
253
|
+
if (!currSchemaKeys.includes(schema)) {
|
|
254
|
+
diff.removedSchemas.push(schema);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Determine changes and severity
|
|
259
|
+
if (diff.removedEndpoints.length > 0) {
|
|
260
|
+
changes.push('endpoints_removed');
|
|
261
|
+
severity = 'breaking';
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (diff.removedSchemas.length > 0) {
|
|
265
|
+
changes.push('schemas_removed');
|
|
266
|
+
severity = 'breaking';
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (diff.modifiedEndpoints.length > 0) {
|
|
270
|
+
changes.push('endpoints_modified');
|
|
271
|
+
if (severity !== 'breaking') severity = 'major';
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (diff.modifiedSchemas.length > 0) {
|
|
275
|
+
changes.push('schemas_modified');
|
|
276
|
+
if (severity !== 'breaking') severity = 'major';
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (diff.addedEndpoints.length > 0) {
|
|
280
|
+
changes.push('endpoints_added');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (diff.addedSchemas.length > 0) {
|
|
284
|
+
changes.push('schemas_added');
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (diff.versionChanged) {
|
|
288
|
+
changes.push('version');
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Check overall hash change
|
|
292
|
+
if (prev.stateHash !== curr.stateHash && changes.length === 0) {
|
|
293
|
+
changes.push('content');
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const result: ChangeDetection = {
|
|
297
|
+
hasChanged: changes.length > 0,
|
|
298
|
+
changes,
|
|
299
|
+
oldVersion: prev.version,
|
|
300
|
+
newVersion: curr.version,
|
|
301
|
+
diff
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
if (changes.length > 0) {
|
|
305
|
+
result.severity = severity;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
}
|