-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
157,216 additions
and
5,025 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
yarnPath: .yarn/releases/yarn-1.22.19.cjs |
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,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(); | ||
} |
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,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); | ||
} |
Oops, something went wrong.