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

Added UploadAvatar #1318

Merged
merged 3 commits into from Jan 4, 2022
Merged

Added UploadAvatar #1318

merged 3 commits into from Jan 4, 2022

Conversation

mitar
Copy link
Contributor

@mitar mitar commented Dec 15, 2021

Fixes #229.

@mitar
Copy link
Contributor Author

mitar commented Dec 23, 2021

Any hope of having this merged in?

@svanharmelen
Copy link
Member

Yes, but I'm kind a mega busy with my day job lately... Will get back on this next week of so.

@svanharmelen
Copy link
Member

@mitar I thought it was maybe better to refactor it all in one go, so I took a stab at it and added a commit to your PR. I don't have good access to a GitLab instance, so it would be really cool if you could help me verify all 3 related methods (UploadFile, UploadAvatar and ImportFromFile).

If they all work as expected I'll merge this PR together with the other backwards incompatible PR. Thanks!

@svanharmelen
Copy link
Member

Hmm... I just pushed another commit with another solution for the same logic. This one used a request option function. The advantage could be that this option can also be used when creating or editing a project, more inline with what @Bouwdie was trying to work towards earlier.

I'm not sure which solution I like most, maybe the option function isn't needed and makes things more confusing for a user? What are your thoughts @mitar and @Bouwdie?

@mitar
Copy link
Contributor Author

mitar commented Dec 30, 2021

It looks good. I like the new approach.

@mitar
Copy link
Contributor Author

mitar commented Dec 30, 2021

I can confirm uploading avatar works.

@svanharmelen
Copy link
Member

Why are you pusing the old version again?

@svanharmelen
Copy link
Member

I still have the updated version locally, so I will push that and create a new PR from that one. Still not sure which approach the take. Using the WithFile option func or the internal UploadRequest method....

@svanharmelen
Copy link
Member

Here is the branch with my changes and the two different solutions (see the individual commits) https://github.com/xanzy/go-gitlab/tree/support-avatar

@svanharmelen
Copy link
Member

As for I think it would be more general if uploadType type would be just a string?, it's not a free format field IMO. We only want to support the types that the GitLab API expects/supports, so I prefer this over a string.

@mitar
Copy link
Contributor Author

mitar commented Dec 30, 2021

Oh, my mistake. I tried to rebase against the latest master. I think I made a mistake. Let me fix that.

@mitar
Copy link
Contributor Author

mitar commented Dec 30, 2021

As for I think it would be more general if uploadType type would be just a string?, it's not a free format field IMO. We only want to support the types that the GitLab API expects/supports, so I prefer this over a string.

Yes, I realized after that so I removed the comment.

@mitar
Copy link
Contributor Author

mitar commented Dec 30, 2021

I restored the brnach.

projects.go Outdated

// Set the buffer as the request body.
if err = req.SetBody(b); err != nil {
options = append(options, WithFile(avatar, "avatar.png", UploadAvatar))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this works. You assume avatars are just .png files here. You should allow filename argument.

@mitar
Copy link
Contributor Author

mitar commented Dec 30, 2021

I can confirm uploading avatar works.

Disregard this. I was testing against my original version. Lol.

@mitar
Copy link
Contributor Author

mitar commented Dec 30, 2021

Still not sure which approach the take.

Both look fine to me.

@Bouwdie
Copy link
Contributor

Bouwdie commented Dec 30, 2021

I like the approach with the option. For the three methods that are now implemented it makes sense, but for the other functions that include a file as input it makes less sense. I do think that ideally we would like the *io.Reader to be part of the create or edit project options instead of a nullable parameter to the signature. If we can somehow provide a hint in the opt that it might have to deal with files and if it does switch over to the multipart body rendering. Because in the end that is what we are doing right, body in json we prefer and if there is a file upload involved switch over to the multipart. If we are ok with a little reflection, that checks for available reader instances in the opt struct and if they are not nil, we can which the body render impl. In the end a user checks out the api docs from gitlab and expects a property to be available in the opt stuct I think. Does this argumentation make sense to you to? 😃

@Bouwdie
Copy link
Contributor

Bouwdie commented Dec 30, 2021

Oh btw small note, we also need to update the go-querystring dependency to v1.1.0, pointers to slices were not rendered correctly, for example tags in the project options would not be rendered properly. 💯

@mitar
Copy link
Contributor Author

mitar commented Dec 31, 2021

Because in the end that is what we are doing right, body in json we prefer and if there is a file upload involved switch over to the multipart.

Which endpoint requires both file upload and additional non-file parameters?

@Bouwdie
Copy link
Contributor

Bouwdie commented Dec 31, 2021

Because in the end that is what we are doing right, body in json we prefer and if there is a file upload involved switch over to the multipart.

Which endpoint requires both file upload and additional non-file parameters?

For example create project https://docs.gitlab.com/ee/api/projects.html#create-project - where the avatar is just one of the request options.

@mitar
Copy link
Contributor Author

mitar commented Dec 31, 2021

I do think that ideally we would like the *io.Reader to be part of the create or edit project options

I think the issue with that is that Reader is not enough, you need also a filename, so that GitLab can figure out the MIME type (they seem to do that based on the file extension in the filename).

@svanharmelen
Copy link
Member

Listening to both your feedback, I actually don't think the option func is very helpful as it probably makes it less clear how and when it can be used and it doesn't mimic a pattern that the GitLab API currently supports. So I think I prefer to use the "internal" UploadRequest method instead.

Let me refactor and update the solution just a bit to see if we can also support the idea of switching based on a value in the opt struct.

@Bouwdie
Copy link
Contributor

Bouwdie commented Jan 1, 2022

@mitar agreed, so probably there will be a new struct type like uploadfile which encapsulates both the reader and the filename

@Bouwdie
Copy link
Contributor

Bouwdie commented Jan 1, 2022

@svanharmelen I think if we have a tule for fileupload with a reference in the opt structs we can have with minimal reflection and a null check an indication whether we should switch to form based body rendering. Thx for both your time guys 😊

@svanharmelen
Copy link
Member

OK, updated the PR again and think it should be good like this.

@Bouwdie I don't like to use reflection based on a specific type to detect if/when to switch the request format. Instead I prefer to check for that explicitly in the methods where it makes sense.

Please let me know any final thoughts or concerns with this approach as I'm ready to merge this one.

@Bouwdie
Copy link
Contributor

Bouwdie commented Jan 4, 2022

@svanharmelen perfectly fine not to use refection, it would only result in different behavior for a couple of methods anyway, so most of it would be useless overhead.

projects.go Outdated Show resolved Hide resolved
@mitar
Copy link
Contributor Author

mitar commented Jan 4, 2022

Just a minor comment, otherwise it looks great!

@svanharmelen
Copy link
Member

Thanks @mitar and @Bouwdie for all the help input and feedback. I'm going to merge this right after I add one last tag before merging both backwards incompatible PR's.

@Bouwdie
Copy link
Contributor

Bouwdie commented Jan 4, 2022

@svanharmelen Thanks for the commits and the fix. I unfortunately am not used to the new review interface of github and didn't push my remarks. One this I would like to stress is to update the go-querystring dependency, refences to slices will otherwise not be encoded properly aka project.Tags[] for example. Thanks again 💯

@mitar
Copy link
Contributor Author

mitar commented Jan 4, 2022

I updated the dependency in my gitlab-config project and I can confirm that all recent changes work.

@svanharmelen
Copy link
Member

Nice 👍🏻

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.

Support for avatar option in create/edit project
3 participants