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

refactor: migrate hooks configuration feature to use python-gitlab library #635

Merged
merged 5 commits into from
Jan 4, 2024

Conversation

TigreModerata
Copy link
Contributor

@TigreModerata TigreModerata commented Nov 4, 2023

Removed ProjectHook methods from gitlab Projects;
Changed HooksProcessor to use python-gitlab methods;
Added a test_hooks_processor file to acceptance tests to check if new hooks processor can create, update, delete

closes #624

Changed hooks processor to use python-gitlab;
Removed hook methods from projects;
Added test file to check if new processor can delete, update, create
Copy link

codecov bot commented Nov 4, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d309d28) 83.27% compared to head (f3276a8) 84.21%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #635      +/-   ##
==========================================
+ Coverage   83.27%   84.21%   +0.94%     
==========================================
  Files          70       70              
  Lines        2744     2743       -1     
==========================================
+ Hits         2285     2310      +25     
+ Misses        459      433      -26     
Files Coverage Δ
gitlabform/gitlab/projects.py 57.14% <ø> (+5.52%) ⬆️
gitlabform/processors/project/hooks_processor.py 97.14% <100.00%> (+62.14%) ⬆️

... and 1 file with indirect coverage changes

Copy link
Collaborator

@amimas amimas left a comment

Choose a reason for hiding this comment

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

@TigreModerata - Thank you so much for this PR. It's appreciated and looks pretty good. A few feedbacks from me, but might be best to have @gdubicki to review it as well.

Let's apply types to variables (ie. myvar: str = "hello").

I had no idea the project didn't already have acceptance test for hooks. Thank you for adding one 😍 . Could you simplify the test file though? Take a look at the other test files to see how they are written. Here's what I can think about off the top of my head:

  • Create separate function/method for different test cases (i.e. create, delete, update, etc)
  • Use existing fixtures in your test functions/methods; this way you necessary group or project will be created for you already. Existing test files might be helpful as a reference.
  • Move the gitlabform config into relevant test methods

gitlabform/processors/project/hooks_processor.py Outdated Show resolved Hide resolved
gitlabform/processors/project/hooks_processor.py Outdated Show resolved Hide resolved
tests/acceptance/standard/test_hooks_processor.py Outdated Show resolved Hide resolved
@amimas amimas changed the title #624 refactor: migrate hooks configuration feature to use python-gitlab library Nov 4, 2023
@TigreModerata
Copy link
Contributor Author

Thank you for the feedback! Will get right on it.

@TigreModerata
Copy link
Contributor Author

Had some mishaps, but it should be better now.
Is there a better way to handle deletes, without having to time.sleep? I have a 6 second sleep because at times even 5 wasn't enough (running gitlab in docker)...

gitlabform/processors/project/hooks_processor.py Outdated Show resolved Hide resolved
tests/acceptance/standard/test_hooks_processor.py Outdated Show resolved Hide resolved
tests/acceptance/standard/test_hooks_processor.py Outdated Show resolved Hide resolved
tests/acceptance/standard/test_hooks_processor.py Outdated Show resolved Hide resolved
gitlabform/processors/project/hooks_processor.py Outdated Show resolved Hide resolved
@amimas
Copy link
Collaborator

amimas commented Nov 6, 2023

Is there a better way to handle deletes, without having to time.sleep? I have a 6 second sleep because at times even 5 wasn't enough (running gitlab in docker)...

Which delete are you referring to? Deleting hooks?

@TigreModerata
Copy link
Contributor Author

Yes, deleting hooks here - but that won't matter once I use the fixtures you mention above, rather than hardcode my configs.
I just wondered in general, about any of the objects, especially when you have to delete + create to update (say protectedbranches, since I was looking at that too)...

@TigreModerata
Copy link
Contributor Author

I can't get the failing test to pass, I don't know what's wrong.
Test file should be better now, using fixtures.

Copy link
Collaborator

@amimas amimas left a comment

Choose a reason for hiding this comment

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

