Skip to content

Commit

Permalink
Merge pull request #50 from alecigne/feature/no-empty-commit
Browse files Browse the repository at this point in the history
Do not create empty commits
  • Loading branch information
alecigne authored Jul 30, 2024
2 parents 1569d82 + 77330c0 commit 7e93341
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.lecigne</groupId>
<artifactId>deezer-datasync</artifactId>
<version>0.4.3</version>
<version>0.5.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.lecigne.deezerdatasync.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.Duration;
import java.time.Instant;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package net.lecigne.deezerdatasync.repository.destinations.github;

import java.util.List;
import java.util.stream.Stream;
import lombok.Builder;
import lombok.Getter;
import net.lecigne.deezerdatasync.model.DeezerData;

@Getter
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ public void save(DeezerData deezerData) {
String latestCommitSha = getLatestCommitSha(repo);
log.debug("Latest commit SHA: {}", latestCommitSha);
GHTree newTree = createNewTree(repo, deezerData, latestCommitSha);
if (isSameTree(newTree, repo.getCommit(latestCommitSha).getTree())) {
log.debug("No changes detected, skipping commit.");
return;
}
GHCommit commit = createCommit(repo, newTree, latestCommitSha);
log.debug("New Commit SHA: {}", commit.getSHA1());
updateBranch(repo, commit);
} catch (IOException e) {
log.error("Error while saving to GitHub", e);
throw new RuntimeException(e);
var err = "Error while saving to GitHub";
log.error(err, e);
throw new GitHubSyncException(err, e);
}
}

Expand All @@ -61,6 +66,10 @@ private GHTree createNewTree(GHRepository repo, DeezerData deezerData, String la
return treeBuilder.create();
}

private boolean isSameTree(GHTree newTree, GHTree oldTree) {
return newTree.getSha().equals(oldTree.getSha());
}

private GHCommit createCommit(GHRepository repo, GHTree newTree, String latestCommitSha) throws IOException {
String commitMessage = String.format("Backup %s", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
return repo.createCommit()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.lecigne.deezerdatasync.repository.destinations.github;

public class GitHubSyncException extends RuntimeException {
public GitHubSyncException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.util.List;

public class Extensions {
public final class Extensions {

private Extensions() {
}

public static <T> List<T> ifEmpty(List<T> sourceList, List<T> otherList) {
return sourceList == null || sourceList.isEmpty() ? otherList : sourceList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.jqwik.api.constraints.StringLength;
import net.lecigne.deezerdatasync.model.DeezerData;
import net.lecigne.deezerdatasync.model.Playlist;
import org.junit.jupiter.api.DisplayName;

@Label("The GitHub mapper")
public class GitHubMapperPropertyTest {
Expand Down

0 comments on commit 7e93341

Please sign in to comment.