@ffflorian/auto-merge 1.4.4 → 1.4.5
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/dist/AutoMerge.js +22 -15
- package/package.json +2 -2
package/dist/AutoMerge.js
CHANGED
|
@@ -3,8 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import logdown from 'logdown';
|
|
4
4
|
const __dirname = import.meta.dirname;
|
|
5
5
|
const packageJsonPath = path.join(__dirname, '../package.json');
|
|
6
|
-
const {
|
|
7
|
-
const toolName = Object.keys(bin)[0];
|
|
6
|
+
const { name: toolName, version: toolVersion } = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
8
7
|
export class AutoMerge {
|
|
9
8
|
constructor(config) {
|
|
10
9
|
this.config = config;
|
|
@@ -16,8 +15,9 @@ export class AutoMerge {
|
|
|
16
15
|
this.baseURL = 'https://api.github.com';
|
|
17
16
|
this.baseHeaders = {
|
|
18
17
|
Accept: 'application/vnd.github+json',
|
|
19
|
-
Authorization: `
|
|
18
|
+
Authorization: `Bearer ${this.config.authToken}`,
|
|
20
19
|
'User-Agent': `${toolName} v${toolVersion}`,
|
|
20
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
21
21
|
};
|
|
22
22
|
this.checkConfig(this.config);
|
|
23
23
|
}
|
|
@@ -105,15 +105,18 @@ export class AutoMerge {
|
|
|
105
105
|
}
|
|
106
106
|
async mergePullRequest(repositorySlug, pullNumber, squash = false) {
|
|
107
107
|
const actionResult = { pullNumber, status: 'good' };
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
await this.putMerge(repositorySlug, pullNumber, squash);
|
|
108
|
+
if (!this.config.dryRun) {
|
|
109
|
+
try {
|
|
110
|
+
const isMerged = await this.putMerge(repositorySlug, pullNumber, squash);
|
|
111
|
+
if (!isMerged) {
|
|
112
|
+
actionResult.status = 'bad';
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
this.logger.error(`Could not merge pull request #${pullNumber} in "${repositorySlug}": ${error.message}`);
|
|
117
|
+
actionResult.status = 'bad';
|
|
118
|
+
actionResult.error = error.toString();
|
|
111
119
|
}
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
this.logger.error(`Could not merge pull request #${pullNumber} in "${repositorySlug}": ${error.message}`);
|
|
115
|
-
actionResult.status = 'bad';
|
|
116
|
-
actionResult.error = error.toString();
|
|
117
120
|
}
|
|
118
121
|
return actionResult;
|
|
119
122
|
}
|
|
@@ -138,8 +141,8 @@ export class AutoMerge {
|
|
|
138
141
|
/** @see https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request */
|
|
139
142
|
async postReview(repositorySlug, pullNumber) {
|
|
140
143
|
const resourceUrl = new URL(`/repos/${repositorySlug}/pulls/${pullNumber}/reviews`, this.baseURL);
|
|
141
|
-
|
|
142
|
-
const response = await fetch(resourceUrl, { headers: this.baseHeaders, method: 'POST' });
|
|
144
|
+
const body = JSON.stringify({ event: 'APPROVE' });
|
|
145
|
+
const response = await fetch(resourceUrl, { body, headers: this.baseHeaders, method: 'POST' });
|
|
143
146
|
if (!response.ok) {
|
|
144
147
|
throw new Error(`Error while approving pull request: ${response.statusText}`);
|
|
145
148
|
}
|
|
@@ -147,13 +150,17 @@ export class AutoMerge {
|
|
|
147
150
|
/** @see https://docs.github.com/en/rest/reference/issues#create-an-issue-comment */
|
|
148
151
|
async putMerge(repositorySlug, pullNumber, squash) {
|
|
149
152
|
const resourceUrl = new URL(`/repos/${repositorySlug}/pulls/${pullNumber}/merge`, this.baseURL);
|
|
153
|
+
const config = { headers: this.baseHeaders, method: 'PUT' };
|
|
150
154
|
if (squash) {
|
|
151
|
-
|
|
155
|
+
const mergeMethod = { merge_method: 'squash' };
|
|
156
|
+
config.body = JSON.stringify(mergeMethod);
|
|
152
157
|
}
|
|
153
|
-
const response = await fetch(resourceUrl,
|
|
158
|
+
const response = await fetch(resourceUrl, config);
|
|
154
159
|
if (!response.ok) {
|
|
155
160
|
throw new Error(`Error while merging pull request: ${response.statusText}`);
|
|
156
161
|
}
|
|
162
|
+
const { merged } = await response.json();
|
|
163
|
+
return merged;
|
|
157
164
|
}
|
|
158
165
|
async getPullRequestsBySlug(repositorySlug) {
|
|
159
166
|
const resourceUrl = new URL(`/repos/${repositorySlug}/pulls`, this.baseURL);
|
package/package.json
CHANGED