volunteer-contributions-fetch 2.3.2 → 2.3.4

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.
@@ -40,10 +40,7 @@ async function gather({ communityPortal }) {
40
40
  const document = await getCommunityPortalDocument(profileUrl);
41
41
  const events = await processEvents(communityPortal, document);
42
42
  const campaigns = await processCampaigns(communityPortal, document);
43
- const contributions = [
44
- ...events,
45
- ...campaigns,
46
- ];
43
+ const contributions = [...events, ...campaigns];
47
44
 
48
45
  debug('Finished gathering contributions');
49
46
  return contributions;
@@ -53,9 +50,8 @@ async function processEvents(communityPortal, document) {
53
50
  debug('Getting events from Community Portal');
54
51
  const allEventNodes = document.querySelectorAll('.profile__event');
55
52
 
56
- const allEvents = Array.from(
57
- allEventNodes,
58
- (entity) => formatEvent(communityPortal.participationType, entity)
53
+ const allEvents = Array.from(allEventNodes, (entity) =>
54
+ formatEvent(communityPortal.participationType, entity)
59
55
  )
60
56
  .sort((a, b) => b.createdAt - a.createdAt)
61
57
  .filter((event) => event.createdAt < new Date());
@@ -66,9 +62,8 @@ async function processCampaigns(communityPortal, document) {
66
62
  debug('Getting campaigns from Community Portal');
67
63
  const allCampaignNodes = document.querySelectorAll('.profile__campaign');
68
64
 
69
- return Array.from(
70
- allCampaignNodes,
71
- (entity) => formatCampaign(communityPortal.campaignType, entity)
65
+ return Array.from(allCampaignNodes, (entity) =>
66
+ formatCampaign(communityPortal.campaignType, entity)
72
67
  )
73
68
  .sort((a, b) => b.createdAt - a.createdAt)
74
69
  .filter((campaign) => campaign.createdAt < new Date());
@@ -82,8 +77,14 @@ async function getCommunityPortalDocument(url) {
82
77
 
83
78
  function formatEvent(type, entity) {
84
79
  return {
85
- createdAt: new Date(entity.querySelector('.profile__event-time').textContent.replace(/[\n\t∙]/g, '')),
86
- description: entity.querySelector('.profile__event-title').textContent.replace(/[\n\t]/g, ''),
80
+ createdAt: new Date(
81
+ entity
82
+ .querySelector('.profile__event-time')
83
+ .textContent.replace(/[\n\t∙]/g, '')
84
+ ),
85
+ description: entity
86
+ .querySelector('.profile__event-title')
87
+ .textContent.replace(/[\n\t]/g, ''),
87
88
  link: entity.getAttribute('href'),
88
89
  type: type || TYPE_PARTICIPATION,
89
90
  source: EVENTS_CATEGORY,
@@ -91,10 +92,15 @@ function formatEvent(type, entity) {
91
92
  }
92
93
 
93
94
  function formatCampaign(type, entity) {
94
- const dateValue = entity.querySelector('.profile__campaign-dates').textContent.replace(/[\n\t∙]/g, '').replace(/\s*-\s*[a-zA-Z]+\s\d{2}/g, '');
95
+ const dateValue = entity
96
+ .querySelector('.profile__campaign-dates')
97
+ .textContent.replace(/[\n\t∙]/g, '')
98
+ .replace(/\s*-\s*[a-zA-Z]+\s\d{2}/g, '');
95
99
  return {
96
100
  createdAt: new Date(dateValue),
97
- description: entity.querySelector('.profile__campaign-title').textContent.replace(/[\n\t]/g, ''),
101
+ description: entity
102
+ .querySelector('.profile__campaign-title')
103
+ .textContent.replace(/[\n\t]/g, ''),
98
104
  link: entity.getAttribute('href'),
99
105
  type: type || TYPE_CAMPAIGN,
100
106
  source: CAMPAIGNS_CATEGORY,
@@ -10,7 +10,7 @@ const config = {
10
10
  enabled: true,
11
11
  baseUrl: 'https://community.mozilla.org',
12
12
  username: 'user',
13
- }
13
+ },
14
14
  };
15
15
 
16
16
  test.beforeEach((t) => {
@@ -65,16 +65,19 @@ test.serial('should fetch even though sections are empty', async (t) => {
65
65
  t.is(contributions.length, 0);
66
66
  });
67
67
 
68
- test.serial('should only fetch events and campaigns that are not in the future', async (t) => {
69
- JSDOM.fromURL.resolves(fixtures.futureData);
70
-
71
- const contributions = await communityPortal.gather(config);
72
- t.is(contributions.length, 2);
73
- t.deepEqual(contributions[0].createdAt, new Date('2022-08-11'));
74
- t.is(contributions[0].description, 'PastEvent');
75
- t.deepEqual(contributions[1].createdAt, new Date('2022-02-01'));
76
- t.is(contributions[1].description, 'PastCampaign');
77
- });
68
+ test.serial(
69
+ 'should only fetch events and campaigns that are not in the future',
70
+ async (t) => {
71
+ JSDOM.fromURL.resolves(fixtures.futureData);
72
+
73
+ const contributions = await communityPortal.gather(config);
74
+ t.is(contributions.length, 2);
75
+ t.deepEqual(contributions[0].createdAt, new Date('2022-08-11'));
76
+ t.is(contributions[0].description, 'PastEvent');
77
+ t.deepEqual(contributions[1].createdAt, new Date('2022-02-01'));
78
+ t.is(contributions[1].description, 'PastCampaign');
79
+ }
80
+ );
78
81
 
79
82
  test.serial('should not fetch if disabled - by enable property', async (t) => {
80
83
  const specificTypeConfig = {
@@ -89,11 +92,14 @@ test.serial('should not fetch if disabled - by enable property', async (t) => {
89
92
  t.is(contributions.length, 0);
90
93
  });
91
94
 
92
- test.serial('should not fetch if disabled - by leaving off config value', async (t) => {
93
- const contributions = await communityPortal.gather({});
95
+ test.serial(
96
+ 'should not fetch if disabled - by leaving off config value',
97
+ async (t) => {
98
+ const contributions = await communityPortal.gather({});
94
99
 
95
- t.is(contributions.length, 0);
96
- });
100
+ t.is(contributions.length, 0);
101
+ }
102
+ );
97
103
 
98
104
  test.serial('should format', async (t) => {
99
105
  JSDOM.fromURL.resolves(fixtures.allData);
@@ -140,14 +146,20 @@ test.serial('should format with custom types', async (t) => {
140
146
  ...config.communityPortal,
141
147
  participationType: 'Event!',
142
148
  campaignType: 'Campaign!',
143
- }
149
+ },
144
150
  };
145
151
 
146
152
  const contributions = await communityPortal.gather(specificTypeConfig);
147
153
 
148
154
  // Event
149
- t.is(contributions[0].type, specificTypeConfig.communityPortal.participationType);
150
- t.is(contributions[1].type, specificTypeConfig.communityPortal.participationType);
155
+ t.is(
156
+ contributions[0].type,
157
+ specificTypeConfig.communityPortal.participationType
158
+ );
159
+ t.is(
160
+ contributions[1].type,
161
+ specificTypeConfig.communityPortal.participationType
162
+ );
151
163
  t.is(contributions[2].type, specificTypeConfig.communityPortal.campaignType);
152
164
  t.is(contributions[3].type, specificTypeConfig.communityPortal.campaignType);
153
165
  });
package/lib/discourse.js CHANGED
@@ -39,10 +39,7 @@ async function gather({ discourse }) {
39
39
 
40
40
  const topics = await processTopics(discourse);
41
41
  const posts = await processPosts(discourse);
42
- const contributions = [
43
- ...topics,
44
- ...posts,
45
- ];
42
+ const contributions = [...topics, ...posts];
46
43
 
47
44
  debug('Finished gathering contributions');
48
45
  return contributions;
@@ -57,7 +54,9 @@ async function processTopics({ baseUrl, username, topicType }) {
57
54
  for await (const topicsResponse of page) {
58
55
  const { topics } = topicsResponse.topic_list;
59
56
  debug(`Got ${topics.length} topics`);
60
- allTopics = allTopics.concat(topics.map((entity) => formatTopic(topicType, baseUrl, entity)));
57
+ allTopics = allTopics.concat(
58
+ topics.map((entity) => formatTopic(topicType, baseUrl, entity))
59
+ );
61
60
 
62
61
  const hasMore = topics.length === RESULTS_PER_PAGE;
63
62
  if (!hasMore) {
@@ -78,7 +77,9 @@ async function processPosts({ baseUrl, username, postType }) {
78
77
  for await (const postsResponse of page) {
79
78
  const posts = postsResponse.user_actions;
80
79
  debug(`Got ${posts.length} posts`);
81
- allPosts = allPosts.concat(posts.map((entity) => formatPost(postType, baseUrl, entity)));
80
+ allPosts = allPosts.concat(
81
+ posts.map((entity) => formatPost(postType, baseUrl, entity))
82
+ );
82
83
 
83
84
  const hasMore = posts.length === RESULTS_PER_PAGE;
84
85
  if (!hasMore) {
@@ -17,12 +17,14 @@ const TOPICS = {
17
17
 
18
18
  const TOPICS_SMALL = {
19
19
  topic_list: {
20
- topics: [{
21
- created_at: new Date('2020-04-13'),
22
- title: 'Topic 1',
23
- slug: 'slug-1',
24
- id: 1,
25
- }],
20
+ topics: [
21
+ {
22
+ created_at: new Date('2020-04-13'),
23
+ title: 'Topic 1',
24
+ slug: 'slug-1',
25
+ id: 1,
26
+ },
27
+ ],
26
28
  },
27
29
  };
28
30
 
@@ -38,14 +40,16 @@ const POSTS = {
38
40
  };
39
41
 
40
42
  const POSTS_SMALL = {
41
- user_actions: [{
42
- created_at: new Date('2020-04-13'),
43
- title: 'Post 1',
44
- slug: 'slug-1',
45
- topic_id: 1,
46
- post_id: 1,
47
- post_number: 1,
48
- }],
43
+ user_actions: [
44
+ {
45
+ created_at: new Date('2020-04-13'),
46
+ title: 'Post 1',
47
+ slug: 'slug-1',
48
+ topic_id: 1,
49
+ post_id: 1,
50
+ post_number: 1,
51
+ },
52
+ ],
49
53
  };
50
54
 
51
55
  const config = {
@@ -53,21 +57,17 @@ const config = {
53
57
  enabled: true,
54
58
  baseUrl: 'https://discourse.mozilla.org',
55
59
  username: 'user',
56
- }
60
+ },
57
61
  };
58
62
 
59
63
  test.beforeEach((t) => {
60
64
  // eslint-disable-next-line no-param-reassign
61
65
  t.context.sandbox = sinon.createSandbox();
62
66
 
63
- t.context.sandbox.stub(axios, 'get').onCall(0)
64
- .resolves({ data: TOPICS });
65
- axios.get.onCall(1)
66
- .resolves({ data: TOPICS_SMALL });
67
- axios.get.onCall(2)
68
- .resolves({ data: POSTS });
69
- axios.get.onCall(3)
70
- .resolves({ data: POSTS_SMALL });
67
+ t.context.sandbox.stub(axios, 'get').onCall(0).resolves({ data: TOPICS });
68
+ axios.get.onCall(1).resolves({ data: TOPICS_SMALL });
69
+ axios.get.onCall(2).resolves({ data: POSTS });
70
+ axios.get.onCall(3).resolves({ data: POSTS_SMALL });
71
71
  });
72
72
 
73
73
  test.afterEach.always((t) => {
@@ -115,11 +115,14 @@ test.serial('should not fetch if disabled - by enable property', async (t) => {
115
115
  t.is(contributions.length, 0);
116
116
  });
117
117
 
118
- test.serial('should not fetch if disabled - by leaving off config value', async (t) => {
119
- const contributions = await discourse.gather({});
118
+ test.serial(
119
+ 'should not fetch if disabled - by leaving off config value',
120
+ async (t) => {
121
+ const contributions = await discourse.gather({});
120
122
 
121
- t.is(contributions.length, 0);
122
- });
123
+ t.is(contributions.length, 0);
124
+ }
125
+ );
123
126
 
124
127
  test.serial('should format', async (t) => {
125
128
  const contributions = await discourse.gather(config);
@@ -148,7 +151,7 @@ test.serial('should format with custom types', async (t) => {
148
151
  ...config.discourse,
149
152
  topicType: 'Topic created!',
150
153
  postType: 'Posted!',
151
- }
154
+ },
152
155
  };
153
156
 
154
157
  const contributions = await discourse.gather(specificTypeConfig);
package/lib/fetch.js CHANGED
@@ -12,7 +12,9 @@ async function fetchAll(config) {
12
12
  validateConfig(config);
13
13
 
14
14
  debug('fetching...');
15
- const existingContributions = JSON.parse(await fs.readFile(config.outputFile));
15
+ const existingContributions = JSON.parse(
16
+ await fs.readFile(config.outputFile)
17
+ );
16
18
 
17
19
  const bugzillaResult = await bugzilla.gather(config, existingContributions);
18
20
  const githubResult = await github.gather(config, existingContributions);
@@ -31,9 +33,11 @@ async function fetchAll(config) {
31
33
  discourse.TOPICS_CATEGORY,
32
34
  wiki.WIKI_CATEGORY,
33
35
  ];
34
- const existingContributionsToSave = existingContributions.filter((activity) => {
35
- return !ALWAYS_FULL_FETCH_SOURCE_ENTRIES.includes(activity.source);
36
- });
36
+ const existingContributionsToSave = existingContributions.filter(
37
+ (activity) => {
38
+ return !ALWAYS_FULL_FETCH_SOURCE_ENTRIES.includes(activity.source);
39
+ }
40
+ );
37
41
  debug('ALREADY_EXISTING_CONTRIBUTIONS', existingContributionsToSave.length);
38
42
 
39
43
  debug('NEW_BUGZILLA_CONTRIBUTIONS', bugzillaResult);
@@ -42,22 +46,29 @@ async function fetchAll(config) {
42
46
  debug('DISCOURSE_CONTRIBUTIONS', discourseResult.length);
43
47
  debug('WIKI_CONTRIBUTIONS', wikiResult.length);
44
48
 
45
- const uniqueContributions = Array.from(new Set([
46
- ...existingContributionsToSave,
47
- ...discourseResult, // always a new fetch (previous entries removed above)
48
- ...wikiResult, // always a new fetch (previous entries removed above)
49
- ...communityPortalResult, // always a new fetch (previous entries removed above)
50
- ...githubResult,
51
- ...bugzillaResult,
52
- ]));
49
+ const uniqueContributions = Array.from(
50
+ new Set([
51
+ ...existingContributionsToSave,
52
+ ...discourseResult, // always a new fetch (previous entries removed above)
53
+ ...wikiResult, // always a new fetch (previous entries removed above)
54
+ ...communityPortalResult, // always a new fetch (previous entries removed above)
55
+ ...githubResult,
56
+ ...bugzillaResult,
57
+ ])
58
+ );
53
59
 
54
60
  if (uniqueContributions.length === existingContributions.length) {
55
61
  debug('NO_UPDATE_ABORTING');
56
62
  return;
57
63
  }
58
64
 
59
- uniqueContributions.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
60
- await fs.writeFile(config.outputFile, `${JSON.stringify(uniqueContributions, null, 2)}\n`);
65
+ uniqueContributions.sort(
66
+ (a, b) => new Date(b.createdAt) - new Date(a.createdAt)
67
+ );
68
+ await fs.writeFile(
69
+ config.outputFile,
70
+ `${JSON.stringify(uniqueContributions, null, 2)}\n`
71
+ );
61
72
  debug('FILE_WRITTEN');
62
73
  }
63
74
 
package/lib/fetch.test.js CHANGED
@@ -40,25 +40,39 @@ test.serial('should not write file if there was nothing to do', async (t) => {
40
40
  t.is(fs.writeFile.callCount, 0);
41
41
  });
42
42
 
43
- test.serial('should consider saved contributions if not full-fetch source', async (t) => {
44
- const existingContributions = [{ source: 'keep_me' }];
45
- JSON.parse.returns(existingContributions);
46
- github.gather.returns([{ source: 'github' }]);
43
+ test.serial(
44
+ 'should consider saved contributions if not full-fetch source',
45
+ async (t) => {
46
+ const existingContributions = [{ source: 'keep_me' }];
47
+ JSON.parse.returns(existingContributions);
48
+ github.gather.returns([{ source: 'github' }]);
47
49
 
48
- await fetch.fetchAll(config);
49
- t.is(fs.writeFile.callCount, 1);
50
- t.is(t.context.sandbox.originalJSONParse(fs.writeFile.getCall(0).args[1]).length, 2);
51
- });
50
+ await fetch.fetchAll(config);
51
+ t.is(fs.writeFile.callCount, 1);
52
+ t.is(
53
+ t.context.sandbox.originalJSONParse(fs.writeFile.getCall(0).args[1])
54
+ .length,
55
+ 2
56
+ );
57
+ }
58
+ );
52
59
 
53
- test.serial('should ignore saved contributions if full-fetch source', async (t) => {
54
- const existingContributions = [{ source: 'wiki' }, { source: 'wiki' }];
55
- JSON.parse.returns(existingContributions);
56
- github.gather.returns([{ source: 'github' }]);
60
+ test.serial(
61
+ 'should ignore saved contributions if full-fetch source',
62
+ async (t) => {
63
+ const existingContributions = [{ source: 'wiki' }, { source: 'wiki' }];
64
+ JSON.parse.returns(existingContributions);
65
+ github.gather.returns([{ source: 'github' }]);
57
66
 
58
- await fetch.fetchAll(config);
59
- t.is(fs.writeFile.callCount, 1);
60
- t.is(t.context.sandbox.originalJSONParse(fs.writeFile.getCall(0).args[1]).length, 1);
61
- });
67
+ await fetch.fetchAll(config);
68
+ t.is(fs.writeFile.callCount, 1);
69
+ t.is(
70
+ t.context.sandbox.originalJSONParse(fs.writeFile.getCall(0).args[1])
71
+ .length,
72
+ 1
73
+ );
74
+ }
75
+ );
62
76
 
63
77
  test.serial('should take all return values', async (t) => {
64
78
  communityPortal.gather.returns([{ source: 'community-portal' }]);
@@ -68,5 +82,8 @@ test.serial('should take all return values', async (t) => {
68
82
 
69
83
  await fetch.fetchAll(config);
70
84
  t.is(fs.writeFile.callCount, 1);
71
- t.is(t.context.sandbox.originalJSONParse(fs.writeFile.getCall(0).args[1]).length, 4);
85
+ t.is(
86
+ t.context.sandbox.originalJSONParse(fs.writeFile.getCall(0).args[1]).length,
87
+ 4
88
+ );
72
89
  });
@@ -2,9 +2,11 @@ const debug = require('debug')('contributions:github-fetcher');
2
2
  const { Octokit } = require('@octokit/rest');
3
3
 
4
4
  const RESULTS_PER_PAGE = 100;
5
+ const PRIVATE_SCOPE_ERROR = 'PRIVATE_SCOPE';
5
6
 
6
7
  module.exports = {
7
8
  RESULTS_PER_PAGE,
9
+ PRIVATE_SCOPE_ERROR,
8
10
  searchCommits,
9
11
  searchIssues,
10
12
  listReviews,
@@ -76,8 +78,16 @@ async function listComments(options) {
76
78
  async function throwIfTokenHasPrivateRepoScope() {
77
79
  const octokit = getOctokit();
78
80
  const response = await octokit.rest.meta.root();
81
+ // If we don't have the scopes, such as for fine-grained
82
+ // access tokens, we error as well. We can't guarantee
83
+ // that the passed token is not indeed allowing private
84
+ // access.
85
+ if (!response.headers['x-oauth-scopes']) {
86
+ throw new Error(PRIVATE_SCOPE_ERROR);
87
+ }
88
+
79
89
  const scopes = response.headers['x-oauth-scopes'].split(', ');
80
90
  if (scopes.includes('repo')) {
81
- throw new Error('PRIVATE_SCOPE');
91
+ throw new Error(PRIVATE_SCOPE_ERROR);
82
92
  }
83
93
  }