Skip to content

Commit 7ea2fca

Browse files
refactor: extract branch name ops to getBranchNameFromRef function (#246)
## Description Replaced all occurences of the get branch from a ref branch logic with a new function getBranchFromRef ## Related Issue Resolves [#24](#24) Co-authored-by: Olabode Lawal-Shittabey <[email protected]>
1 parent 09cdb94 commit 7ea2fca

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

src/lib/actions/do-contribution-stats.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { LABELS, PROJECT_REPO_DETAILS } from "../../../constants.js";
12
import app from "../octokit/app.js";
23
import { decrypt } from "../utils/crypto.js";
3-
import { buildStatsUrl } from "../utils/index.js";
4-
import { PROJECT_REPO_DETAILS, LABELS } from "../../../constants.js";
4+
import { buildStatsUrl, getBranchNameFromRef } from "../utils/index.js";
55

66
/**
77
* Get some jargons contribution stats for current user on the Jargons Editor
@@ -21,7 +21,7 @@ export default async function doContributionStats(astroGlobal) {
2121
const viewerLogin = me?.login;
2222

2323
const branchInfo = repoMainBranchRef
24-
? repoMainBranchRef.split("/").slice(2).join("/")
24+
? getBranchNameFromRef(repoMainBranchRef)
2525
: "";
2626

2727
const baseQuery = `repo:${repoFullname} is:pull-request type:pr author:${viewerLogin} base:${branchInfo}`;

src/lib/submit-word.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LABELS } from "../../constants.js";
2-
import { getRepoParts } from "./utils/index.js";
3-
import newWordPRTemp from "./templates/new-word-pr.md.js";
42
import editWordPRTemp from "./templates/edit-word-pr.md.js";
3+
import newWordPRTemp from "./templates/new-word-pr.md.js";
4+
import { getBranchNameFromRef, getRepoParts } from "./utils/index.js";
55

66
/**
77
* Submit word - create a Pull Request to add word to project repository
@@ -27,11 +27,10 @@ export async function submitWord(
2727
const { repoOwner: forkedRepoOwner } = getRepoParts(
2828
forkedRepoDetails.repoFullname,
2929
);
30-
const baseBranch = repoMainBranchRef.split("/").slice(2).join("/");
31-
const headBranch = forkedRepoDetails.repoChangeBranchRef
32-
.split("/")
33-
.slice(2)
34-
.join("/");
30+
const baseBranch = getBranchNameFromRef(repoMainBranchRef);
31+
const headBranch = getBranchNameFromRef(
32+
forkedRepoDetails.repoChangeBranchRef,
33+
);
3534

3635
const title =
3736
action === "new"

src/lib/utils/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,12 @@ export function buildWordPathname(slug) {
9999
export function buildWordSlug(id) {
100100
return id.split(".")[0];
101101
}
102+
103+
/**
104+
* Get branch name from branch ref
105+
* @param {string} branchRef
106+
* @returns {string}
107+
*/
108+
export function getBranchNameFromRef(branchRef) {
109+
return branchRef.split("/").slice(2).join("/");
110+
}

src/lib/word-editor.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import wordFileTemplate from "./templates/word.md.js";
2-
import { getRepoParts, normalizeAsUrl } from "./utils/index.js";
2+
import {
3+
getBranchNameFromRef,
4+
getRepoParts,
5+
normalizeAsUrl,
6+
} from "./utils/index.js";
37

48
/**
59
* Write and add a new word to user's forked dictionary
@@ -14,7 +18,7 @@ export async function writeNewWord(
1418
) {
1519
const { repoFullname, repoChangeBranchRef } = forkedRepoDetails;
1620
const { repoOwner, repoName } = getRepoParts(repoFullname);
17-
const branch = repoChangeBranchRef.split("/").slice(2).join("/");
21+
const branch = getBranchNameFromRef(repoChangeBranchRef);
1822
const wordFileContent = writeWordFileContent(title, content);
1923

2024
try {
@@ -52,7 +56,7 @@ export async function updateExistingWord(
5256
) {
5357
const { repoFullname, repoChangeBranchRef } = forkedRepoDetails;
5458
const { repoOwner, repoName } = getRepoParts(repoFullname);
55-
const branch = repoChangeBranchRef.split("/").slice(2).join("/");
59+
const branch = getBranchNameFromRef(repoChangeBranchRef);
5660
const wordFileContent = writeWordFileContent(title, content);
5761

5862
try {

0 commit comments

Comments
 (0)