Spaces:
Sleeping
Sleeping
Commit ·
7fe8a75
1
Parent(s): 3e83297
fix artifact position
Browse files- formatters.py +17 -19
- github_pr.py +1 -2
formatters.py
CHANGED
|
@@ -109,31 +109,29 @@ def render_entry(type_: str, fields: dict) -> str:
|
|
| 109 |
return "\n".join(lines)
|
| 110 |
|
| 111 |
|
| 112 |
-
def insert_entry(file_content: str, type_: str, entry_block: str
|
| 113 |
-
"""Insert entry_block
|
| 114 |
-
|
| 115 |
_, array_name, _ = TARGETS[type_]
|
| 116 |
lines = file_content.split("\n")
|
| 117 |
opening = f"export const {array_name} = ["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
insert_at = None
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
for j in range(i + 1, len(lines)):
|
| 125 |
-
if lines[j] == " },":
|
| 126 |
-
insert_at = j + 1
|
| 127 |
-
break
|
| 128 |
-
# keep scanning in case a later group is a better (last) match
|
| 129 |
-
if insert_at is None:
|
| 130 |
-
for i, line in enumerate(lines):
|
| 131 |
-
if line.strip() == opening:
|
| 132 |
-
insert_at = i + 1
|
| 133 |
-
break
|
| 134 |
-
|
| 135 |
if insert_at is None:
|
| 136 |
-
raise ValueError(f"Could not find `{opening}` in target file")
|
| 137 |
|
| 138 |
new_lines = lines[:insert_at] + entry_block.split("\n") + lines[insert_at:]
|
| 139 |
return "\n".join(new_lines)
|
|
|
|
| 109 |
return "\n".join(lines)
|
| 110 |
|
| 111 |
|
| 112 |
+
def insert_entry(file_content: str, type_: str, entry_block: str) -> str:
|
| 113 |
+
"""Insert entry_block at the end of the array, right before its closing
|
| 114 |
+
`];` line."""
|
| 115 |
_, array_name, _ = TARGETS[type_]
|
| 116 |
lines = file_content.split("\n")
|
| 117 |
opening = f"export const {array_name} = ["
|
| 118 |
+
closing = "];"
|
| 119 |
+
|
| 120 |
+
opening_at = None
|
| 121 |
+
for i, line in enumerate(lines):
|
| 122 |
+
if line.strip() == opening:
|
| 123 |
+
opening_at = i
|
| 124 |
+
break
|
| 125 |
+
if opening_at is None:
|
| 126 |
+
raise ValueError(f"Could not find `{opening}` in target file")
|
| 127 |
|
| 128 |
insert_at = None
|
| 129 |
+
for j in range(opening_at + 1, len(lines)):
|
| 130 |
+
if lines[j].strip() == closing:
|
| 131 |
+
insert_at = j
|
| 132 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
if insert_at is None:
|
| 134 |
+
raise ValueError(f"Could not find closing `{closing}` for `{opening}` in target file")
|
| 135 |
|
| 136 |
new_lines = lines[:insert_at] + entry_block.split("\n") + lines[insert_at:]
|
| 137 |
return "\n".join(new_lines)
|
github_pr.py
CHANGED
|
@@ -92,7 +92,6 @@ def open_pr_for_submission(submission: dict, fields: dict) -> str:
|
|
| 92 |
type_ = submission["type"]
|
| 93 |
file_path = formatters.target_file(type_)
|
| 94 |
entry_block = formatters.render_entry(type_, fields)
|
| 95 |
-
org_id = fields.get("orgId")
|
| 96 |
|
| 97 |
base = _default_branch()
|
| 98 |
base_sha = _branch_sha(base)
|
|
@@ -102,7 +101,7 @@ def open_pr_for_submission(submission: dict, fields: dict) -> str:
|
|
| 102 |
_create_branch(branch, base_sha)
|
| 103 |
|
| 104 |
content, sha = _get_file(file_path, branch)
|
| 105 |
-
new_content = formatters.insert_entry(content, type_, entry_block
|
| 106 |
_update_file(
|
| 107 |
file_path, branch, new_content, sha,
|
| 108 |
message=f"Add {type_}: {fields.get('name') or fields.get('title') or slug}",
|
|
|
|
| 92 |
type_ = submission["type"]
|
| 93 |
file_path = formatters.target_file(type_)
|
| 94 |
entry_block = formatters.render_entry(type_, fields)
|
|
|
|
| 95 |
|
| 96 |
base = _default_branch()
|
| 97 |
base_sha = _branch_sha(base)
|
|
|
|
| 101 |
_create_branch(branch, base_sha)
|
| 102 |
|
| 103 |
content, sha = _get_file(file_path, branch)
|
| 104 |
+
new_content = formatters.insert_entry(content, type_, entry_block)
|
| 105 |
_update_file(
|
| 106 |
file_path, branch, new_content, sha,
|
| 107 |
message=f"Add {type_}: {fields.get('name') or fields.get('title') or slug}",
|