@ffflorian/auto-merge 1.4.4 → 1.4.6
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 +25 -17
- package/package.json +5 -5
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
|
}
|
|
@@ -69,7 +69,8 @@ export class AutoMerge {
|
|
|
69
69
|
if (!response.ok) {
|
|
70
70
|
throw new Error(`Error while checking merge request: ${response.statusText}`);
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
const pullRequestData = await response.json();
|
|
73
|
+
return pullRequestData.mergeable_state === 'clean';
|
|
73
74
|
}
|
|
74
75
|
async mergeByMatch(regex, repositories) {
|
|
75
76
|
const allRepositories = repositories || (await this.getRepositoriesWithOpenPullRequests());
|
|
@@ -78,7 +79,7 @@ export class AutoMerge {
|
|
|
78
79
|
for (const { pullRequests, repositorySlug } of matchingRepositories) {
|
|
79
80
|
const actionResults = [];
|
|
80
81
|
for (const pullRequest of pullRequests) {
|
|
81
|
-
const isMergeable = this.isPullRequestMergeable(repositorySlug, pullRequest.number);
|
|
82
|
+
const isMergeable = await this.isPullRequestMergeable(repositorySlug, pullRequest.number);
|
|
82
83
|
if (!isMergeable) {
|
|
83
84
|
this.logger.warn(`Pull request #${pullRequest.number} in "${repositorySlug}" is not mergeable. Skipping.`);
|
|
84
85
|
continue;
|
|
@@ -105,15 +106,18 @@ export class AutoMerge {
|
|
|
105
106
|
}
|
|
106
107
|
async mergePullRequest(repositorySlug, pullNumber, squash = false) {
|
|
107
108
|
const actionResult = { pullNumber, status: 'good' };
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
await this.putMerge(repositorySlug, pullNumber, squash);
|
|
109
|
+
if (!this.config.dryRun) {
|
|
110
|
+
try {
|
|
111
|
+
const isMerged = await this.putMerge(repositorySlug, pullNumber, squash);
|
|
112
|
+
if (!isMerged) {
|
|
113
|
+
actionResult.status = 'bad';
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
this.logger.error(`Could not merge pull request #${pullNumber} in "${repositorySlug}": ${error.message}`);
|
|
118
|
+
actionResult.status = 'bad';
|
|
119
|
+
actionResult.error = error.toString();
|
|
111
120
|
}
|
|
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
121
|
}
|
|
118
122
|
return actionResult;
|
|
119
123
|
}
|
|
@@ -138,8 +142,8 @@ export class AutoMerge {
|
|
|
138
142
|
/** @see https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request */
|
|
139
143
|
async postReview(repositorySlug, pullNumber) {
|
|
140
144
|
const resourceUrl = new URL(`/repos/${repositorySlug}/pulls/${pullNumber}/reviews`, this.baseURL);
|
|
141
|
-
|
|
142
|
-
const response = await fetch(resourceUrl, { headers: this.baseHeaders, method: 'POST' });
|
|
145
|
+
const body = JSON.stringify({ event: 'APPROVE' });
|
|
146
|
+
const response = await fetch(resourceUrl, { body, headers: this.baseHeaders, method: 'POST' });
|
|
143
147
|
if (!response.ok) {
|
|
144
148
|
throw new Error(`Error while approving pull request: ${response.statusText}`);
|
|
145
149
|
}
|
|
@@ -147,13 +151,17 @@ export class AutoMerge {
|
|
|
147
151
|
/** @see https://docs.github.com/en/rest/reference/issues#create-an-issue-comment */
|
|
148
152
|
async putMerge(repositorySlug, pullNumber, squash) {
|
|
149
153
|
const resourceUrl = new URL(`/repos/${repositorySlug}/pulls/${pullNumber}/merge`, this.baseURL);
|
|
154
|
+
const config = { headers: this.baseHeaders, method: 'PUT' };
|
|
150
155
|
if (squash) {
|
|
151
|
-
|
|
156
|
+
const mergeMethod = { merge_method: 'squash' };
|
|
157
|
+
config.body = JSON.stringify(mergeMethod);
|
|
152
158
|
}
|
|
153
|
-
const response = await fetch(resourceUrl,
|
|
159
|
+
const response = await fetch(resourceUrl, config);
|
|
154
160
|
if (!response.ok) {
|
|
155
161
|
throw new Error(`Error while merging pull request: ${response.statusText}`);
|
|
156
162
|
}
|
|
163
|
+
const { merged } = await response.json();
|
|
164
|
+
return merged;
|
|
157
165
|
}
|
|
158
166
|
async getPullRequestsBySlug(repositorySlug) {
|
|
159
167
|
const resourceUrl = new URL(`/repos/${repositorySlug}/pulls`, this.baseURL);
|
package/package.json
CHANGED
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"http-status-codes": "2.3.0",
|
|
12
12
|
"nock": "14.0.10",
|
|
13
|
-
"rimraf": "6.1.
|
|
14
|
-
"tsx": "4.
|
|
13
|
+
"rimraf": "6.1.2",
|
|
14
|
+
"tsx": "4.21.0",
|
|
15
15
|
"typescript": "5.9.3",
|
|
16
|
-
"vitest": "4.0.
|
|
16
|
+
"vitest": "4.0.15"
|
|
17
17
|
},
|
|
18
18
|
"engines": {
|
|
19
19
|
"node": ">= 18.0"
|
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
"test": "vitest run"
|
|
39
39
|
},
|
|
40
40
|
"type": "module",
|
|
41
|
-
"version": "1.4.
|
|
42
|
-
"gitHead": "
|
|
41
|
+
"version": "1.4.6",
|
|
42
|
+
"gitHead": "a6dc75c712d4c99a7eedffb1b0f1516d5da42535"
|
|
43
43
|
}
|