Skip to content

Commit

Permalink
Add github workflow
Browse files Browse the repository at this point in the history
Signed-off-by: David Caro <[email protected]>
  • Loading branch information
david-caro committed Jun 11, 2023
1 parent 4a90c2b commit 4264255
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 43 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI
on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Setup Podman
run: |
sudo apt update
sudo apt-get -y install podman
- name: Get source
uses: actions/checkout@v3
with:
path: "expenses-server"
- name: start app
run: |
cd expenses-server
./scripts/start_devdb.sh populate
# fake accoutns config
mkdir -p ../home-lab-secrets/home_automation/expenses/
echo "accounts-config: {'accounts': []}" > ../home-lab-secrets/home_automation/expenses/application-accounts.yml
podman run --rm mariadb
./scripts/start_dev.sh
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// Needed to sign digital signatures for wise accounts
implementation 'org.bouncycastle:bcprov-jdk15on:1.64'
Expand Down
67 changes: 52 additions & 15 deletions scripts/start_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,56 @@ set -o pipefail
hash podman && DOCKER="sudo podman" || DOCKER=docker



set -x
$DOCKER build -f Dockerfile.dev -t expenses-server:dev
$DOCKER run \
--tty \
--interactive \
--volume $PWD:/src:rw,idmap \
--volume $PWD/../home-lab-secrets/:/home-lab-secrets:rw,idmap \
--publish 8080:8080 \
--name expenses-server-dev \
--rm \
expenses-server:dev \
"/src/gradlew"\
"bootRun" \
"--args='--spring.datasource.url=jdbc:mysql://$(hostname -i | grep -Po '192.168.1.\w*(?:$| )'):3306/expenses'" \
"$@"

check_if_backend_alive() {
curl --silent http://127.0.0.1:8080/actuator/health | grep "UP" || {
echo "failed to get the health of the backend server"
curl -v http://127.0.0.1:8080/actuator/health
echo "container logs:"
$DOCKER logs expenses-server-dev
return 1
}
echo "Your development server is up and running at http://127.0.0.1:8080"
return 0
}

main() {
$DOCKER build -f Dockerfile.dev -t expenses-server:dev
$DOCKER rm -f expenses-server-dev || :
db_host=$(hostname -i | grep -Po '192.168.1.\w*(?:$| )')
if [[ $db_host == "" ]]; then
# fallback in case we run on ci
db_host=$(hostname -i | awk '{print $1}')
fi
$DOCKER run \
--tty \
--interactive \
--user $UID \
--volume $PWD:/src:rw \
--volume $PWD/../home-lab-secrets/:/home-lab-secrets:rw \
--publish 8080:8080 \
--name expenses-server-dev \
--detach \
expenses-server:dev \
"/src/gradlew"\
"bootRun" \
"--args='--spring.datasource.url=jdbc:mysql://${db_host}:3306/expenses'" \
"$@"


echo "Waiting for the backend to come up..."
local count=0
while ! check_if_backend_alive; do
count=$((count + 1))
if [[ $count -ge 15 ]]; then
echo "The server container never came up!"
echo "You might want to try running with sudo :/"
return 1
fi
echo "Checking again in 5s..."
sleep 5
done
}

main "$@"
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.shareexpenses.server.expenses;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;

public interface ExpensesRepository extends JpaRepository<Expense, Long> {
public interface ExpensesRepository extends JpaRepository<Expense, Long> {
List<Expense> findAll();

@Query(value = "SELECT 1 FROM expenses limit 1", nativeQuery = true)
// The return value does not really matter, just that it did not blow up
List<Integer> checkDB();

boolean existsByExternalId(String externalId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,49 @@ public List<Expense> getAllExpenses() {
return expensesRepository.findAll();
}


public Expense addExpense(IncomingExpenseDTO newExpense) {
Account account = this
.accountRepository
.findById(newExpense.getAccountId())
.orElseThrow(() -> new EntityNotFoundException("No account found with id " + newExpense.getAccountId() + "."));
Account account = this.accountRepository
.findById(newExpense.getAccountId())
.orElseThrow(() -> new EntityNotFoundException("No account found with id " + newExpense.getAccountId() + "."));

Expense expense = Expense
.builder()
.description(newExpense.getDescription())
.amount(newExpense.getAmount())
.currency(newExpense.getCurrency().label)
.timestamp(newExpense.getTimestamp())
.account(account)
.category(newExpense.getCategory())
.build();
.builder()
.description(newExpense.getDescription())
.amount(newExpense.getAmount())
.currency(newExpense.getCurrency().label)
.timestamp(newExpense.getTimestamp())
.account(account)
.category(newExpense.getCategory())
.build();
return this.expensesRepository.save(expense);
}

public Expense updateExpense(Long id, IncomingExpenseDTO updatedExpense) {
Account account = this
.accountRepository
.findById(updatedExpense.getAccountId())
.orElseThrow(() -> new EntityNotFoundException("No account found with id " + updatedExpense.getAccountId() + "."));
Account account = this.accountRepository
.findById(updatedExpense.getAccountId())
.orElseThrow(
() -> new EntityNotFoundException("No account found with id " + updatedExpense.getAccountId() + "."));

Expense expense = Expense
.builder()
.id(id)
.description(updatedExpense.getDescription())
.amount(updatedExpense.getAmount())
.currency(updatedExpense.getCurrency().label)
.timestamp(updatedExpense.getTimestamp())
.account(account)
.category(updatedExpense.getCategory())
.build();
.builder()
.id(id)
.description(updatedExpense.getDescription())
.amount(updatedExpense.getAmount())
.currency(updatedExpense.getCurrency().label)
.timestamp(updatedExpense.getTimestamp())
.account(account)
.category(updatedExpense.getCategory())
.build();
return this.expensesRepository.save(expense);
}

public void deleteExpense(long expenseId) {
Expense expense = this.expensesRepository.findById(expenseId).orElseThrow(() -> new EntityNotFoundException("No expense found with id " + expenseId + "."));
Expense expense = this.expensesRepository.findById(expenseId)
.orElseThrow(() -> new EntityNotFoundException("No expense found with id " + expenseId + "."));
this.expensesRepository.delete(expense);
}

public void checkDB() {
this.expensesRepository.checkDB();
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ spring:
username: "expenses"
password: "dummypass"
data.rest.basePath: "/api"
management:
endpoint:
health:
show-details: always

0 comments on commit 4264255

Please sign in to comment.