-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
94a7d41
commit 74682c5
Showing
4 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |