@opentermsarchive/engine 2.3.1 → 2.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentermsarchive/engine",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "description": "Tracks and makes visible changes to the terms of online services",
5
5
  "homepage": "https://opentermsarchive.org",
6
6
  "bugs": {
@@ -79,28 +79,15 @@ export default class GitHub {
79
79
  return issue;
80
80
  }
81
81
 
82
- async setIssueLabels({ issue, labels }) {
83
- await this.octokit.request('PUT /repos/{owner}/{repo}/issues/{issue_number}/labels', {
82
+ async updateIssue(issue, { state, labels }) {
83
+ const { data: updatedIssue } = await this.octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
84
84
  ...this.commonParams,
85
85
  issue_number: issue.number,
86
+ state,
86
87
  labels,
87
88
  });
88
- }
89
89
 
90
- async openIssue(issue) {
91
- await this.octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
92
- ...this.commonParams,
93
- issue_number: issue.number,
94
- state: GitHub.ISSUE_STATE_OPEN,
95
- });
96
- }
97
-
98
- async closeIssue(issue) {
99
- await this.octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
100
- ...this.commonParams,
101
- issue_number: issue.number,
102
- state: GitHub.ISSUE_STATE_CLOSED,
103
- });
90
+ return updatedIssue;
104
91
  }
105
92
 
106
93
  async getIssue({ title, ...searchParams }) {
@@ -110,7 +97,7 @@ export default class GitHub {
110
97
  ...searchParams,
111
98
  }, response => response.data);
112
99
 
113
- const [issue] = issues.filter(item => item.title === title); // since only one is expected, use the first one
100
+ const [issue] = issues.filter(item => item.title === title); // Since only one is expected, use the first one
114
101
 
115
102
  return issue;
116
103
  }
@@ -134,9 +121,10 @@ export default class GitHub {
134
121
  }
135
122
 
136
123
  await this.addCommentToIssue({ issue: openedIssue, comment });
137
- await this.closeIssue(openedIssue);
124
+ logger.info(`Added comment to issue #${openedIssue.number}: ${openedIssue.html_url}`);
138
125
 
139
- return logger.info(`Closed issue #${openedIssue.number}: ${openedIssue.html_url}`);
126
+ await this.updateIssue(openedIssue, { state: GitHub.ISSUE_STATE_CLOSED });
127
+ logger.info(`Closed issue #${openedIssue.number}: ${openedIssue.html_url}`);
140
128
  } catch (error) {
141
129
  logger.error(`Failed to update issue "${title}": ${error.message}`);
142
130
  }
@@ -152,23 +140,22 @@ export default class GitHub {
152
140
  return logger.info(`Created issue #${createdIssue.number} "${title}": ${createdIssue.html_url}`);
153
141
  }
154
142
 
155
- if (issue.state == GitHub.ISSUE_STATE_CLOSED) {
156
- await this.openIssue(issue);
157
- logger.info(`Reopened issue #${issue.number}: ${issue.html_url}`);
158
- }
159
-
160
143
  const managedLabelsNames = this.MANAGED_LABELS.map(label => label.name);
161
- const [managedLabel] = issue.labels.filter(label => managedLabelsNames.includes(label.name)); // it is assumed that only one specific reason for failure is possible at a time, making managed labels mutually exclusive
144
+ const labelsNotManagedToKeep = issue.labels.map(label => label.name).filter(label => !managedLabelsNames.includes(label));
145
+ const [managedLabel] = issue.labels.filter(label => managedLabelsNames.includes(label.name)); // It is assumed that only one specific reason for failure is possible at a time, making managed labels mutually exclusive
162
146
 
163
- if (managedLabel?.name == label) { // if the label is already assigned to the issue, the error is redundant with the one already reported and no further action is necessary
147
+ if (issue.state !== GitHub.ISSUE_STATE_CLOSED && managedLabel?.name === label) {
164
148
  return;
165
149
  }
166
150
 
167
- const labelsNotManagedToKeep = issue.labels.map(label => label.name).filter(label => !managedLabelsNames.includes(label));
168
-
169
- await this.setIssueLabels({ issue, labels: [ label, ...labelsNotManagedToKeep ] });
170
- await this.addCommentToIssue({ issue, comment: description });
151
+ await this.updateIssue(issue, {
152
+ state: GitHub.ISSUE_STATE_OPEN,
153
+ labels: [ label, ...labelsNotManagedToKeep ],
154
+ });
171
155
  logger.info(`Updated issue #${issue.number}: ${issue.html_url}`);
156
+ await this.addCommentToIssue({ issue, comment: description });
157
+
158
+ logger.info(`Added comment to issue #${issue.number}: ${issue.html_url}`);
172
159
  } catch (error) {
173
160
  logger.error(`Failed to update issue "${title}": ${error.message}`);
174
161
  }
@@ -118,66 +118,6 @@ describe('GitHub', function () {
118
118
  });
119
119
  });
120
120
 
