diff --git a/README.md b/README.md index 14157ba..e36c125 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,10 @@ After preparation of your local environment the script will execute the jscm com It will execute both possible command: -- teams-develop (in failed/successful state) -- teams-production (in failed/successful state) +- teams-develop (in failed/successful state/without artifacts) +- teams-production (in failed/successful state/without artifacts) -→ This will result in 4 messages being sent to the specified teams channel +→ This will result in 6(4 failed/2 successful) messages being sent to the specified teams channel. ### Calling the test script diff --git a/package-lock.json b/package-lock.json index dfe176c..e9a7d5c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@js-soft/codemagic-tools", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@js-soft/codemagic-tools", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "dependencies": { "axios": "^1.6.2", @@ -1333,9 +1333,9 @@ "peer": true }, "node_modules/follow-redirects": { - "version": "1.15.3", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", - "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", + "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", "funding": [ { "type": "individual", diff --git a/package.json b/package.json index fea286b..8cf2afd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@js-soft/codemagic-tools", - "version": "1.0.0", + "version": "1.0.1", "description": "Codemagic extended tooling", "homepage": "https://github.com/js-soft/codemagic-tools#readme", "bugs": { diff --git a/src/commands/TeamsProductionMessaging.ts b/src/commands/TeamsProductionMessaging.ts index 3ab08ad..436c513 100644 --- a/src/commands/TeamsProductionMessaging.ts +++ b/src/commands/TeamsProductionMessaging.ts @@ -7,6 +7,7 @@ export interface TeamsProductionMessagingOptions { buildUrl: string; buildNumber: number; webhook: string; + successfulBuild: boolean; } export async function runTeamsProductionMessagingCommand(options: TeamsProductionMessagingOptions): Promise { @@ -22,15 +23,20 @@ export async function runTeamsProductionMessagingCommand(options: TeamsProductio const platformIdentifier = options.platform.toUpperCase(); const storeName = platformIdentifier === "IOS" ? "App Store" : "Google Play Store"; + const productionMessage = options.successfulBuild + ? `New release is now available in the ${storeName}` + : "New release could not be created."; + const messageContents = { - title: `${options.projectName}: New release is now available in the ${storeName} [${platformIdentifier}]`, + title: options.successfulBuild + ? `${options.projectName}: New release is now available in the ${storeName} [${platformIdentifier}]` + : `${options.projectName}: New release could not be created [${platformIdentifier}]`, summary: `New Release - ${platformIdentifier}`, - text: `New Release: #${options.buildNumber} - ${platformIdentifier}
- The newly released version is now available in the ${storeName}. `, + text: `New Release: #${options.buildNumber} - ${platformIdentifier}
${productionMessage}`, potentialAction: [ { "@type": "OpenUri", - name: "Open Build", + name: options.successfulBuild ? "Open Build" : "Open Build Logs", targets: [{ os: "default", uri: options.buildUrl }] } ] diff --git a/src/index.ts b/src/index.ts index 1834fb2..efdfc23 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,13 +3,13 @@ import fs from "fs"; import os from "os"; import yargs from "yargs"; -import { CmArtifactLink } from "./CmArtifactLink"; + import { TeamsCommandLineOptions } from "./commands/TeamsCommandLineOptions"; import { runTeamsDevelopMessagingCommand } from "./commands/TeamsDevelopMessaging"; import { runTeamsProductionMessagingCommand } from "./commands/TeamsProductionMessaging"; async function run() { - const buildWasSuccessful = fs.existsSync(`${os.homedir()}/SUCCESS`); + let buildWasSuccessful = fs.existsSync(`${os.homedir()}/SUCCESS`); const webhook = process.env.teams_webhook_url!; const buildId = process.env.CM_BUILD_ID!; @@ -17,24 +17,23 @@ async function run() { const buildNumber = parseInt(process.env.BUILD_NUMBER!); const buildUrl = `https://codemagic.io/app/${projectId}/build/${buildId}`; - if (process.env.CM_ARTIFACT_LINKS === undefined) { - console.error( - "To ensure correct execution the environment variable CM_ARTIFACT_LINKS must be present in the system" - ); - process.exit(1); - } - let artifactUrl: string; let artifactType: string; - const cmArtifactLinks: CmArtifactLink[] = JSON.parse(process.env.CM_ARTIFACT_LINKS!).filter( - (element: any) => element.type === "apk" || element.type === "ipa" - ); - if (cmArtifactLinks.filter((element: any) => element.type === "apk" || element.type === "ipa").length !== 0) { - const pickedElement = cmArtifactLinks.filter((element: any) => element.type === "apk" || element.type === "ipa")[0]; - artifactUrl = pickedElement.url; - artifactType = pickedElement.type; - } else { + if (process.env.CM_ARTIFACT_LINKS !== undefined) { + const cmArtifactLinks = JSON.parse(process.env.CM_ARTIFACT_LINKS); + if (cmArtifactLinks.filter((element: any) => element.type === "apk" || element.type === "ipa").length === 0) { + buildWasSuccessful = false; + } else { + const pickedElement = cmArtifactLinks.filter( + (element: any) => element.type === "apk" || element.type === "ipa" + )[0]; + artifactUrl = pickedElement.url; + artifactType = pickedElement.type; + } + } + + if (!buildWasSuccessful) { // should link to the workflow-log can be determined from buildId and projectId artifactUrl = buildUrl; artifactType = "logs"; @@ -80,14 +79,18 @@ async function run() { () => { return; }, - async (args) => + async (args) => { + checkArtifactLinkMatchesPlatform(args, artifactType); + await runTeamsProductionMessagingCommand({ projectName: args.projectName, platform: args.platform, buildUrl, buildNumber, - webhook - }) + webhook, + successfulBuild: buildWasSuccessful + }); + } ) .demandCommand(1, "Must provide a valid command from the ones listed above.") .scriptName("jscm") diff --git a/test/test_teams_messaging.sh b/test/test_teams_messaging.sh index bb914b6..f5c7157 100644 --- a/test/test_teams_messaging.sh +++ b/test/test_teams_messaging.sh @@ -17,11 +17,18 @@ echo "Enter the BUILD_NUMBER :" read BUILD_NUMBER_VALUE export BUILD_NUMBER=$BUILD_NUMBER_VALUE -export CM_ARTIFACT_LINKS=$(cat artifactLinks.json) +# we want to test the execution without any artifacts present +echo "Testing without the artifactLinks environment variable" +jscm teams-develop --platform "ios" --projectName "Mein Codemagic Test Projekt" +jscm teams-production --platform "ios" --projectName "Mein Codemagic Test Projekt" +echo "Finished teams messaging tests without artifactLinks" -echo "webhook url: $teams_webhook_url" +echo "--------------------------------------------" +export CM_ARTIFACT_LINKS=$(cat artifactLinks.json) +echo "created the artifactLinks environment variable" +echo "--------------------------------------------" -echo "Starting teams messaging tests" +echo "Testing with the artifactLinks environment variable" jscm teams-develop --platform "ios" --projectName "Mein Codemagic Test Projekt" jscm teams-production --platform "ios" --projectName "Mein Codemagic Test Projekt" touch ~/SUCCESS