Skip to content

Commit 3412335

Browse files
babblebeyCopilot
andauthored
refactor(editor): replace gh-search-api consumtion with graphql in jargons editor (#161)
### Description <!-- Please add PR description (don't leave blank) - example: This PR [adds/removes/fixes/replaces] the [feature/bug/etc] --> This pull request improves the way contribution stats are queried by switching to a single GraphQL request from github search-api which is getting removed after getting deprecated and narrowing results to the main branch. These changes enhance maintainability, accuracy, and future extensibility. introduces a standardized set of label constants for contribution-related pull requests and refactors the codebase to use these constants consistently. **Label standardization and usage:** * Added a `LABELS` constant to `constants.js` to centralize label definitions for new words, edited words, and editor-originated contributions. * Refactored label usage in `submitWord` to use the new constants instead of hardcoded strings. [[1]](diffhunk://#diff-da5c3e61f828ff6207118ba3e3196d6263237671b3897650d8642fa5576a00c6R1) [[2]](diffhunk://#diff-da5c3e61f828ff6207118ba3e3196d6263237671b3897650d8642fa5576a00c6L47-R49) **Contribution stats improvements:** * Refactored `doContributionStats` to use a single GraphQL query for all stats, improving performance and maintainability. * Updated contribution stats queries to use the main branch and the standardized labels, ensuring more accurate results. **Code consistency:** * Updated imports in relevant files (`do-contribution-stats.js`, `submit-word.js`, `index.astro`) to use the new `LABELS` constant. [[1]](diffhunk://#diff-66fb997961c22f18ff765f0e831b43870a845fe98eadf9d176e26288f677a4ddL4-R4) [[2]](diffhunk://#diff-da5c3e61f828ff6207118ba3e3196d6263237671b3897650d8642fa5576a00c6R1) [[3]](diffhunk://#diff-19c5ae8617d4b91be3692fd77b705dd7e871e0b6750543a0d3a78293582cee4bL8-R8) * Updated the total words stats query in `index.astro` to use the standardized labels and improved query syntax. ### Related Issue <!-- Please prefix the issue number with Fixes/Resolves - example: Fixes #123 or Resolves #123 --> Nil ### Screenshots/Screencasts <!-- Please provide screenshots or video recording that demos your changes (especially if it's a visual change) --> <img width="960" height="540" alt="image" src="https://github.com/user-attachments/assets/27902060-d20e-4054-8314-7cb3756687e2" /> ### Notes to Reviewer <!-- Please state here if you added a new npm packages, or any extra information that can help reviewer better review you changes --> Nil --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b1fa81b commit 3412335

4 files changed

Lines changed: 46 additions & 32 deletions

File tree

constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ export const PROJECT_REPO_DETAILS = {
33
repoMainBranchRef: import.meta.env.PUBLIC_PROJECT_REPO_BRANCH_REF
44
}
55

6+
export const LABELS = {
7+
NEW_WORD: "📖new-word",
8+
EDIT_WORD: "📖edit-word",
9+
VIA_EDITOR: "💻via-jargons-editor",
10+
};
11+
612
export const SITE_META_DESCRIPTION = "A community-driven dictionary that simplifies software, engineering and tech terms for all levels. Curated by contributors, jargons.dev offers clear, easy-to-understand definitions."
713

814
export const SITE_META_KEYWORDS = [

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

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

66
/**
77
* Get some jargons contribution stats for current user on the Jargons Editor
@@ -16,49 +16,56 @@ export default async function doContributionStats(astroGlobal) {
1616
});
1717
const userOctokit = app.getUserOctokit({ token: accessToken.value });
1818

19-
/**
20-
* @todo Implement narrowed search to project's main branch
21-
*/
22-
const baseQuery = `repo:${repoFullname} is:pull-request type:pr author:@me`;
23-
const baseStatsUrlQuery = `is:pr author:@me`;
19+
// Build queries using the viewer's login and narrow to main branch if available
20+
const { data: me } = await userOctokit.request("GET /user");
21+
const viewerLogin = me?.login;
2422

25-
/**
26-
* @todo [thoughts]: would be nice to have all these requests in one, and use a filter to separate by labels
27-
* [potential bottleneck]: no way to know whether a PR is "merged" considering that "closed" doesn't mean merged
28-
*/
29-
const { data: newType } = await userOctokit.request("GET /search/issues", {
30-
q: `${baseQuery} label:":book: new word" is:merged is:closed`,
31-
});
32-
const { data: editType } = await userOctokit.request("GET /search/issues", {
33-
q: `${baseQuery} label:":book: edit word" is:merged is:closed`,
23+
const branchInfo = repoMainBranchRef ? repoMainBranchRef.split("/").slice(2).join("/") : "";
24+
25+
const baseQuery = `repo:${repoFullname} is:pull-request type:pr author:${viewerLogin} base:${branchInfo}`;
26+
const baseStatsUrlQuery = `is:pr author:@me base:${branchInfo}`;
27+
28+
const newMergedQuery = `${baseQuery} label:"${LABELS.NEW_WORD}" is:merged is:closed`;
29+
const editMergedQuery = `${baseQuery} label:"${LABELS.EDIT_WORD}" is:merged is:closed`;
30+
const pendingOpenQuery = `${baseQuery} label:"${LABELS.VIA_EDITOR}" is:open`;
31+
32+
const data = await userOctokit.graphql(`
33+
#graphql
34+
query ContributionStats($newMergedQuery: String!, $editMergedQuery: String!, $pendingOpenQuery: String!) {
35+
newMerged: search(query: $newMergedQuery, type: ISSUE, first: 1) { issueCount }
36+
editMerged: search(query: $editMergedQuery, type: ISSUE, first: 1) { issueCount }
37+
pendingOpen: search(query: $pendingOpenQuery, type: ISSUE, first: 1) { issueCount }
38+
}
39+
`, {
40+
newMergedQuery,
41+
editMergedQuery,
42+
pendingOpenQuery,
3443
});
35-
const { data: pendingType } = await userOctokit.request(
36-
"GET /search/issues",
37-
{
38-
q: `${baseQuery} label:":book: edit word",":book: new word" is:unmerged is:open`,
39-
}
40-
);
44+
45+
const newCount = data?.newMerged?.issueCount ?? 0;
46+
const editCount = data?.editMerged?.issueCount ?? 0;
47+
const pendingCount = data?.pendingOpen?.issueCount ?? 0;
4148

4249
return {
4350
newWords: {
44-
count: newType.total_count,
51+
count: newCount,
4552
url: buildStatsUrl(
4653
repoFullname,
47-
`${baseStatsUrlQuery} is:merged is:closed label:":book: new word"`
54+
`${baseStatsUrlQuery} is:merged is:closed label:"${LABELS.NEW_WORD}"`
4855
),
4956
},
5057
editedWords: {
51-
count: editType.total_count,
58+
count: editCount,
5259
url: buildStatsUrl(
5360
repoFullname,
54-
`${baseStatsUrlQuery} is:merged is:closed label:":book: edit word"`
61+
`${baseStatsUrlQuery} is:merged is:closed label:"${LABELS.EDIT_WORD}"`
5562
),
5663
},
5764
pendingWords: {
58-
count: pendingType.total_count,
65+
count: pendingCount,
5966
url: buildStatsUrl(
6067
repoFullname,
61-
`${baseStatsUrlQuery} is:unmerged is:open`
68+
`${baseStatsUrlQuery} is:open label:"${LABELS.VIA_EDITOR}"`
6269
),
6370
},
6471
};

src/lib/submit-word.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LABELS } from "../../constants.js";
12
import { getRepoParts } from "./utils/index.js";
23
import newWordPRTemp from "./templates/new-word-pr.md.js";
34
import editWordPRTemp from "./templates/edit-word-pr.md.js";
@@ -44,8 +45,8 @@ export async function submitWord(jargonsdevOctokit, userOctokit, action, project
4445
repo: repoName,
4546
issue_number: response.data.number,
4647
labels: [
47-
`:book: ${action} word`,
48-
":computer: via jargons-editor"
48+
action === "new" ? LABELS.NEW_WORD : LABELS.EDIT_WORD,
49+
LABELS.VIA_EDITOR
4950
]
5051
});
5152

src/pages/editor/index.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import doAuth from "../../lib/actions/do-auth.js";
55
import Navbar from "../../components/navbar.astro";
66
import { buildStatsUrl } from "../../lib/utils/index.js";
77
import Profile from "../../components/islands/profile.jsx";
8-
import { PROJECT_REPO_DETAILS } from "../../../constants.js";
8+
import { PROJECT_REPO_DETAILS, LABELS } from "../../../constants.js";
99
import doContributionStats from "../../lib/actions/do-contribution-stats.js";
1010
import ContributionCTA from "../../components/contribution-cta.astro";
1111
@@ -18,8 +18,8 @@ const { newWords, editedWords, pendingWords } = await doContributionStats(Astro)
1818
const totalWords = {
1919
count: newWords.count + editedWords.count,
2020
url: buildStatsUrl(
21-
PROJECT_REPO_DETAILS.repoFullname,
22-
`label:":book: edit word",":book: new word" is:pr is:closed is:merged author:@me`
21+
PROJECT_REPO_DETAILS.repoFullname,
22+
`(label:"${LABELS.EDIT_WORD}" OR label:"${LABELS.NEW_WORD}") is:pr is:closed is:merged author:@me`
2323
)
2424
}
2525
---

0 commit comments

Comments
 (0)