Skip to content

Commit

Permalink
Feat/rebase if repo existing (#11)
Browse files Browse the repository at this point in the history
* chore: implement rebase

* chore: tiny refactoring where pull rebase code was moved into its own method

* chore: added missing properties in configuration for GIT cloning service

* chore: updated readme docs around docker images and db liquibase to flyway

* chore: removed legacy db-sync python scripts as it's all java now
  • Loading branch information
nemo83 authored Feb 15, 2024
1 parent 85a286c commit c33e121
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 467 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ The simplest approach to running the application after having built it is to use
$ docker compose up
```

> [!NOTE]
> If you change the code, in order to make the changes visible to docker compose you need to rebuild the local image
> with `docker compose build` and then `docker compose up` will take care to restart containers who have a new image available
The complete Docker Compose setup runs the following services:
1. Setup a Postgres database in a docker container and exposes it to `localhost` on port `5432` (service name `db`)
2. Initialize the database schema and creates and configures access for the users required by the API (service name `bootstrap-db`)
3. Performs a one time sync of the data available in the mainnet and testnet registries from Cardano Foundation and IOG (service name `sync-db-once`)
4. Starts a service that syncs the repository in a fixed interval (service name `sync-db-cron`)
5. Starts the actual Spring application that exposes the CIP-26 REST API and exposes it to `localhost` on port `8081` (service name `api`)
2. Starts the actual Spring application that exposes the CIP-26 REST API and exposes it to `localhost` on port `8081` (service name `api`)

To test if the API is running query its health endpoint by executing:
```console
Expand All @@ -57,12 +58,8 @@ $ curl http://localhost:8081/actuator/health

Have a look at the [.env file](./.env) for the various configuration options.

If you ony need the database without starting the REST API or the data synced from the registries simply use the following command:
```console
$ docker compose up db bootstrap-db
```

At the moment the application needs a PostgreSQL database as a storage layer which might change in the future. You can use the [liquibase](https://www.liquibase.org/) database migration scripts provided in our [database folder](./database) to initialize this database.
At the moment the application needs a PostgreSQL database as a storage layer which might change in the future. Database evolutions
are managed by the `api` project using [flyway](https://flywaydb.org/).

## Features

Expand Down
7 changes: 7 additions & 0 deletions api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@ spring.flyway.baselineOnMigrate=true
spring.flyway.enabled=true
spring.flyway.validateMigrationNaming=true

# Git Configuration
git.organization=${GITHUB_ORGANIZATION:cardano-foundation}
git.projectName=${GITHUB_PROJECT_NAME:cardano-token-registry}
git.mappingsFolder=${GITHUB_MAPPINGS_FOLDER:mappings}
git.tmp.folder=${GITHUB_TMP_FOLDER:/tmp}
git.forceClone=${GITHUB_FORCE_CLONE:false}

# Github Token Metadata Sync
token.metadata.job.enabled=${TOKEN_METADATA_SYNC_JOB:false}
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,62 @@ public class GitService {
private String mappingsFolderName;
@Value("${git.tmp.folder:/tmp}")
private String gitTempFolder;
@Value("${git.forceClone:false}")
private boolean forceClone;

public Optional<Path> cloneCardanoTokenRegistryGitRepository() {
var gitFolder = getGitFolder();
if (gitFolder.exists()) {

boolean repoReady;
if (gitFolder.exists() && (forceClone || !isGitRepo())) {
log.info("exists and either force clone or not a git repo");
FileSystemUtils.deleteRecursively(gitFolder);
repoReady = cloneRepo();
} else if (gitFolder.exists() && isGitRepo()) {
log.info("exists and is git repo");
repoReady = pullRebaseRepo();
} else {
repoReady = cloneRepo();
}
try {

if (repoReady) {
return Optional.of(getMappingsFolder());
} else {
return Optional.empty();
}
}

private boolean cloneRepo() {
try {
var process = new ProcessBuilder()
.directory(gitFolder.getParentFile())
.directory(getGitFolder().getParentFile())
.command("sh", "-c", String.format("git clone https://github.com/%s/%s.git", organization, projectName))
.start();

var exitCode = process.waitFor();

if (exitCode == 0) {
return Optional.of(getMappingsFolder());
} else {
return Optional.empty();
}


return exitCode == 0;
} catch (Exception e) {
log.warn(String.format("It was not possible to clone the %s project", projectName), e);
return Optional.empty();
return false;
}
}

private boolean pullRebaseRepo() {
try {
var process = new ProcessBuilder()
.directory(getGitFolder())
.command("sh", "-c", "git pull --rebase")
.start();

var exitCode = process.waitFor();
return exitCode == 0;
} catch (Exception e) {
log.warn("it was not possible to update repo. cloning from scratch", e);
return false;
}
}

private boolean isGitRepo() {
return getGitFolder().toPath().resolve(".git").toFile().exists();
}

private File getGitFolder() {
Expand Down
1 change: 0 additions & 1 deletion gitsync/.dockerignore

This file was deleted.

38 changes: 0 additions & 38 deletions gitsync/Dockerfile.sync-cron

This file was deleted.

11 changes: 0 additions & 11 deletions gitsync/Dockerfile.sync-once

This file was deleted.

44 changes: 0 additions & 44 deletions gitsync/daos.py

This file was deleted.

Loading

0 comments on commit c33e121

Please sign in to comment.