---
title: "Access Gitea from R"
author: "ixpantia"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Access Gitea from R}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
editor_options: 
  chunk_output_type: console
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(gitear)
library(dplyr)
library(jsonlite)

r <- readRDS(system.file("helper_data/response_example.RDS",
                              package = "gitear"))

content_an_organization <- fromJSON(system.file("helper_data/get_an_org.json",
                                                package = "gitear"))

content_issues <- fromJSON(system.file("helper_data/get_issues.json",
                                      package = "gitear"))

content_list_repos_org <- fromJSON(system.file("helper_data/get_repos_org.json",
                                               package = "gitear"))

content_list_users <- fromJSON(system.file("helper_data/get_users.json",
                                           package = "gitear"))

content_commits <- fromJSON(system.file("helper_data/get_commits.json",
                                        package = "gitear"))

```

Gitea is a community managed, lightweight code hosting solution  were projects
and their respective git repositories can be managed  <https://gitea.io>. This
package gives an interface to the 'Gitea' API to  access and manage
repositories, issues and organizations directly in R.

## Starting with `gitear`

If you want to start using the functions from `gitear` you should first go to
your gitea service and create an API KEY. You will find this under your avatar,
configuration, application and then generate a new token.

Be aware that you should save this token somewhere because its shown just once.

This is going to be your API TOKEN. Then you can use a function like this:

```{r, echo = FALSE}
mockery::stub(where = get_issues,
              what = "GET",
              how = r)

mockery::stub(where = get_issues,
              what = "fromJSON",
              how = content_issues)
```  

```{r}
# Credentials

api_token <- "gfdsgfd8ba18a866bsdfgsdfgs3a2dc9303453b0c92dcfb19"
url_ixpantia <- "https://prueba.com"

# Example function use:

issues <- get_issues(base_url = url_ixpantia,
                       api_key = api_token,
                       owner = "empresa",
                       repo = "repo_prueba")

glimpse(issues)
```


For the examples below, we are going to use credentials stored in a .Renviron
file, which we are going to call from this variables:

```{r}
example_key <- Sys.getenv("example_key")
example_url <- Sys.getenv("example_url")
```

## Get information about your organization

We can also obtain information from some organization defined in our gitea
service:

```{r, echo = FALSE}
mockery::stub(where = get_an_organization,
              what = "GET",
              how = r)

mockery::stub(where = get_an_organization,
              what = "fromJSON",
              how = content_an_organization)
```

```{r}
# Example function use

organizations <- get_an_organization(base_url = example_url,
                                     api_key = example_key,
                                     org = "empresa") 

glimpse(organizations)
```

## Get repositories information from one organization

And we can also see which repositories are associated with that same organization:

```{r, echo = FALSE}
mockery::stub(where = get_list_repos_org,
              what = "GET",
              how = r)

mockery::stub(where = get_list_repos_org,
              what = "fromJSON",
              how = content_list_repos_org)
```

```{r}
# Example function use

repos <- get_list_repos_org(base_url = example_url, 
                            api_key = example_key, 
                            org = "empresa")

glimpse(repos)
```

## User information

But we can not only get information about projects and repositories, we can
also see specific information about users:

```{r, echo = FALSE}
mockery::stub(where = get_list_users,
              what = "GET",
              how = r)

mockery::stub(where = get_list_users,
              what = "fromJSON",
              how = content_list_users)
```

```{r}
# Example function use

users <- get_list_users(base_url = example_url, 
                        api_key = example_key)

glimpse(users)
```

## All commits from a specific repository

We can also see the commits made to a certain repository in a very simple way

```{r, echo = FALSE}
mockery::stub(where = get_commits,
              what = "GET",
              how = r)

mockery::stub(where = get_commits,
              what = "fromJSON",
              how = content_commits)
```

```{r}
# Example function use

commits <- get_commits(base_url = example_url, 
                     api_key = example_key,
                     owner = "empresa",
                     repo = "repo_prueba")

head(commits)
```

These are just some of the functionality of gitea, there are other information
that we can obtain about a gitea service.