121
- describe('#setIssueLabels', () => {
122
- let scope;
123
- const ISSUE_NUMBER = 123;
124
- const LABELS = [ 'bug', 'enhancement' ];
125
-
126
- before(async () => {
127
- scope = nock('https://api.github.com')
128
- .put(`/repos/owner/repo/issues/${ISSUE_NUMBER}/labels`, { labels: LABELS })
129
- .reply(200);
130
-
131
- await github.setIssueLabels({ issue: { number: ISSUE_NUMBER }, labels: LABELS });
132
- });
133
-
134
- after(nock.cleanAll);
135
-
136
- it('sets labels on the issue', () => {
137
- expect(scope.isDone()).to.be.true;
138
- });
139
- });
140
-
141
- describe('#openIssue', () => {
142
- let scope;
143
- const ISSUE = { number: 123 };
144
- const EXPECTED_REQUEST_BODY = { state: 'open' };
145
-
146
- before(async () => {
147
- scope = nock('https://api.github.com')
148
- .patch(`/repos/owner/repo/issues/${ISSUE.number}`, EXPECTED_REQUEST_BODY)
149
- .reply(200);
150
-
151
- await github.openIssue(ISSUE);
152
- });
153
-
154
- after(nock.cleanAll);
155
-
156
- it('opens the issue', () => {
157
- expect(scope.isDone()).to.be.true;
158
- });
159
- });
160
-
161
- describe('#closeIssue', () => {
162
- let scope;
163
- const ISSUE = { number: 123 };
164
- const EXPECTED_REQUEST_BODY = { state: 'closed' };
165
-
166
- before(async () => {
167
- scope = nock('https://api.github.com')
168
- .patch(`/repos/owner/repo/issues/${ISSUE.number}`, EXPECTED_REQUEST_BODY)
169
- .reply(200);
170
-
171
- await github.closeIssue(ISSUE);
172
- });
173
-
174
- after(nock.cleanAll);
175
-
176
- it('closes the issue', () => {
177
- expect(scope.isDone()).to.be.true;
178
- });
179
- });
180
-
181
121
  describe('#getIssue', () => {
182
122
  let scope;
183
123
  let result;
@@ -376,9 +316,8 @@ describe('GitHub', function () {
376
316
  };
377
317
 
378
318
  context('when issue is closed', () => {
379
- let setIssueLabelsScope;
319
+ let updateIssueScope;
380
320
  let addCommentScope;
381
- let openIssueScope;
382
321
 
383
322
  const GITHUB_RESPONSE_FOR_EXISTING_ISSUE = {
384
323
  number: 123,
@@ -394,12 +333,8 @@ describe('GitHub', function () {
394
333
  .query(true)
395
334
  .reply(200, [GITHUB_RESPONSE_FOR_EXISTING_ISSUE]);
396
335
 
397
- openIssueScope = nock('https://api.github.com')
398
- .patch(`/repos/owner/repo/issues/${GITHUB_RESPONSE_FOR_EXISTING_ISSUE.number}`, { state: GitHub.ISSUE_STATE_OPEN })
399
- .reply(200);
400
-
401
- setIssueLabelsScope = nock('https://api.github.com')
402
- .put(`/repos/owner/repo/issues/${GITHUB_RESPONSE_FOR_EXISTING_ISSUE.number}/labels`, { labels: ['location'] })
336
+ updateIssueScope = nock('https://api.github.com')
337
+ .patch(`/repos/owner/repo/issues/${GITHUB_RESPONSE_FOR_EXISTING_ISSUE.number}`, { state: GitHub.ISSUE_STATE_OPEN, labels: ['location'] })
403
338
  .reply(200);
404
339
 
405
340
  addCommentScope = nock('https://api.github.com')
@@ -409,12 +344,8 @@ describe('GitHub', function () {
409
344
  await github.createOrUpdateIssue(ISSUE);
410
345
  });
411
346
 
412
- it('reopens the issue', () => {
413
- expect(openIssueScope.isDone()).to.be.true;
414
- });
415
-
416
- it("updates the issue's label", () => {
417
- expect(setIssueLabelsScope.isDone()).to.be.true;
347
+ it('reopens the issue and its labels', () => {
348
+ expect(updateIssueScope.isDone()).to.be.true;
418
349
  });
419
350
 
420
351
  it('adds comment to the issue', () => {
@@ -423,9 +354,8 @@ describe('GitHub', function () {
423
354
  });
424
355
 
425
356
  context('when issue is already opened', () => {
426
- let setIssueLabelsScope;
427
357
  let addCommentScope;
428
- let openIssueScope;
358
+ let updateIssueScope;
429
359
 
430
360
  const GITHUB_RESPONSE_FOR_EXISTING_ISSUE = {
431
361
  number: 123,
@@ -441,12 +371,8 @@ describe('GitHub', function () {
441
371
  .query(true)
442
372
  .reply(200, [GITHUB_RESPONSE_FOR_EXISTING_ISSUE]);
443
373
 
444
- openIssueScope = nock('https://api.github.com')
445
- .patch(`/repos/owner/repo/issues/${GITHUB_RESPONSE_FOR_EXISTING_ISSUE.number}`, { state: GitHub.ISSUE_STATE_OPEN })
446
- .reply(200);
447
-
448
- setIssueLabelsScope = nock('https://api.github.com')
449
- .put(`/repos/owner/repo/issues/${GITHUB_RESPONSE_FOR_EXISTING_ISSUE.number}/labels`, { labels: ['location'] })
374
+ updateIssueScope = nock('https://api.github.com')
375
+ .patch(`/repos/owner/repo/issues/${GITHUB_RESPONSE_FOR_EXISTING_ISSUE.number}`, { state: GitHub.ISSUE_STATE_OPEN, labels: ['location'] })
450
376
  .reply(200);
451
377
 
452
378
  addCommentScope = nock('https://api.github.com')
@@ -456,12 +382,8 @@ describe('GitHub', function () {
456
382
  await github.createOrUpdateIssue(ISSUE);
457
383
  });
458
384
 
459
- it('does not change the issue state', () => {
460
- expect(openIssueScope.isDone()).to.be.false;
461
- });
462
-
463
- it("updates the issue's label", () => {
464
- expect(setIssueLabelsScope.isDone()).to.be.true;
385
+ it("updates the issue's labels", () => {
386
+ expect(updateIssueScope.isDone()).to.be.true;
465
387
  });
466
388
 
467
389
  it('adds comment to the issue', () => {