From d331b2d1c2e094dcd6c42d4a9acaebd261f37402 Mon Sep 17 00:00:00 2001 From: rich Date: Fri, 13 Sep 2024 10:16:25 +0100 Subject: [PATCH] Tidy utils files It bothered me having all these functions distrubted throughout the repo. They now all live in a 'utils' folder. I have started a folder for functions that relate to the github API. I have renamed the 'utils.js' file to 'index.js' now that it lives within a utils dir. --- .../githubApi}/fetchAllRepos.js | 2 +- utils/githubApi/fetchOpenPrs.js | 5 ++++ utils/githubApi/fetchOpenPrs.test.js | 13 +++++++++ utils.js => utils/index.js | 0 utils.test.js => utils/index.test.js | 2 +- .../renovate}/dependencyDashboard.js | 27 +++++++++---------- .../renovate}/dependencyDashboard.test.js | 24 +++++------------ 7 files changed, 39 insertions(+), 34 deletions(-) rename {dataScripts => utils/githubApi}/fetchAllRepos.js (96%) create mode 100644 utils/githubApi/fetchOpenPrs.js create mode 100644 utils/githubApi/fetchOpenPrs.test.js rename utils.js => utils/index.js (100%) rename utils.test.js => utils/index.test.js (99%) rename {renovate => utils/renovate}/dependencyDashboard.js (65%) rename {renovate => utils/renovate}/dependencyDashboard.test.js (72%) diff --git a/dataScripts/fetchAllRepos.js b/utils/githubApi/fetchAllRepos.js similarity index 96% rename from dataScripts/fetchAllRepos.js rename to utils/githubApi/fetchAllRepos.js index d5d6c25..2b4e7af 100644 --- a/dataScripts/fetchAllRepos.js +++ b/utils/githubApi/fetchAllRepos.js @@ -1,4 +1,4 @@ -import { OctokitApp } from "../octokitApp.js"; +import { OctokitApp } from "../../octokitApp.js"; import { writeFile, mkdir } from "fs/promises"; import { mapRepoFromApiForStorage } from "../utils.js"; import path from "path"; diff --git a/utils/githubApi/fetchOpenPrs.js b/utils/githubApi/fetchOpenPrs.js new file mode 100644 index 0000000..18545d6 --- /dev/null +++ b/utils/githubApi/fetchOpenPrs.js @@ -0,0 +1,5 @@ +export const getOpenPRsForRepo = ({ octokit, repository }) => { + return octokit.request(repository.pulls_url).then(handlePrsApiResponse); +}; + +export const handlePrsApiResponse = ({ data }) => data?.length || 0; diff --git a/utils/githubApi/fetchOpenPrs.test.js b/utils/githubApi/fetchOpenPrs.test.js new file mode 100644 index 0000000..3902801 --- /dev/null +++ b/utils/githubApi/fetchOpenPrs.test.js @@ -0,0 +1,13 @@ +import { describe, it } from "node:test"; +import expect from "node:assert"; +import { handlePrsApiResponse } from "./fetchOpenPrs.js"; + +describe("handlePrsApiResponse", () => { + it("returns the length of the array containing PRs", () => { + expect.equal(handlePrsApiResponse({ data: [1, 2, 3] }), 3); + }); + + it("returns 0 if there are no open PRs", () => { + expect.equal(handlePrsApiResponse({ data: undefined }), 0); + }); +}); diff --git a/utils.js b/utils/index.js similarity index 100% rename from utils.js rename to utils/index.js diff --git a/utils.test.js b/utils/index.test.js similarity index 99% rename from utils.test.js rename to utils/index.test.js index 0eaf171..48edecd 100644 --- a/utils.test.js +++ b/utils/index.test.js @@ -1,6 +1,6 @@ import { describe, it } from "node:test"; import expect from "node:assert"; -import { mapRepoFromStorageToUi, mapRepoFromApiForStorage } from "./utils.js"; +import { mapRepoFromStorageToUi, mapRepoFromApiForStorage } from "./index.js"; describe("mapRepoFromStorageToUi", () => { it("converts the ISO8601 date to a human-readable date", () => { diff --git a/renovate/dependencyDashboard.js b/utils/renovate/dependencyDashboard.js similarity index 65% rename from renovate/dependencyDashboard.js rename to utils/renovate/dependencyDashboard.js index 94c697e..bcb2071 100644 --- a/renovate/dependencyDashboard.js +++ b/utils/renovate/dependencyDashboard.js @@ -25,13 +25,14 @@ const LINE_SEPARATOR_REGEX = /\r?\n|\r|\n/g; // Group 2: ^7.20.2 const DEPENDENCY_NAME_AND_VERSION_REGEX = /`(\S+?) (.*)`/; -const issueIsRenovateDependencyDashboard = (issue) => issue.user.login === "renovate[bot]" && issue.pull_request === undefined; +const issueIsRenovateDependencyDashboard = (issue) => + issue.user.login === "renovate[bot]" && issue.pull_request === undefined; -const parseDependenciesFromDashboard = (issue) => issue - .body - .split(LINE_SEPARATOR_REGEX) - .map(parseDependencyFromLine) - .filter((dependency) => dependency !== null); +const parseDependenciesFromDashboard = (issue) => + issue.body + .split(LINE_SEPARATOR_REGEX) + .map(parseDependencyFromLine) + .filter((dependency) => dependency !== null); const parseDependencyFromLine = (line) => { const match = line.match(DEPENDENCY_NAME_AND_VERSION_REGEX); @@ -41,24 +42,20 @@ const parseDependencyFromLine = (line) => { } return new Dependency(match[1], match[2]); -} +}; export const handleIssuesApiResponse = (response) => { - const dependencyDashboardIssue = response.data.find(issueIsRenovateDependencyDashboard); + const dependencyDashboardIssue = response.data.find( + issueIsRenovateDependencyDashboard + ); if (!dependencyDashboardIssue) { return []; } return parseDependenciesFromDashboard(dependencyDashboardIssue); -} - -export const handlePrsApiResponse = ({ data }) => data?.length || 0; +}; export const getDependenciesForRepo = ({ octokit, repository }) => { return octokit.request(repository.issues_url).then(handleIssuesApiResponse); }; - -export const getOpenPRsForRepo = ({ octokit, repository }) => { - return octokit.request(repository.pulls_url).then(handlePrsApiResponse); -}; diff --git a/renovate/dependencyDashboard.test.js b/utils/renovate/dependencyDashboard.test.js similarity index 72% rename from renovate/dependencyDashboard.test.js rename to utils/renovate/dependencyDashboard.test.js index 7e061ae..a99d45a 100644 --- a/renovate/dependencyDashboard.test.js +++ b/utils/renovate/dependencyDashboard.test.js @@ -1,6 +1,6 @@ import { describe, it } from "node:test"; import expect from "node:assert"; -import { Dependency, handleIssuesApiResponse, handlePrsApiResponse } from "./dependencyDashboard.js"; +import { Dependency, handleIssuesApiResponse } from "./dependencyDashboard.js"; describe("handleIssuesApiResponse", () => { it("should extract dependency version information from the Renovate Dependency Dashboard if it exists", async () => { @@ -24,9 +24,9 @@ describe("handleIssuesApiResponse", () => { user: { login: "renovate[bot]", }, - body: "# Dependency Dashboard\nList of dependencies:\n- `libquux v4.1.1.rc4`\n- `@xyzzy/utils \"~> 22.04 Questing Quokka\"`\n\nHere's some more:\n- `baz-framework ^0.1`", - } - ] + body: '# Dependency Dashboard\nList of dependencies:\n- `libquux v4.1.1.rc4`\n- `@xyzzy/utils "~> 22.04 Questing Quokka"`\n\nHere\'s some more:\n- `baz-framework ^0.1`', + }, + ], }; const expectedDependencies = [ @@ -37,7 +37,7 @@ describe("handleIssuesApiResponse", () => { expect.deepEqual( handleIssuesApiResponse(issuesApiResponse), - expectedDependencies, + expectedDependencies ); }); @@ -58,24 +58,14 @@ describe("handleIssuesApiResponse", () => { pull_request: {}, body: "Configure Renovate", }, - ] + ], }; const expectedDependencies = []; expect.deepEqual( handleIssuesApiResponse(issuesApiResponse), - expectedDependencies, + expectedDependencies ); }); - - describe("handlePrsApiResponse", () => { - it("returns the length of the array containing PRs", () => { - expect.equal(handlePrsApiResponse({ data: [1, 2, 3] }), 3); - }); - - it("returns 0 if there are no open PRs", () => { - expect.equal(handlePrsApiResponse({ data: undefined }), 0); - }); - }); });