Skip to content

feat: Add support for Protected Environments #2084

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

Merged
merged 1 commit into from
Jun 22, 2022

Conversation

calve
Copy link
Contributor

@calve calve commented Jun 21, 2022

no write operation are implemented yet as I have no use case right now
and am not sure how it should be done

@calve
Copy link
Contributor Author

calve commented Jun 21, 2022

more specically, I guess all crud operation are inherited from the CRUD mixin but we should ideally implement stop(), protect() and unprotect() methods ?

@codecov-commenter
Copy link

codecov-commenter commented Jun 21, 2022

Codecov Report

Merging #2084 (169375f) into main (8342f53) will increase coverage by 9.02%.
The diff coverage is 92.85%.

@@            Coverage Diff             @@
##             main    #2084      +/-   ##
==========================================
+ Coverage   85.62%   94.64%   +9.02%     
==========================================
  Files          78       78              
  Lines        5007     5020      +13     
==========================================
+ Hits         4287     4751     +464     
+ Misses        720      269     -451     
Flag Coverage Δ
cli_func_v4 82.43% <92.85%> (?)
py_func_v4 81.01% <92.85%> (?)
unit 85.63% <92.85%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
gitlab/v4/objects/environments.py 97.14% <91.66%> (+5.83%) ⬆️
gitlab/v4/objects/projects.py 89.96% <100.00%> (+8.42%) ⬆️
gitlab/config.py 100.00% <0.00%> (+1.14%) ⬆️
gitlab/v4/objects/members.py 94.82% <0.00%> (+1.72%) ⬆️
gitlab/types.py 98.07% <0.00%> (+1.92%) ⬆️
gitlab/v4/objects/export_import.py 94.73% <0.00%> (+2.63%) ⬆️
gitlab/v4/objects/wikis.py 96.55% <0.00%> (+3.44%) ⬆️
gitlab/v4/objects/notes.py 94.28% <0.00%> (+3.80%) ⬆️
gitlab/v4/objects/groups.py 88.73% <0.00%> (+4.22%) ⬆️
gitlab/v4/objects/jobs.py 77.27% <0.00%> (+4.54%) ⬆️
... and 31 more

Copy link
Member

@JohnVillalovos JohnVillalovos left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Overall looks good. In addition to the one thing I mentioned, would also be good to create a unit test for this.

@calve calve force-pushed the protected-environments branch 2 times, most recently from 5ca5390 to 28a689c Compare June 22, 2022 09:29
@nejch
Copy link
Member

nejch commented Jun 22, 2022

more specically, I guess all crud operation are inherited from the CRUD mixin but we should ideally implement stop(), protect() and unprotect() methods ?

To answer your question: stop() is already implemented on the /environments endpoint, so we're ok - seems like /protected_environments does not have a separate stop endpoint for that.

We would need to add protect/unprotect, yes. If you'd like to add this, you can take a look at the (since deprecated) branch protect methods: 9656a16, although that's quite old, you'd probably have a simpler-looking method for that without the custom argument handling.

BTW: There is also a group-level protected environments endpoint: https://docs.gitlab.com/ee/api/group_protected_environments.html

Edit: ah, looking at your PR now, we're actually fine - create/delete from CRUDMixin already cover protecting and unprotecting the environment. But we should maybe document that create() protects and delete() unprotects the protected environment. This is actually consistent with our protected branches approach to avoid custom code: https://python-gitlab.readthedocs.io/en/stable/gl_objects/protected_branches.html.

I'll also add a note in the docs for our protected branches/tags that create/delete does protect/unprotect.

@nejch
Copy link
Member

nejch commented Jun 22, 2022

Thanks for the great PR and quick rework @calve. It'd be cool if you could add a tiny bit of docs for this (see protected branches) if you have the time & energy as well :)

@calve
Copy link
Contributor Author

calve commented Jun 22, 2022

Thanks both of you for the quick review.

BTW: There is also a group-level protected environments endpoint: https://docs.gitlab.com/ee/api/group_protected_environments.html

I'm inclined to let the implementation to a future reader ?

