# Git Manager

### 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).

![Image 1. Basic OAuth process](https://3734728532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv4jB8NpBIvaKhVxmJR3c%2Fuploads%2FIavEdsfymO3HuyFvaaJe%2FOAuth.png?alt=media\&token=cbbaa038-ac0d-43c1-834c-f69480435864)

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:\ <mark style="color:yellow;">"@mobilize/bds-git-manager": "^{version}"</mark>

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.

![Image 2. Adapter-Adaptee pattern class diagram](https://3734728532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv4jB8NpBIvaKhVxmJR3c%2Fuploads%2FyDAKmgNJpR2SaATF061p%2Fadapter-adaptee.PNG?alt=media\&token=7c0339a7-c981-4b94-9180-b8024027d3be)

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:

![Image 3. Adapter-Adaptee implementation on Git Manager](https://3734728532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv4jB8NpBIvaKhVxmJR3c%2Fuploads%2FLb5Kc4ZYeTcnUVAnLPII%2FGitManager%20for%20BDS.drawio%20\(1\).png?alt=media\&token=bbfe9b2b-3e3f-4e3e-b0dc-d2eb20d9f42a)

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:

<mark style="color:yellow;">GitManager.GetOAuthAdapter().GetOAuthAdaptee();</mark>

**For example:**

<mark style="color:yellow;">GitManager.GetOAuthAdapter().GetOAuthAdaptee().GetProviderName();</mark> 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.&#x20;

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

<mark style="color:yellow;">GitManager.GetGitAdapter().GetGitAdaptee();</mark>

**For example:**

<mark style="color:yellow;">GitManager.GetGitAdapter().GetGitAdaptee().GetProviderName();</mark> 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.
