volunteer-contributions-fetch 2.4.0 → 2.5.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/README.md CHANGED
@@ -50,7 +50,7 @@ First install the package from npm:
50
50
  npm install volunteer-contribution-fetch
51
51
  ```
52
52
 
53
- Then you can create a file in your project which runs the fetch operation and provides the necesary config:
53
+ Then you can create a file in your project which runs the fetch operation and provides the necessary config:
54
54
 
55
55
  ```js
56
56
  const { fetchAll } = require('volunteer-contributions-fetch');
@@ -60,6 +60,22 @@ const config = {}; // See below for documentation and a sample config
60
60
  fetchAll(config);
61
61
  ```
62
62
 
63
+ ### Usage without output file
64
+
65
+ 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 works the same as having an `outputFile`.
66
+
67
+ ```js
68
+ const { fetchAll } = require('volunteer-contributions-fetch');
69
+
70
+ const config = {}; // See below for documentation and a sample config
71
+ const existingContributions = [{ ... }];
72
+
73
+ const results = fetchAll(config, existingContributions);
74
+ // results is now all existing contributions plus any newly fetched contribution
75
+ ```
76
+
77
+ Note that passing existing contributions this way will be ignored if a `outputFile` is used.
78
+
63
79
  ## Configuration
64
80
 
65
81
  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 +92,14 @@ This section documents all the possible configuration values. For examples see t
76
92
 
77
93
  #### General
78
94
 
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) |
95
+ | Field | Data Type | Required | Default | Description |
96
+ | ----------------- | --------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------ |
97
+ | `outputFile` | string | No | - | Path to contributions output file. Contributions are read from this file and new contributions will be added here. |
98
+ | `bugzilla` | Object | No | - | Bugzilla configuration options (see below) |
99
+ | `communityPortal` | Object | No | - | Community Portal configuration options (see below) |
100
+ | `discourse` | Object | No | - | Discourse configuration options (see below) |
101
+ | `github` | Object | No | - | GitHub configuration options (see below) |
102
+ | `mediaWiki` | Object | No | - | MediaWiki configuration options (see below) |
87
103
 
88
104
  #### Bugzilla
89
105
 
package/lib/fetch.js CHANGED
@@ -8,14 +8,26 @@ const github = require('./github');
8
8
  const { validateConfig } = require('./validator');
9
9
  const wiki = require('./wiki');
10
10
 
11
- async function fetchAll(config) {
11
+ async function fetchAll(config, contributions = []) {
12
12
  validateConfig(config);
13
13
 
14
- debug('fetching...');
15
- const existingContributions = JSON.parse(
16
- await fs.readFile(config.outputFile)
17
- );
14
+ let existingContributions = contributions;
15
+ if (config.outputFile) {
16
+ debug('checking output file...');
17
+ try {
18
+ await fs.stat(config.outputFile);
19
+ // Passing existing contributions through the parameter is overwritten
20
+ // if an outputFile exists.
21
+ existingContributions = JSON.parse(await fs.readFile(config.outputFile));
22
+ } catch (error) {
23
+ if (error.code === 'ENOENT') {
24
+ debug('output file does not exist, creating!');
25
+ await fs.writeFile(config.outputFile, JSON.stringify([]));
26
+ }
27
+ }
28
+ }
18
29
 
30
+ debug('fetching...');
19
31
  const bugzillaResult = await bugzilla.gather(config, existingContributions);
20
32
  const githubResult = await github.gather(config, existingContributions);
21
33
  const wikiResult = await wiki.gather(config);
@@ -61,17 +73,22 @@ async function fetchAll(config) {
61
73
 
62
74
  if (uniqueContributions.length === existingContributions.length) {
63
75
  debug('NO_UPDATE_ABORTING');
64
- return;
76
+ return uniqueContributions;
65
77
  }
66
78
 
67
79
  uniqueContributions.sort(
68
80
  (a, b) => new Date(b.createdAt) - new Date(a.createdAt)
69
81
  );
70
- await fs.writeFile(
71
- config.outputFile,
72
- `${JSON.stringify(uniqueContributions, null, 2)}\n`
73
- );
74
- debug('FILE_WRITTEN');
82
+
83
+ if (config.outputFile) {
84
+ await fs.writeFile(
85
+ config.outputFile,
86
+ `${JSON.stringify(uniqueContributions, null, 2)}\n`
87
+ );
88
+ debug('FILE_WRITTEN');
89
+ }
90
+
91
+ return uniqueContributions;
75
92
  }
76
93
 
77
94
  module.exports = {
package/lib/fetch.test.js CHANGED
@@ -21,6 +21,7 @@ test.beforeEach((t) => {
21
21
  // eslint-disable-next-line no-param-reassign
22
22
  t.context.sandbox.originalJSONParse = JSON.parse;
23
23
  t.context.sandbox.stub(fs, 'readFile').resolves('');
24
+ t.context.sandbox.stub(fs, 'stat').resolves('');
24
25
  t.context.sandbox.stub(fs, 'writeFile').resolves('');
25
26
  t.context.sandbox.stub(JSON, 'parse').returns([]);
26
27
  t.context.sandbox.stub(validator, 'validateConfig').returns([]);
@@ -34,9 +35,21 @@ test.afterEach.always((t) => {
34
35
  t.context.sandbox.restore();
35
36
  });
36
37
 
38
+ test.serial(
39
+ 'should create a contributions file if file does not exist',
40
+ async (t) => {
41
+ const error = new Error('oh no, no config file!');
42
+ error.code = 'ENOENT';
43
+ fs.stat.throws(error);
44
+ await fetch.fetchAll(config);
45
+ t.is(fs.writeFile.callCount, 1);
46
+ t.is(fs.writeFile.getCall(0).args[0], config.outputFile);
47
+ t.is(fs.writeFile.getCall(0).args[1], '[]');
48
+ }
49
+ );
50
+
37
51
  test.serial('should not write file if there was nothing to do', async (t) => {
38
- const result = await fetch.fetchAll(config);
39
- t.is(result, undefined);
52
+ await fetch.fetchAll(config);
40
53
  t.is(fs.writeFile.callCount, 0);
41
54
  });
42
55
 
@@ -87,3 +100,23 @@ test.serial('should take all return values', async (t) => {
87
100
  4
88
101
  );
89
102
  });
103
+
104
+ test.serial('should return contributions', async (t) => {
105
+ const contributions = [{ source: 'community-portal' }];
106
+ communityPortal.gather.returns(contributions);
107
+
108
+ const result = await fetch.fetchAll(config);
109
+ t.deepEqual(result, contributions);
110
+ });
111
+
112
+ test.serial('should consider passed contributions', async (t) => {
113
+ const contributions = [{ source: 'community-portal' }];
114
+ communityPortal.gather.returns(contributions);
115
+ const existingContributions = [
116
+ { source: 'existing' },
117
+ { source: 'existing' },
118
+ ];
119
+
120
+ const result = await fetch.fetchAll({}, existingContributions);
121
+ t.deepEqual(result, [...existingContributions, ...contributions]);
122
+ });
package/lib/validator.js CHANGED
@@ -15,10 +15,6 @@ function validateConfig(config) {
15
15
  throw new Error('No config passed!');
16
16
  }
17
17
 
18
- if (!config.outputFile) {
19
- throw new Error('outputFile is required');
20
- }
21
-
22
18
  validateCommunityPortal(config.communityPortal);
23
19
  validateDiscourse(config.discourse);
24
20
  validateWiki(config.mediaWiki);
@@ -34,10 +34,6 @@ test.serial('should throw when missing config', (t) => {
34
34
  t.throws(() => validator.validateConfig());
35
35
  });
36
36
 
37
- test.serial('should throw when outputFile is not defined', (t) => {
38
- t.throws(() => validator.validateConfig({ somethingElse: true }));
39
- });
40
-
41
37
  test.serial('should throw when community portal validation fails', (t) => {
42
38
  communityPortal.validate.rejects(new Error('oh no'));
43
39
  t.throws(() => validator.validateConfig());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "volunteer-contributions-fetch",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "Library to fetch volunteer contributions from several sources such as GitHub",
5
5
  "author": "Michael Kohler",
6
6
  "license": "MIT",
@@ -39,7 +39,8 @@
39
39
  "test": "nyc ava"
40
40
  },
41
41
  "prettier": {
42
- "singleQuote": true
42
+ "singleQuote": true,
43
+ "trailingComma": "es5"
43
44
  },
44
45
  "release": {
45
46
  "branches": [