Skip to content

Commit

Permalink
WIP Switch to codemirror
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Collonval committed Feb 23, 2021
1 parent adc74d8 commit 05e731a
Show file tree
Hide file tree
Showing 33 changed files with 1,510 additions and 674 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false

[*.{css,json,ts,tsx}]
indent_size = 2
4 changes: 2 additions & 2 deletions jupyterlab_pullrequests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

class PRCommentReply(NamedTuple):
text: str
in_reply_to: str
inReplyTo: str


class PRCommentNew(NamedTuple):
text: str
commit_id: str
commitId: str
filename: str
position: int

Expand Down
4 changes: 2 additions & 2 deletions jupyterlab_pullrequests/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ class PullRequestsFileNBDiffHandler(PullRequestsAPIHandler):
async def post(self):
data = get_body_value(self)
try:
prev_content = data["prev_content"]
curr_content = data["curr_content"]
prev_content = data["previousContent"]
curr_content = data["currentContent"]
except KeyError as e:
get_logger().error(f"Missing key in POST request.", exc_info=e)
raise tornado.web.HTTPError(
Expand Down
30 changes: 14 additions & 16 deletions jupyterlab_pullrequests/managers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ async def list_prs(self, username: str, pr_filter: str) -> List[Dict[str, str]]:
"id": result["pull_request"]["url"],
"title": result["title"],
"body": result["body"],
"internal_id": result["id"],
"url": result["html_url"],
"internalId": result["id"],
"link": result["html_url"],
}
)

Expand All @@ -78,8 +78,6 @@ async def list_files(self, pr_id: str) -> List[Dict[str, str]]:
{
"name": result["filename"],
"status": result["status"],
"additions": result["additions"],
"deletions": result["deletions"],
}
)

Expand All @@ -101,7 +99,7 @@ async def get_pr_links(self, pr_id: str, filename: str) -> Dict[str, str]:
{"ref": data["head"]["ref"]},
)
commit_id = data["head"]["sha"]
return {"base_url": base_url, "head_url": head_url, "commit_id": commit_id}
return {"baseUrl": base_url, "headUrl": head_url, "commitId": commit_id}

async def validate_pr_link(self, link: str):
try:
Expand All @@ -124,16 +122,16 @@ async def get_file_content(self, pr_id: str, filename: str) -> Dict[str, str]:

links = await self.get_pr_links(pr_id, filename)

base_raw_url = await self.validate_pr_link(links["base_url"])
head_raw_url = await self.validate_pr_link(links["head_url"])
base_raw_url = await self.validate_pr_link(links["baseUrl"])
head_raw_url = await self.validate_pr_link(links["headUrl"])

base_content = await self.get_link_content(base_raw_url)
head_content = await self.get_link_content(head_raw_url)

return {
"base_content": base_content,
"head_content": head_content,
"commit_id": links["commit_id"],
"baseContent": base_content,
"headContent": head_content,
"commitId": links["commitId"],
}

# -----------------------------------------------------------------------------
Expand All @@ -143,14 +141,14 @@ async def get_file_content(self, pr_id: str, filename: str) -> Dict[str, str]:
def file_comment_response(self, result: Dict[str, str]) -> Dict[str, str]:
data = {
"id": result["id"],
"line_number": result["position"],
"lineNumber": result["position"],
"text": result["body"],
"updated_at": result["updated_at"],
"user_name": result["user"]["login"],
"user_pic": result["user"]["avatar_url"],
"updatedAt": result["updated_at"],
"userName": result["user"]["login"],
"userPic": result["user"]["avatar_url"],
}
if "in_reply_to_id" in result:
data["in_reply_to_id"] = result["in_reply_to_id"]
data["inReplyToId"] = result["in_reply_to_id"]
return data

async def get_file_comments(
Expand All @@ -174,7 +172,7 @@ async def post_file_comment(
else:
body = {
"body": body.text,
"commit_id": body.commit_id,
"commit_id": body.commitId,
"path": body.filename,
"position": body.position,
}
Expand Down
34 changes: 16 additions & 18 deletions jupyterlab_pullrequests/managers/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ async def list_prs(self, username: str, pr_filter: str) -> List[Dict[str, str]]:
"id": url,
"title": result["title"],
"body": result["description"],
"internal_id": result["id"],
"url": result["web_url"],
"internalId": result["id"],
"link": result["web_url"],
}
)

Expand Down Expand Up @@ -94,8 +94,6 @@ async def list_files(self, pr_id: str) -> List[Dict[str, str]]:
{
"name": result["new_path"],
"status": status,
"additions": 0,
"deletions": 0,
}
)

Expand Down Expand Up @@ -131,7 +129,7 @@ async def get_pr_links(self, pr_id: str, filename: str) -> Dict[str, str]:
{"ref": data["source_branch"]},
)
commit_id = data["diff_refs"]["head_sha"]
return {"base_url": base_url, "head_url": head_url, "commit_id": commit_id}
return {"baseUrl": base_url, "headUrl": head_url, "commitId": commit_id}

async def get_link_content(self, file_url: str):
try:
Expand All @@ -143,13 +141,13 @@ async def get_file_content(self, pr_id: str, filename: str) -> Dict[str, str]:

links = await self.get_pr_links(pr_id, filename)

base_content = await self.get_link_content(links["base_url"])
head_content = await self.get_link_content(links["head_url"])
base_content = await self.get_link_content(links["baseUrl"])
head_content = await self.get_link_content(links["headUrl"])

