@jsenv/github-pull-request-impact 1.6.5 → 1.6.9

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
@@ -1,17 +1,13 @@
1
- # GitHub pull request impact
1
+ # GitHub pull request impact [![npm package](https://img.shields.io/npm/v/@jsenv/github-pull-request-impact.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/github-pull-request-impact)
2
2
 
3
3
  Report pull request impact on something.
4
4
 
5
- [![npm package](https://img.shields.io/npm/v/@jsenv/github-pull-request-impact.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/github-pull-request-impact)
6
- [![github main](https://github.com/jsenv/github-pull-request-impact/workflows/main/badge.svg)](https://github.com/jsenv/github-pull-request-impact/actions?workflow=main)
7
- [![codecov coverage](https://codecov.io/gh/jsenv/github-pull-request-impact/branch/main/graph/badge.svg)](https://codecov.io/gh/jsenv/github-pull-request-impact)
8
-
9
5
  # Presentation
10
6
 
11
7
  Under construction.
12
8
 
13
- This repository publishes `@jsenv/github-pull-request-impact` on npm. This package is reused internally by:
9
+ This repository publishes `@jsenv/github-pull-request-impact` on NPM. This package is reused internally by:
14
10
 
15
- - [@jsenv/performance-impact](https://github.com/jsenv/performance-impact)
16
- - [@jsenv/file-size-impact](https://github.com/jsenv/file-size-impact)
17
- - [@jsenv/lighthouse-score-impact](https://github.com/jsenv/jsenv-lighthouse-score-impact)
11
+ - [@jsenv/performance-impact](../jsenv-performance-impact)
12
+ - [@jsenv/file-size-impact](../jsenv-file-size-impact)
13
+ - [@jsenv/lighthouse-score-impact](../jsenv-performance-impact)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/github-pull-request-impact",
3
- "version": "1.6.5",
3
+ "version": "1.6.9",
4
4
  "description": "Report pull request impact on something",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -17,7 +17,8 @@
17
17
  "node": ">=16.13.0"
18
18
  },
19
19
  "publishConfig": {
20
- "access": "public"
20
+ "access": "public",
21
+ "registry": "https://registry.npmjs.org"
21
22
  },
22
23
  "type": "module",
23
24
  "exports": {
@@ -34,23 +35,23 @@
34
35
  "scripts": {
35
36
  "eslint": "npx eslint . --ext=.js,.mjs",
36
37
  "importmap": "node ./script/importmap/importmap.mjs",
37
- "test": "node --experimental-json-modules ./script/test/test.mjs",
38
+ "test": "node ./script/test/test.mjs",
38
39
  "test-with-coverage": "npm run test -- --coverage",
39
40
  "prettier": "prettier --write ."
40
41
  },
41
42
  "dependencies": {
42
43
  "@jsenv/filesystem": "2.7.1",
43
44
  "@jsenv/logger": "4.0.1",
44
- "@jsenv/server": "12.4.0"
45
+ "node-fetch": "2.6.7"
45
46
  },
46
47
  "devDependencies": {
47
48
  "@jsenv/assert": "2.4.1",
48
49
  "@jsenv/core": "25.3.0",
49
50
  "@jsenv/eslint-config": "16.0.9",
50
51
  "@jsenv/importmap-eslint-resolver": "5.2.5",
51
- "@jsenv/importmap-node-module": "5.1.2",
52
+ "@jsenv/importmap-node-module": "5.1.3",
52
53
  "eslint": "8.7.0",
53
54
  "eslint-plugin-import": "2.25.4",
54
55
  "prettier": "2.5.1"
55
56
  }
56
- }
57
+ }
@@ -4,14 +4,14 @@
4
4
  * @jsenv/file-size-impact, @jsenv/performance-impact and @jsenv/lighthouse-score-impact
5
5
  */
6
6
 
7
- import { createLogger } from "@jsenv/logger"
8
7
  import {
9
8
  assertAndNormalizeDirectoryUrl,
10
9
  urlToFileSystemPath,
11
10
  } from "@jsenv/filesystem"
11
+ import { createLogger } from "@jsenv/logger"
12
12
 
13
+ import * as githubRESTAPI from "./internal/github_rest_api.js"
13
14
  import { exec } from "./internal/exec.js"
14
- import { GET, POST, PATCH } from "./internal/git_hub_api.js"
15
15
  import { renderGeneratedByText } from "./internal/renderGeneratedByText.js"
16
16
  import { createGitHubPullRequestCommentText } from "./internal/createGitHubPullRequestCommentText.js"
17
17
 
@@ -146,7 +146,8 @@ const commentPrImpact = async ({
146
146
  const pullApiUrl = `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/pulls/${pullRequestNumber}`
147
147
  // https://developer.github.com/v3/pulls/#get-a-pull-request
148
148
  logger.debug(`get pull request ${pullApiUrl}`)
149
- const pullRequest = await GET(pullApiUrl, {
149
+ const pullRequest = await githubRESTAPI.GET({
150
+ url: pullApiUrl,
150
151
  githubToken,
151
152
  })
152
153
  const pullRequestHtmlUrl = pullRequest.html_url
@@ -176,7 +177,8 @@ const commentPrImpact = async ({
176
177
 
177
178
  logger.debug(`searching comment in pull request ${pullRequestHtmlUrl}`)
178
179
  const pullRequestCommentApiUrl = pullRequest.comments_url
179
- const comments = await GET(pullRequestCommentApiUrl, {
180
+ const comments = await githubRESTAPI.GET({
181
+ url: pullRequestCommentApiUrl,
180
182
  githubToken,
181
183
  })
182
184
  const existingComment = comments.find(({ body }) =>
@@ -214,21 +216,21 @@ const commentPrImpact = async ({
214
216
  return existingComment
215
217
  }
216
218
  logger.info(`updating comment at ${existingComment.html_url}`)
217
- const comment = await PATCH(
218
- `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/issues/comments/${existingComment.id}`,
219
- { body: commentBody },
220
- { githubToken },
221
- )
219
+ const comment = await githubRESTAPI.PATCH({
220
+ url: `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/issues/comments/${existingComment.id}`,
221
+ githubToken,
222
+ body: { body: commentBody },
223
+ })
222
224
  logger.info("comment updated")
223
225
  return comment
224
226
  }
225
227
 
226
228
  logger.info(`creating comment`)
227
- const comment = await POST(
228
- `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/issues/${pullRequestNumber}/comments`,
229
- { body: commentBody },
230
- { githubToken },
231
- )
229
+ const comment = await githubRESTAPI.POST({
230
+ url: `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/issues/${pullRequestNumber}/comments`,
231
+ githubToken,
232
+ body: { body: commentBody },
233
+ })
232
234
  logger.info(`comment created at ${comment.html_url}`)
233
235
  return comment
234
236
  }
@@ -1,6 +1,9 @@
1
- import { exec as nodeExec } from "child_process"
1
+ import { exec as nodeExec } from "node:child_process"
2
2
 
3
- export const exec = (command, { cwd, onLog = () => {}, onErrorLog = () => {} } = {}) => {
3
+ export const exec = (
4
+ command,
5
+ { cwd, onLog = () => {}, onErrorLog = () => {} } = {},
6
+ ) => {
4
7
  return new Promise((resolve, reject) => {
5
8
  const commandProcess = nodeExec(
6
9
  command,
@@ -1,10 +1,14 @@
1
- import { fetchUrl } from "@jsenv/server"
1
+ import nodeFetch from "node-fetch"
2
2
 
3
- export const GET = async (url, { githubToken } = {}) => {
4
- return sendHttpRequest(url, {
3
+ export const GET = async ({ url, githubToken, headers }) => {
4
+ return sendHttpRequest({
5
+ url,
5
6
  method: "GET",
6
- headers: tokenToHeaders(githubToken),
7
- responseStatusMap: {
7
+ headers: {
8
+ ...tokenToHeaders(githubToken),
9
+ headers,
10
+ },
11
+ responseStatusHandlers: {
8
12
  200: async (response) => {
9
13
  const json = await response.json()
10
14
  return json
@@ -14,12 +18,16 @@ export const GET = async (url, { githubToken } = {}) => {
14
18
  })
15
19
  }
16
20
 
17
- export const POST = (url, body, { githubToken } = {}) => {
18
- return sendHttpRequest(url, {
21
+ export const POST = ({ url, body, githubToken, headers }) => {
22
+ return sendHttpRequest({
23
+ url,
19
24
  method: "POST",
20
- headers: tokenToHeaders(githubToken),
25
+ headers: {
26
+ ...tokenToHeaders(githubToken),
27
+ headers,
28
+ },
21
29
  body: JSON.stringify(body),
22
- responseStatusMap: {
30
+ responseStatusHandlers: {
23
31
  201: async (response) => {
24
32
  const json = await response.json()
25
33
  return json
@@ -28,12 +36,16 @@ export const POST = (url, body, { githubToken } = {}) => {
28
36
  })
29
37
  }
30
38
 
31
- export const PATCH = async (url, body, { githubToken } = {}) => {
32
- return sendHttpRequest(url, {
39
+ export const PATCH = async ({ url, body, githubToken, headers }) => {
40
+ return sendHttpRequest({
41
+ url,
33
42
  method: "PATCH",
34
- headers: tokenToHeaders(githubToken),
43
+ headers: {
44
+ ...tokenToHeaders(githubToken),
45
+ headers,
46
+ },
35
47
  body: JSON.stringify(body),
36
- responseStatusMap: {
48
+ responseStatusHandlers: {
37
49
  200: async (response) => {
38
50
  const json = await response.json()
39
51
  return json
@@ -51,13 +63,16 @@ const tokenToHeaders = (token) => {
51
63
  }
52
64
  }
53
65
 
54
- const sendHttpRequest = async (
66
+ const sendHttpRequest = async ({
55
67
  url,
56
- { method, headers, body, responseStatusMap },
57
- ) => {
68
+ method,
69
+ headers,
70
+ body,
71
+ responseStatusHandlers = {},
72
+ }) => {
58
73
  let response
59
74
  try {
60
- response = await fetchUrl(url, {
75
+ response = await nodeFetch(url, {
61
76
  method,
62
77
  headers: {
63
78
  ...(typeof body === "undefined"
@@ -78,16 +93,16 @@ ${error.stack}`)
78
93
  }
79
94
 
80
95
  const { status } = response
81
- if (status in responseStatusMap) {
82
- return responseStatusMap[response.status](response)
96
+ const responseStatusHandler = responseStatusHandlers[status]
97
+ if (responseStatusHandler) {
98
+ return responseStatusHandler(response)
83
99
  }
84
-
85
100
  const responseBodyAsJson = await response.json()
86
101
  const error = new Error(`unexpected response status.
87
102
  --- response status ---
88
103
  ${response.status}
89
104
  --- expected response status ---
90
- ${Object.keys(responseStatusMap).join(", ")}
105
+ ${Object.keys(responseStatusHandlers).join(", ")}
91
106
  --- request method ---
92
107
  ${method}
93
108
  --- request url ---