From fc0143919970594b20b8d3a8e4d20edfd17eace6 Mon Sep 17 00:00:00 2001 From: SebClem Date: Thu, 30 Jun 2022 18:02:32 +0200 Subject: [PATCH] Clean for release --- .github/workflows/build.yml | 14 ++-- .github/workflows/release.yml | 78 +++++++++++++++++++ Dockerfile | 4 +- build.gradle | 40 +++------- renovate.json | 2 +- .../Api/Controllers/GuildController.java | 4 +- .../Api/Controllers/HelloController.java | 21 ----- .../net/Broken/Api/OpenApi/OpenApiConfig.java | 31 ++++---- .../Broken/Api/Security/SecurityConfig.java | 1 + .../net/Broken/Api/Services/GuildService.java | 5 ++ src/main/java/net/Broken/MainBot.java | 16 ++++ src/main/java/net/Broken/VersionLoader.java | 24 ++++++ src/main/resources/application.yml | 17 +++- 13 files changed, 179 insertions(+), 78 deletions(-) create mode 100644 .github/workflows/release.yml delete mode 100644 src/main/java/net/Broken/Api/Controllers/HelloController.java create mode 100644 src/main/java/net/Broken/VersionLoader.java diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5b564d6..68d3229 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,7 +3,12 @@ name: Build -on: push +on: + push: + branches-ignore: + - "renovate/**" + tags-ignore: + - "**" jobs: build-gradle: @@ -16,6 +21,7 @@ jobs: - name: Set up JDK 17 uses: actions/setup-java@v3 with: + distribution: 'temurin' java-version: 17 - name: Grant execute permission for gradlew @@ -59,13 +65,7 @@ jobs: id: branch-name uses: tj-actions/branch-names@v5.3 - - name: Set tag master - if: steps.branch-name.outputs.current_branch == 'master' - run: | - echo "tag=latest" >> $GITHUB_ENV - - name: Set tag - if: steps.branch-name.outputs.current_branch != 'master' run: | echo "tag=${{ steps.branch-name.outputs.current_branch }}" >> $GITHUB_ENV diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..99a2c60 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,78 @@ +# This workflow will build a Java project with Gradle +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle + +name: Build Release + +on: + push: + tags: + - "**" + +jobs: + build-gradle: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: 17 + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build with Gradle + run: ./gradlew build + + - name: Upload Jar File + uses: actions/upload-artifact@v2-preview + with: + name: claptrap_jar + path: build/libs/ + + build-docker: + runs-on: ubuntu-latest + needs: + - build-gradle + steps: + - uses: actions/checkout@v3 + + - name: Download artifact + uses: actions/download-artifact@v3.0.0 + with: + # Artifact name + name: claptrap_jar + # Destination path + path: build/libs/ + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to ghcr.io + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.CR_PAT }} + + - name: Get branch name + id: branch-name + uses: tj-actions/branch-names@v5.3 + + - name: Set tag + run: | + echo "tag=${{ steps.branch-name.outputs.tag }}" >> $GITHUB_ENV + + - name: Build and push Docker + uses: docker/build-push-action@v3 + with: + push: true + context: . + tags: | + ghcr.io/sebclem/claptrapbot:${{ env.tag }} + ghcr.io/sebclem/claptrapbot:latest + file: ./Dockerfile diff --git a/Dockerfile b/Dockerfile index 8f6bb66..9ae49ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ ARG BUILD_NBR ARG BRANCH_NAME ARG BRANCH_NAME ARG GITHUB_RUN_NUMBER -ADD build/libs/ClaptrapBot-*.jar /bot_src/bot.jar +ADD build/libs/ClaptrapBot.jar /bot_src/claptrapbot.jar RUN java -version -CMD java -jar bot.jar +CMD java -jar claptrapbot.jar LABEL org.opencontainers.image.source=https://github.com/Sebclem/ClaptrapBot/ \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1dc92d0..fc1cdf3 100644 --- a/build.gradle +++ b/build.gradle @@ -5,18 +5,13 @@ plugins { id 'groovy' id 'org.liquibase.gradle' version '2.1.1' id "nebula.lint" version "17.7.0" + id "com.gorylenko.gradle-git-properties" version "2.4.1" } - -def versionObj = new Version(major: 0, minor: 2, revision: 0) - group = "net.broken" archivesBaseName = "ClaptrapBot" -version = "$versionObj" - sourceCompatibility = '17' - repositories { mavenCentral() maven { @@ -27,7 +22,6 @@ jar { enabled(false) } - configurations.implementation { exclude group: "org.springframework.boot", module: "spring-boot-starter-logging" } @@ -38,6 +32,8 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-log4j2") implementation("org.springframework.boot:spring-boot-starter-oauth2-client") + implementation('org.springframework.boot:spring-boot-starter-actuator') + implementation('io.micrometer:micrometer-registry-prometheus:1.9.1') implementation('org.springdoc:springdoc-openapi-ui:1.6.9') implementation('org.springdoc:springdoc-openapi-security:1.6.9') @@ -48,21 +44,21 @@ dependencies { implementation('io.jsonwebtoken:jjwt-impl:0.11.5') implementation('io.jsonwebtoken:jjwt-jackson:0.11.5') - implementation 'com.sedmelluq:lavaplayer:1.3.77' - implementation 'net.dv8tion:JDA:4.4.0_350' + implementation('com.sedmelluq:lavaplayer:1.3.77') + implementation('net.dv8tion:JDA:4.4.0_350') implementation(platform("org.apache.logging.log4j:log4j-bom:2.17.1")) implementation group: 'org.hibernate', name: 'hibernate-validator', version: '7.0.4.Final' // Use MySQL Connector-J - implementation 'mysql:mysql-connector-java:8.0.29' - implementation 'org.reflections:reflections:0.10.2' - implementation 'org.apache.commons:commons-lang3:3.12.0' + implementation('mysql:mysql-connector-java:8.0.29') + implementation('org.reflections:reflections:0.10.2') + implementation('org.apache.commons:commons-lang3:3.12.0') - liquibaseRuntime 'org.liquibase:liquibase-core:4.12.0' - liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:3.0.2' - liquibaseRuntime 'mysql:mysql-connector-java:8.0.29' + liquibaseRuntime('org.liquibase:liquibase-core:4.12.0') + liquibaseRuntime('org.liquibase:liquibase-groovy-dsl:3.0.2') + liquibaseRuntime('mysql:mysql-connector-java:8.0.29') liquibaseRuntime group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1' liquibaseRuntime group: 'org.liquibase.ext', name: 'liquibase-hibernate5', version: '4.12.0' liquibaseRuntime 'org.springframework.boot:spring-boot-starter-data-jpa' @@ -89,17 +85,3 @@ liquibase { } } } - - -class Version { - String major, minor, revision - - static String getBuild() { - System.getenv("GITHUB_RUN_NUMBER") ?: System.getProperty("BUILD_NUMBER") ?: - System.getenv("GIT_COMMIT")?.substring(0, 7) ?: System.getProperty("GIT_COMMIT")?.substring(0, 7) ?: "DEV" - } - - String toString() { - "${major}.${minor}.${revision}_$build" - } -} diff --git a/renovate.json b/renovate.json index 8a0c502..19c1539 100644 --- a/renovate.json +++ b/renovate.json @@ -2,5 +2,5 @@ "extends": [ "config:base" ], - "commitMessageSuffix": "[skip ci]" + "commitMessagePrefix": ":arrow_up:" } diff --git a/src/main/java/net/Broken/Api/Controllers/GuildController.java b/src/main/java/net/Broken/Api/Controllers/GuildController.java index 0109b6c..a0bc11e 100644 --- a/src/main/java/net/Broken/Api/Controllers/GuildController.java +++ b/src/main/java/net/Broken/Api/Controllers/GuildController.java @@ -33,8 +33,8 @@ public class GuildController { @GetMapping("inviteLink") public InviteLink getInviteLink() { - String link = MainBot.jda.setRequiredScopes("applications.commands").getInviteUrl(Permission.getPermissions(1644971949399L)); - return new InviteLink(link); + + return new InviteLink(guildService.getInviteLink()); } @GetMapping("/{guildId}/voiceChannels") diff --git a/src/main/java/net/Broken/Api/Controllers/HelloController.java b/src/main/java/net/Broken/Api/Controllers/HelloController.java deleted file mode 100644 index 2287cfb..0000000 --- a/src/main/java/net/Broken/Api/Controllers/HelloController.java +++ /dev/null @@ -1,21 +0,0 @@ -package net.Broken.Api.Controllers; - -import net.Broken.Api.Security.Data.JwtPrincipal; -import org.springframework.security.core.Authentication; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/api/v2/hello") -@CrossOrigin(origins = "*", maxAge = 3600) -public class HelloController { - - - @GetMapping("world") - public String helloWorld(Authentication authentication){ - JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal(); - return "Hello " + principal.user().getUsername(); - } -} diff --git a/src/main/java/net/Broken/Api/OpenApi/OpenApiConfig.java b/src/main/java/net/Broken/Api/OpenApi/OpenApiConfig.java index 64d839a..681e437 100644 --- a/src/main/java/net/Broken/Api/OpenApi/OpenApiConfig.java +++ b/src/main/java/net/Broken/Api/OpenApi/OpenApiConfig.java @@ -2,33 +2,34 @@ package net.Broken.Api.OpenApi; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; import io.swagger.v3.oas.models.servers.Server; +import net.Broken.VersionLoader; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class OpenApiConfig { + private final VersionLoader versionLoader; + public OpenApiConfig(VersionLoader version) { + this.versionLoader = version; + } @Bean public OpenAPI customOpenAPI() { final String securitySchemeName = "JWT"; - - // final String apiTitle = String.format("%s API", StringUtils.capitalize(moduleName)); - return new OpenAPI() - .addSecurityItem(new SecurityRequirement().addList(securitySchemeName)) - .components( - new Components() - .addSecuritySchemes(securitySchemeName, - new SecurityScheme() - .name(securitySchemeName) - .type(SecurityScheme.Type.HTTP) - .scheme("bearer") - .bearerFormat("JWT") - ) - ) - .addServersItem(new Server().url("/").description("Default")); + return new OpenAPI().addSecurityItem( + new SecurityRequirement().addList(securitySchemeName)).components( + new Components().addSecuritySchemes( + securitySchemeName, + new SecurityScheme().name(securitySchemeName) + .type(SecurityScheme.Type.HTTP) + .scheme("bearer") + .bearerFormat("JWT")) + ).addServersItem(new Server().url("/").description("Default")) + .info(new Info().title("ClaptrapBot API").version(versionLoader.getVersion())); } } \ No newline at end of file diff --git a/src/main/java/net/Broken/Api/Security/SecurityConfig.java b/src/main/java/net/Broken/Api/Security/SecurityConfig.java index dfaee16..7a6dfde 100644 --- a/src/main/java/net/Broken/Api/Security/SecurityConfig.java +++ b/src/main/java/net/Broken/Api/Security/SecurityConfig.java @@ -29,6 +29,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .antMatchers("/swagger-ui/**").permitAll() .antMatchers("/swagger-ui.html").permitAll() .antMatchers("/v3/api-docs/**").permitAll() + .antMatchers("/actuator/**").permitAll() .anyRequest().authenticated(); http.addFilterBefore(jwtFilter(), UsernamePasswordAuthenticationFilter.class); diff --git a/src/main/java/net/Broken/Api/Services/GuildService.java b/src/main/java/net/Broken/Api/Services/GuildService.java index 007f2d9..867c086 100644 --- a/src/main/java/net/Broken/Api/Services/GuildService.java +++ b/src/main/java/net/Broken/Api/Services/GuildService.java @@ -66,4 +66,9 @@ public class GuildService { return roles; } + + public String getInviteLink(){ + return MainBot.jda.setRequiredScopes("applications.commands").getInviteUrl(Permission.getPermissions(1644971949399L)); + } + } diff --git a/src/main/java/net/Broken/MainBot.java b/src/main/java/net/Broken/MainBot.java index 7a74655..38da955 100644 --- a/src/main/java/net/Broken/MainBot.java +++ b/src/main/java/net/Broken/MainBot.java @@ -7,6 +7,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.core.io.ClassPathResource; import java.util.HashMap; @@ -32,11 +35,14 @@ public class MainBot { ConfigurableApplicationContext ctx = SpringApplication.run(MainBot.class, args); BotConfigLoader config = ctx.getBean(BotConfigLoader.class); + VersionLoader versionLoader = ctx.getBean(VersionLoader.class); logger.info("======================================="); logger.info("--------------Starting Bot-------------"); logger.info("======================================="); + logger.info("Version: " + versionLoader.getVersion()); + jda = Init.initJda(config); if (jda == null) { System.exit(SpringApplication.exit(ctx, () -> { @@ -47,4 +53,14 @@ public class MainBot { Init.polish(jda, config); ready = true; } + + @Bean + public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { + PropertySourcesPlaceholderConfigurer propsConfig + = new PropertySourcesPlaceholderConfigurer(); + propsConfig.setLocation(new ClassPathResource("git.properties")); + propsConfig.setIgnoreResourceNotFound(true); + propsConfig.setIgnoreUnresolvablePlaceholders(true); + return propsConfig; + } } diff --git a/src/main/java/net/Broken/VersionLoader.java b/src/main/java/net/Broken/VersionLoader.java new file mode 100644 index 0000000..1ba672d --- /dev/null +++ b/src/main/java/net/Broken/VersionLoader.java @@ -0,0 +1,24 @@ +package net.Broken; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class VersionLoader { + @Value("${git.branch}") + private String branch; + @Value("${git.tags}") + private String tags; + @Value("${git.commit.id.abbrev}") + private String commitId; + + public String getVersion(){ + String version; + if(tags.isEmpty()){ + version = "DEV-" + branch + "-" + commitId; + }else{ + version = tags; + } + return version; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 04f3f7b..6246eb3 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -27,4 +27,19 @@ discord: randomApiKey: ${RANDOM_API_KEY} springdoc: - paths-to-match: /api/v2/** \ No newline at end of file + paths-to-match: /api/v2/** + show-actuator: true + +management: + info: + git: + mode: full + enabled: true + endpoints: + enabled-by-default: true + web: + exposure: + include: health, info, prometheus + endpoint: + prometheus: + enabled: true \ No newline at end of file