From d0b658729ae7f91f48bf524d7cc210d4bb199390 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 19 Jan 2021 10:45:47 +0000 Subject: [PATCH 1/7] Initial buildinfo version using ReplaceTokens --- build.gradle | 26 +++++++++++ .../com/dumbdogdiner/stickyapi/StickyAPI.java | 33 +++++++++++++ .../dumbdogdiner/stickyapi/StickyAPITest.java | 46 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java diff --git a/build.gradle b/build.gradle index a31f146d..a0c69102 100644 --- a/build.gradle +++ b/build.gradle @@ -4,6 +4,8 @@ plugins { id 'jacoco' id "io.freefair.lombok" version "5.3.0" id "com.github.hierynomus.license" version "0.15.0" + // Nemerosa Versioning Plugin for the build info + id "net.nemerosa.versioning" version "2.8.2" } jacoco { @@ -95,6 +97,30 @@ compileTestJava.options.encoding = "UTF-8" javadoc.options.encoding = "UTF-8" +// Build Info +// Set the timestamp format +def dataBuildTimestamp = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" + +// Import the filter +import org.apache.tools.ant.filters.ReplaceTokens + +// Create task to replace the tokens with their actual values +// NOTE: At the moment this replaces tokens *globally* (format eg. @BUILDINFO_COMMIT@ in source code) +task processSourceTokens(type: Sync) { + from sourceSets.main.java + into 'build/processed/src/main/java' + filter(ReplaceTokens, tokens: [ + BUILDINFO_DATEFORMAT: dataBuildTimestamp, + BUILDINFO_TIMESTAMP: new java.text.SimpleDateFormat(dataBuildTimestamp).format(new Date()), + BUILDINFO_COMMIT: versioning.info.commit, + BUILDINFO_BRANCH: versioning.info.branch, + BUILDINFO_ISDIRTY: versioning.info.dirty.toString() + ]) +} +// Use the filter task as the input for compileJava +compileJava.source = processSourceTokens.outputs + + tasks.publish.dependsOn build, sources diff --git a/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java b/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java index fe91da45..0ba84b55 100644 --- a/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java +++ b/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java @@ -4,6 +4,9 @@ */ package com.dumbdogdiner.stickyapi; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.logging.Logger; @@ -25,4 +28,34 @@ public class StickyAPI { @Getter @Setter private static ExecutorService pool = Executors.newCachedThreadPool(); + + // Build Info Start + private static final String dateFormat = "@BUILDINFO_DATEFORMAT@"; + // Custom Getter + private static final String timestamp = "@BUILDINFO_TIMESTAMP@"; + @Getter + private static final String commit = "@BUILDINFO_COMMIT@"; + @Getter + private static final String branch = "@BUILDINFO_BRANCH@"; + // Custom Getter + private static final String isDirty = "@BUILDINFO_ISDIRTY@"; + + public static Date getTimestamp() { + SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); + try { + Date date = formatter.parse(timestamp); + return date; + } catch (ParseException e) { + e.printStackTrace(); + return null; + } + } + + public static String getSha() { + return commit.substring(0, 7); + } + + public static Boolean getIsDirty() { + return Boolean.parseBoolean(isDirty); + } } diff --git a/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java b/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java new file mode 100644 index 00000000..1c9cdfb7 --- /dev/null +++ b/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020-2021 DumbDogDiner . All rights reserved. + * Licensed under the MIT license, see LICENSE for more information... + */ +package com.dumbdogdiner.stickyapi; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.jupiter.api.Test; + +public class StickyAPITest { + + @Test + public void testGetTimestamp() { + assertNotNull(StickyAPI.getTimestamp()); + } + + @Test + public void testGetCommit() { + assertEquals(40, StickyAPI.getCommit().length()); + } + + @Test + public void testGetSha() { + String sha = StickyAPI.getSha(); + + assertEquals(7, sha.length()); + assertEquals(StickyAPI.getCommit().substring(0, 7), sha); + } + + @Test + public void testGetBranch() { + String branch = StickyAPI.getBranch(); + + assertNotNull(branch); + assertTrue(branch.length() != 0); + } + + @Test + // This test is kinda useless? + public void testGetIsDirty() { + assertTrue(StickyAPI.getIsDirty() instanceof Boolean); + } +} From 3c93446fbe661347105d9aa6149fa5c3478c06a8 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 19 Jan 2021 11:05:39 +0000 Subject: [PATCH 2/7] [ci] Install git in all workflows --- .github/workflows/ci.yml | 2 ++ .github/workflows/publish.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a3487780..65d55752 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,8 @@ jobs: container: image: jcxldn/openjdk-alpine:${{matrix.version}}-jdk steps: + - name: Install git + run: apk add --no-cache git - uses: actions/checkout@v2 - name: Grant execute permission for gradlew run: chmod +x gradlew diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index a5724fbe..4f3a2d4a 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,6 +11,8 @@ jobs: container: image: jcxldn/openjdk-alpine:11-jdk steps: + - name: Install git + run: apk add --no-cache git - uses: actions/checkout@v2 - name: Echo branch name run: echo ${GITHUB_REF##*/} From 180b509df2935408dad1308d1baf930849de491f Mon Sep 17 00:00:00 2001 From: James Date: Tue, 19 Jan 2021 16:44:26 +0000 Subject: [PATCH 3/7] Pretty print the build info --- build.gradle | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index a0c69102..bdfe169e 100644 --- a/build.gradle +++ b/build.gradle @@ -104,18 +104,25 @@ def dataBuildTimestamp = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" // Import the filter import org.apache.tools.ant.filters.ReplaceTokens -// Create task to replace the tokens with their actual values -// NOTE: At the moment this replaces tokens *globally* (format eg. @BUILDINFO_COMMIT@ in source code) -task processSourceTokens(type: Sync) { - from sourceSets.main.java - into 'build/processed/src/main/java' - filter(ReplaceTokens, tokens: [ +// Define the map containing the tokens we want to replace +def tokensMap = [ BUILDINFO_DATEFORMAT: dataBuildTimestamp, BUILDINFO_TIMESTAMP: new java.text.SimpleDateFormat(dataBuildTimestamp).format(new Date()), BUILDINFO_COMMIT: versioning.info.commit, BUILDINFO_BRANCH: versioning.info.branch, BUILDINFO_ISDIRTY: versioning.info.dirty.toString() - ]) + ] + +// Create task to replace the tokens with their actual values +// NOTE: At the moment this replaces tokens *globally* (format eg. @BUILDINFO_COMMIT@ in source code) +task processSourceTokens(type: Sync) { + from sourceSets.main.java + into 'build/processed/src/main/java' + filter(ReplaceTokens, tokens: tokensMap) + + // Pretty print the build info + println("\n----- StickyAPI Build Info -----\n") + tokensMap.each { println "${String.format("%1\$-" + 10 + "s", it.key.replace("BUILDINFO_", "").toLowerCase())}\t${it.value}" } } // Use the filter task as the input for compileJava compileJava.source = processSourceTokens.outputs From 6b888d1f796a9ea6625ee0362c4f4d8cc6773d1d Mon Sep 17 00:00:00 2001 From: James Date: Wed, 20 Jan 2021 18:54:21 +0000 Subject: [PATCH 4/7] [ci] Also install git in javadoc-check and license-header-check Co-authored-by: Andrew Katz --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 65d55752..74bfca1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,8 @@ jobs: container: image: jcxldn/openjdk-alpine:11-jdk steps: + - name: Install git + run: apk add --no-cache git - uses: actions/checkout@v2 - name: Grant execute permission for gradlew run: chmod +x gradlew @@ -44,6 +46,8 @@ jobs: container: image: jcxldn/openjdk-alpine:11-jdk steps: + - name: Install git + run: apk add --no-cache git - uses: actions/checkout@v2 - name: Grant execute permission for gradlew run: chmod +x gradlew From 752daa876ede12897bb259f7cc015b94b4e7dd29 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 21 Jan 2021 09:10:09 +0000 Subject: [PATCH 5/7] [ci] Add artifacts uploading to javadoc-check Co-authored-by: Andrew Katz --- .github/workflows/ci.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 74bfca1f..079884ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,3 +53,20 @@ jobs: run: chmod +x gradlew - name: Build javadocs run: ./gradlew javadoc + - name: Save javadocs + uses: actions/upload-artifact@v2 + with: + name: javadocs + path: build/docs/ # upload entire directory + - name: Save failed javadocs build output + if: ${{ failure() }} + uses: actions/upload-artifact@v2 + with: + name: failed-javadocs-build-output + path: build/ # upload entire directory + - name: Save failed javadocs build options + if: ${{ failure() }} + uses: actions/upload-artifact@v2 + with: + name: failed-javadocs-options-file + path: build/tmp/javadoc/javadoc.options From 48f6ccc878bf41396d369ddc24947d700d10ad82 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 23 Jan 2021 17:05:20 +0000 Subject: [PATCH 6/7] Add javadocs for buildinfo getters --- .../com/dumbdogdiner/stickyapi/StickyAPI.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java b/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java index 0ba84b55..427af9ad 100644 --- a/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java +++ b/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java @@ -33,13 +33,33 @@ public class StickyAPI { private static final String dateFormat = "@BUILDINFO_DATEFORMAT@"; // Custom Getter private static final String timestamp = "@BUILDINFO_TIMESTAMP@"; + + /** + * Get a string with the latest commit (id) at API's build-time. + * + * @since TBA + * @return {@link String} commit id + */ @Getter private static final String commit = "@BUILDINFO_COMMIT@"; + /** + * Get a string with the current branch at API's build-time. + * + * @since TBA + * @return {@link String} branch name + */ @Getter private static final String branch = "@BUILDINFO_BRANCH@"; // Custom Getter - private static final String isDirty = "@BUILDINFO_ISDIRTY@"; + private static final String isDirty = "@BUILDINFO_ISDIRTY@"; + + /** + * Get a Date object showing the current time at API's build-time. + * + * @since TBA + * @return {@link Date} date + */ public static Date getTimestamp() { SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); try { @@ -51,10 +71,24 @@ public static Date getTimestamp() { } } + /** + * Get a string with the latest commit sha at API's build-time. + * + * @since TBA + * @return {@link String} sha + */ public static String getSha() { return commit.substring(0, 7); } + /** + * Get a boolean showing if the working tree was dirty at API's build-time. + *

+ * If the working directory was dirty, this will return true, meaning there were modified tracked files and staged changes at build-time. + * + * @since TBA + * @return {@link Boolean} isDirty + */ public static Boolean getIsDirty() { return Boolean.parseBoolean(isDirty); } From ce7801efcf7a82029fd17df0c92a9ebf677efd73 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 23 Jan 2021 17:19:30 +0000 Subject: [PATCH 7/7] add getVersion buildinfo getter; remove version annotation from main class --- build.gradle | 1 + .../com/dumbdogdiner/stickyapi/StickyAPI.java | 20 ++++++++++++++++--- .../dumbdogdiner/stickyapi/StickyAPITest.java | 9 +++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index bdfe169e..f239286d 100644 --- a/build.gradle +++ b/build.gradle @@ -106,6 +106,7 @@ import org.apache.tools.ant.filters.ReplaceTokens // Define the map containing the tokens we want to replace def tokensMap = [ + BUILDINFO_VERSION: project.version, BUILDINFO_DATEFORMAT: dataBuildTimestamp, BUILDINFO_TIMESTAMP: new java.text.SimpleDateFormat(dataBuildTimestamp).format(new Date()), BUILDINFO_COMMIT: versioning.info.commit, diff --git a/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java b/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java index 427af9ad..2f246a8b 100644 --- a/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java +++ b/src/main/java/com/dumbdogdiner/stickyapi/StickyAPI.java @@ -19,7 +19,6 @@ * code-dupe-annihilating code for DDD plugins. * * @author DumbDogDiner (dumbdogdiner.com) - * @version 2.0.0 */ public class StickyAPI { @Getter @@ -30,8 +29,21 @@ public class StickyAPI { private static ExecutorService pool = Executors.newCachedThreadPool(); // Build Info Start + + /** + * Get the current version of API. + * + * @since TBA + * @return {@link String} version + * + */ + @Getter + private static final String version = "@BUILDINFO_VERSION"; + + // Getter not required private static final String dateFormat = "@BUILDINFO_DATEFORMAT@"; - // Custom Getter + + // Custom Getter (see below) private static final String timestamp = "@BUILDINFO_TIMESTAMP@"; /** @@ -42,6 +54,7 @@ public class StickyAPI { */ @Getter private static final String commit = "@BUILDINFO_COMMIT@"; + /** * Get a string with the current branch at API's build-time. * @@ -50,7 +63,8 @@ public class StickyAPI { */ @Getter private static final String branch = "@BUILDINFO_BRANCH@"; - // Custom Getter + + // Custom Getter (see below) private static final String isDirty = "@BUILDINFO_ISDIRTY@"; diff --git a/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java b/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java index 1c9cdfb7..5778f503 100644 --- a/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java +++ b/src/test/java/com/dumbdogdiner/stickyapi/StickyAPITest.java @@ -5,6 +5,7 @@ package com.dumbdogdiner.stickyapi; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -12,6 +13,14 @@ public class StickyAPITest { + @Test + public void testGetVersion() { + String version = StickyAPI.getVersion(); + + assertNotNull(version); + assertFalse(version.length() == 0); + } + @Test public void testGetTimestamp() { assertNotNull(StickyAPI.getTimestamp());