Skip to content

Commit

Permalink
Merge pull request #134 from sinch/DEVEXP-510-store-sdk-information-w…
Browse files Browse the repository at this point in the history
…ith-sources

feat (SDK/Versioning): Store SDK related informations wihtin sources
  • Loading branch information
JPPortier authored Sep 2, 2024
2 parents 6d6e91f + 5482b54 commit 9ba0cfe
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 30 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ on:
releaseVersion:
description: "Version number to be released (use semver semantic: 'X.Y.Z')"
required: true
developmentVersion:
description: "Next development version to be set (use semver semantic: 'X.Y.Z', `-SNAPSHOT` suffix will be automatically added)"
nextDevelopmentVersion:
description: "Next development version to be set (use semver semantic: 'X.Y.Z', `-SNAPSHOT` artifact's suffix will be automatically added)"
required: true

permissions:
Expand Down Expand Up @@ -52,13 +52,13 @@ jobs:

- name: Create and switch to dedicated release branch
run: |
git checkout -b release-${{ github.event.inputs.releaseVersion }}
git checkout -b "release-${{ github.event.inputs.releaseVersion }}"
- name: Release
run: |
mvn --batch-mode -s settings.xml release:clean release:prepare -Dtag=v${{ github.event.inputs.releaseVersion }} -DreleaseVersion=${{ github.event.inputs.releaseVersion }} -DdevelopmentVersion=${{ github.event.inputs.developmentVersion }}-SNAPSHOT -DscmReleaseCommitComment="[release] Set release & tag: ${{ github.event.inputs.releaseVersion }}" -DscmDevelopmentCommitComment="[release] Set next version: ${{ github.event.inputs.developmentVersion }}-SNAPSHOT"
mvn --batch-mode -s settings.xml release:perform -Dusername=${{ env.GITHUB_USERNAME }} -Dpassword=${{ env.GITHUB_TOKEN }} -DskipTests=true
run: scripts/release.sh
env:
RELEASE_VERSION: ${{ github.event.inputs.releaseVersion }}
NEXT_VERSION: ${{ github.event.inputs.nextDevelopmentVersion }}
OSSRH_USER: ${{ secrets.OSSRH_USERNAME }}
OSSRH_PWD: ${{ secrets.OSSRH_PWD }}
GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }}
Expand All @@ -68,6 +68,7 @@ jobs:


