[gitlab] Add GitLab connection and EventInfo classes#370
Conversation
Contains the BaseEventInfo class, the GitHubEventInfo and GitLabEventInfo classes implementing it, and the function create_event_info_instance.
|
Had to temporarily switch the base branch to get rid of some old commits 🤦 Ready for review now. |
trz42
left a comment
There was a problem hiding this comment.
This is a solid PR tackling the first step towards supporting repositories hosted on GitLab and GitHub.
What's a bit missing is explaining some kind of data model that is used to form the interface defined by BaseEventInfo. This doesn't need to cover all kinds of events and actions GitHub and GitLab support, but only the ones we currently use in the bot which only supports GitHub.
While the difference in how events are generated for labels might trigger a more substantial UI change (replacing setting the label bot:deploy with a new command bot:deploy [ARGS]), we might live with a workaround as the bot only supports one specific label (bot:deploy).
More comments (mostly suggesting changes ... not really requesting them) are provided with the code.
Python 3.9 needs its own pin as python-gitlab dropped support with version 7.0.0.
Co-authored-by: Thomas Röblitz <trz42@users.noreply.github.com>
Currently unused - may be reintroduced later if needed
pr_number and pr_url would previously also be used to get issue numbers/URLs
The new name more accurately describes the property
Co-authored-by: Thomas Röblitz <trz42@users.noreply.github.com>
I have these notes (which I've cleaned up a bit) from when I prepared to start working on GL support for the bot. Hopefully they show the basis for For the section documenting Where fields are used
|
First step towards implementing GitLab support for the bot. This PR adds:
python-gitlab,PyGHeewith GitLab support)gitandgitlabconnections/gitlab.pyfor connecting to GitLabtools/git.pywith functions:get_hosting_platform()gets the configured Git hosting platform (GitHub/GitLab)connect_to_host()performs initial connection to GitHub/GitLabtools/event_info.pywith:BaseEventInfoclass intended as an "interface" to implement for each Git hosting platformGitHubEventInfoclass implementingBaseEventInfofor GitHubGitLabEventInfoclass implementingBaseEventInfofor GitLabcreate_event_info_instance()creating an instance of the correct EventInfo classhandle_event()override in the event handler to use the new EventInfo classesThe PR does not any methods for handling GitLab events, but the events will be logged, e.g.:
get_hosting_platform()is intended to be used whenever platform-specific logic is required, to simplify adding support for other platforms in the future (e.g. Forgejo/Codeberg, Gitea, ...).The
*EventInfoclasses were implemented to handle the differences between the GitHub and GitLab webhooks, such that the necessary information can be retrieved through class properties. For example,event_info.actionreturns the event action. IfGitLabEventInfois used, theactionproperty converts the action to its GH equivalent before returning, e.g.updatebecomesedited.The classes currently have support for (as far as I can tell) all
event_infofields used by the bot.They also have a
__getitem__()override to support subscripting for compatibility with the existing uses ofevent_info- this version of the bot should therefore still have full GitHub support.Usage
Because webhooks signatures are not yet supported by GitLab (though they are in development), it is necessary to run a custom Smee server to sign the webhooks. I have a fork here which should sign the events in the same way as they will be signed on GitLab in the future. I have also added a "Quickstart" section on top of the README in the fork with instructions on how to run the server.
To "use" the bot in its current state on GitLab, you need to do the following:
pip install -r requirements.txt)Ensure the correct version of PyGHee (commit
c5e1063...) is installed.Settings->Webhooks->Add new)The webhook should have the
CommentsandMerge request eventstriggers, and must be configured with a secret token. The secret token can be generated e.g. by runningpython -c "import secrets; print(secrets.token_hex())", and must be stored as the environment variableGITLAB_WEBHOOK_SECRET_TOKEN. The webhooks must also be set up to send events to the custom Smee server.Settings->Access tokens->Add new token)In the future, the
apiscope andPlannerwill be required for the bot to be able to write comments. For this PR, however, it just needs to be able to connect to GitLab, so theread_apiscope andGuestrole should be sufficient. The access token must be stored as the environment variableGITLAB_PROJECT_ACCESS_TOKEN.gitandgitlabsections inapp.cfgIn the
gitsection,hosting_platform = gitlabmust be set (if it is set togithubinstead, the bot should function normally).In the
gitlabsection, theapi_timeout,bot_name, andinstance_urloptions must be set. For theinstance_urloption, you must enter the base URL of your GitLab instance, which is used along with the access token to connect to GitLab.