Skip to content

Use removesuffix/removeprefix instead of strip for URL and alias parsing#2394

Open
eeshsaxena wants to merge 1 commit into
aboutcode-org:mainfrom
eeshsaxena:fix/strip-suffix-parsing
Open

Use removesuffix/removeprefix instead of strip for URL and alias parsing#2394
eeshsaxena wants to merge 1 commit into
aboutcode-org:mainfrom
eeshsaxena:fix/strip-suffix-parsing

Conversation

@eeshsaxena

Copy link
Copy Markdown

Command.create_pull_request derives the owner and repo from the export repository URL like this:

path_parts = url_parts.strip("/").rstrip(".git").split("/")

rstrip takes a set of characters rather than a suffix, so after removing .git it keeps removing any trailing ., g, i or t. Repo names that end in one of those lose characters:

https://github.com/aboutcode-org/vulnerablecode-data.git    -> ['aboutcode-org', 'vulnerablecode-data']
https://github.com/aboutcode-org/vulnerablecode-import.git  -> ['aboutcode-org', 'vulnerablecode-impor']
https://github.com/nexB/scancode-toolkit.git                -> ['nexB', 'scancode-toolk']
https://github.com/aboutcode-org/purldb-git.git             -> ['aboutcode-org', 'purldb-']

The mangled name goes straight into https://api.github.com/repos/{repo_owner}/{repo_name}/pulls, so the export PR fails with a 404 that reads like a permissions problem. It works today only because the repo currently used ends in a.

AdvisoryAlias.url and AliasV2.url have the same idiom:

if alias.startswith("NPM-"):
    id = alias.lstrip("NPM-")

lstrip("NPM-") removes any leading N, P, M or -, so it happens to work for the numeric ids that nodejs/security-wg uses, but it would eat the front of anything else. Since the startswith check has already confirmed the prefix, removeprefix says what is meant and cannot over-strip.

Verification: this repo's suite needs Postgres, which I do not have set up here, so I ran the old and new expressions side by side over the URLs above rather than running pytest. The change is pure string handling on three lines and does not touch any query or model behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant