RozeFound / mmc-export

Export MMC modpack to other modpack formats
MIT License
16 stars 3 forks source link

Bug in _get_github function #70

Closed Brancieq-Paul closed 1 year ago

Brancieq-Paul commented 1 year ago

Bug in _get_github function

Description

When the "contact" field in the metadata dictionary contains a link to a GitHub profile instead of a repository, the following error is raised:

Traceback (most recent call last):
  File "/home/paulb/.pyenv/versions/modpack-creator/lib/python3.10/site-packages/tenacity/_asyncio.py", line 51, in __call__
    result = await fn(*args, **kwargs)
  File "/home/paulb/.pyenv/versions/modpack-creator/lib/python3.10/site-packages/mmc_export/Helpers/resourceAPI.py", line 99, in _get_github
    owner, repo = parsed_link.path[1:].split('/')[:2]
ValueError: not enough values to unpack (expected 2, got 1)

This is caused by the _get_github function trying to unpack the link into two parts (owner and repository), but only receiving one when the link is not to a repository.

Reproduce

Run the function with the following meta, or another meta containing a link to a github profile page in the contact dictionary. This one is the meta from the Not Enough Animations mod:

{
    "schemaVersion": 1,
    "id": "notenoughanimations",
    "version": "1.6.2",
    "name": "NotEnoughAnimations",
    "description": "Adding and improving animations in Third-Person.",
    "authors": [
        "tr7zw"
    ],
    "contact": {
        "homepage": "https: //www.curseforge.com/minecraft/mc-mods/not-enough-animations",
        "sources": "https://github.com/tr7zw"
    },
    "license": "tr7zw Protective License",
    "icon": "assets/notenoughanimations/icon.png",
    "environment": "client",
    "entrypoints": {
        "main": [
            "dev.tr7zw.notenoughanimations.NEAnimationsMod"
        ],
        "modmenu": [
            "dev.tr7zw.notenoughanimations.NEAModMenu"
        ]
    },
    "mixins": [
        "notenoughanimations.mixins.json"
    ],
    "depends": {
        "minecraft": "1.19.x"
    },
    "suggests": {
        "firstperson": "*"
    }
}

Expected Results

The program should not raise an error and should handle the link to the GitHub profile appropriately.

Actual Results

The program raises a ValueError and fails to handle the link to the GitHub profile.

Reproducibility

This bug can be reproduced consistently by adding a link to a GitHub profile in the "contact" field of the metadata dictionary.

Fix

To fix this issue, the _get_github function should check if the link points to a repository or a profile before attempting to unpack it. If it is a profile, the function should handle it appropriately without trying to unpack it into two parts. Below is a modified version of the function with this fix implemented:

async def _get_github(self, meta: dict, resource: Resource) -> None:
    if "contact" not in meta or "GitHub" in self.excluded_providers:
        return

    for link in meta['contact'].values():
        parsed_link = urlparse(link)

        if parsed_link.netloc == "github.com":
            path_parts = parsed_link.path[1:].split('/')
            if len(path_parts) < 2:
                resource.links.append(f"https://github.com/{path_parts[0]}")
            else:
                owner, repo = path_parts[:2]
                repo = repo.removesuffix(".git")
                resource.links.append(f"https://github.com/{owner}/{repo}")
            break
    else:
        return

!!!The fix was generated by chatGPT. It's just an idea of how to fix it and I didn't test it myself.!!! (I think you use owner variable lower in the same function for example)

RozeFound commented 1 year ago

Link to GitHub profile is basically useless, so we just throw it out if repo is not present. Btw fix in 2.8.2, please report if the problem is gone.