Thanks again for all the updates and keep digging into this. Looking a lot cleaner now.

gitlabform/processors/project/hooks_processor.py Outdated Show resolved Hide resolved
tests/acceptance/standard/test_hooks_processor.py Outdated Show resolved Hide resolved
tests/acceptance/conftest.py Outdated Show resolved Hide resolved
tests/acceptance/standard/test_hooks_processor.py Outdated Show resolved Hide resolved
tests/acceptance/standard/test_hooks_processor.py Outdated Show resolved Hide resolved
tests/acceptance/conftest.py Outdated Show resolved Hide resolved
@TigreModerata
Copy link
Contributor Author

Type still not happy with ListRESTObjects for list of Project Hooks...
test_files sometimes fails on here, but seems fine "on my machine"

@TigreModerata
Copy link
Contributor Author

Made the changes and resubmitted.

Copy link
Collaborator

@amimas amimas left a comment

Choose a reason for hiding this comment

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

Latest change looks good to me. Would be great to get some feedback from @gdubicki when he has some time.

@amimas
Copy link
Collaborator

amimas commented Nov 16, 2023

Looks good 👍

@amimas
Copy link
Collaborator

amimas commented Nov 24, 2023

@gdubicki -- should we merge this? Will you have time for a sanity check?

project_and_group, hook, configuration["hooks"][hook]
created_hook: RESTObject = project.hooks.create(hook_config)
debug("Created hook '%s'", created_hook)
elif gitlab_hook and gitlab_hook.asdict() != hook_config:
Copy link
Member

Choose a reason for hiding this comment

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

Hi @TigreModerata!

Thank you for the contribution. :)

However I suspected that this condition will always be true because in hook_config we have only a few fields from a hook that we get from the API (gitlab_hook.asdict()), f.e. we don't have the field id.

And it turned out to be true - I modified a test test_hooks_update to do the update 2 times and in the second time the update is done again.

When I runned it with pytest tests/acceptance/standard/test_hooks.py -k 'test_hooks_create or test_hooks_update'.

First time logs excerpt:

Processing hooks...
http://localhost:80 "GET /api/v4/projects/gitlabform_group_papaya%2Fgitlabform_project_stony HTTP/1.1" 200 None
http://localhost:80 "GET /api/v4/projects/8/hooks HTTP/1.1" 200 None
Changing existing hook 'http://hooks/gitlabform_hook_festival.org'
http://localhost:80 "PUT /api/v4/projects/8/hooks/9 HTTP/1.1" 200 None
Changed hook to '{'id': 9, 'url': 'http://hooks/gitlabform_hook_festival.org', 'created_at': '2023-12-03T11:41:51.849Z', 'push_events': True, 'tag_push_events': False, 'merge_requests_events': False, 'repository_update_events': False, 'enable_ssl_verification': True, 'alert_status': 'executable', 'disabled_until': None, 'url_variables': [], 'project_id': 8, 'issues_events': False, 'confidential_issues_events': False, 'note_events': True, 'confidential_note_events': None, 'pipeline_events': False, 'wiki_page_events': False, 'deployment_events': False, 'job_events': True, 'releases_events': False, 'push_events_branch_filter': None, 'emoji_events': False}'
Changing existing hook 'http://hooks/gitlabform_hook_tiring.org'
http://localhost:80 "PUT /api/v4/projects/8/hooks/10 HTTP/1.1" 200 None
Changed hook to '{'id': 10, 'url': 'http://hooks/gitlabform_hook_tiring.org', 'created_at': '2023-12-03T11:41:51.907Z', 'push_events': False, 'tag_push_events': False, 'merge_requests_events': False, 'repository_update_events': False, 'enable_ssl_verification': True, 'alert_status': 'executable', 'disabled_until': None, 'url_variables': [], 'project_id': 8, 'issues_events': False, 'confidential_issues_events': False, 'note_events': True, 'confidential_note_events': None, 'pipeline_events': False, 'wiki_page_events': False, 'deployment_events': False, 'job_events': False, 'releases_events': False, 'push_events_branch_filter': None, 'emoji_events': False}'
* (1/1) FINISHED Processing project: gitlabform_group_papaya/gitlabform_project_stony

