Skip to content

Commit

Permalink
adds new test for commits
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackBz committed Jan 15, 2024
1 parent 58f201e commit e759a35
Show file tree
Hide file tree
Showing 10 changed files with 157,216 additions and 5,025 deletions.
147,529 changes: 147,529 additions & 0 deletions .yarn/releases/yarn-1.22.19.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-1.22.19.cjs
52 changes: 32 additions & 20 deletions discord-commits/api.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
import st from "stjs";

export async function loadTemplate(name) {
try {
return (await import(`./templates/${name}.js`)).default
} catch (err) {
return (await import(`./templates/plain.js`)).default
}
try {
return (await import(`./templates/${name}.js`)).default;
} catch (err) {
return (await import(`./templates/plain.js`)).default;
}
}

export function stringToBoolean(string) {
switch (string.toLowerCase().trim()) {
case "false": case "no": case "0": case "": case null: return false;
default: return true;
}
switch (string.toLowerCase().trim()) {
case "false":
case "no":
case "0":
case "":
case null:
return false;
default:
return true;
}
}

export function stringOrFalse(string) {
switch (string.toLowerCase().trim()) {
case "false": case "no": case "0": case "": case null: return false;
default: return string;
}
switch (string.toLowerCase().trim()) {
case "false":
case "no":
case "0":
case "":
case null:
return false;
default:
return string;
}
}

export function createCommit(commit) {
const messageSections = commit.message.split("\n\n")
return {
title: messageSections[0],
description: messageSections.slice(1).join("\n\n"),
...commit
}
const messageSections = commit.message.split("\n\n");
return {
title: messageSections[0],
description: messageSections.slice(1).join("\n\n"),
...commit,
};
}

export function parseTemplate(data, template) {
return st.select(data).transformWith(template).root()
return st.select(data).transformWith(template).root();
}
70 changes: 38 additions & 32 deletions discord-commits/index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,64 @@
import core from "@actions/core";
import github from "@actions/github";
import fetch from "node-fetch";
import { createCommit, loadTemplate, parseTemplate, stringOrFalse, stringToBoolean } from "./api.js";
import defaultPayload from "./defaults/payload-commits.js"
import {
createCommit,
loadTemplate,
parseTemplate,
stringOrFalse,
stringToBoolean,
} from "./api.js";

const templateName = core.getInput("template") || "plain";
const template = await loadTemplate(templateName)
const template = await loadTemplate(templateName);

const message = core.getInput("message") || template.message
const message = core.getInput("message") || template.message;
const webhook = core.getInput("webhook");
const lastCommitOnly = stringToBoolean(core.getInput("last-commit-only"))
const extraEmbeds = stringToBoolean(core.getInput("include-extras")) ? template.extras || [] : []
const lastCommitOnly = stringToBoolean(core.getInput("last-commit-only"));
const extraEmbeds = stringToBoolean(core.getInput("include-extras"))
? template.extras || []
: [];

const embed = stringOrFalse(core.getInput("embed")) || JSON.stringify(template.embed)
const embed =
stringOrFalse(core.getInput("embed")) || JSON.stringify(template.embed);

const DATA = {
env: { ...process.env },
github: { ...github },
}

github.context.payload.commits ??= defaultPayload

};
// console.log(github.context.payload.commits);
if (lastCommitOnly) {
github.context.payload.commits = github.context.payload.commits.slice(-1)
github.context.payload.commits = github.context.payload.commits.slice(-1);
}

let embeds = github.context.payload.commits.map(commit => {
return parseTemplate({
...DATA,
commit: createCommit(commit),
}, JSON.parse(embed));
})

embeds = embeds.concat(extraEmbeds.map(embed => parseTemplate(DATA, embed)))

let embeds = github.context.payload.commits.map((commit) => {
const titledCommmit = createCommit(commit);
return parseTemplate(
{
...DATA,
commit: titledCommmit,
},
JSON.parse(embed)
);
});
embeds = embeds.concat(extraEmbeds.map((embed) => parseTemplate(DATA, embed)));
console.log(embeds);
const payload = {
content: parseTemplate(DATA, message),
embeds: embeds.filter(x => x)
}
embeds: embeds.filter((x) => x),
};

try {
await fetch(`${webhook}?wait=true`, {
method: 'POST',
method: "POST",
headers: {
"Content-Type": "application/json",
"X-GitHub-Event": "push",
},
body: JSON.stringify(payload)
})
body: JSON.stringify(payload),
});
} catch (err) {
console.error(err)
core.error(err)
core.setFailed(
"Message :",
err.response ? err.response.data : err.message
);
console.error(err);
core.error(err);
core.setFailed("Message :", err.response ? err.response.data : err.message);
}
Loading

0 comments on commit e759a35

Please sign in to comment.