# GitHub

Integrate with Github APIs using Tray.io Github connector

## Overview

> **Info:** **API INFO**: The Base URL used for the GitHub connector is **<https://api.github.com>**. More information can be found on their main [API documentation](https://docs.github.com/en/rest) site.Here is their [rate limits](https://docs.github.com/en/developers/apps/building-github-apps/rate-limits-for-github-apps) page.

### List repositories and branches

Below is an example of a way in which you could potentially use the GitHub connector to list all the branches from each repository
The following workflow gives an example of how you can achieve this:
![github-list-repo-complete-wf](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-a1f8a204_github-list-repo-complete-wf.png)

1. Set up using a trigger (be it [Manual](https://tray.ai/documentation/connectors/trigger/manual-trigger/), [Scheduled](https://tray.ai/documentation/connectors/trigger/scheduled-trigger/), [Callable](https://tray.ai/documentation/connectors/trigger/callable-trigger/) etc).
2. The **List your repositories** is a GitHub connector's operation that lists all your GitHub repositories.
   ![github-list-repo-list-repo-step](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-9ecead3c_github-list-repo-list-repo-step.png)
3. **Loop** is a Loop connector step to 'Loop List' all the GitHub repositories received from the previous step.
   A jsonpath for the 'List' field should appear similar to this: `$.steps.github-1.results`. You can always use a connector-snake to find the jsonpath.
   ![github-list-repo-loop](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-6b2a7690_github-list-repo-loop.png)

> **Info:** **JSONPATHS:** For more information on what jsonpaths are and how to use jsonpaths with Tray, please see our pages on [Basic data concepts](https://tray.ai/documentation/platform/automation-integration/building-workflows/mapping-data/basic-data-concepts) and [Mapping data between steps](https://tray.ai/documentation/platform/automation-integration/building-workflows/mapping-data/mapping-data-between-steps)

1. **List Branches** is a GithHub connector's operation that lists branches of GitHub repository based on the provided 'User/Organisation' (`$.steps.loop-1.value.owner.login`) and 'Repository' (`$.steps.loop-1.value.name`) names.
   It takes jsonpaths for the 'User/Organisation' and 'Repository' field from the **Loop** step.
   ![github-list-repo-list-branches](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-bbf8a166_github-list-repo-list-branches.png)
   The fetched list of branches should look similar to this:
   ![github-list-repo-final-op](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-45c667de_github-list-repo-final-op.png)
   Click on the [Debug](https://tray.ai/documentation/platform/troubleshooting/logs-and-debugging/) tab to view Input and Output for individual steps.

### Consolidate security information of all

> **Info:** **PLEASE NOTE**: The GitHub account that you authenticate with in the Tray app must have the **Owner** permission level to view security information. Please visit the [GitHub Docs](https://help.github.com/en/github/setting-up-and-managing-organizations-and-teams/permission-levels-for-an-organization#permission-levels-for-an-organization) for more information on permission levels.

Below is an advanced workflow that will get the security information of all repositories within your account and store them in a data storage.
In this case, we are using Google sheets as a generic placeholder for any service or database you may wish to use.
The resulting document will look similar to this:
![Google Sheets output](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-7afc19c0_sec-info-6.png)
This is what the finished workflow will look like:
![github-security-info-complete-wf](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-487e696e_github-security-info-complete-wf.png)

1. Set up using a trigger (be it [Manual](https://tray.ai/documentation/connectors/trigger/manual-trigger/), [Scheduled](https://tray.ai/documentation/connectors/trigger/scheduled-trigger/), [Callable](https://tray.ai/documentation/connectors/trigger/callable-trigger/) etc).
2. The **List your repositories** is a GitHub connector's operation that lists all your GitHub repositories.

> **Info:** **PLEASE NOTE**: The 'List your repositories' operation will return a maximum of 100 repositories per page. If your collection is greater than 100 repositories you will need to paginate. Please refer to the [Pagination Docs](https://tray.ai/documentation/platform/automation-integration/advanced-use-cases/batching-queueing/pagination) for instructions.

```html
![github-security-info-list-repoo](github-security-info-list-repoo.png)
```

1. **Loop** is a Loop connector step to 'Loop List' results received from the previous step.
   A jsonpath for the 'List' field should appear similar to this: `$.steps.github-2.results`. You can always use a connector-snake to find the jsonpath.
   ![github-security-info-loop](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-54c85eb9_github-security-info-loop.png)

> **Info:** **JSONPATHS:** For more information on what jsonpaths are and how to use jsonpaths with Tray, please see our pages on [Basic data concepts](https://tray.ai/documentation/platform/automation-integration/building-workflows/mapping-data/basic-data-concepts) and [Mapping data between steps](https://tray.ai/documentation/platform/automation-integration/building-workflows/mapping-data/mapping-data-between-steps)

1. **Get branch protection** is a Gaurdian connector step that retrieves protection rules for the branch based on the provided 'Repository' (`$.steps.loop-1.value.name`)and 'Branch' (`$.steps.loop-1.value.default_branch`) name.
   This step takes jsonpaths from the **Loop** step.
   ![github-security-info-get-branch-protection](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-21a1487b_github-security-info-get-branch-protection.png)
   If the provided repository does not have branch protection the workflow will return an error.
   **Manual error handling**
   In this case, you should use [Manual error handling](https://tray.ai/documentation/platform/troubleshooting/error-handling/) method instead.
   To do so for the **Get branch protection** step in the properties panel, click 'Handle errors' and set the error handling to 'Manual'.
   ![github-security-info-error-handling](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-974c7ca3_github-security-info-error-handling.png)
   The way we handle an error from the **Get branch protection** step here is:

* **Success:** This means GitHub connector has successfully returned the protection information and we can manipulate the payload and then store it into Google sheets (**No branch protection** step).
* **Error:** This means the provided repository does not have branch protection and the GitHub connector has returned no data. In which case, we can pass the branch information from the **Loop** step directly into the Google Sheets connector (**Has branch protection** step).

> **Info:** **PLEASE NOTE**: There is no fixed way of working with error handling. This is just one simple example of handling errors. Exactly how you handle errors depends entirely on your specific requirements.

1. **No branch protection** step is a Google Sheets step that will add data from the **Loop** step into a Google Sheet using the 'Create row' operation. The operation requires 'Spreadsheet ID' and 'Worksheet name' to add data to the specific spreadsheet.
   For information on how to set up the Google Sheets connector please visit the [Google Sheets Docs](https://tray.ai/documentation/connectors/service/google-sheets).
   ![github-security-info-no-branch-protection](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-1d16ecf4_github-security-info-no-branch-protection.png)
   You can always use a [Date Time Helper](https://tray.ai/documentation/connectors/helper/date-time-helper) to make the **pushed at** and **created at** values readable.
2. **Has field restrictions?** is a Boolean connector step that checks if restrictions 'Property exists' or not. The step accepts 'JSONpath Property' from the **Get branch protection** step.
   If the specified GitHub property exists, the workflow will set GitHub restrictions (**Set restrictions** step), if not, then the default restrictions are set (**Set default restrictions** step).
   ![github-security-info-has-field-restrictions](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-00d1f846_github-security-info-has-field-restrictions.png)
3. **Set default restrictions** is a [Data storage](https://tray.ai/documentation/connectors/core/data-storage) connector step that is storing a default value `''` under the specified 'Key' `restrictions` using the 'Set Value' operation.
   This step sets a default value when a field restriction value is not available and, the result for the Boolean connector in the **Has field restrictions?** step is **FALSE**.
   ![github-security-info-set-default-restrictions](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-518e456a_github-security-info-set-default-restrictions.png)
4. **Set restrictions** is a Data storage connector step that is storing restrictions (`$.steps.github-1.restrictions`) value under under the specified 'Key' `restrictions` using the 'Set Value' operation.
   This restriction field is set, when the result for the Boolean connector in the **Has field restrictions?** step is **TRUE**.
   ![github-security-info-set-restrictions](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-390757c9_github-security-info-set-restrictions.png)
5. **Get restrictions** is again a Data storage connector step to get the values set under the 'Key' `restrictions` using the 'Get Value' operation.
   ![github-security-info-get-restrictionsabc](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-74a8c529_github-security-info-get-restrictions.png)
6. **Has branch protection** is again a Google Sheets step like a **No branch protection** step. Again, your input will vary depending on the required spreadsheet content. In this step, along with all the fields included in the **No branch protection** step, we have included the following additional fields:
   ![github-security-info-has-branch-protection](https://tray.ai/documentation/images/connectors/service/github/28a3bc35-e81e6575_github-security-info-has-branch-protection.png)
   The additional fields will grab the protection information from the **Get branch protection** step or a **Data Storage** connector. Before you add the data to the Google Sheets connector, you may want to enter a few extra steps to make the data readable using the [Helper](https://tray.ai/documentation/connectors/helper/) connectors.

> **Info:** \*\*INTERPOLATION: \*\*When you wish to include JSON generated data within another input/output result, use our Interpolation method as described [here](https://tray.ai/documentation/platform/automation-integration/building-workflows/mapping-data/mapping-data-between-steps#interpolated-mode).
