Go-github: Remove trailing slashes from file "paths" to build correct urls

Created on 10 Sep 2020  路  10Comments  路  Source: google/go-github

Hello,
I try to download a file from a different branch than master.

Request to:

https://api.github.com/repos/StarpTech/shikaka/contents/.github/workflows/?ref=feature%2Fghconfig_update_workflows

Response:

[
    {
        "name": "nodejs.yml",
        "path": ".github/workflows/nodejs.yml",
        "sha": "cb4ce2e0cee2cf5af199fc69610b7442e9118b2b",
        "size": 709,
        "url": "https://api.github.com/repos/StarpTech/shikaka/contents/.github/workflows/nodejs.yml?ref=master",
        "html_url": "https://github.com/StarpTech/shikaka/blob/master/.github/workflows/nodejs.yml",
        "git_url": "https://api.github.com/repos/StarpTech/shikaka/git/blobs/cb4ce2e0cee2cf5af199fc69610b7442e9118b2b",
        "download_url": "https://raw.githubusercontent.com/StarpTech/shikaka/master/.github/workflows/nodejs.yml",
        "type": "file",
        "_links": {
            "self": "https://api.github.com/repos/StarpTech/shikaka/contents/.github/workflows/nodejs.yml?ref=master",
            "git": "https://api.github.com/repos/StarpTech/shikaka/git/blobs/cb4ce2e0cee2cf5af199fc69610b7442e9118b2b",
            "html": "https://github.com/StarpTech/shikaka/blob/master/.github/workflows/nodejs.yml"
        }
    }
]

returns always file references to master and it doesn't matter what ref I pass.

This looks like a Github API issue. I added it here due to transparency reasons and to improve the integration test suite.

Most helpful comment

Yeah, I can provide a PR.

All 10 comments

I found it. If you pass a path like .github/workflows/ it will be concatenated to https://api.github.com/repos/StarpTech/shikaka/contents/.github/workflows/?ref=XXX which is fine from fileSystem perspective but wrong as URL. This module should remove trailing slashes because it's not obvious from the user perspective.

Can you please provide a minimal code sample to demonstrate the issue?

@gmlewis

    _, dirContent, _, err := opts.GithubClient.Repositories.GetContents(
        opts.Context,
        updateOptions.Owner,
        updateOptions.Repo,
        ".github/workflows/",
        &github.RepositoryContentGetOptions{
            Ref: updateOptions.BaseRef,
        },
    )

Produces the wrong URL with a trailing slash but works as a file URL.

So are you saying that this endpoint always needs to strip trailing slashes?

What does it mean to download the contents of a folder? Is that supported by the GitHub v3 API?
I thought it was for individual files (and am on a phone right now or I would check).

Are there any downsides to changing this endpoint to always strip trailing slashes?

You can download a folder. It will return all files in it. The issue is not the specific endpoint. If you have a trailing slash in the path all requests options are ignored because path/?ref= is invalid. path?ref is valid.

Oh, OK... So everywhere this repo creates a ref, the ref itself should not end in a slash, if I understand you correctly.

No, everywhere we pass a filePath to an endpoint the trailing slash must be removed. It's an issue how we build the URL. The trailing slash of all file paths must be removed before we build the URL.

Do you want to own this issue and create a PR? Or do you want to open this up to other volunteers to address?

Part of resolving this would be to determine the complete list of endpoints that this issue affects.

Yeah, I can provide a PR.

Thank you, @StarpTech !

Please see our CONTRIBUTING.md guide for helpful information.

Was this page helpful?
0 / 5 - 0 ratings