Skip to content

Commit

Permalink
Tidy utils files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rich committed Sep 13, 2024
1 parent bb2f2ed commit d331b2d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
5 changes: 5 additions & 0 deletions utils/githubApi/fetchOpenPrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getOpenPRsForRepo = ({ octokit, repository }) => {
return octokit.request(repository.pulls_url).then(handlePrsApiResponse);
};

export const handlePrsApiResponse = ({ data }) => data?.length || 0;
13 changes: 13 additions & 0 deletions utils/githubApi/fetchOpenPrs.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
File renamed without changes.
2 changes: 1 addition & 1 deletion utils.test.js → utils/index.test.js
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
};
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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 = [
Expand All @@ -37,7 +37,7 @@ describe("handleIssuesApiResponse", () => {

expect.deepEqual(
handleIssuesApiResponse(issuesApiResponse),
expectedDependencies,
expectedDependencies
);
});

Expand All @@ -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);
});
});
});

0 comments on commit d331b2d

Please sign in to comment.