Second time logs excerpt:

Processing hooks...
http://localhost:80 "GET /api/v4/projects/gitlabform_group_papaya%2Fgitlabform_project_stony HTTP/1.1" 200 None
http://localhost:80 "GET /api/v4/projects/8/hooks HTTP/1.1" 200 None
Changing existing hook 'http://hooks/gitlabform_hook_festival.org'
http://localhost:80 "PUT /api/v4/projects/8/hooks/9 HTTP/1.1" 200 None
Changed hook to '{'id': 9, 'url': 'http://hooks/gitlabform_hook_festival.org', 'created_at': '2023-12-03T11:41:51.849Z', 'push_events': True, 'tag_push_events': False, 'merge_requests_events': False, 'repository_update_events': False, 'enable_ssl_verification': True, 'alert_status': 'executable', 'disabled_until': None, 'url_variables': [], 'project_id': 8, 'issues_events': False, 'confidential_issues_events': False, 'note_events': True, 'confidential_note_events': None, 'pipeline_events': False, 'wiki_page_events': False, 'deployment_events': False, 'job_events': True, 'releases_events': False, 'push_events_branch_filter': None, 'emoji_events': False}'
Changing existing hook 'http://hooks/gitlabform_hook_tiring.org'
http://localhost:80 "PUT /api/v4/projects/8/hooks/10 HTTP/1.1" 200 None
Changed hook to '{'id': 10, 'url': 'http://hooks/gitlabform_hook_tiring.org', 'created_at': '2023-12-03T11:41:51.907Z', 'push_events': False, 'tag_push_events': False, 'merge_requests_events': False, 'repository_update_events': False, 'enable_ssl_verification': True, 'alert_status': 'executable', 'disabled_until': None, 'url_variables': [], 'project_id': 8, 'issues_events': False, 'confidential_issues_events': False, 'note_events': True, 'confidential_note_events': None, 'pipeline_events': False, 'wiki_page_events': False, 'deployment_events': False, 'job_events': False, 'releases_events': False, 'push_events_branch_filter': None, 'emoji_events': False}'
* (1/1) FINISHED Processing project: gitlabform_group_papaya/gitlabform_project_stony

This is not great as it lowers the performance of a run and may cause unnecessary audit events in the projects on each run ("Hook ... has been updated").

I have pushed my commit to show the problem here d8b84e1.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@TigreModerata - no rush but just wondering if you will be able to look into this feedback? I feel this PR is so close to being merged🤞

It's a good callout from @gdubicki . I missed this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi both, sorry for the late reply, I was traveling. I will have a look today, thanks a lot for the feedback!

@TigreModerata
Copy link
Contributor Author

OK I think I fixed it. I included a test, reading the debug logs to check if indeed an identical config is not getting updated.

@amimas
Copy link
Collaborator

amimas commented Dec 10, 2023

OK I think I fixed it. I included a test, reading the debug logs to check if indeed an identical config is not getting updated.

Thank you @TigreModerata . appreciate it.

@gdubicki - could we get one more review please?

@amimas amimas temporarily deployed to Integrate Pull Request January 3, 2024 07:05 — with GitHub Actions Inactive
Copy link
Collaborator

@amimas amimas left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks again for this PR.

@amimas amimas merged commit b5513de into gitlabform:main Jan 4, 2024
23 checks passed
@TigreModerata
Copy link
Contributor Author

Cool, sorry again for all the missteps!

@TigreModerata TigreModerata deleted the #624 branch January 4, 2024 13:27
@jimisola
Copy link
Collaborator

jimisola commented Jan 4, 2024

Cool, sorry again for all the missteps!

Absolutely no reason to say sorry. Thank you for giving your time and contributing.

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.

Migrate hooks configuration feature to use python-gitlab library
4 participants