Skip to content

Commit

Permalink
Feature/Teams command (#2)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Julian König <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2023
1 parent 94a7d41 commit 74682c5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
Expand Down
81 changes: 81 additions & 0 deletions src/commands/TeamsMessaging.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<TeamsMessagingOptions> {
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
}
}
}
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 74682c5

Please sign in to comment.