@devrev/git-sdk 0.11.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.
Files changed (95) hide show
  1. package/README.md +2 -0
  2. package/dist/adapter/adapter.d.ts +9 -0
  3. package/dist/adapter/adapter.js +3 -0
  4. package/dist/adapter/adapter.js.map +1 -0
  5. package/dist/config/create-config.d.ts +34 -0
  6. package/dist/config/create-config.js +33 -0
  7. package/dist/config/create-config.js.map +1 -0
  8. package/dist/config/index.d.ts +4 -0
  9. package/dist/config/index.js +9 -0
  10. package/dist/config/index.js.map +1 -0
  11. package/dist/devrev/client.d.ts +104 -0
  12. package/dist/devrev/client.js +113 -0
  13. package/dist/devrev/client.js.map +1 -0
  14. package/dist/devrev/http.d.ts +42 -0
  15. package/dist/devrev/http.js +105 -0
  16. package/dist/devrev/http.js.map +1 -0
  17. package/dist/devrev/index.d.ts +7 -0
  18. package/dist/devrev/index.js +43 -0
  19. package/dist/devrev/index.js.map +1 -0
  20. package/dist/devrev/managers/code_change_manager.d.ts +48 -0
  21. package/dist/devrev/managers/code_change_manager.js +227 -0
  22. package/dist/devrev/managers/code_change_manager.js.map +1 -0
  23. package/dist/devrev/managers/code_change_schema.d.ts +12 -0
  24. package/dist/devrev/managers/code_change_schema.js +317 -0
  25. package/dist/devrev/managers/code_change_schema.js.map +1 -0
  26. package/dist/devrev/managers/custom_link_type_manager.d.ts +33 -0
  27. package/dist/devrev/managers/custom_link_type_manager.js +94 -0
  28. package/dist/devrev/managers/custom_link_type_manager.js.map +1 -0
  29. package/dist/devrev/managers/dev_user_manager.d.ts +53 -0
  30. package/dist/devrev/managers/dev_user_manager.js +155 -0
  31. package/dist/devrev/managers/dev_user_manager.js.map +1 -0
  32. package/dist/devrev/managers/index.d.ts +9 -0
  33. package/dist/devrev/managers/index.js +20 -0
  34. package/dist/devrev/managers/index.js.map +1 -0
  35. package/dist/devrev/managers/link_manager.d.ts +41 -0
  36. package/dist/devrev/managers/link_manager.js +111 -0
  37. package/dist/devrev/managers/link_manager.js.map +1 -0
  38. package/dist/devrev/managers/tenant_fragment_manager.d.ts +26 -0
  39. package/dist/devrev/managers/tenant_fragment_manager.js +68 -0
  40. package/dist/devrev/managers/tenant_fragment_manager.js.map +1 -0
  41. package/dist/devrev/managers/timeline_manager.d.ts +86 -0
  42. package/dist/devrev/managers/timeline_manager.js +184 -0
  43. package/dist/devrev/managers/timeline_manager.js.map +1 -0
  44. package/dist/devrev/types.d.ts +139 -0
  45. package/dist/devrev/types.js +127 -0
  46. package/dist/devrev/types.js.map +1 -0
  47. package/dist/handlers/commit_push_handler.d.ts +31 -0
  48. package/dist/handlers/commit_push_handler.js +145 -0
  49. package/dist/handlers/commit_push_handler.js.map +1 -0
  50. package/dist/handlers/helpers.d.ts +87 -0
  51. package/dist/handlers/helpers.js +201 -0
  52. package/dist/handlers/helpers.js.map +1 -0
  53. package/dist/handlers/index.d.ts +12 -0
  54. package/dist/handlers/index.js +26 -0
  55. package/dist/handlers/index.js.map +1 -0
  56. package/dist/handlers/pr_create_handler.d.ts +36 -0
  57. package/dist/handlers/pr_create_handler.js +121 -0
  58. package/dist/handlers/pr_create_handler.js.map +1 -0
  59. package/dist/handlers/pr_merge_handler.d.ts +29 -0
  60. package/dist/handlers/pr_merge_handler.js +86 -0
  61. package/dist/handlers/pr_merge_handler.js.map +1 -0
  62. package/dist/handlers/pr_review_handler.d.ts +30 -0
  63. package/dist/handlers/pr_review_handler.js +109 -0
  64. package/dist/handlers/pr_review_handler.js.map +1 -0
  65. package/dist/handlers/registry.d.ts +9 -0
  66. package/dist/handlers/registry.js +22 -0
  67. package/dist/handlers/registry.js.map +1 -0
  68. package/dist/handlers/types.d.ts +46 -0
  69. package/dist/handlers/types.js +6 -0
  70. package/dist/handlers/types.js.map +1 -0
  71. package/dist/index.d.ts +12 -0
  72. package/dist/index.js +65 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/orchestrator/event_orchestrator.d.ts +99 -0
  75. package/dist/orchestrator/event_orchestrator.js +137 -0
  76. package/dist/orchestrator/event_orchestrator.js.map +1 -0
  77. package/dist/orchestrator/index.d.ts +7 -0
  78. package/dist/orchestrator/index.js +12 -0
  79. package/dist/orchestrator/index.js.map +1 -0
  80. package/dist/types/config.d.ts +22 -0
  81. package/dist/types/config.js +6 -0
  82. package/dist/types/config.js.map +1 -0
  83. package/dist/types/events.d.ts +50 -0
  84. package/dist/types/events.js +14 -0
  85. package/dist/types/events.js.map +1 -0
  86. package/dist/types/index.d.ts +7 -0
  87. package/dist/types/index.js +24 -0
  88. package/dist/types/index.js.map +1 -0
  89. package/dist/types/provider.d.ts +20 -0
  90. package/dist/types/provider.js +14 -0
  91. package/dist/types/provider.js.map +1 -0
  92. package/dist/types/types.d.ts +48 -0
  93. package/dist/types/types.js +9 -0
  94. package/dist/types/types.js.map +1 -0
  95. package/package.json +61 -0
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ /**
3
+ * Handler for push events.
4
+ *
5
+ * This handler processes push events and updates the associated code_change
6
+ * custom objects with commit information. It also creates timeline entries
7
+ * for each commit in the push.
8
+ */
9
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
10
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
13
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
14
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
15
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
16
+ });
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.handlePushEvent = void 0;
20
+ const types_1 = require("../devrev/types");
21
+ const helpers_1 = require("./helpers");
22
+ // =============================================================================
23
+ // Helper Functions
24
+ // =============================================================================
25
+ /**
26
+ * Updates a code_change with commit information.
27
+ *
28
+ * @param codeChange - The code_change custom object
29
+ * @param commits - The commits from the push event
30
+ * @param context - The handler context
31
+ */
32
+ function updateCodeChangeWithCommits(codeChange, commits, context) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const { client } = context;
35
+ if (commits.length === 0) {
36
+ return;
37
+ }
38
+ const latestCommit = commits[commits.length - 1];
39
+ // Check if first_commit_at is already set on the object
40
+ const existingFirstCommitAt = (0, helpers_1.getCodeChangeCustomField)(codeChange, types_1.CodeChangeFields.FIRST_COMMIT_AT);
41
+ // Build update request
42
+ const updateRequest = {
43
+ id: codeChange.id,
44
+ last_commit_at: latestCommit.timestamp,
45
+ source_commits: [latestCommit.sha],
46
+ };
47
+ // Only set first_commit_at if not already populated
48
+ if (!existingFirstCommitAt) {
49
+ updateRequest.first_commit_at = commits[0].timestamp;
50
+ }
51
+ yield client.codeChange.update(updateRequest, codeChange);
52
+ });
53
+ }
54
+ // =============================================================================
55
+ // Main Handler
56
+ // =============================================================================
57
+ /**
58
+ * Process a push event.
59
+ *
60
+ * This function handles the core business logic for creating/updating custom code_change
61
+ * objects and timeline entries when commits are pushed to a PR branch.
62
+ *
63
+ * Behavior:
64
+ * - Push events with no commits are skipped (returns success with skip message)
65
+ * - Push events with no associated PRs are skipped (returns success with skip message)
66
+ * - For new code_change objects, historical commits are fetched from the adapter
67
+ * and populated (first_commit_at, last_commit_at, timeline entries)
68
+ *
69
+ * For each associated PR:
70
+ * 1. Gets or creates the code_change custom object
71
+ * 2. Updates source_commits, first_commit_at (if not set), and last_commit_at
72
+ * 3. Creates timeline entries for each commit (when updating existing code_change)
73
+ *
74
+ * @param input - The normalized push event
75
+ * @param context - The handler context with DevRev client and adapter
76
+ * @returns Handler result indicating success or failure
77
+ */
78
+ function handlePushEvent(input, context) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ const { actor, commits, repository } = input;
81
+ const { eventType, adapter } = context;
82
+ const repoName = repository.name;
83
+ // If no commits in the push, nothing to update
84
+ if (commits.length === 0) {
85
+ return {
86
+ message: 'No commits in push event. Skipping.',
87
+ success: true,
88
+ };
89
+ }
90
+ // Get associated PRs using the adapter
91
+ // Use the latest commit to find associated PRs
92
+ const latestCommit = commits[commits.length - 1];
93
+ const associatedPRs = adapter.getPullRequestByCommit(latestCommit);
94
+ // If no associated PRs, nothing to process
95
+ if (associatedPRs.length === 0) {
96
+ return {
97
+ message: 'No associated PRs found for this push event. Skipping.',
98
+ success: true,
99
+ };
100
+ }
101
+ const results = [];
102
+ for (const pr of associatedPRs) {
103
+ try {
104
+ // Get or create code_change custom object for this PR using shared helper
105
+ const { codeChange, isNew } = yield (0, helpers_1.getOrCreateCodeChangeForPR)(pr, repoName, context);
106
+ // Update commit fields on the code_change
107
+ yield updateCodeChangeWithCommits(codeChange, commits, context);
108
+ // Create timeline entries for commits (only if not a new code_change)
109
+ // When a code_change is new, we may have already created timeline entries
110
+ // for all historical commits via adapter.getHistoricCommitsForPullRequest
111
+ if (!isNew) {
112
+ yield (0, helpers_1.createCommitTimelineEntries)(codeChange.id, actor, commits, pr.sourceBranch.name, repoName, eventType, context);
113
+ }
114
+ results.push({
115
+ message: isNew
116
+ ? `Created code_change and processed ${commits.length} commits`
117
+ : `Updated code_change with ${commits.length} commits`,
118
+ pr: pr.url,
119
+ success: true,
120
+ });
121
+ }
122
+ catch (error) {
123
+ results.push({
124
+ message: (0, types_1.getErrorMessage)(error),
125
+ pr: pr.url,
126
+ success: false,
127
+ });
128
+ }
129
+ }
130
+ // Determine overall success
131
+ const allSucceeded = results.every((r) => r.success);
132
+ const anySucceeded = results.some((r) => r.success);
133
+ return {
134
+ data: { results },
135
+ message: allSucceeded
136
+ ? `Successfully processed commits for ${results.length} PR(s)`
137
+ : anySucceeded
138
+ ? `Partially processed commits: ${results.filter((r) => r.success).length}/${results.length} PRs succeeded`
139
+ : `Failed to process commits for all ${results.length} PR(s)`,
140
+ success: anySucceeded,
141
+ };
142
+ });
143
+ }
144
+ exports.handlePushEvent = handlePushEvent;
145
+ //# sourceMappingURL=commit_push_handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commit_push_handler.js","sourceRoot":"","sources":["../../src/handlers/commit_push_handler.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;;AAIH,2CAIyB;AAIzB,uCAImB;AAGnB,gFAAgF;AAChF,mBAAmB;AACnB,gFAAgF;AAEhF;;;;;;GAMG;AACH,SAAe,2BAA2B,CACxC,UAAwB,EACxB,OAA2B,EAC3B,OAAuB;;QAEvB,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAE3B,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE;YACxB,OAAO;SACR;QAED,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAEjD,wDAAwD;QACxD,MAAM,qBAAqB,GAAG,IAAA,kCAAwB,EAAC,UAAU,EAAE,wBAAgB,CAAC,eAAe,CAAC,CAAC;QAErG,uBAAuB;QACvB,MAAM,aAAa,GAAkC;YACnD,EAAE,EAAE,UAAU,CAAC,EAAE;YACjB,cAAc,EAAE,YAAY,CAAC,SAAS;YACtC,cAAc,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC;SACnC,CAAC;QAEF,oDAAoD;QACpD,IAAI,CAAC,qBAAqB,EAAE;YAC1B,aAAa,CAAC,eAAe,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;SACtD;QAED,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,aAAa,EAAE,UAAU,CAAC,CAAC;IAC5D,CAAC;CAAA;AAED,gFAAgF;AAChF,eAAe;AACf,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAsB,eAAe,CACnC,KAA0B,EAC1B,OAAuB;;QAEvB,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;QAC7C,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,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C,MAAM,UAAU;oBACxD,EAAE,EAAE,EAAE,CAAC,GAAG;oBACV,OAAO,EAAE,IAAI;iBACd,CAAC,CAAC;aACJ;YAAC,OAAO,KAAK,EAAE;gBACd,OAAO,CAAC,IAAI,CAAC;oBACX,OAAO,EAAE,IAAA,uBAAe,EAAC,KAAK,CAAC;oBAC/B,EAAE,EAAE,EAAE,CAAC,GAAG;oBACV,OAAO,EAAE,KAAK;iBACf,CAAC,CAAC;aACJ;SACF;QAED,4BAA4B;QAC5B,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;QACrD,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;QAEpD,OAAO;YACL,IAAI,EAAE,EAAE,OAAO,EAAE;YACjB,OAAO,EAAE,YAAY;gBACnB,CAAC,CAAC,sCAAsC,OAAO,CAAC,MAAM,QAAQ;gBAC9D,CAAC,CAAC,YAAY;oBACZ,CAAC,CAAC,gCAAgC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,gBAAgB;oBAC3G,CAAC,CAAC,qCAAqC,OAAO,CAAC,MAAM,QAAQ;YACjE,OAAO,EAAE,YAAY;SACtB,CAAC;IACJ,CAAC;CAAA;AAvFD,0CAuFC"}
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Shared helper functions for event handlers.
3
+ *
4
+ * This module contains common utilities used by multiple handlers to avoid
5
+ * code duplication.
6
+ */
7
+ import { CustomObject } from '@devrev/typescript-sdk/dist/auto-generated/beta/beta-devrev-sdk';
8
+ import { NormalizedActor, NormalizedCommit, NormalizedPullRequest } from '../types/types';
9
+ import { HandlerContext, HandlerResult } from './types';
10
+ /**
11
+ * Normalizes a NormalizedActor to the format required by timeline API calls.
12
+ * Handles undefined values by converting to empty strings.
13
+ *
14
+ * @param actor - The normalized actor from event data
15
+ * @returns Actor info formatted for timeline API
16
+ */
17
+ export declare function normalizeActorForTimeline(actor: NormalizedActor): {
18
+ id: string;
19
+ username: string;
20
+ url: string;
21
+ };
22
+ /**
23
+ * Reads a custom field value from a code_change object.
24
+ * Centralizes the casting and prefixing logic for reading tenant-prefixed custom fields.
25
+ *
26
+ * @param codeChange - The code_change custom object
27
+ * @param field - The field name (without the tnt__ prefix)
28
+ * @returns The field value, or undefined if not set
29
+ */
30
+ export declare function getCodeChangeCustomField(codeChange: CustomObject, field: string): unknown;
31
+ /**
32
+ * Creates a standardized error result for PR handler failures.
33
+ * Reduces duplicate error result construction across PR handlers.
34
+ *
35
+ * @param operation - Description of the failed operation (e.g., "PR creation", "PR review")
36
+ * @param pullRequest - The pull request involved
37
+ * @param error - The caught error
38
+ * @returns A HandlerResult indicating failure
39
+ */
40
+ export declare function createPRHandlerErrorResult(operation: string, pullRequest: {
41
+ number: number;
42
+ url: string;
43
+ }, error: unknown): HandlerResult;
44
+ /**
45
+ * Creates a timeline entry for a PR event.
46
+ * This is a common helper used by all PR handlers to reduce code duplication.
47
+ *
48
+ * @param codeChangeId - The code_change object ID
49
+ * @param pr - The pull request
50
+ * @param repoName - The repository name
51
+ * @param timestamp - The event timestamp
52
+ * @param message - The event message
53
+ * @param context - The handler context
54
+ * @param metadata - Optional additional metadata
55
+ */
56
+ export declare function createPRTimelineEntry(codeChangeId: string, pr: NormalizedPullRequest, repoName: string, timestamp: string, message: string, context: HandlerContext, metadata?: Record<string, unknown>): Promise<void>;
57
+ /**
58
+ * Creates timeline entries for commits.
59
+ *
60
+ * @param codeChangeId - The code_change object ID
61
+ * @param actor - The actor who pushed/authored the commits
62
+ * @param commits - The commits to create entries for
63
+ * @param branchName - The branch name
64
+ * @param repoName - The repository name
65
+ * @param eventType - The event type
66
+ * @param context - The handler context
67
+ */
68
+ export declare function createCommitTimelineEntries(codeChangeId: string, actor: NormalizedActor, commits: NormalizedCommit[], branchName: string, repoName: string, eventType: string, context: HandlerContext): Promise<void>;
69
+ /**
70
+ * Result of getOrCreateCodeChangeForPR operation.
71
+ */
72
+ export interface GetOrCreateCodeChangeResult {
73
+ codeChange: CustomObject;
74
+ isNew: boolean;
75
+ commits: NormalizedCommit[];
76
+ }
77
+ /**
78
+ * Gets an existing code_change or creates a new one for a PR.
79
+ * This is a shared helper that consolidates the common logic between
80
+ * commit_push_handler and pr_create_handler.
81
+ *
82
+ * @param pr - The normalized pull request
83
+ * @param repoName - The repository name
84
+ * @param context - The handler context
85
+ * @returns The code_change object, whether it was newly created, and fetched commits
86
+ */
87
+ export declare function getOrCreateCodeChangeForPR(pr: NormalizedPullRequest, repoName: string, context: HandlerContext): Promise<GetOrCreateCodeChangeResult>;
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+ /**
3
+ * Shared helper functions for event handlers.
4
+ *
5
+ * This module contains common utilities used by multiple handlers to avoid
6
+ * code duplication.
7
+ */
8
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.getOrCreateCodeChangeForPR = exports.createCommitTimelineEntries = exports.createPRTimelineEntry = exports.createPRHandlerErrorResult = exports.getCodeChangeCustomField = exports.normalizeActorForTimeline = void 0;
19
+ const types_1 = require("../devrev/types");
20
+ /**
21
+ * Normalizes a NormalizedActor to the format required by timeline API calls.
22
+ * Handles undefined values by converting to empty strings.
23
+ *
24
+ * @param actor - The normalized actor from event data
25
+ * @returns Actor info formatted for timeline API
26
+ */
27
+ function normalizeActorForTimeline(actor) {
28
+ return {
29
+ id: actor.external_id || '',
30
+ url: actor.url || '',
31
+ username: actor.username || '',
32
+ };
33
+ }
34
+ exports.normalizeActorForTimeline = normalizeActorForTimeline;
35
+ /**
36
+ * Reads a custom field value from a code_change object.
37
+ * Centralizes the casting and prefixing logic for reading tenant-prefixed custom fields.
38
+ *
39
+ * @param codeChange - The code_change custom object
40
+ * @param field - The field name (without the tnt__ prefix)
41
+ * @returns The field value, or undefined if not set
42
+ */
43
+ function getCodeChangeCustomField(codeChange, field) {
44
+ const customFields = codeChange.custom_fields;
45
+ return customFields === null || customFields === void 0 ? void 0 : customFields[(0, types_1.tf)(field)];
46
+ }
47
+ exports.getCodeChangeCustomField = getCodeChangeCustomField;
48
+ /**
49
+ * Creates a standardized error result for PR handler failures.
50
+ * Reduces duplicate error result construction across PR handlers.
51
+ *
52
+ * @param operation - Description of the failed operation (e.g., "PR creation", "PR review")
53
+ * @param pullRequest - The pull request involved
54
+ * @param error - The caught error
55
+ * @returns A HandlerResult indicating failure
56
+ */
57
+ function createPRHandlerErrorResult(operation, pullRequest, error) {
58
+ return {
59
+ data: {
60
+ prNumber: pullRequest.number,
61
+ prUrl: pullRequest.url,
62
+ },
63
+ message: `Failed to process ${operation}: ${(0, types_1.getErrorMessage)(error)}`,
64
+ success: false,
65
+ };
66
+ }
67
+ exports.createPRHandlerErrorResult = createPRHandlerErrorResult;
68
+ /**
69
+ * Creates a timeline entry for a PR event.
70
+ * This is a common helper used by all PR handlers to reduce code duplication.
71
+ *
72
+ * @param codeChangeId - The code_change object ID
73
+ * @param pr - The pull request
74
+ * @param repoName - The repository name
75
+ * @param timestamp - The event timestamp
76
+ * @param message - The event message
77
+ * @param context - The handler context
78
+ * @param metadata - Optional additional metadata
79
+ */
80
+ function createPRTimelineEntry(codeChangeId, pr, repoName, timestamp, message, context, metadata) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const { client, eventType } = context;
83
+ try {
84
+ const changeEventBody = client.timeline.createChangeEventBody(normalizeActorForTimeline(pr.author), pr.sourceBranch.name, repoName, timestamp, pr.url, eventType, message);
85
+ if (metadata) {
86
+ Object.assign(changeEventBody, metadata);
87
+ }
88
+ const result = yield client.timeline.createEventEntry(codeChangeId, changeEventBody);
89
+ if (!result.success) {
90
+ console.log(`Warning: Could not create timeline entry: ${result.message}`);
91
+ }
92
+ }
93
+ catch (error) {
94
+ console.log(`Warning: Error creating timeline entry: ${error}`);
95
+ }
96
+ });
97
+ }
98
+ exports.createPRTimelineEntry = createPRTimelineEntry;
99
+ /**
100
+ * Creates timeline entries for commits.
101
+ *
102
+ * @param codeChangeId - The code_change object ID
103
+ * @param actor - The actor who pushed/authored the commits
104
+ * @param commits - The commits to create entries for
105
+ * @param branchName - The branch name
106
+ * @param repoName - The repository name
107
+ * @param eventType - The event type
108
+ * @param context - The handler context
109
+ */
110
+ function createCommitTimelineEntries(codeChangeId, actor, commits, branchName, repoName, eventType, context) {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ const { client } = context;
113
+ const normalizedActor = normalizeActorForTimeline(actor);
114
+ for (const commit of commits) {
115
+ try {
116
+ const changeEventBody = client.timeline.createChangeEventBody(normalizedActor, branchName, repoName, commit.timestamp, commit.url, eventType, commit.message);
117
+ const result = yield client.timeline.createEventEntry(codeChangeId, changeEventBody);
118
+ if (!result.success) {
119
+ console.log(`Warning: Could not create timeline entry for commit ${commit.sha}: ${result.message}`);
120
+ }
121
+ }
122
+ catch (error) {
123
+ console.log(`Warning: Error creating timeline entry for commit ${commit.sha}: ${error}`);
124
+ }
125
+ }
126
+ });
127
+ }
128
+ exports.createCommitTimelineEntries = createCommitTimelineEntries;
129
+ /**
130
+ * Gets an existing code_change or creates a new one for a PR.
131
+ * This is a shared helper that consolidates the common logic between
132
+ * commit_push_handler and pr_create_handler.
133
+ *
134
+ * @param pr - The normalized pull request
135
+ * @param repoName - The repository name
136
+ * @param context - The handler context
137
+ * @returns The code_change object, whether it was newly created, and fetched commits
138
+ */
139
+ function getOrCreateCodeChangeForPR(pr, repoName, context) {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ const { client, source, eventType, adapter } = context;
142
+ // Check if code_change already exists for this PR
143
+ const existingCodeChange = yield client.codeChange.getByPrUrl(pr.url);
144
+ if (existingCodeChange) {
145
+ return { codeChange: existingCodeChange, isNew: false, commits: [] };
146
+ }
147
+ // Resolve author to DevRev user ID
148
+ const authorDevUserId = yield client.devUser.resolveByExternalId(pr.author.external_id, pr.author.email);
149
+ // Fetch all commits for the PR to get first_commit_at
150
+ let firstCommitAt;
151
+ let lastCommitAt;
152
+ let allCommits = [];
153
+ try {
154
+ allCommits = adapter.getHistoricCommitsForPullRequest(pr);
155
+ if (allCommits.length > 0) {
156
+ firstCommitAt = allCommits[0].timestamp;
157
+ lastCommitAt = allCommits[allCommits.length - 1].timestamp;
158
+ }
159
+ }
160
+ catch (error) {
161
+ console.log(`Warning: Could not fetch historical commits: ${error}`);
162
+ }
163
+ // Create the code_change
164
+ const createRequest = {
165
+ author: authorDevUserId,
166
+ external_url: pr.url,
167
+ first_commit_at: firstCommitAt,
168
+ last_commit_at: lastCommitAt,
169
+ pr_created_at: pr.createdAt,
170
+ repo_name: repoName,
171
+ source,
172
+ source_branch: pr.sourceBranch.name,
173
+ status: pr.isDraft ? types_1.CodeChangeStatus.DRAFT : types_1.CodeChangeStatus.WAITING_FOR_REVIEW,
174
+ target_branch: pr.targetBranch.name,
175
+ title: pr.title,
176
+ };
177
+ const newCodeChange = yield client.codeChange.create(createRequest);
178
+ // Create links to issues using adapter
179
+ try {
180
+ const workIds = adapter.extractWorkIds(pr);
181
+ for (const workId of workIds) {
182
+ try {
183
+ yield client.link.createIssueCodeChangeLink(workId, newCodeChange.id);
184
+ }
185
+ catch (linkError) {
186
+ console.log(`Warning: Could not create link for work item ${workId}: ${linkError}`);
187
+ }
188
+ }
189
+ }
190
+ catch (error) {
191
+ console.log(`Warning: Could not extract work IDs: ${error}`);
192
+ }
193
+ // Create timeline entries for all historical commits
194
+ if (allCommits.length > 0) {
195
+ yield createCommitTimelineEntries(newCodeChange.id, pr.author, allCommits, pr.sourceBranch.name, repoName, eventType, context);
196
+ }
197
+ return { codeChange: newCodeChange, isNew: true, commits: allCommits };
198
+ });
199
+ }
200
+ exports.getOrCreateCodeChangeForPR = getOrCreateCodeChangeForPR;
201
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/handlers/helpers.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;AAIH,2CAMyB;AAKzB;;;;;;GAMG;AACH,SAAgB,yBAAyB,CAAC,KAAsB;IAK9D,OAAO;QACL,EAAE,EAAE,KAAK,CAAC,WAAW,IAAI,EAAE;QAC3B,GAAG,EAAE,KAAK,CAAC,GAAG,IAAI,EAAE;QACpB,QAAQ,EAAE,KAAK,CAAC,QAAQ,IAAI,EAAE;KAC/B,CAAC;AACJ,CAAC;AAVD,8DAUC;AAED;;;;;;;GAOG;AACH,SAAgB,wBAAwB,CACtC,UAAwB,EACxB,KAAa;IAEb,MAAM,YAAY,GAAG,UAAU,CAAC,aAEnB,CAAC;IACd,OAAO,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAG,IAAA,UAAE,EAAC,KAAK,CAAC,CAAC,CAAC;AACnC,CAAC;AARD,4DAQC;AAED;;;;;;;;GAQG;AACH,SAAgB,0BAA0B,CACxC,SAAiB,EACjB,WAA4C,EAC5C,KAAc;IAEd,OAAO;QACL,IAAI,EAAE;YACJ,QAAQ,EAAE,WAAW,CAAC,MAAM;YAC5B,KAAK,EAAE,WAAW,CAAC,GAAG;SACvB;QACD,OAAO,EAAE,qBAAqB,SAAS,KAAK,IAAA,uBAAe,EAAC,KAAK,CAAC,EAAE;QACpE,OAAO,EAAE,KAAK;KACf,CAAC;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AAO,CAAC,GAAG,CACT,gDAAgD,MAAM,KAAK,SAAS,EAAE,CACvE,CAAC;iBACH;aACF;SACF;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,GAAG,CAAC,wCAAwC,KAAK,EAAE,CAAC,CAAC;SAC9D;QAED,qDAAqD;QACrD,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;YACzB,MAAM,2BAA2B,CAC/B,aAAa,CAAC,EAAE,EAChB,EAAE,CAAC,MAAM,EACT,UAAU,EACV,EAAE,CAAC,YAAY,CAAC,IAAI,EACpB,QAAQ,EACR,SAAS,EACT,OAAO,CACR,CAAC;SACH;QAED,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;IACzE,CAAC;CAAA;AAnFD,gEAmFC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Event handlers module.
3
+ *
4
+ * This module provides handlers for processing Git events and updating
5
+ * DevRev code_change custom objects.
6
+ */
7
+ export { handlePushEvent } from './commit_push_handler';
8
+ export { handlePRCreate, handlePRClose, } from './pr_create_handler';
9
+ export { handlePRReview } from './pr_review_handler';
10
+ export { handlePRMerge } from './pr_merge_handler';
11
+ export { DEFAULT_HANDLER_REGISTRY } from './registry';
12
+ export type { EventHandler, HandlerContext, HandlerRegistry, HandlerResult, } from './types';
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * Event handlers module.
4
+ *
5
+ * This module provides handlers for processing Git events and updating
6
+ * DevRev code_change custom objects.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.DEFAULT_HANDLER_REGISTRY = exports.handlePRMerge = exports.handlePRReview = exports.handlePRClose = exports.handlePRCreate = exports.handlePushEvent = void 0;
10
+ // Push event handler
11
+ var commit_push_handler_1 = require("./commit_push_handler");
12
+ Object.defineProperty(exports, "handlePushEvent", { enumerable: true, get: function () { return commit_push_handler_1.handlePushEvent; } });
13
+ // PR handlers
14
+ var pr_create_handler_1 = require("./pr_create_handler");
15
+ Object.defineProperty(exports, "handlePRCreate", { enumerable: true, get: function () { return pr_create_handler_1.handlePRCreate; } });
16
+ Object.defineProperty(exports, "handlePRClose", { enumerable: true, get: function () { return pr_create_handler_1.handlePRClose; } });
17
+ // PR review handler
18
+ var pr_review_handler_1 = require("./pr_review_handler");
19
+ Object.defineProperty(exports, "handlePRReview", { enumerable: true, get: function () { return pr_review_handler_1.handlePRReview; } });
20
+ // PR merge handler
21
+ var pr_merge_handler_1 = require("./pr_merge_handler");
22
+ Object.defineProperty(exports, "handlePRMerge", { enumerable: true, get: function () { return pr_merge_handler_1.handlePRMerge; } });
23
+ // Handler registry
24
+ var registry_1 = require("./registry");
25
+ Object.defineProperty(exports, "DEFAULT_HANDLER_REGISTRY", { enumerable: true, get: function () { return registry_1.DEFAULT_HANDLER_REGISTRY; } });
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/handlers/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,qBAAqB;AACrB,6DAAwD;AAA/C,sHAAA,eAAe,OAAA;AAExB,cAAc;AACd,yDAG6B;AAF3B,mHAAA,cAAc,OAAA;AACd,kHAAA,aAAa,OAAA;AAGf,oBAAoB;AACpB,yDAAqD;AAA5C,mHAAA,cAAc,OAAA;AAEvB,mBAAmB;AACnB,uDAAmD;AAA1C,iHAAA,aAAa,OAAA;AAEtB,mBAAmB;AACnB,uCAAsD;AAA7C,oHAAA,wBAAwB,OAAA"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Handler for pull request events.
3
+ *
4
+ * This module handles PR creation and close events, creating the corresponding
5
+ * code_change custom objects with all necessary metadata. It also creates
6
+ * links to associated issues and timeline entries.
7
+ */
8
+ import { NormalizedPullRequestCreateEvent, NormalizedPullRequestCloseEvent } from '../types/events';
9
+ import { HandlerContext, HandlerResult } from './types';
10
+ /**
11
+ * Process a pull request creation event.
12
+ *
13
+ * This function handles the core business logic for creating custom code_change
14
+ * objects when a new pull request is opened.
15
+ *
16
+ * The handler will:
17
+ * 1. Check if a code_change already exists for this PR (by URL)
18
+ * 2. If not, create a new code_change with PR metadata
19
+ * 3. Extract issue IDs from branch/PR title/body and create links
20
+ * 4. Fetch and store commit timestamps (first_commit_at, last_commit_at)
21
+ * 5. Create timeline entries for historical commits
22
+ * 6. Create a timeline entry for the PR opened event
23
+ *
24
+ * @param input - The normalized PR creation event
25
+ * @param context - The handler context with DevRev client and adapter
26
+ * @returns Handler result indicating success or failure
27
+ */
28
+ export declare function handlePRCreate(input: NormalizedPullRequestCreateEvent, context: HandlerContext): Promise<HandlerResult>;
29
+ /**
30
+ * Process a pull request close event (without merge).
31
+ *
32
+ * @param input - The normalized PR close event
33
+ * @param context - The handler context
34
+ * @returns Handler result indicating success or failure
35
+ */
36
+ export declare function handlePRClose(input: NormalizedPullRequestCloseEvent, context: HandlerContext): Promise<HandlerResult>;
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ /**
3
+ * Handler for pull request events.
4
+ *
5
+ * This module handles PR creation and close events, creating the corresponding
6
+ * code_change custom objects with all necessary metadata. It also creates
7
+ * links to associated issues and timeline entries.
8
+ */
9
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
10
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
13
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
14
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
15
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
16
+ });
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.handlePRClose = exports.handlePRCreate = void 0;
20
+ const types_1 = require("../devrev/types");
21
+ const helpers_1 = require("./helpers");
22
+ // =============================================================================
23
+ // PR Create Handler
24
+ // =============================================================================
25
+ /**
26
+ * Process a pull request creation event.
27
+ *
28
+ * This function handles the core business logic for creating custom code_change
29
+ * objects when a new pull request is opened.
30
+ *
31
+ * The handler will:
32
+ * 1. Check if a code_change already exists for this PR (by URL)
33
+ * 2. If not, create a new code_change with PR metadata
34
+ * 3. Extract issue IDs from branch/PR title/body and create links
35
+ * 4. Fetch and store commit timestamps (first_commit_at, last_commit_at)
36
+ * 5. Create timeline entries for historical commits
37
+ * 6. Create a timeline entry for the PR opened event
38
+ *
39
+ * @param input - The normalized PR creation event
40
+ * @param context - The handler context with DevRev client and adapter
41
+ * @returns Handler result indicating success or failure
42
+ */
43
+ function handlePRCreate(input, context) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const { pullRequest, repository } = input;
46
+ const repoName = repository.name;
47
+ try {
48
+ // Use shared helper to get or create code_change
49
+ const { codeChange, isNew, commits } = yield (0, helpers_1.getOrCreateCodeChangeForPR)(pullRequest, repoName, context);
50
+ // Create timeline entry for PR opened event
51
+ const message = `PR #${pullRequest.number}: ${pullRequest.title}`;
52
+ yield (0, helpers_1.createPRTimelineEntry)(codeChange.id, pullRequest, repoName, pullRequest.createdAt, message, context);
53
+ if (!isNew) {
54
+ return {
55
+ data: {
56
+ codeChangeId: codeChange.id,
57
+ isNew: false,
58
+ },
59
+ message: 'Code change already exists for this PR. Timeline entry created.',
60
+ success: true,
61
+ };
62
+ }
63
+ return {
64
+ data: {
65
+ codeChangeId: codeChange.id,
66
+ commitsProcessed: commits.length,
67
+ isNew: true,
68
+ },
69
+ message: `Created code_change for PR #${pullRequest.number} with ${commits.length} commits`,
70
+ success: true,
71
+ };
72
+ }
73
+ catch (error) {
74
+ return (0, helpers_1.createPRHandlerErrorResult)('PR creation', pullRequest, error);
75
+ }
76
+ });
77
+ }
78
+ exports.handlePRCreate = handlePRCreate;
79
+ // =============================================================================
80
+ // PR Close Handler
81
+ // =============================================================================
82
+ /**
83
+ * Process a pull request close event (without merge).
84
+ *
85
+ * @param input - The normalized PR close event
86
+ * @param context - The handler context
87
+ * @returns Handler result indicating success or failure
88
+ */
89
+ function handlePRClose(input, context) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ const { pullRequest, repository, closedAt } = input;
92
+ const repoName = repository.name;
93
+ const { client } = context;
94
+ try {
95
+ // Use shared helper to get or create code_change
96
+ const { codeChange } = yield (0, helpers_1.getOrCreateCodeChangeForPR)(pullRequest, repoName, context);
97
+ // Update the code_change with close info
98
+ yield client.codeChange.update({
99
+ closed_at: closedAt,
100
+ id: codeChange.id,
101
+ status: types_1.CodeChangeStatus.CLOSED,
102
+ }, codeChange);
103
+ // Create timeline entry for close
104
+ const message = `PR #${pullRequest.number} closed`;
105
+ yield (0, helpers_1.createPRTimelineEntry)(codeChange.id, pullRequest, repoName, closedAt, message, context);
106
+ return {
107
+ data: {
108
+ closedAt,
109
+ codeChangeId: codeChange.id,
110
+ },
111
+ message: `Processed close for PR #${pullRequest.number}`,
112
+ success: true,
113
+ };
114
+ }
115
+ catch (error) {
116
+ return (0, helpers_1.createPRHandlerErrorResult)('PR close', pullRequest, error);
117
+ }
118
+ });
119
+ }
120
+ exports.handlePRClose = handlePRClose;
121
+ //# sourceMappingURL=pr_create_handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pr_create_handler.js","sourceRoot":"","sources":["../../src/handlers/pr_create_handler.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;;AAEH,2CAEyB;AAGzB,uCAA0G;AAG1G,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAsB,cAAc,CAClC,KAAuC,EACvC,OAAuB;;QAEvB,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;QAC1C,MAAM,QAAQ,GAAG,UAAU,CAAC,IAAI,CAAC;QAEjC,IAAI;YACF,iDAAiD;YAC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