volunteer-contributions-fetch 2.4.0 → 3.0.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.
package/.env.example ADDED
@@ -0,0 +1 @@
1
+ GITHUB_TOKEN=""
package/.eslintrc CHANGED
@@ -14,6 +14,8 @@
14
14
  "rules": {
15
15
  "arrow-body-style": 0,
16
16
  "comma-dangle": 0,
17
+ "import/extensions": 0,
18
+ "import/prefer-default-export": 0,
17
19
  "no-plusplus": 0,
18
20
  "no-restricted-syntax": 0,
19
21
  "no-use-before-define": 0
@@ -7,16 +7,16 @@ on:
7
7
 
8
8
  jobs:
9
9
  release:
10
- runs-on: ubuntu-22.04
10
+ runs-on: ubuntu-latest
11
11
  permissions:
12
12
  contents: write
13
13
  id-token: write
14
14
  issues: write
15
15
  steps:
16
- - uses: actions/checkout@v3
17
- - uses: actions/setup-node@v3
16
+ - uses: actions/checkout@v4
17
+ - uses: actions/setup-node@v4
18
18
  with:
19
- node-version: 18
19
+ node-version: 20
20
20
  - run: npm ci
21
21
  - run: npm test
22
22
  - name: Release
@@ -6,13 +6,11 @@ jobs:
6
6
  build:
7
7
  runs-on: ubuntu-latest
8
8
  steps:
9
- - uses: actions/checkout@v3
9
+ - uses: actions/checkout@v4
10
+ - name: Use Node.js 20
11
+ uses: actions/setup-node@v4
10
12
  with:
11
- submodules: 'recursive'
12
- - name: Use Node.js 16
13
- uses: actions/setup-node@v3
14
- with:
15
- node-version: 18
13
+ node-version: 20
16
14
  - name: Install Dependencies
17
15
  run: npm ci
18
16
  - name: Linting
package/.husky/commit-msg CHANGED
@@ -1,4 +1 @@
1
- #!/bin/sh
2
- . "$(dirname "$0")/_/husky.sh"
3
-
4
1
  npx --no -- commitlint --edit "${1}"
@@ -0,0 +1 @@
1
+ npm test
package/LICENSE CHANGED
@@ -1,7 +1,6 @@
1
-
2
1
  The MIT License (MIT)
3
2
 
4
- Copyright (c) 2022 Michael Kohler
3
+ Copyright (c) 2024 Michael Kohler
5
4
 
6
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
6
 
package/README.md CHANGED
@@ -27,16 +27,12 @@ Currently the following sources are supported. Feel free to contribute a PR if y
27
27
 
28
28
  Each of the sources can be enabled separately. See the configuration section below.
29
29
 
30
- ### Planned Sources
31
-
32
- The following sources are planned:
30
+ ### Possible future sources
33
31
 
34
32
  - Phabricator
35
33
  - Revision created
36
34
  - Revision updated
37
35
 
38
- ### Possible future sources
39
-
40
36
  The following sources would be interesting for (at least) Mozilla contributors, however they do not expose an activity API endpoint to easily gather information for a certain user.
41
37
 
42
38
  - support.mozilla.org
@@ -50,16 +46,32 @@ First install the package from npm:
50
46
  npm install volunteer-contribution-fetch
51
47
  ```
52
48
 
53
- Then you can create a file in your project which runs the fetch operation and provides the necesary config:
49
+ Then you can create a file in your project which runs the fetch operation and provides the necessary config:
54
50
 
55
51
  ```js
56
- const { fetchAll } = require('volunteer-contributions-fetch');
52
+ import { fetchAll } from 'volunteer-contributions-fetch';
57
53
 
58
54
  const config = {}; // See below for documentation and a sample config
59
55
 
60
56
  fetchAll(config);
61
57
  ```
62
58
 
59
+ ### Usage without output file
60
+
61
+ This can also be used without having to rely on an `outputFile`. For this case the `fetchAll` function returns all contributions. Additionally you can also pass existing contributions to the function and these will be considered as well. The return value and the parameter of the function work the same as having an `outputFile`.
62
+
63
+ ```js
64
+ import { fetchAll } from 'volunteer-contributions-fetch';
65
+
66
+ const config = {}; // See below for documentation and a sample config
67
+ const existingContributions = [{ ... }];
68
+
69
+ const results = fetchAll(config, existingContributions);
70
+ // results is now all existing contributions plus any newly fetched contribution
71
+ ```
72
+
73
+ Note that passing existing contributions this way will be ignored if a `outputFile` is used. Instead the existing contributions will be read from the `outputFile` if it already exists.
74
+
63
75
  ## Configuration
64
76
 
65
77
  You can configure the different providers with a config you pass into the `fetchAll` function call. These config parameters are outlined below. The config gets validated and you might get an error if you haven't specified all necessary properties.
@@ -76,14 +88,14 @@ This section documents all the possible configuration values. For examples see t
76
88
 
77
89
  #### General
78
90
 
79
- | Field | Data Type | Required | Default | Description |
80
- | ----------------- | --------- | -------- | ------- | -------------------------------------------------- |
81
- | `outputFile` | string | Yes | - | Path to contributions output file |
82
- | `bugzilla` | Object | No | - | Bugzilla configuration options (see below) |
83
- | `communityPortal` | Object | No | - | Community Portal configuration options (see below) |
84
- | `discourse` | Object | No | - | Discourse configuration options (see below) |
85
- | `github` | Object | No | - | GitHub configuration options (see below) |
86
- | `mediaWiki` | Object | No | - | MediaWiki configuration options (see below) |
91
+ | Field | Data Type | Required | Default | Description |
92
+ | ----------------- | --------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------ |
93
+ | `outputFile` | string | No | - | Path to contributions output file. Contributions are read from this file and new contributions will be added here. |
94
+ | `bugzilla` | Object | No | - | Bugzilla configuration options (see below) |
95
+ | `communityPortal` | Object | No | - | Community Portal configuration options (see below) |
96
+ | `discourse` | Object | No | - | Discourse configuration options (see below) |
97
+ | `github` | Object | No | - | GitHub configuration options (see below) |
98
+ | `mediaWiki` | Object | No | - | MediaWiki configuration options (see below) |
87
99
 
88
100
  #### Bugzilla
89
101
 
@@ -224,7 +236,13 @@ npm test
224
236
 
225
237
  ### Testing changes
226
238
 
227
- You can also add a temporary file with a test config as well as the call to `fetchAll` if you would like to test changes within this project. A sample config can be found in the configuration section above. Please make sure to not include that config file when committing!
239
+ First copy the example env file and adjust it to reflect your tokens.
240
+
241
+ ```bash
242
+ cp .env.example .env
243
+ ```
244
+
245
+ You then can use `npm run example` which uses `fetchAll` if you would like to test changes within this project. The configuration for this can be adjusted in `example/index.js`. A full sample config can be found in the configuration section above. Please make sure to not include testing config file changes when committing! The file is not ignored to make it so that new checkouts do not have to copy around a file first.
228
246
 
229
247
  ### Deployments
230
248
 
@@ -0,0 +1,36 @@
1
+ import { fetchAll } from '../lib/fetch.js';
2
+
3
+ const config = {
4
+ github: {
5
+ allowPrivate: true,
6
+ enabled: true,
7
+ username: 'MichaelKohler',
8
+ stopDate: '2025-01-01',
9
+ filter: 'mozilla',
10
+ types: {
11
+ commit: 'GitHub Commit',
12
+ privateCommit: 'Commit in private repository',
13
+ createdPR: 'Created PR',
14
+ createdIssue: 'Created Issue Report',
15
+ commentedPR: 'Commented on a Pull Request',
16
+ approvedPR: 'Approved a Pull Request',
17
+ changesRequestedPR: 'Requested changes on a Pull Request',
18
+ reviewedPR: 'Reviewed a Pull Request',
19
+ },
20
+ },
21
+ };
22
+
23
+ const existingContributions = [
24
+ {
25
+ createdAt: '2008-03-10T00:21:39.000Z',
26
+ description: 'Fix sorting and labels for the bookmark library restore menu',
27
+ link: 'https://bugzilla.mozilla.org/show_bug.cgi?id=421834#c0',
28
+ type: 'Created a Bug Report',
29
+ source: 'bugzilla-created',
30
+ },
31
+ ];
32
+
33
+ const contributions = await fetchAll(config, existingContributions);
34
+
35
+ // eslint-disable-next-line no-console
36
+ console.log(contributions);
@@ -1,4 +1,4 @@
1
- const allData = {
1
+ export const allData = {
2
2
  window: {
3
3
  document: {
4
4
  querySelectorAll: (query) => {
@@ -122,7 +122,7 @@ const allData = {
122
122
  },
123
123
  };
124
124
 
125
- const noData = {
125
+ export const noData = {
126
126
  window: {
127
127
  document: {
128
128
  querySelectorAll: (query) => {
@@ -140,7 +140,7 @@ const noData = {
140
140
  },
141
141
  };
142
142
 
143
- const futureData = {
143
+ export const futureData = {
144
144
  window: {
145
145
  document: {
146
146
  querySelectorAll: (query) => {
@@ -263,9 +263,3 @@ const futureData = {
263
263
  },
264
264
  },
265
265
  };
266
-
267
- module.exports = {
268
- allData,
269
- noData,
270
- futureData,
271
- };
package/fixtures/wiki.js CHANGED
@@ -1,4 +1,4 @@
1
- const firstMonth = `
1
+ export const firstMonth = `
2
2
  <rss version="2.0">
3
3
  <channel>
4
4
  <title>MozillaWiki - User contributions [en]</title>
@@ -29,7 +29,7 @@ const firstMonth = `
29
29
  </rss>
30
30
  `;
31
31
 
32
- const secondMonth = `
32
+ export const secondMonth = `
33
33
  <rss version="2.0">
34
34
  <channel>
35
35
  <title>MozillaWiki - User contributions [en]</title>
@@ -50,8 +50,3 @@ const secondMonth = `
50
50
  </channel>
51
51
  </rss>
52
52
  `;
53
-
54
- module.exports = {
55
- firstMonth,
56
- secondMonth,
57
- };
package/index.js CHANGED
@@ -1,5 +1 @@
1
- const { fetchAll } = require('./lib/fetch');
2
-
3
- module.exports = {
4
- fetchAll,
5
- };
1
+ export { fetchAll } from './lib/fetch';
package/lib/bugzilla.js CHANGED
@@ -1,7 +1,8 @@
1
- const debug = require('debug')('contributions:bugzilla');
2
- const axios = require('axios');
1
+ import Debug from 'debug';
3
2
 
4
- const { getPadded } = require('./utils');
3
+ import { getPadded } from './utils.js';
4
+
5
+ const debug = Debug('contributions:bugzilla');
5
6
 
6
7
  const BUGZILLA_CREATED_CATEGORY = 'bugzilla-created';
7
8
  const BUGZILLA_COMMENTED_CATEGORY = 'bugzilla-comments';
@@ -12,12 +13,7 @@ const TYPE_COMMENTED = 'Commented on a Bug Report';
12
13
  const RESULTS_PER_PAGE = 500;
13
14
  const FETCH_TIMEOUT = 120000;
14
15
 
15
- module.exports = {
16
- gather,
17
- validate,
18
- };
19
-
20
- function validate(config) {
16
+ export function validate(config) {
21
17
  debug('Validating Bugzilla config');
22
18
 
23
19
  if (!config) {
@@ -37,7 +33,7 @@ function validate(config) {
37
33
  }
38
34
  }
39
35
 
40
- async function gather({ bugzilla }, existingContributions = []) {
36
+ export async function gather({ bugzilla }, existingContributions = []) {
41
37
  if (!bugzilla || (bugzilla && !bugzilla.enabled)) {
42
38
  debug('Bugzilla source not enabled, skipping');
43
39
  return [];
@@ -92,11 +88,11 @@ async function* bugPageGenerator(bugzilla, stopDate) {
92
88
  const bugsUrl = `${bugzilla.baseUrl}/rest/bug?${query}`;
93
89
  debug(`Fetching from ${bugsUrl}`);
94
90
  // eslint-disable-next-line no-await-in-loop
95
- const { data } = await axios.request({
96
- method: 'GET',
97
- url: bugsUrl,
98
- timeout: FETCH_TIMEOUT,
91
+ const response = await fetch(bugsUrl, {
92
+ signal: AbortSignal.timeout(FETCH_TIMEOUT),
99
93
  });
94
+ // eslint-disable-next-line no-await-in-loop
95
+ const data = await response.json();
100
96
  debug(`Got ${data.bugs.length} bugs results`);
101
97
  // If we have less results than the max result, we're at the end
102
98
  if (data.bugs.length < RESULTS_PER_PAGE) {
@@ -115,11 +111,10 @@ async function* getComments(bugzilla, bugs, titleMap) {
115
111
  const commentsUrl = `${bugzilla.baseUrl}/rest/bug/${bug.id}/comment`;
116
112
  debug(`Fetching from ${commentsUrl}`);
117
113
  // eslint-disable-next-line no-await-in-loop
118
- const { data } = await axios.request({
119
- method: 'GET',
120
- url: commentsUrl,
121
- timeout: FETCH_TIMEOUT,
114
+ const response = await fetch(commentsUrl, {
115
+ signal: AbortSignal.timeout(FETCH_TIMEOUT),
122
116
  });
117
+ const data = await response.json();
123
118
 
124
119
  const ownComments = data.bugs[bug.id].comments.filter(
125
120
  (comment) => comment.creator === bugzilla.username
@@ -1,8 +1,6 @@
1
- const test = require('ava');
2
- const axios = require('axios');
3
- const sinon = require('sinon');
1
+ import { afterEach, beforeEach, expect, test, vi } from 'vitest';
4
2
 
5
- const bugzilla = require('./bugzilla');
3
+ import { gather, validate } from './bugzilla.js';
6
4
 
7
5
  const config = {
8
6
  bugzilla: {
@@ -13,10 +11,7 @@ const config = {
13
11
  },
14
12
  };
15
13
 
16
- test.beforeEach((t) => {
17
- // eslint-disable-next-line no-param-reassign
18
- t.context.sandbox = sinon.createSandbox();
19
-
14
+ beforeEach(() => {
20
15
  const firstBugsPage = {
21
16
  bugs: new Array(500).fill(0).map(() => ({
22
17
  created_at: new Date('2022-04-13'),
@@ -64,61 +59,64 @@ test.beforeEach((t) => {
64
59
  },
65
60
  };
66
61
 
67
- t.context.sandbox.stub(axios, 'request');
68
- axios.request.resolves({ data: comments });
69
- axios.request.onCall(0).resolves({ data: firstBugsPage });
70
- axios.request.onCall(1).resolves({ data: secondBugsPage });
62
+ const mockedFetch = vi
63
+ .fn()
64
+ .mockResolvedValueOnce({ json: vi.fn().mockResolvedValue(firstBugsPage) })
65
+ .mockResolvedValueOnce({ json: vi.fn().mockResolvedValue(secondBugsPage) })
66
+ .mockResolvedValue({ json: vi.fn().mockResolvedValue(comments) });
67
+
68
+ vi.stubGlobal('fetch', mockedFetch);
71
69
  });
72
70
 
73
- test.afterEach.always((t) => {
74
- t.context.sandbox.restore();
71
+ afterEach(() => {
72
+ vi.restoreAllMocks();
75
73
  });
76
74
 
77
- test.serial('should validate config - undefined', (t) => {
78
- t.notThrows(() => bugzilla.validate(undefined));
75
+ test('should validate config - undefined', () => {
76
+ expect(() => validate(undefined)).not.toThrow();
79
77
  });
80
78
 
81
- test.serial('should validate config - missing completely', (t) => {
79
+ test('should validate config - missing completely', () => {
82
80
  const emptyConfig = {};
83
- t.notThrows(() => bugzilla.validate(emptyConfig));
81
+ expect(() => validate(emptyConfig)).not.toThrow();
84
82
  });
85
83
 
86
- test.serial('should validate config - missing baseUrl', (t) => {
84
+ test('should validate config - missing baseUrl', () => {
87
85
  const failConfig = {
88
86
  enabled: true,
89
87
  username: 'A',
90
88
  stopDate: new Date(),
91
89
  };
92
90
 
93
- t.throws(() => bugzilla.validate(failConfig));
91
+ expect(() => validate(failConfig)).toThrow();
94
92
  });
95
93
 
96
- test.serial('should validate config - missing username', (t) => {
94
+ test('should validate config - missing username', () => {
97
95
  const failConfig = {
98
96
  enabled: true,
99
97
  baseUrl: 'A',
100
98
  stopDate: new Date(),
101
99
  };
102
100
 
103
- t.throws(() => bugzilla.validate(failConfig));
101
+ expect(() => validate(failConfig)).toThrow();
104
102
  });
105
103
 
106
- test.serial('should validate config - missing stopDate', (t) => {
104
+ test('should validate config - missing stopDate', () => {
107
105
  const failConfig = {
108
106
  enabled: true,
109
107
  baseUrl: 'A',
110
108
  username: 'user',
111
109
  };
112
110
 
113
- t.throws(() => bugzilla.validate(failConfig));
111
+ expect(() => validate(failConfig)).toThrow();
114
112
  });
115
113
 
116
- test.serial('should fetch', async (t) => {
117
- const contributions = await bugzilla.gather(config);
118
- t.is(contributions.length, (500 + 1) * 2);
114
+ test('should fetch', async () => {
115
+ const contributions = await gather(config);
116
+ expect(contributions.length).toBe((500 + 1) * 2);
119
117
  });
120
118
 
121
- test.serial('should not fetch if disabled - by enable property', async (t) => {
119
+ test('should not fetch if disabled - by enable property', async () => {
122
120
  const specificTypeConfig = {
123
121
  bugzilla: {
124
122
  ...config.bugzilla,
@@ -126,31 +124,28 @@ test.serial('should not fetch if disabled - by enable property', async (t) => {
126
124
  },
127
125
  };
128
126
 
129
- const contributions = await bugzilla.gather(specificTypeConfig);
127
+ const contributions = await gather(specificTypeConfig);
130
128
 
131
- t.is(contributions.length, 0);
129
+ expect(contributions.length).toBe(0);
132
130
  });
133
131
 
134
- test.serial(
135
- 'should not fetch if disabled - by leaving off config value',
136
- async (t) => {
137
- const contributions = await bugzilla.gather({});
132
+ test('should not fetch if disabled - by leaving off config value', async () => {
133
+ const contributions = await gather({});
138
134
 
139
- t.is(contributions.length, 0);
140
- }
141
- );
135
+ expect(contributions.length).toBe(0);
136
+ });
142
137
 
143
- test.serial('should format', async (t) => {
144
- const contributions = await bugzilla.gather(config);
138
+ test('should format', async () => {
139
+ const contributions = await gather(config);
145
140
 
146
- t.deepEqual(contributions[0], {
141
+ expect(contributions[0]).toStrictEqual({
147
142
  createdAt: new Date('2022-04-13'),
148
143
  description: 'SecondSummary',
149
144
  link: 'https://bugzilla.mozilla.org/show_bug.cgi?id=1#c0',
150
145
  type: 'Created a Bug Report',
151
146
  source: 'bugzilla-created',
152
147
  });
153
- t.deepEqual(contributions[1], {
148
+ expect(contributions[1]).toStrictEqual({
154
149
  createdAt: new Date('2022-04-13'),
155
150
  description: 'SecondSummary',
156
151
  link: 'https://bugzilla.mozilla.org/show_bug.cgi?id=1#c1',
@@ -159,7 +154,7 @@ test.serial('should format', async (t) => {
159
154
  });
160
155
  });
161
156
 
162
- test.serial('should format with custom types', async (t) => {
157
+ test('should format with custom types', async () => {
163
158
  const specificTypeConfig = {
164
159
  bugzilla: {
165
160
  ...config.bugzilla,
@@ -170,13 +165,17 @@ test.serial('should format with custom types', async (t) => {
170
165
  },
171
166
  };
172
167
 
173
- const contributions = await bugzilla.gather(specificTypeConfig);
168
+ const contributions = await gather(specificTypeConfig);
174
169
 
175
- t.is(contributions[0].type, specificTypeConfig.bugzilla.types.createdType);
176
- t.is(contributions[1].type, specificTypeConfig.bugzilla.types.commentedType);
170
+ expect(contributions[0].type).toBe(
171
+ specificTypeConfig.bugzilla.types.createdType
172
+ );
173
+ expect(contributions[1].type).toBe(
174
+ specificTypeConfig.bugzilla.types.commentedType
175
+ );
177
176
  });
178
177
 
179
- test.serial('should ignore comments older than stopDate', async (t) => {
178
+ test('should ignore comments older than stopDate', async () => {
180
179
  const bugsPage = {
181
180
  bugs: [
182
181
  {
@@ -208,9 +207,12 @@ test.serial('should ignore comments older than stopDate', async (t) => {
208
207
  },
209
208
  };
210
209
 
211
- axios.request.onCall(0).resolves({ data: bugsPage });
212
- axios.request.onCall(1).resolves({ data: oldComments });
210
+ const mockedFetch = vi
211
+ .fn()
212
+ .mockResolvedValueOnce({ json: vi.fn().mockResolvedValue(bugsPage) })
213
+ .mockResolvedValueOnce({ json: vi.fn().mockResolvedValue(oldComments) });
214
+ vi.stubGlobal('fetch', mockedFetch);
213
215
 
214
- const contributions = await bugzilla.gather(config);
215
- t.is(contributions.length, 0);
216
+ const contributions = await gather(config);
217
+ expect(contributions.length).toBe(0);
216
218
  });
@@ -1,20 +1,15 @@
1
- const debug = require('debug')('contributions:community-portal');
2
- const { JSDOM } = require('jsdom');
1
+ import Debug from 'debug';
2
+ import { JSDOM } from 'jsdom';
3
3
 
4
- const EVENTS_CATEGORY = 'community-portal-events';
5
- const CAMPAIGNS_CATEGORY = 'community-portal-campaigns';
4
+ const debug = Debug('contributions:community-portal');
5
+
6
+ export const EVENTS_CATEGORY = 'community-portal-events';
7
+ export const CAMPAIGNS_CATEGORY = 'community-portal-campaigns';
6
8
 
7
9
  const TYPE_PARTICIPATION = 'Participated in an event';
8
10
  const TYPE_CAMPAIGN = 'Participated in a campaign';
9
11
 
10
- module.exports = {
11
- EVENTS_CATEGORY,
12
- CAMPAIGNS_CATEGORY,
13
- gather,
14
- validate,
15
- };
16
-
17
- function validate(config) {
12
+ export function validate(config) {
18
13
  debug('Validating Community Portal config');
19
14
 
20
15
  if (!config) {
@@ -30,7 +25,7 @@ function validate(config) {
30
25
  }
31
26
  }
32
27
 
33
- async function gather({ communityPortal }) {
28
+ export async function gather({ communityPortal }) {
34
29
  if (!communityPortal || (communityPortal && !communityPortal.enabled)) {
35
30
  debug('Community Portal source not enabled, skipping');
36
31
  return [];