-
-
Notifications
You must be signed in to change notification settings - Fork 443
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
1 changed file
with
22 additions
and
31 deletions.
There are no files selected for viewing
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,87 +1,78 @@ | ||
import * as core from "@actions/core"; | ||
import * as github from "@actions/github"; | ||
import fetch from 'node-fetch'; | ||
import * as core from '@actions/core'; | ||
import * as github from '@actions/github'; | ||
|
||
const GITHUB_TOKEN = process.env.GITHUB_TOKEN; | ||
const WEBHOOK_URL = process.env.DISCORD_URQL_WEBHOOK_URL; | ||
const WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL; | ||
|
||
const octokit = github.getOctokit(GITHUB_TOKEN); | ||
|
||
const formatBody = (input) => { | ||
const titleRe = /(?:^|\n)#+[^\n]+/g; | ||
const updatedDepsRe = /\n-\s*Updated dependencies[\s\S]+\n(\n\s+-[\s\S]+)*/ig; | ||
const updatedDepsRe = /\n-\s*Updated dependencies[\s\S]+\n(\n\s+-[\s\S]+)*/gi; | ||
const markdownLinkRe = /\[([^\]]+)\]\(([^\)]+)\)/g; | ||
const creditRe = new RegExp(`Submitted by (?:undefined|${markdownLinkRe.source})`, 'ig'); | ||
const repeatedNewlineRe = /(\n[ ]*)+/g; | ||
const repeatedNewlineRe = /(?:\n[ ]*)*(\n[ ]*)/g; | ||
return input | ||
.replace(titleRe, '') | ||
.replace(updatedDepsRe, '') | ||
.replace(creditRe, (_match, text, url) => { | ||
if (!text || /@kitten|@JoviDeCroock/i.test(text)) | ||
return ''; | ||
if (!text || /@kitten|@JoviDeCroock/i.test(text)) return ''; | ||
return `Submitted by [${text}](${url})`; | ||
}) | ||
.replace(markdownLinkRe, (_match, text, url) => { | ||
return `[${text}](<${url}>)`; | ||
}) | ||
.replace(repeatedNewlineRe, '\n') | ||
.replace(markdownLinkRe, (_match, text, url) => `[${text}](<${url}>)`) | ||
.replace(repeatedNewlineRe, (_match, text) => text ? ` ${text}` : '\n') | ||
.trim(); | ||
}; | ||
|
||
async function getReleaseBody(name, version) { | ||
const tag = `${name}@${version}`; | ||
const result = await octokit.rest.repos.getReleaseByTag({ | ||
owner: 'urql-graphql', | ||
repo: 'urql', | ||
tag, | ||
}); | ||
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/'); | ||
const result = await octokit.rest.repos.getReleaseByTag({ owner, repo, tag }); | ||
|
||
const release = result.status === 200 ? result.data : undefined; | ||
if (!release || !release.body) return; | ||
|
||
const title = ':package: [${tag}](<${release.html_url}>)\n'; | ||
const title = `:package: [${tag}](<${release.html_url}>)`; | ||
const body = formatBody(release.body); | ||
if (!body) return; | ||
|
||
return `${title}\n${body}`; | ||
} | ||
|
||
async function main() { | ||
const inputPackages = core.getInput("publishedPackages"); | ||
const inputPackages = core.getInput('publishedPackages'); | ||
let packages; | ||
|
||
try { | ||
packages = JSON.parse(inputPackages) | ||
packages = JSON.parse(inputPackages); | ||
} catch (e) { | ||
console.error('invalid JSON in publishedPackages input.') | ||
console.error('invalid JSON in publishedPackages input.'); | ||
return; | ||
} | ||
|
||
// Get releases | ||
const releasePromises = packages.map(entry => { | ||
const releasePromises = packages.map((entry) => { | ||
return getReleaseBody(entry.name, entry.version); | ||
}); | ||
|
||
const content = (await Promise.allSettled(releasePromises)) | ||
.map(x => x.status === 'fulfilled' && x.value) | ||
.map((x) => x.status === 'fulfilled' && x.value) | ||
.filter(Boolean) | ||
.join('\n\n'); | ||
|
||
// Send message through a discord webhook or bot | ||
const response = fetch(WEBHOOK_URL, { | ||
const response = await fetch(WEBHOOK_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ content }) | ||
}) | ||
body: JSON.stringify({ content }), | ||
}); | ||
|
||
if (!response.ok) { | ||
console.log('Something went wrong while sending the discord webhook.'); | ||
return; | ||
console.error('Something went wrong while sending the discord webhook.', response.status); | ||
console.error(await response.text()); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
main().then().catch(console.error) | ||
main().then().catch(console.error); |