I'm not sure about UpdateMixin - does the API allow PUT on existing protected environments? Even protected branches don't support editing, so I'd think that no.

I'll also add a note in the docs for our protected branches/tags that create/delete does protect/unprotect.

And globally on the create/delete/protect/unprotect behavior, I have to say I did not have those usages right now and so haven't actually tested anything related to other operation than list/get.

I just realised the documentation isn't autogenerated as I initially thought, I'm going to amend the commit to add a specific pages.

@nejch
Copy link
Member

nejch commented Jun 22, 2022

And globally on the create/delete/protect/unprotect behavior, I have to say I did not have those usages right now and so haven't actually tested anything related to other operation than list/get.

I just realised the documentation isn't autogenerated as I initially thought, I'm going to amend the commit to add a specific pages.

Thanks for the quick reply @calve. We auto-generate the API reference but add some prose docs with examples usually, as some people find that easier to grok.

Sounds good, the rest can be a follow-up :)

@calve calve force-pushed the protected-environments branch from 28a689c to 8eb44ea Compare June 22, 2022 11:30
@calve
Copy link
Contributor Author

calve commented Jun 22, 2022

I amended the commit to

  • remove the UpdateMixin, and the associated .update() method
  • set _create_attrs and _list_filters to what seems to be the actual spec
  • document that create() and delete() actually protect/unprotected existing environments
  • manually (and successfully) test a full CRUD cycle on my gitlab-ee instance

please let me know for any other changes or information :)

@calve calve force-pushed the protected-environments branch 3 times, most recently from aa19c34 to 01abc64 Compare June 22, 2022 11:47
@calve calve changed the title feat: Add support to list Protected Environments feat: Add support for Protected Environments Jun 22, 2022
@nejch
Copy link
Member

nejch commented Jun 22, 2022

Thanks! Just a few more typos/nitpicks from me, then we should be good to go. Our handling of IDs is a bit different from how GitLab documents them, because we create objects to manipulate, so I've added some comments.

@calve calve force-pushed the protected-environments branch from 01abc64 to 9bb51cf Compare June 22, 2022 13:16
@calve
Copy link
Contributor Author

calve commented Jun 22, 2022

Updated with your suggestions, thanks for the review.

for reference here are my manual tests

In [12]: p.protected_environments.create({"name": "testenv", "deploy_access_levels": [{"access_level": 40}]})
Out[12]: <ProjectProtectedEnvironment name:testenv>

In [13]: print(p.protected_environments.get("testenv"))
<class 'gitlab.v4.objects.environments.ProjectProtectedEnvironment'> => {'name': 'testenv', 'deploy_access_levels': [{'access_level': 40, 'access_level_description': 'Maintainers', 'user_id': None, 'group_id': None, 'group_inheritance_type': 0}], 'required_approval_count': 0, 'approval_rules': []}

In [14]: print(p.protected_environments.list()[0])
<class 'gitlab.v4.objects.environments.ProjectProtectedEnvironment'> => {'name': 'testenv', 'deploy_access_levels': [{'access_level': 40, 'access_level_description': 'Maintainers', 'user_id': None, 'group_id': None, 'group_inheritance_type': 0}], 'required_approval_count': 0, 'approval_rules': []}

In [15]: print(p.protected_environments.list()[0].delete())
None

In [16]: print(p.protected_environments.get("testenv"))

- https://docs.gitlab.com/ee/api/protected_environments.html
- python-gitlab#1130

no write operation are implemented yet as I have no use case right now
and am not sure how it should be done
@calve calve force-pushed the protected-environments branch from 9bb51cf to 1dc9d0f Compare June 22, 2022 13:17
@nejch
Copy link
Member

nejch commented Jun 22, 2022

Thanks for the super quick reactions @calve. LGTM now, this should make it into the next scheduled release on the 28th.

@nejch nejch merged commit 1feabc0 into python-gitlab:main Jun 22, 2022
@calve
Copy link
Contributor Author

calve commented Jun 23, 2022

Thanks for your time, the super review and velocity 🤝

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.

4 participants