Skip to content

Commit ba34b67

Browse files
authored
Merge pull request #91 from legendu-net/push-uvsomptkxqvs
Merge push-uvsomptkxqvs Into main
2 parents fa289a0 + e04e3c8 commit ba34b67

18 files changed

Lines changed: 123 additions & 35 deletions

github_rest_api/github.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
"""Simple wrapper of GitHub REST APIs."""
22

3+
import re
34
from abc import ABCMeta, abstractmethod
45
from base64 import b64encode
56
from collections.abc import Sequence
67
from enum import StrEnum
7-
from typing import Any, Callable
88
from pathlib import Path
9+
from typing import Any, Callable
10+
911
import requests
1012
from nacl import encoding, public
1113

1214
URL_API = "https://api.github.com"
1315

16+
_SECRET_NAME_PATTERN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
17+
18+
19+
def _validate_secret_name(name: str) -> None:
20+
"""Validate a secret name against GitHub's naming rules.
21+
22+
GitHub rejects invalid secret names with a 422 response. Validating the
23+
name client-side surfaces a clear error before the request is sent.
24+
25+
:param name: The name of the secret.
26+
:raises ValueError: If the name is empty, starts with the reserved
27+
``GITHUB_`` prefix, starts with a digit, or contains characters other
28+
than alphanumerics and underscores.
29+
"""
30+
if not name:
31+
raise ValueError("A secret name must not be empty.")
32+
if name.upper().startswith("GITHUB_"):
33+
raise ValueError(
34+
f"Invalid secret name {name!r}: names must not start with the "
35+
"reserved 'GITHUB_' prefix."
36+
)
37+
if not _SECRET_NAME_PATTERN.fullmatch(name):
38+
raise ValueError(
39+
f"Invalid secret name {name!r}: names may only contain alphanumeric "
40+
"characters and underscores, and must not start with a digit."
41+
)
42+
1443

1544
def _encrypt_secret(public_key: str, value: str) -> str:
1645
"""Encrypt a secret value using a LibSodium sealed box.
@@ -354,6 +383,7 @@ def create_or_update_secret(
354383
automatically. Fetch it once and reuse it to avoid a redundant
355384
request when creating or updating multiple secrets.
356385
"""
386+
_validate_secret_name(name)
357387
if public_key is None:
358388
public_key = self.get_secret_public_key()
359389
return self._put(
@@ -549,6 +579,7 @@ def create_or_update_secret(
549579
:param selected_repository_ids: Repository IDs that can access the secret
550580
when visibility is `selected`.
551581
"""
582+
_validate_secret_name(name)
552583
if selected_repository_ids and visibility != SecretVisibility.SELECTED:
553584
raise ValueError(
554585
"`selected_repository_ids` can only be provided when `visibility` is 'selected'."

github_rest_api/scripts/cargo/benchmark.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
"""Benchmark action using cargo criterion."""
22

3-
from typing import Callable
4-
import tempfile
5-
from pathlib import Path
63
import datetime
74
import shutil
85
import subprocess as sp
6+
import tempfile
7+
from pathlib import Path
8+
from typing import Callable
9+
910
from dulwich import porcelain
11+
1012
from ..utils import (
13+
commit_benchmarks,
1114
config_git,
12-
switch_branch,
13-
push_branch,
1415
gen_temp_branch,
15-
commit_benchmarks,
16+
push_branch,
17+
switch_branch,
1618
)
1719

1820

github_rest_api/scripts/cargo/profiling.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""Utils for profiling Rust applications."""
22

3-
from typing import Iterable
4-
from pathlib import Path
5-
import time
63
import datetime
74
import subprocess as sp
5+
import time
6+
from pathlib import Path
7+
from typing import Iterable
8+
89
import psutil
9-
from .utils import build_project
10-
from ..utils import config_git, switch_branch, push_branch, commit_profiling
10+
1111
from ...utils import partition
12+
from ..utils import commit_profiling, config_git, push_branch, switch_branch
13+
from .utils import build_project
1214

1315

1416
def launch_application(cmd: list[str]) -> int:

github_rest_api/scripts/container/build_container_images.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import argparse
2-
from collections.abc import Sequence
32
import datetime
4-
from pathlib import Path
53
import subprocess as sp
64
import sys
5+
from collections.abc import Sequence
6+
from pathlib import Path
77
from typing import cast
8+
89
import yaml
9-
from dulwich.repo import Repo
10-
from dulwich.refs import Ref
11-
from dulwich.objects import Commit
12-
from dulwich.errors import NotGitRepository
1310
from dulwich.diff_tree import tree_changes
11+
from dulwich.errors import NotGitRepository
12+
from dulwich.objects import Commit
13+
from dulwich.refs import Ref
14+
from dulwich.repo import Repo
1415
from tenacity import retry, stop_after_attempt, wait_exponential
1516

1617

github_rest_api/scripts/container/config_container.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import argparse
22
import json
33
import shutil
4+
import subprocess as sp
45
import sys
56
import tomllib
6-
import tomli_w
77
from pathlib import Path
8-
import subprocess as sp
8+
9+
import tomli_w
910

1011

1112
def config_docker(data_root: str = "/mnt/docker"):

github_rest_api/scripts/container/update_version_containerfile.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import argparse
22
import datetime
33
import os
4+
import re
45
import sys
56
from pathlib import Path
6-
import re
7+
78
from dulwich import porcelain
9+
from requests.exceptions import HTTPError
10+
811
from github_rest_api import Repository
912
from github_rest_api.utils import next_minor_or_strip_patch
10-
from requests.exceptions import HTTPError
1113

1214

1315
def parse_latest_version(repo: str) -> str:

github_rest_api/scripts/github/add_github_repo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import sys
88
from collections.abc import Sequence
99
from pathlib import Path
10+
1011
from dulwich import porcelain
1112

12-
from github_rest_api import User, Organization
13+
from github_rest_api import Organization, User
1314

1415

1516
def _validate_repo(repo: str) -> None:

github_rest_api/scripts/github/create_pull_request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
The branch is updated (using dev) before creating the PR.
33
"""
44

5-
from argparse import ArgumentParser, Namespace
65
import os
76
import sys
7+
from argparse import ArgumentParser, Namespace
8+
89
from github_rest_api import Repository
910
from github_rest_api.utils import compile_patterns
1011

github_rest_api/scripts/github/release_on_github.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import argparse
2+
import getpass
13
import os
24
import re
35
import sys
4-
import argparse
5-
import getpass
66
from pathlib import Path
7+
78
from github_rest_api import Repository
89
from github_rest_api.scripts.utils import (
910
find_project_root,

github_rest_api/scripts/github/remove_branch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import argparse
2+
import datetime
23
import re
34
import sys
4-
import datetime
5+
56
from github_rest_api import Repository
67

78

0 commit comments

Comments
 (0)