From c290443957ea4b80a6489cfc96283fa6769d2a0f Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Wed, 10 Jan 2024 10:48:34 +0100 Subject: [PATCH 01/10] fix: build execution path not requiring artifacts --- src/index.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1834fb2..9e31e71 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ 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"; @@ -17,24 +17,27 @@ 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; + let failedState = false; + + if (process.env.CM_ARTIFACT_LINKS === undefined) { + failedState = true; } else { + const cmArtifactLinks = JSON.parse(process.env.CM_ARTIFACT_LINKS); + if (cmArtifactLinks.filter((element: any) => element.type === "apk" || element.type === "ipa").length === 0) { + failedState = true; + } else { + const pickedElement = cmArtifactLinks.filter( + (element: any) => element.type === "apk" || element.type === "ipa" + )[0]; + artifactUrl = pickedElement.url; + artifactType = pickedElement.type; + } + } + + if (failedState) { // should link to the workflow-log can be determined from buildId and projectId artifactUrl = buildUrl; artifactType = "logs"; From 511a7f7724d25306520cc5afcb93811343fa918a Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Wed, 10 Jan 2024 10:50:54 +0100 Subject: [PATCH 02/10] test: adding test case for new execution path --- test/test_teams_messaging.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/test_teams_messaging.sh b/test/test_teams_messaging.sh index bb914b6..d4fb7aa 100644 --- a/test/test_teams_messaging.sh +++ b/test/test_teams_messaging.sh @@ -17,11 +17,20 @@ echo "Enter the BUILD_NUMBER :" read BUILD_NUMBER_VALUE export BUILD_NUMBER=$BUILD_NUMBER_VALUE +# 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 "--------------------------------------------" export CM_ARTIFACT_LINKS=$(cat artifactLinks.json) +echo "created the artifactLinks environment variable" +echo "--------------------------------------------" echo "webhook url: $teams_webhook_url" -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 From ebeb6f49d97e71505595983686f48e3649ab4a81 Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Wed, 10 Jan 2024 11:03:45 +0100 Subject: [PATCH 03/10] fix: remove misleading positive release messages --- src/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9e31e71..7a14e71 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,14 +83,23 @@ async function run() { () => { return; }, - async (args) => + async (args) => { + if (failedState) { + console.log("Cannot send production message for a build, which did not produce an artifact."); + process.exit(1); + } + if (!buildWasSuccessful) { + console.log("Cannot send production message for failed build."); + process.exit(1); + } await runTeamsProductionMessagingCommand({ projectName: args.projectName, platform: args.platform, buildUrl, buildNumber, webhook - }) + }); + } ) .demandCommand(1, "Must provide a valid command from the ones listed above.") .scriptName("jscm") From 2e0ade15b5761cfe2ef440a07ceaf6e2b07baa39 Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Wed, 10 Jan 2024 11:04:14 +0100 Subject: [PATCH 04/10] chore: clean tests and their description --- README.md | 6 +++--- test/test_teams_messaging.sh | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 14157ba..1139e09 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 4(2 failed/2 successful) messages being sent to the specified teams channel as well as 2 exceptions exceptions. ### Calling the test script diff --git a/test/test_teams_messaging.sh b/test/test_teams_messaging.sh index d4fb7aa..f5c7157 100644 --- a/test/test_teams_messaging.sh +++ b/test/test_teams_messaging.sh @@ -28,8 +28,6 @@ export CM_ARTIFACT_LINKS=$(cat artifactLinks.json) echo "created the artifactLinks environment variable" echo "--------------------------------------------" -echo "webhook url: $teams_webhook_url" - 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" From 08324a29035c76c8049f4a72cb428f7444998207 Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Wed, 10 Jan 2024 11:26:00 +0100 Subject: [PATCH 05/10] chore: bump up version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index dfe176c..a8d6d2b 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", 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": { From 3132e0442834962162865dfec40f8973a6d1d3f7 Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Wed, 10 Jan 2024 13:48:42 +0100 Subject: [PATCH 06/10] chore: fix vulnerabilities --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index a8d6d2b..e9a7d5c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", From 8831a6bbe3980fd4e9d56356150cbe96a6c8bdd6 Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Thu, 11 Jan 2024 21:28:50 +0100 Subject: [PATCH 07/10] feature: add failed production command --- src/commands/TeamsProductionMessaging.ts | 14 ++++++++++---- src/index.ts | 13 +++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/commands/TeamsProductionMessaging.ts b/src/commands/TeamsProductionMessaging.ts index 3ab08ad..512e40a 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 failed to build"; + 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 failed to build [${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 7a14e71..4ab448e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { runTeamsDevelopMessagingCommand } from "./commands/TeamsDevelopMessagin 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!; @@ -85,19 +85,16 @@ async function run() { }, async (args) => { if (failedState) { - console.log("Cannot send production message for a build, which did not produce an artifact."); - process.exit(1); - } - if (!buildWasSuccessful) { - console.log("Cannot send production message for failed build."); - process.exit(1); + buildWasSuccessful = false; } + await runTeamsProductionMessagingCommand({ projectName: args.projectName, platform: args.platform, buildUrl, buildNumber, - webhook + webhook, + successfulBuild: buildWasSuccessful }); } ) From ef566290725961d2ea59d8a38fc62d9f0d85d5a2 Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Thu, 11 Jan 2024 21:42:20 +0100 Subject: [PATCH 08/10] refactor: simplify fail logic --- src/index.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4ab448e..efdfc23 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,14 +20,10 @@ async function run() { let artifactUrl: string; let artifactType: string; - let failedState = false; - - if (process.env.CM_ARTIFACT_LINKS === undefined) { - failedState = true; - } 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) { - failedState = true; + buildWasSuccessful = false; } else { const pickedElement = cmArtifactLinks.filter( (element: any) => element.type === "apk" || element.type === "ipa" @@ -37,7 +33,7 @@ async function run() { } } - if (failedState) { + if (!buildWasSuccessful) { // should link to the workflow-log can be determined from buildId and projectId artifactUrl = buildUrl; artifactType = "logs"; @@ -84,9 +80,7 @@ async function run() { return; }, async (args) => { - if (failedState) { - buildWasSuccessful = false; - } + checkArtifactLinkMatchesPlatform(args, artifactType); await runTeamsProductionMessagingCommand({ projectName: args.projectName, From de8553c57d5c18ab6116a698b61911f6ac1b4926 Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Thu, 11 Jan 2024 21:43:47 +0100 Subject: [PATCH 09/10] refactor: improve message contents --- src/commands/TeamsProductionMessaging.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/TeamsProductionMessaging.ts b/src/commands/TeamsProductionMessaging.ts index 512e40a..436c513 100644 --- a/src/commands/TeamsProductionMessaging.ts +++ b/src/commands/TeamsProductionMessaging.ts @@ -25,12 +25,12 @@ export async function runTeamsProductionMessagingCommand(options: TeamsProductio const storeName = platformIdentifier === "IOS" ? "App Store" : "Google Play Store"; const productionMessage = options.successfulBuild ? `New release is now available in the ${storeName}` - : "New release failed to build"; + : "New release could not be created."; const messageContents = { title: options.successfulBuild ? `${options.projectName}: New release is now available in the ${storeName} [${platformIdentifier}]` - : `${options.projectName}: New release failed to build [${platformIdentifier}]`, + : `${options.projectName}: New release could not be created [${platformIdentifier}]`, summary: `New Release - ${platformIdentifier}`, text: `New Release: #${options.buildNumber} - ${platformIdentifier}
${productionMessage}`, potentialAction: [ From 68fb0eaaf9d4f7cdbb74da337225be12e354f450 Mon Sep 17 00:00:00 2001 From: Jakob Erben Date: Thu, 11 Jan 2024 21:44:56 +0100 Subject: [PATCH 10/10] chore: update readMe --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1139e09..e36c125 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ It will execute both possible command: - teams-develop (in failed/successful state/without artifacts) - teams-production (in failed/successful state/without artifacts) -→ This will result in 4(2 failed/2 successful) messages being sent to the specified teams channel as well as 2 exceptions exceptions. +→ This will result in 6(4 failed/2 successful) messages being sent to the specified teams channel. ### Calling the test script