Git Manager

The Git Manager is a node module created to manage the common git operations for different git providers, including the OAuth process.

OAuth process

The OAuth is defined for Authorization instead of Authentication, this process allows an application (A) to request permissions to access to a predifined ammount of information on the user account into another application (B) without give the authentication data (user and password).

The final result of the OAuth process is the reception of an access token, that must be stored and associated to the user in order to used it on the nexts requests.

Note: This process is managed internally by the Git Manager according to the defined Git provider.

Git Manager implementation

The Git Manager is a node module that can be installed through the dependencies on the Package.json file as follows: "@mobilize/bds-git-manager": "^{version}"

The Git Manager has been implemented using the Adapter-Adaptee pattern in order to allow us to implement different git and oauth providers using a base implementation model.

Adapter-Adaptee pattern

This pattern allows to implement different behaviors for the same approachs, converting the interface of a class into another that the client application expects. This lets classes to work together that couldn't otherwise because incompatible interface.

The client sees only the target interface and not the adapter. The adapter implements the target interface. Adapter delegates all the requests to adaptee.

Adventages of Adapter-Adaptee pattern

  • Helps to achieve the reusability and flexibility.

  • Client class is not complicated by having to use a different interface and can use polymorphism to swap between different implementations of adapters.

Disadventages of Adapter-Adaptee pattern

  • All requests are forewarded, so there is a slight increase in the overhead.

  • Sometimes many adaptations are required along an adapter chain to reach the type which is required.

Adapter-Adaptee Pattern applied on Git Manager

The following diagram represents the implementation of the Adapter-Adaptee pattern on the Git Manager module:

The entry point for every application is the GitManager class, that uses two (2) adapters, one for the OAuth process (OAuthAdapter) and the another one is for the git operations (GitAdapter). These adapters only contains the initialization method that is invoked automatically on the GitManager constructor (according to the given paramters values) and the method to obtains the corresponding Adaptee that is represented by an interface with his corresponding methods.

OAuthAdaptee

The OAuth adaptee is in charge of the OAuth process, that is different for every OAuth provider, but exposes the common operations.

  • RequestAuthorization: Is the method that must be used to initialize the authorization process.

  • RequestAccessToken: Is the method to request the access token to the corresponding OAuth provider.

  • AccessTokenIsValid: Is the method to validates the availability of the access token.

  • GetAuthorizationEndpoint: Allows us to obtains the authorization endpoint for the OAuth provider.

  • GetProviderName: Allows to obtains the current used OAuth provider.

How to add a new OAuthAdaptee

In order to generate a new implementation for OAuthAdaptee for a new OAuthProvider, we must create a class that implements the IOAuthAdaptee interface and implement the corresponding processes. This will allows us to generate different implementations for every OAuthProviders, that wouldn't known by the application.

To invoke the methods related to the OAuthProvider we need to follow the next path:

GitManager.GetOAuthAdapter().GetOAuthAdaptee();

For example:

GitManager.GetOAuthAdapter().GetOAuthAdaptee().GetProviderName(); The result will be acccording to the implementation for the defined Adaptee.

The new OAuthAdapter must be added to the enum called OAuthProvider in order to be recognized by the GitManager constructor. Also, we'll need to add the creation of the OAuthAdaptee into the ConfigureOAuthAdaptee method on OAuthAdapter.

GitAdaptee

The Git adaptee has the methods that will allow us to obtains the user information on the Git provider and execute the basic Git operations (create repository, initialize repository, clone, add files to stage, commit, push).

The following are the available methods:

  • GetRepositoryInformation: Allows to obtains the information of a single repository.

  • GetUserRepositories: Allows us to Obtains the user repositories (need access token).

  • GetUserInformation: Allows us to obtains the user account data (need access token).

  • CreateRepository: Allows us to implement the repository creation.

  • InviteToRepository: Allows us to implement the invitation process.

  • GetProviderName: Obtains the defined provider name.

  • GetUrlWithAccessToken: Obtains the URL for the repository including the access token validation, this will be need to execute git operations.

  • InitRepository: Initialize the repository.

  • CloneRepository: Clone a repository into a particular folder.

  • AddFilesToStage: Add the defined files to the stage (equals to simple git).

  • CommitToRepository: Execute a commit on the repository.

  • PushToRepository: Push the commits to the repository.

How to add a new GitAdaptee

In order to generate a new implementation for GitAdaptee for a new GitProvider, we must create a new class that implements the interface IGitAdaptee and define the corresponding processes for each method.

To invoke the methods related to the GitProvider we need to follow the next path:

GitManager.GetGitAdapter().GetGitAdaptee();

For example:

GitManager.GetGitAdapter().GetGitAdaptee().GetProviderName(); The result will be acccording to the implementation for the defined Adaptee.

The new GitAdapter must be added to the enum called GitProvider in order to be recognized by the GitManager constructor. Also, we'll need to add the creation of the GitAdaptee into the ConfigureGitAdaptee method on GitAdapter.

Last updated