xanzy / go-gitlab

GitLab Go SDK
Apache License 2.0
2.43k stars 960 forks source link

ProtectedBranches.UpdateProtectedBranch doesn't seem to work #1657

Closed MUlt1mate closed 1 year ago

MUlt1mate commented 1 year ago

Hey there. I'm using v0.80.1 I think UpdateProtectedBranch method is broken.

I call it and I get an error

devPermission := gitlab.DeveloperPermissions
maintainerPermission := gitlab.MaintainerPermissions
_, _, err = c.gl.ProtectedBranches.UpdateProtectedBranch(project.ID, project.DefaultBranch, &gitlab.UpdateProtectedBranchOptions{
    AllowedToPush:  &[]*gitlab.BranchPermissionOptions{{AccessLevel: &maintainerPermission}},
    AllowedToMerge: &[]*gitlab.BranchPermissionOptions{{AccessLevel: &devPermission}},
})

SetMergePermissions error PATCH: 400 {error: allowed_to_push is invalid, allowed_to_merge is invalid}

RawQuery for the request is allowed_to_merge={<nil> <nil> <nil> 0xc0003f24d0}&allowed_to_push={<nil> <nil> <nil> 0xc0003f24d8}

I've tried to override NewRequest and send the payload in the body, but marshaling result was <unsupported type: []uint8>

Also, as far as I understood from docs, you have to send permission ID in the request. But BranchPermissionOptions struct lacks the field.

svanharmelen commented 1 year ago

Looking at the examples this should indeed be a call the uses the request body to send the data instead of query params. Wonder if we need to add PATH to this line https://github.com/xanzy/go-gitlab/blob/master/gitlab.go#L579

I don't see anything re a permission ID in the examples... Care to share what you mean?

MUlt1mate commented 1 year ago

I mean this: https://docs.gitlab.com/ee/api/protected_branches.html#example-update-a-push_access_level-record
'{"allowed_to_push": [{"id": 12, "access_level": 0}]'

MUlt1mate commented 1 year ago

What's our next steps?

bmsareias commented 1 year ago

Is there any news on this issue, I can confirm that even on v0.81.0 this issue occurs, the only viable bypass would be to recreate the protected branch object, which is very inconvenient as requires an update to the approval rules (if attached directly to such branch) every time.

svanharmelen commented 1 year ago

Work is being done in PR #1680 to fix this one...

D-3lf commented 1 year ago

For folks that don't want to modify the library there is a workaround that I've tested for managing push and merge rules on protected branches. Code to destroy an access level:

var aas = []accessstub{
      {
        Destroy: true,
        ID:      pal.ID,
    },
}
var pupp = &updatePushProtection{
      AllowedToPush: aas,
}
ret_req, err := git.NewRequest("PATCH", path, pupp, nil)
mpupp, _ := json.Marshal(pupp)
ret_req.Header.Set("Content-Type", "application/json")
ret_req.SetBody(mpupp)
_, err = git.Do(ret_req, nil)