return {
"base_content": base_content,
"head_content": head_content,
"commit_id": links["commit_id"],
"baseContent": base_content,
"headContent": head_content,
"commitId": links["commitId"],
}

# -----------------------------------------------------------------------------
Expand All @@ -159,14 +157,14 @@ async def get_file_content(self, pr_id: str, filename: str) -> Dict[str, str]:
def file_comment_response(self, result: Dict[str, str]) -> Dict[str, str]:
data = {
"id": result["id"],
"line_number": result["position"]["new_line"],
"lineNumber": result["position"]["new_line"],
"text": result["body"],
"updated_at": result["updated_at"],
"user_name": result["author"]["username"],
"user_pic": result["author"]["avatar_url"],
"updatedAt": result["updated_at"],
"userName": result["author"]["username"],
"userPic": result["author"]["avatar_url"],
}
if "in_reply_to_id" in result:
data["in_reply_to_id"] = result["in_reply_to_id"]
data["inReplyToId"] = result["in_reply_to_id"]
return data

async def get_file_comments(
Expand All @@ -190,13 +188,13 @@ async def post_file_comment(
):
if isinstance(body, PRCommentReply):
body = {"body": body.text}
# git_url = url_path_join(pr_id, "discussions", "1", "notes")
# response = await self._call_gitlab(git_url, method="POST", body=body)
git_url = url_path_join(pr_id, "discussions", "1", "notes")
response = await self._call_gitlab(git_url, method="POST", body=body)
return self.file_comment_response(response)
else:
body = {
"body": body.text,
"commit_id": body.commit_id,
"commitId": body.commit_id,
"path": body.filename,
"position": {"position_type": "text", "new_line": body.position},
}
Expand Down
12 changes: 12 additions & 0 deletions jupyterlab_pullrequests/managers/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import re

UPPER_CASE = re.compile(r"(?<!^)(?=[A-Z])")


def snake_to_camel_case(name: str) -> str:
first, *rest = name.split("_")
return "".join([first.lower(), *map(str.title, rest)])


def camel_to_snake_case(name: str) -> str:
return UPPER_CASE.sub("_", name).lower()
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def test_call(self, mock_call_github):
"https://api.github.com/repos/octocat/repo/pulls/1/files"
)
assert result == [
{"name": "README.md", "status": "added", "additions": 1, "deletions": 0}
{"name": "README.md", "status": "added"}
]


Expand Down
19 changes: 19 additions & 0 deletions jupyterlab_pullrequests/tests/test_manager_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from jupyterlab_pullrequests.managers.utils import snake_to_camel_case, camel_to_snake_case

TO_BE_TESTED = [
("banana", "banana"),
("banana_split", "bananaSplit"),
("the_famous_banana_split", "theFamousBananaSplit"),
]


@pytest.mark.parametrize("input_, expected", TO_BE_TESTED)
def test_snake_to_camel_case(input_, expected):
assert snake_to_camel_case(input_) == expected


@pytest.mark.parametrize("expected, input_", TO_BE_TESTED)
def test_camel_to_snake_case(expected, input_):
assert camel_to_snake_case(input_) == expected
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"scripts": {
"build": "jlpm run build:lib",
"build:labextension": "cd jupyterlab_pullrequests && rimraf labextension && mkdirp labextension && cd labextension && npm pack ../..",
"build:lib": "webpack && tsc",
"build:lib": "tsc",
"build:all": "jlpm run build:labextension",
"clean": "jlpm run clean:lib",
"clean:lib": "rimraf lib tsconfig.tsbuildinfo",
Expand All @@ -44,14 +44,20 @@
},
"dependencies": {
"@jupyterlab/application": "^2.0.0",
"@jupyterlab/apputils": "^2.0.0",
"@jupyterlab/codemirror": "^2.0.0",
"@jupyterlab/coreutils": "^4.0.0",
"@jupyterlab/docregistry": "^2.0.0",
"@jupyterlab/filebrowser": "^2.0.0",
"@jupyterlab/git": "0.21.0 - 0.30.0",
"@jupyterlab/mainmenu": "^2.0.0",
"@jupyterlab/nbformat": "^2.0.0",
"@jupyterlab/rendermime": "^2.0.0",
"@jupyterlab/services": "^5.0.0",
"@jupyterlab/settingregistry": "^2.0.0",
"@jupyterlab/ui-components": "^2.0.0",
"@lumino/commands": "^1.12.0",
"@lumino/widgets": "^1.16.0",
"base64-js": "^1.3.0",
"json-source-map": "^0.4.0",
"moment": "^2.24.0",
Expand All @@ -61,7 +67,8 @@
"react-dom": "~16.9.0",
"react-monaco-editor": "^0.26.2",
"react-resize-detector": "^4.2.0",
"react-spinners": "0.5.12"
"react-spinners": "0.5.12",
"react-window": "^1.8.5"
},
"devDependencies": {
"@babel/core": "^7.5.0",
Expand All @@ -78,6 +85,7 @@
"@types/react": "~16.8.19",
"@types/react-dom": "~16.0.5",
"@types/react-resize-detector": "^4.0.1",
"@types/react-window": "^1.8.0",
"@typescript-eslint/eslint-plugin": "^2.25.0",
"@typescript-eslint/parser": "^2.25.0",
"d3-color": "^1.2.8",
Expand Down
Loading

0 comments on commit 05e731a

Please sign in to comment.