- name: Create dedicated release pull request
run: gh pr create -d -B main -H release-${{ github.event.inputs.releaseVersion }} --title "Merge release release-${{ github.event.inputs.releaseVersion }} branch into main" --body 'Created by Github action'
run: gh pr create -d -B main -H "release-${RELEASE_VERSION}" --title "Merge release '"release-${RELEASE_VERSION}"' branch into main" --body 'Created by Github action'
env:
RELEASE_VERSION: ${{ github.event.inputs.releaseVersion }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23 changes: 23 additions & 0 deletions client/resources/SDKTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class VersionTemplating {

static final String SDK_FILE_PATH = "client/src/main/com/sinch/sdk/SDK.java";

public static void main(String[] args) throws IOException {

String versionValue = args[0];

Stream<String> lines = Files.lines(Paths.get(SDK_FILE_PATH));
String content = lines.collect(Collectors.joining("\n"));
lines.close();

System.out.println(
content.replaceAll("VERSION = \".*\"", String.format("VERSION = \"%s\"", versionValue)));
System.out.println("");
}
}
3 changes: 0 additions & 3 deletions client/resources/version.properties

This file was deleted.

8 changes: 8 additions & 0 deletions client/src/main/com/sinch/sdk/SDK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sinch.sdk;

public class SDK {

public static final String NAME = "Sinch Java SDK";
public static final String VERSION = "1.3.0-dev";
public static final String AUXILIARY_FLAG = "";
}
29 changes: 9 additions & 20 deletions client/src/main/com/sinch/sdk/SinchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
public class SinchClient {

private static final String DEFAULT_PROPERTIES_FILE_NAME = "/config-default.properties";
private static final String VERSION_PROPERTIES_FILE_NAME = "/version.properties";

private static final String OAUTH_URL_KEY = "oauth-url";
private static final String NUMBERS_SERVER_KEY = "numbers-server";
Expand All @@ -41,18 +40,13 @@ public class SinchClient {

private static final String VERIFICATION_SERVER_KEY = "verification-server";

private static final String PROJECT_NAME_KEY = "project.name";
private static final String PROJECT_VERSION_KEY = "project.version";
private static final String PROJECT_AUXILIARY_FLAG = "project.auxiliary_flag";

// sinch-sdk/{sdk_version} ({language}/{language_version}; {implementation_type};
// {auxiliary_flag})
private static final String SDK_USER_AGENT_HEADER = "User-Agent";
private static final String SDK_USER_AGENT_FORMAT = "sinch-sdk/%s (%s/%s; %s; %s)";
private static final Logger LOGGER = Logger.getLogger(SinchClient.class.getName());

private final Configuration configuration;
private final Properties versionProperties;

private NumbersService numbers;
private SMSService sms;
Expand Down Expand Up @@ -85,12 +79,7 @@ public SinchClient(Configuration configuration) {
checkConfiguration(newConfiguration);
this.configuration = newConfiguration;

versionProperties = handlePropertiesFile(VERSION_PROPERTIES_FILE_NAME);
LOGGER.fine(
String.format(
"%s (%s) started",
versionProperties.getProperty(PROJECT_NAME_KEY),
versionProperties.getProperty(PROJECT_VERSION_KEY)));
LOGGER.fine(String.format("%s (%s) started", SDK.NAME, SDK.VERSION));
}

private void handleDefaultNumbersSettings(
Expand Down Expand Up @@ -320,32 +309,32 @@ private HttpClientApache getHttpClient() {
this.httpClient = new HttpClientApache();

// set SDK User-Agent
String userAgent = formatSdkUserAgentHeader(versionProperties);
String userAgent = formatSdkUserAgentHeader();
this.httpClient.setRequestHeaders(
Stream.of(new String[][] {{SDK_USER_AGENT_HEADER, userAgent}})
.collect(Collectors.toMap(data -> data[0], data -> data[1])));

LOGGER.fine("HTTP client loaded");
LOGGER.finest(String.format("HTTP client loaded (%s='%s'", SDK_USER_AGENT_HEADER, userAgent));
}
return this.httpClient;
}

private String formatSdkUserAgentHeader(Properties versionProperties) {
private String formatSdkUserAgentHeader() {
return String.format(
SDK_USER_AGENT_FORMAT,
versionProperties.get(PROJECT_VERSION_KEY),
SDK.VERSION,
"Java",
System.getProperty("java.version"),
"Apache",
formatAuxiliaryFlag((String) versionProperties.get(PROJECT_AUXILIARY_FLAG)));
formatAuxiliaryFlag());
}

private String formatAuxiliaryFlag(String auxiliaryFlag) {
private String formatAuxiliaryFlag() {

Collection<String> values = Collections.singletonList(System.getProperty("java.vendor"));

if (!StringUtil.isEmpty(auxiliaryFlag)) {
values.add(auxiliaryFlag);
if (!StringUtil.isEmpty(SDK.AUXILIARY_FLAG)) {
values.add(SDK.AUXILIARY_FLAG);
}
return String.join(",", values);
}
Expand Down
47 changes: 47 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh

if [ ! "${RELEASE_VERSION}" ]; then
echo "Missing 'RELEASE_VERSION' env value"
exit 1
fi

if [ ! "${NEXT_VERSION}" ]; then
echo "Missing 'NEXT_VERSION' env value"
exit 1
fi

if [ ! "${GITHUB_USERNAME}" ]; then
echo "Missing 'GITHUB_USERNAME' env value"
exit 1
fi

if [ ! "${GITHUB_TOKEN}" ]; then
echo "Missing 'GITHUB_TOKEN' env value"
exit 1
fi

SDKFILE_PATH="client/src/main/com/sinch/sdk/SDK.java"

NEXT_VERSION_SNAPSHOT="${NEXT_VERSION}-SNAPSHOT"
NEXT_VERSION_DEV="${NEXT_VERSION}-dev"

# Create SDK.java file with version information
SDK=$(java client/resources/SDKTemplate.java "$RELEASE_VERSION") && echo "$SDK" > "$SDKFILE_PATH" || exit 1
git add "$SDKFILE_PATH" && git commit -m "build (release): Bump version to $RELEASE_VERSION for sources" || exit 1

mvn --batch-mode -s settings.xml release:clean release:prepare \
-Dtag="v$RELEASE_VERSION" \
-DreleaseVersion="$RELEASE_VERSION" \
-DdevelopmentVersion="${NEXT_VERSION_SNAPSHOT}" \
-DscmReleaseCommitComment="[release] Set release & tag: $RELEASE_VERSION" \
-DscmDevelopmentCommitComment="[release] Set next version: ${NEXT_VERSION_SNAPSHOT}" || exit 1

mvn --batch-mode -s settings.xml release:perform \
-Dusername="${GITHUB_USERNAME}" \
-Dpassword="${GITHUB_TOKEN}" \
-DskipTests=true || exit 1

# Update SDK.java file with next version information
SDK=$(java client/resources/SDKTemplate.java "$NEXT_VERSION_DEV") && echo "$SDK" > "$SDKFILE_PATH" || exit 1
git add "$SDKFILE_PATH" && git commit -m "build (release): Set next version to $NEXT_VERSION_DEV for sources" || exit 1
git push -u origin HEAD || exit 1

0 comments on commit 9ba0cfe

Please sign in to comment.