Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect URL in error messages #1899

Open
bradleyjames opened this issue Mar 18, 2024 · 0 comments
Open

Incorrect URL in error messages #1899

bradleyjames opened this issue Mar 18, 2024 · 0 comments

Comments

@bradleyjames
Copy link

I've spent a good amount of time recently trying to debug issues in goreleaser which uses go-gitlab. What I've found is that error messages go-gitlab returns include URLs which are not the URL that is being invoked. The URL is being unescaped when it shouldn't in go-gitlab's error handling code. This makes debugging difficult as it's sent me down the wrong path multiple times because the calls aren't valid.

Here's a test I'm using. The referenced repos are private and not available to anyone outside our organization. The fileName is an invalid path.

package main

import (
	"fmt"
	"os"

	"github.com/xanzy/go-gitlab"
)

func main() {
	accessToken := os.Getenv("GITLAB_TOKEN")
	glClient, _ := gitlab.NewClient(accessToken)

	projectID := "shipium-corp/engineering/homebrew-shipium"
	ref := "main"
	fileName := "Formula/invalid.r"
	opts := &gitlab.GetFileOptions{Ref: &ref}

	f, _, err := glClient.RepositoryFiles.GetFile(projectID, fileName, opts)
	if err != nil {
		fmt.Printf("%s\n", err)
		os.Exit(1)
	}
	fmt.Printf("File: %v\n", f)
}

The error that is printed is:

GET https://gitlab.com/api/v4/projects/shipium-corp/engineering/homebrew-shipium/repository/files/Formula/invalid.r: 404 {message: 404 File Not Found}

The actual URL invoked is https://gitlab.com/api/v4/projects/shipium-corp%2Fengineering%2Fhomebrew-shipium/repository/files/Formula%2Finvalid%2Er. The issue is the URL unescaping performed by the error handling code when building the error message. The repository name and the file name must be URL encoded to be valid when invoking gitlab's REST API.

The problematic line is the following at

path, _ := url.QueryUnescape(e.Response.Request.URL.Path)
.

I believe this should be the following instead:

	path := e.Response.Request.URL.RawPath

With this update, the error include the appropriate URL in the error message.

GET https://gitlab.com/api/v4/projects/shipium-corp%2Fengineering%2Fhomebrew-sh ipium/repository/files/Formula%2Finvalid%2Er: 404 {message: 404 File Not Found}

There is still a remaining issue in that the query params are not appended. I haven't tackled that yet but I assume the solution is on the same path as what I included above.

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

No branches or pull requests

2 participants