@bernierllc/github-parser 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.
@@ -0,0 +1,46 @@
1
+ import type { GitHubWebhookPayload, ParsedWebhookEvent, ParsedCommit, ParsedRepository } from './types';
2
+ /**
3
+ * GitHub webhook payload parser
4
+ */
5
+ export declare class GitHubParser {
6
+ /**
7
+ * Parse GitHub webhook payload
8
+ */
9
+ parse(payload: GitHubWebhookPayload): ParsedWebhookEvent;
10
+ /**
11
+ * Extract commits from push webhook
12
+ */
13
+ extractCommits(payload: GitHubWebhookPayload): ParsedCommit[];
14
+ /**
15
+ * Extract single commit
16
+ */
17
+ extractCommit(payload: GitHubWebhookPayload): ParsedCommit | null;
18
+ /**
19
+ * Parse commit object
20
+ */
21
+ private parseCommit;
22
+ /**
23
+ * Extract repository information
24
+ */
25
+ extractRepository(payload: GitHubWebhookPayload): ParsedRepository;
26
+ /**
27
+ * Normalize event type
28
+ */
29
+ normalizeEventType(payload: GitHubWebhookPayload): string;
30
+ /**
31
+ * Extract action (for pull_request, issues, etc.)
32
+ */
33
+ private extractAction;
34
+ /**
35
+ * Extract branch name
36
+ */
37
+ private extractBranch;
38
+ /**
39
+ * Extract git ref
40
+ */
41
+ private extractRef;
42
+ /**
43
+ * Extract sender information
44
+ */
45
+ private extractSender;
46
+ }
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+ /*
3
+ Copyright (c) 2025 Bernier LLC
4
+
5
+ This file is licensed to the client under a limited-use license.
6
+ The client may use and modify this code *only within the scope of the project it was delivered for*.
7
+ Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.GitHubParser = void 0;
11
+ const logger_1 = require("@bernierllc/logger");
12
+ const logger = (0, logger_1.createLogger)();
13
+ /**
14
+ * GitHub webhook payload parser
15
+ */
16
+ class GitHubParser {
17
+ /**
18
+ * Parse GitHub webhook payload
19
+ */
20
+ parse(payload) {
21
+ const eventType = this.normalizeEventType(payload);
22
+ const repository = this.extractRepository(payload);
23
+ const parsed = {
24
+ type: eventType,
25
+ repository,
26
+ action: this.extractAction(payload),
27
+ branch: this.extractBranch(payload),
28
+ ref: this.extractRef(payload),
29
+ sender: this.extractSender(payload)
30
+ };
31
+ // Extract commits based on event type
32
+ if (eventType === 'push') {
33
+ parsed.commits = this.extractCommits(payload);
34
+ }
35
+ else {
36
+ const commit = this.extractCommit(payload);
37
+ if (commit) {
38
+ parsed.commit = commit;
39
+ }
40
+ }
41
+ logger.debug('Parsed GitHub webhook', { eventType, repository: repository.fullName });
42
+ return parsed;
43
+ }
44
+ /**
45
+ * Extract commits from push webhook
46
+ */
47
+ extractCommits(payload) {
48
+ const commits = payload.commits || [];
49
+ return commits.map((commit) => this.parseCommit(commit));
50
+ }
51
+ /**
52
+ * Extract single commit
53
+ */
54
+ extractCommit(payload) {
55
+ const commit = payload.commit;
56
+ if (!commit) {
57
+ return null;
58
+ }
59
+ return this.parseCommit(commit);
60
+ }
61
+ /**
62
+ * Parse commit object
63
+ */
64
+ parseCommit(commit) {
65
+ const author = commit.author;
66
+ const committer = commit.committer;
67
+ // Parse additions and deletions - can be arrays or numbers
68
+ let additions = 0;
69
+ let deletions = 0;
70
+ if (Array.isArray(commit.added)) {
71
+ additions = commit.added.length;
72
+ }
73
+ else if (typeof commit.additions === 'number') {
74
+ additions = commit.additions;
75
+ }
76
+ if (Array.isArray(commit.removed)) {
77
+ deletions = commit.removed.length;
78
+ }
79
+ else if (typeof commit.deletions === 'number') {
80
+ deletions = commit.deletions;
81
+ }
82
+ return {
83
+ sha: String(commit.sha || commit.id || ''),
84
+ message: String(commit.message || ''),
85
+ author: {
86
+ name: String(author?.name || committer?.name || ''),
87
+ email: String(author?.email || committer?.email || ''),
88
+ username: author?.username ? String(author.username) : undefined
89
+ },
90
+ timestamp: commit.timestamp
91
+ ? new Date(String(commit.timestamp))
92
+ : new Date(),
93
+ url: String(commit.url || ''),
94
+ additions,
95
+ deletions,
96
+ files: commit.modified || []
97
+ };
98
+ }
99
+ /**
100
+ * Extract repository information
101
+ */
102
+ extractRepository(payload) {
103
+ const repo = payload.repository;
104
+ if (!repo) {
105
+ throw new Error('Repository information not found in payload');
106
+ }
107
+ const owner = repo.owner;
108
+ return {
109
+ id: Number(repo.id || 0),
110
+ name: String(repo.name || ''),
111
+ fullName: String(repo.full_name || repo.fullName || ''),
112
+ owner: {
113
+ login: String(owner?.login || ''),
114
+ type: String(owner?.type || 'User')
115
+ },
116
+ defaultBranch: String(repo.default_branch || repo.defaultBranch || 'main'),
117
+ url: String(repo.html_url || repo.htmlUrl || repo.url || ''),
118
+ private: Boolean(repo.private)
119
+ };
120
+ }
121
+ /**
122
+ * Normalize event type
123
+ */
124
+ normalizeEventType(payload) {
125
+ // Check X-GitHub-Event header or payload field
126
+ const eventType = payload['X-GitHub-Event'] ||
127
+ payload.action ||
128
+ payload.type;
129
+ // If we have an explicit event type, normalize it
130
+ if (eventType) {
131
+ if (eventType.includes('push'))
132
+ return 'push';
133
+ if (eventType.includes('pull_request'))
134
+ return 'pull_request';
135
+ if (eventType.includes('issue'))
136
+ return 'issues';
137
+ if (eventType.includes('release'))
138
+ return 'release';
139
+ return eventType.toLowerCase();
140
+ }
141
+ // Infer event type from payload structure
142
+ if (payload.ref && typeof payload.ref === 'string' && payload.ref.startsWith('refs/heads/')) {
143
+ return 'push';
144
+ }
145
+ if (payload.pull_request) {
146
+ return 'pull_request';
147
+ }
148
+ if (payload.issue) {
149
+ return 'issues';
150
+ }
151
+ if (payload.release) {
152
+ return 'release';
153
+ }
154
+ return 'unknown';
155
+ }
156
+ /**
157
+ * Extract action (for pull_request, issues, etc.)
158
+ */
159
+ extractAction(payload) {
160
+ return payload.action;
161
+ }
162
+ /**
163
+ * Extract branch name
164
+ */
165
+ extractBranch(payload) {
166
+ const ref = this.extractRef(payload);
167
+ if (!ref)
168
+ return undefined;
169
+ // Extract branch from ref (e.g., "refs/heads/main" -> "main")
170
+ const match = ref.match(/^refs\/heads\/(.+)$/);
171
+ return match ? match[1] : ref;
172
+ }
173
+ /**
174
+ * Extract git ref
175
+ */
176
+ extractRef(payload) {
177
+ return payload.ref || undefined;
178
+ }
179
+ /**
180
+ * Extract sender information
181
+ */
182
+ extractSender(payload) {
183
+ const sender = payload.sender;
184
+ if (!sender)
185
+ return undefined;
186
+ return {
187
+ login: String(sender.login || ''),
188
+ type: String(sender.type || 'User')
189
+ };
190
+ }
191
+ }
192
+ exports.GitHubParser = GitHubParser;
@@ -0,0 +1,2 @@
1
+ export { GitHubParser } from './github-parser';
2
+ export type { GitHubWebhookPayload, ParsedWebhookEvent, ParsedCommit, ParsedRepository } from './types';
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ /*
3
+ Copyright (c) 2025 Bernier LLC
4
+
5
+ This file is licensed to the client under a limited-use license.
6
+ The client may use and modify this code *only within the scope of the project it was delivered for*.
7
+ Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.GitHubParser = void 0;
11
+ var github_parser_1 = require("./github-parser");
12
+ Object.defineProperty(exports, "GitHubParser", { enumerable: true, get: function () { return github_parser_1.GitHubParser; } });
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Raw GitHub webhook payload
3
+ */
4
+ export interface GitHubWebhookPayload {
5
+ [key: string]: unknown;
6
+ }
7
+ /**
8
+ * Parsed commit information
9
+ */
10
+ export interface ParsedCommit {
11
+ /** Commit SHA */
12
+ sha: string;
13
+ /** Commit message */
14
+ message: string;
15
+ /** Commit author */
16
+ author: {
17
+ name: string;
18
+ email: string;
19
+ username?: string;
20
+ };
21
+ /** Commit timestamp */
22
+ timestamp: Date;
23
+ /** Commit URL */
24
+ url: string;
25
+ /** Lines added */
26
+ additions: number;
27
+ /** Lines deleted */
28
+ deletions: number;
29
+ /** Files changed */
30
+ files: string[];
31
+ }
32
+ /**
33
+ * Parsed repository information
34
+ */
35
+ export interface ParsedRepository {
36
+ /** Repository ID */
37
+ id: number;
38
+ /** Repository name */
39
+ name: string;
40
+ /** Full repository name (owner/repo) */
41
+ fullName: string;
42
+ /** Repository owner */
43
+ owner: {
44
+ login: string;
45
+ type: string;
46
+ };
47
+ /** Default branch */
48
+ defaultBranch: string;
49
+ /** Repository URL */
50
+ url: string;
51
+ /** Whether repository is private */
52
+ private: boolean;
53
+ }
54
+ /**
55
+ * Parsed webhook event
56
+ */
57
+ export interface ParsedWebhookEvent {
58
+ /** Event type */
59
+ type: 'push' | 'pull_request' | 'issues' | 'release' | string;
60
+ /** Event action (for pull_request, issues, etc.) */
61
+ action?: string;
62
+ /** Repository information */
63
+ repository: ParsedRepository;
64
+ /** Commits (for push events) */
65
+ commits?: ParsedCommit[];
66
+ /** Single commit (for some events) */
67
+ commit?: ParsedCommit;
68
+ /** Branch name */
69
+ branch?: string;
70
+ /** Git ref */
71
+ ref?: string;
72
+ /** Event sender */
73
+ sender?: {
74
+ login: string;
75
+ type: string;
76
+ };
77
+ }
package/dist/types.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ /*
3
+ Copyright (c) 2025 Bernier LLC
4
+
5
+ This file is licensed to the client under a limited-use license.
6
+ The client may use and modify this code *only within the scope of the project it was delivered for*.
7
+ Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,30 @@
1
+ /*
2
+ Copyright (c) 2025 Bernier LLC
3
+
4
+ This file is licensed to the client under a limited-use license.
5
+ The client may use and modify this code *only within the scope of the project it was delivered for*.
6
+ Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
7
+ */
8
+
9
+ module.exports = {
10
+ preset: 'ts-jest',
11
+ testEnvironment: '<rootDir>/../../../jest-environment-node-fixed.cjs',
12
+ roots: ['<rootDir>/__tests__'],
13
+ testMatch: ['**/*.test.ts'],
14
+ collectCoverageFrom: [
15
+ 'src/**/*.ts',
16
+ '!src/**/*.d.ts',
17
+ '!src/index.ts'
18
+ ],
19
+ coverageThreshold: {
20
+ global: {
21
+ branches: 90,
22
+ functions: 90,
23
+ lines: 90,
24
+ statements: 90
25
+ }
26
+ },
27
+ coverageDirectory: 'coverage',
28
+ verbose: true
29
+ };
30
+
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@bernierllc/github-parser",
3
+ "version": "1.0.0",
4
+ "description": "GitHub webhook payload parser and transformer",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "keywords": [
8
+ "github",
9
+ "webhook",
10
+ "parser",
11
+ "git"
12
+ ],
13
+ "author": "Bernier LLC",
14
+ "license": "SEE LICENSE IN LICENSE",
15
+ "dependencies": {
16
+ "@bernierllc/logger": "1.0.3"
17
+ },
18
+ "devDependencies": {
19
+ "@types/jest": "^29.5.0",
20
+ "@types/node": "^20.0.0",
21
+ "@typescript-eslint/eslint-plugin": "^6.0.0",
22
+ "@typescript-eslint/parser": "^6.0.0",
23
+ "eslint": "^8.0.0",
24
+ "jest": "^29.5.0",
25
+ "rimraf": "^5.0.0",
26
+ "ts-jest": "^29.1.0",
27
+ "typescript": "^5.0.0"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public",
31
+ "registry": "https://registry.npmjs.org/"
32
+ },
33
+ "scripts": {
34
+ "build": "tsc",
35
+ "test": "jest",
36
+ "test:run": "jest",
37
+ "test:watch": "jest --watch",
38
+ "test:coverage": "jest --coverage",
39
+ "lint": "eslint src --ext .ts",
40
+ "clean": "rimraf dist"
41
+ }
42
+ }
@@ -0,0 +1,213 @@
1
+ /*
2
+ Copyright (c) 2025 Bernier LLC
3
+
4
+ This file is licensed to the client under a limited-use license.
5
+ The client may use and modify this code *only within the scope of the project it was delivered for*.
6
+ Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
7
+ */
8
+
9
+ import { createLogger } from '@bernierllc/logger';
10
+ import type {
11
+ GitHubWebhookPayload,
12
+ ParsedWebhookEvent,
13
+ ParsedCommit,
14
+ ParsedRepository
15
+ } from './types';
16
+
17
+ const logger = createLogger();
18
+
19
+ /**
20
+ * GitHub webhook payload parser
21
+ */
22
+ export class GitHubParser {
23
+ /**
24
+ * Parse GitHub webhook payload
25
+ */
26
+ parse(payload: GitHubWebhookPayload): ParsedWebhookEvent {
27
+ const eventType = this.normalizeEventType(payload);
28
+ const repository = this.extractRepository(payload);
29
+
30
+ const parsed: ParsedWebhookEvent = {
31
+ type: eventType,
32
+ repository,
33
+ action: this.extractAction(payload),
34
+ branch: this.extractBranch(payload),
35
+ ref: this.extractRef(payload),
36
+ sender: this.extractSender(payload)
37
+ };
38
+
39
+ // Extract commits based on event type
40
+ if (eventType === 'push') {
41
+ parsed.commits = this.extractCommits(payload);
42
+ } else {
43
+ const commit = this.extractCommit(payload);
44
+ if (commit) {
45
+ parsed.commit = commit;
46
+ }
47
+ }
48
+
49
+ logger.debug('Parsed GitHub webhook', { eventType, repository: repository.fullName });
50
+
51
+ return parsed;
52
+ }
53
+
54
+ /**
55
+ * Extract commits from push webhook
56
+ */
57
+ extractCommits(payload: GitHubWebhookPayload): ParsedCommit[] {
58
+ const commits = (payload.commits as unknown[]) || [];
59
+ return commits.map((commit: unknown) => this.parseCommit(commit as Record<string, unknown>));
60
+ }
61
+
62
+ /**
63
+ * Extract single commit
64
+ */
65
+ extractCommit(payload: GitHubWebhookPayload): ParsedCommit | null {
66
+ const commit = payload.commit as Record<string, unknown> | undefined;
67
+ if (!commit) {
68
+ return null;
69
+ }
70
+ return this.parseCommit(commit);
71
+ }
72
+
73
+ /**
74
+ * Parse commit object
75
+ */
76
+ private parseCommit(commit: Record<string, unknown>): ParsedCommit {
77
+ const author = commit.author as Record<string, unknown> | undefined;
78
+ const committer = commit.committer as Record<string, unknown> | undefined;
79
+
80
+ // Parse additions and deletions - can be arrays or numbers
81
+ let additions = 0;
82
+ let deletions = 0;
83
+
84
+ if (Array.isArray(commit.added)) {
85
+ additions = commit.added.length;
86
+ } else if (typeof commit.additions === 'number') {
87
+ additions = commit.additions;
88
+ }
89
+
90
+ if (Array.isArray(commit.removed)) {
91
+ deletions = commit.removed.length;
92
+ } else if (typeof commit.deletions === 'number') {
93
+ deletions = commit.deletions;
94
+ }
95
+
96
+ return {
97
+ sha: String(commit.sha || commit.id || ''),
98
+ message: String(commit.message || ''),
99
+ author: {
100
+ name: String(author?.name || committer?.name || ''),
101
+ email: String(author?.email || committer?.email || ''),
102
+ username: author?.username ? String(author.username) : undefined
103
+ },
104
+ timestamp: commit.timestamp
105
+ ? new Date(String(commit.timestamp))
106
+ : new Date(),
107
+ url: String(commit.url || ''),
108
+ additions,
109
+ deletions,
110
+ files: (commit.modified as string[]) || []
111
+ };
112
+ }
113
+
114
+ /**
115
+ * Extract repository information
116
+ */
117
+ extractRepository(payload: GitHubWebhookPayload): ParsedRepository {
118
+ const repo = payload.repository as Record<string, unknown> | undefined;
119
+ if (!repo) {
120
+ throw new Error('Repository information not found in payload');
121
+ }
122
+
123
+ const owner = repo.owner as Record<string, unknown> | undefined;
124
+
125
+ return {
126
+ id: Number(repo.id || 0),
127
+ name: String(repo.name || ''),
128
+ fullName: String(repo.full_name || repo.fullName || ''),
129
+ owner: {
130
+ login: String(owner?.login || ''),
131
+ type: String(owner?.type || 'User')
132
+ },
133
+ defaultBranch: String(repo.default_branch || repo.defaultBranch || 'main'),
134
+ url: String(repo.html_url || repo.htmlUrl || repo.url || ''),
135
+ private: Boolean(repo.private)
136
+ };
137
+ }
138
+
139
+ /**
140
+ * Normalize event type
141
+ */
142
+ normalizeEventType(payload: GitHubWebhookPayload): string {
143
+ // Check X-GitHub-Event header or payload field
144
+ const eventType = (payload['X-GitHub-Event'] as string) ||
145
+ (payload.action as string) ||
146
+ (payload.type as string);
147
+
148
+ // If we have an explicit event type, normalize it
149
+ if (eventType) {
150
+ if (eventType.includes('push')) return 'push';
151
+ if (eventType.includes('pull_request')) return 'pull_request';
152
+ if (eventType.includes('issue')) return 'issues';
153
+ if (eventType.includes('release')) return 'release';
154
+ return eventType.toLowerCase();
155
+ }
156
+
157
+ // Infer event type from payload structure
158
+ if (payload.ref && typeof payload.ref === 'string' && payload.ref.startsWith('refs/heads/')) {
159
+ return 'push';
160
+ }
161
+ if (payload.pull_request) {
162
+ return 'pull_request';
163
+ }
164
+ if (payload.issue) {
165
+ return 'issues';
166
+ }
167
+ if (payload.release) {
168
+ return 'release';
169
+ }
170
+
171
+ return 'unknown';
172
+ }
173
+
174
+ /**
175
+ * Extract action (for pull_request, issues, etc.)
176
+ */
177
+ private extractAction(payload: GitHubWebhookPayload): string | undefined {
178
+ return payload.action as string | undefined;
179
+ }
180
+
181
+ /**
182
+ * Extract branch name
183
+ */
184
+ private extractBranch(payload: GitHubWebhookPayload): string | undefined {
185
+ const ref = this.extractRef(payload);
186
+ if (!ref) return undefined;
187
+
188
+ // Extract branch from ref (e.g., "refs/heads/main" -> "main")
189
+ const match = ref.match(/^refs\/heads\/(.+)$/);
190
+ return match ? match[1] : ref;
191
+ }
192
+
193
+ /**
194
+ * Extract git ref
195
+ */
196
+ private extractRef(payload: GitHubWebhookPayload): string | undefined {
197
+ return (payload.ref as string) || undefined;
198
+ }
199
+
200
+ /**
201
+ * Extract sender information
202
+ */
203
+ private extractSender(payload: GitHubWebhookPayload): { login: string; type: string } | undefined {
204
+ const sender = payload.sender as Record<string, unknown> | undefined;
205
+ if (!sender) return undefined;
206
+
207
+ return {
208
+ login: String(sender.login || ''),
209
+ type: String(sender.type || 'User')
210
+ };
211
+ }
212
+ }
213
+
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ /*
2
+ Copyright (c) 2025 Bernier LLC
3
+
4
+ This file is licensed to the client under a limited-use license.
5
+ The client may use and modify this code *only within the scope of the project it was delivered for*.
6
+ Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
7
+ */
8
+
9
+ export { GitHubParser } from './github-parser';
10
+ export type {
11
+ GitHubWebhookPayload,
12
+ ParsedWebhookEvent,
13
+ ParsedCommit,
14
+ ParsedRepository
15
+ } from './types';
16
+