From 74682c583e21909b8e6298ae161c6d9e2aff02fd Mon Sep 17 00:00:00 2001 From: Jakob Erben <34318568+erbenjak@users.noreply.github.com> Date: Wed, 22 Nov 2023 13:26:20 +0100 Subject: [PATCH] Feature/Teams command (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * adding teams messaging feature via webhook * fix: removing wrong deactivation of provenance * refactor: correcting formating of the files * refactor: correcting file name * chore: removing unwanted file * refactor: reformatting index file * chore: increase version * refactor: correct folder name * refactor: fix folder name in import * refactor: proper reformat * refactor: improve promise handling * refactor: improve output-message * refactor: restructure for better readability * feat: define choices for platform argument * refactor: move platform choices to definition * refactor: cleanup --------- Co-authored-by: Julian König <33655937+jkoenig134@users.noreply.github.com> Co-authored-by: Julian König --- package-lock.json | 4 +- package.json | 5 ++- src/commands/TeamsMessaging.ts | 81 ++++++++++++++++++++++++++++++++++ src/index.ts | 23 ++++++++-- 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 src/commands/TeamsMessaging.ts diff --git a/package-lock.json b/package-lock.json index 3803539..582e54c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@js-soft/codemagic-tools", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@js-soft/codemagic-tools", - "version": "0.0.1", + "version": "0.0.2", "license": "MIT", "dependencies": { "axios": "^1.6.2", diff --git a/package.json b/package.json index 3c45448..ad3b101 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@js-soft/codemagic-tools", - "version": "0.0.1", + "version": "0.0.2", "description": "Codemagic extended tooling", "homepage": "https://github.com/js-soft/codemagic-tools#readme", "bugs": { @@ -21,6 +21,9 @@ "lint:eslint": "eslint --ext ts ./src", "lint:prettier": "prettier --check ." }, + "files": [ + "dist" + ], "dependencies": { "axios": "^1.6.2", "yargs": "^17.7.2" diff --git a/src/commands/TeamsMessaging.ts b/src/commands/TeamsMessaging.ts new file mode 100644 index 0000000..c52d4a4 --- /dev/null +++ b/src/commands/TeamsMessaging.ts @@ -0,0 +1,81 @@ +import axios from "axios" +import yargs from "yargs" + +export interface TeamsMessagingOptions { + webhook: string + platform: string + artifactUrl: string + wasBuildSuccessful: boolean +} + +export class TeamsMessaging { + public async run(options: TeamsMessagingOptions): Promise { + if (!this.isUrlValid(options.webhook)) { + console.error("The given webhook is not valid.") + process.exit(1) + } + + if (!this.isUrlValid(options.artifactUrl)) { + console.error("The given artifactUrl is not valid.") + process.exit(1) + } + + const statusIdentifier = options.wasBuildSuccessful ? "Successful" : "Failed" + const platformIdentifier = options.platform.toUpperCase() + + const messageContents = { + title: `New ${statusIdentifier.toLocaleLowerCase()} codemagic build - ${platformIdentifier}`, + summary: `${statusIdentifier} build - ${platformIdentifier}`, + text: options.wasBuildSuccessful + ? "The newly released version did build and is now available as an artifact." + : "A problem occurred while building the newly released version. The corresponding logs are available.", + potentialAction: [ + { + "@type": "OpenUri", + name: options.wasBuildSuccessful ? "Download Build" : "Read Logs", + targets: [{ os: "default", uri: options.artifactUrl }] + } + ] + } + + await axios.post(options.webhook, messageContents).catch((_) => { + console.log("Could not send message to teams channel.") + process.exit(1) + }) + } + + public parseCLIOptions(argv: yargs.Argv<{}>): TeamsMessagingOptions | Promise { + return argv + .option("platform", { + description: "identifier of the platform for which the build was created", + required: true, + type: "string", + choices: ["ios", "android"] + }) + .option("artifactUrl", { + description: "download link for the generated artifact (logs or build)", + required: true, + type: "string" + }) + .option("wasBuildSuccessful", { + description: "status of the finished build", + required: true, + type: "boolean" + }) + .option("webhook", { + description: "the webhook of the teams channel, that should receive the message", + required: true, + type: "string" + }).argv + } + + private isUrlValid(url: string): boolean { + try { + // eslint-disable-next-line no-new + new URL(url) + return true + } catch (err) { + return false + } + } +} diff --git a/src/index.ts b/src/index.ts index 91ac70b..89123cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,24 @@ #!/usr/bin/env node import yargs from "yargs" +import { TeamsMessaging } from "./commands/TeamsMessaging" -const argv = yargs(process.argv.slice(2)) - .options({ a: { type: "boolean", default: false } }) - .parseSync() +async function run() { + await yargs(process.argv.slice(2)) + .command("teams", "This command is used to send a teams message via a passed webhook", async (args) => { + const teamsMessagingCommand = new TeamsMessaging() + const options = await teamsMessagingCommand.parseCLIOptions(args) + await teamsMessagingCommand.run(options) + return options + }) + .demand(1, "Must provide a valid command from the ones listed above.") + .scriptName("jscm") + .parseAsync() +} -console.log(argv) +run() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error) + process.exit(1) + })