-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpr.ts
More file actions
153 lines (144 loc) · 3.39 KB
/
Copy pathpr.ts
File metadata and controls
153 lines (144 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Octokit } from '@octokit/rest';
import { logger } from '../config/logger';
import { GitHubError } from '../utils/error';
import { getOctokitInstance } from './app';
interface CreatePRParams {
owner: string;
repo: string;
title: string;
head: string;
base: string;
body: string;
labels?: string[];
assignees?: string[];
}
/**
* Create a pull request
*/
export const createPullRequest = async (
octokit: Octokit,
params: CreatePRParams,
): Promise<string> => {
try {
logger.info(
{
owner: params.owner,
repo: params.repo,
head: params.head,
base: params.base,
},
'Creating pull request',
);
// Create PR
const response = await octokit.pulls.create({
owner: params.owner,
repo: params.repo,
title: params.title,
head: params.head,
base: params.base,
body: params.body,
});
// Add labels if provided
if (params.labels && params.labels.length > 0) {
try {
await octokit.issues.addLabels({
owner: params.owner,
repo: params.repo,
issue_number: response.data.number,
labels: params.labels,
});
} catch (error) {
logger.error({ error: error }, 'Failed to add labels to pull request');
}
}
// Add assignees if provided
if (params.assignees && params.assignees.length > 0) {
try {
await octokit.issues.addAssignees({
owner: params.owner,
repo: params.repo,
issue_number: response.data.number,
assignees: params.assignees,
});
} catch (error) {
logger.error({ error: error }, 'Failed to add assignees to pull request');
}
}
logger.info(
{
owner: params.owner,
repo: params.repo,
prNumber: response.data.number,
prUrl: response.data.html_url,
},
'Pull request created successfully',
);
return response.data.html_url;
} catch (error) {
logger.error(
{
owner: params.owner,
repo: params.repo,
error,
},
'Failed to create pull request',
);
throw new GitHubError(
`Failed to create pull request: ${error instanceof Error ? error.message : String(error)}`,
);
}
};
/**
* Add a comment to a pull request review comment.
*/
export const addCommentToPullRequest = async (
owner: string,
repo: string,
pull_number: number,
comment_id: number,
body: string,
installationId: number,
): Promise<void> => {
try {
const octokit = await getOctokitInstance(installationId);
logger.info(
{
owner,
repo,
pull_number,
comment_id,
},
'Adding comment to pull request review comment',
);
await octokit.pulls.createReviewComment({
owner,
repo,
pull_number,
comment_id,
body,
});
logger.info(
{
owner,
repo,
pull_number,
comment_id,
},
'Comment added to pull request review comment successfully',
);
} catch (error) {
logger.error(
{
owner,
repo,
pull_number,
comment_id,
error,
},
'Failed to add comment to pull request review comment',
);
throw new GitHubError(
`Failed to add comment to pull request review comment: ${error instanceof Error ? error.message : String(error)}`,
);
}
};