Skip to content

Commit d775b92

Browse files
authored
feat: implement submit-word script (#25)
This Pull request implements the `submit-word` script; this script takes `projectRepoDetails` and `forkedRepoDetails`, more... and the `word` we intended submit to dictionary; its leverages the github pull request api and creates a pull request using the changes found on the `forkedRepoDetails`. ### Changes Made - Implemented the `submitWord` function in the `submit-word` script; this function performs the operations creating a pull request to merge changes from a forked Repo to the main project Repo with the `projectRepoDetails` and `forkedRepoDetails` params, using the `userOctokit` instance which must've been pre-authenticated from session auth token. - Integrated the `devJargons` app to perform the action of adding the label `📖 new|edit word` and `💻 via word-editor` to the PR signifying that the PR was generated from the jargon.dev word-editor. - Added and integrated a `New word` and `Edit word` PR template into the `submitWord`; this template added to the `src/lib/templates` dir Exports 2 String i.e. word `title` and `content` which carries placeholders that are replaced with real `word` data received as params by the `submitWord` function at point of PR creation via the GitHub rest API. ### Screencast/Screenshot [screencast-bpconcjcammlapcogcnnelfmaeghhagj-2024.04.08-03_43_46.webm](https://github.com/babblebey/jargons.dev/assets/25631971/c39c0a16-9719-4749-99ad-bf66475d4d33) 📖
1 parent e4874c1 commit d775b92

5 files changed

Lines changed: 76 additions & 1 deletion

File tree

src/lib/submit-word.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import app from "./octokit/app.js";
2+
import { getRepoParts } from "./utils/index.js";
3+
import newWordPRTemp from "./templates/new-word-pr.md.js";
4+
import editWordPRTemp from "./templates/edit-word-pr.md.js";
5+
6+
/**
7+
* Submit word - create a Pull Request to add word to project repository
8+
* @param {import("octokit").Octokit} userOctokit
9+
* @param {"new" | "edit"} action
10+
* @param {{ repoFullname: string, repoMainBranchRef: string }} projectRepoDetails
11+
* @param {{ repoFullname: string, repoChangeBranchRef: string }} forkedRepoDetails
12+
* @param {{ title: string, content: string }} word
13+
*
14+
* @todo implement (submit as) `draft` feature - [idea]
15+
* @todo implement `maintainer_can_modify` toggle - [idea]
16+
*/
17+
export async function submitWord(userOctokit, action, projectRepoDetails, forkedRepoDetails, word) {
18+
const { repoFullname, repoMainBranchRef } = projectRepoDetails;
19+
const { repoName, repoOwner } = getRepoParts(repoFullname);
20+
const { repoOwner: forkedRepoOwner } = getRepoParts(forkedRepoDetails.repoFullname);
21+
const baseBranch = repoMainBranchRef.split("/").slice(2).join("/");
22+
const headBranch = forkedRepoDetails.repoChangeBranchRef.split("/").slice(2).join("/");
23+
24+
const title = action === "new"
25+
? newWordPRTemp.title.replace("$word_title", word.title)
26+
: editWordPRTemp.title.replace("$word_title", word.title);
27+
28+
const body = action === "new"
29+
? newWordPRTemp.content.replace("$word_title", word.title).replace("$word_content", word.content)
30+
: editWordPRTemp.content.replace("$word_title", word.title).replace("$word_content", word.content);
31+
32+
const response = await userOctokit.request("POST /repos/{owner}/{repo}/pulls", {
33+
owner: repoOwner,
34+
repo: repoName,
35+
head: `${forkedRepoOwner}:${headBranch}`,
36+
base: baseBranch,
37+
title,
38+
body
39+
});
40+
41+
// DevJargons (bot) App adds related labels to PR
42+
app.devJargonsOctokit.request("POST /repos/{owner}/{repo}/issues/{issue_number}/labels", {
43+
owner: repoOwner,
44+
repo: repoName,
45+
issue_number: response.data.number,
46+
labels: [
47+
`:book: ${action} word`,
48+
":computer: via word-editor"
49+
]
50+
});
51+
52+
return response.data;
53+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const title = `Dictionary (Edit Word): $word_title`;
2+
3+
const content = `### Word
4+
5+
#### $word_title
6+
7+
### Updated Meaning/Definition
8+
9+
$word_content`;
10+
11+
export default { title, content };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const title = `Dictionary (New Word): $word_title`;
2+
3+
const content = `### Word
4+
5+
#### $word_title
6+
7+
### Meaning/Definition
8+
9+
$word_content`;
10+
11+
export default { title, content };

src/lib/word-editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import wordFileTemplate from "./templates/word.md.js";
12
import { getRepoParts, normalizeAsUrl } from "./utils/index.js";
2-
import wordFileTemplate from "./template/word.md.js";
33

44
/**
55
* Write and add a new word to user's forked dictionary

0 commit comments

Comments
 (0)