gitlabr icon indicating copy to clipboard operation
gitlabr copied to clipboard

I have found and fixed an error in function:gl_push_file(), check it out!!

Open littlefish0331 opened this issue 4 years ago • 1 comments

hi, this is my first time submit an issue.
If I do anything wrong, please just let me know.

Error in gl_push_file()

When I push a file (e.g. csv) to parent directory in project, it works fine!!
But it will get some error when I push a file in sub-directory. following is a simple example code

# first connect to ur project
my_project <- gl_project_connection(gitlab_url, project, private_token)

# push a file to parent directory, look careful to parameter file_path
my_project(gl_push_file, file_path = "upload_to_parent_dir.csv", content, commit_message)
> this can work

# push a file to sub-directory
my_project(gl_push_file, file_path = "folder/upload_to_sub_dir.csv", content, commit_message)
> this will fail

Reason and How to fix

The reason that cause this error is "reserved character" (e.g. "/").
One way to fix this problem is to transfer your file_path by URLencoding().

new_file_path  <- URLencode(URL = "folder/upload_to_sub_dir.csv", reserved = T)

# push a file to sub-directory
my_project(gl_push_file, file_path = new_file_path  , content, commit_message)
> success!!

p.s. "/"p.s. "/" will become "%2F" after URLencoding.


However, other functions in gitlabr can work fine, even not transfer though URLencoding.

my_project(gl_file_exists, file_path = "folder/upload_to_sub_dir.csv", ref = "master")
> this can work

my_project(gl_file_exists, file_path = "folder%2Fupload_to_sub_dir.csv", ref = "master")
> this fail

Therefore, I suggest that gitlabr should unify the usage of "file_path" parameter !!


Moreover, if you try to push the same file multiple times by using "overwrite = T" in gl_push_file().
Here comes up another problem.

# first push, success
new_file_path  <- URLencode(URL = "folder/upload_to_sub_dir.csv", reserved = T)
my_project(gl_push_file, file_path = new_file_path  , content, commit_message)

# push again to overwrite, fail!!!!
new_file_path  <- URLencode(URL = "folder/upload_to_sub_dir.csv", reserved = T)
my_project(gl_push_file, file_path = new_file_path  , content, commit_message)

# change preserved character back, success
my_project(gl_push_file, file_path = "folder/upload_to_sub_dir.csv," content, commit_message)

so, I look the code in gl_push_file(), and I think the problem is in httr::PUT and httr::POST.

for now, if you want to push the same file multiple times, I have a temporarily solution.

file_exist <- my_project(gl_file_exists, file_path, ref)
if (file_exist) new_file_path <- URLdecode(URL = file_path)
else new_file_path <- file_path

my_project(gl_push_file, file_path = new_file_path , content, commit_message)

That's all. Hope my suggestion can give some help~

sincerely, Steve

littlefish0331 avatar Mar 11 '20 15:03 littlefish0331

Thanks @littlefish0331! Spent an hour tying to figure out why gl_get_file worked with a file in main directory and failed when using something deeper - your solution with URLencode cracked it.

RPanczak avatar Jan 16 '23 14:01 RPanczak