Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: use web core slack app #4247

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ GH_TOKEN=
GATSBY_GOOGLE_CLIENT_ID=
FIGMA_TOKEN=
NPM_TOKEN=
SLACK_TOKEN=
SLACK_API_READ_PLZ_ORBIT=
SLACK_CHANGELOG_WEBHOOK_URL=
GATSBY_ORBIT_STORAGE_PATH=
ORBIT_STORAGE_PATH=
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} # must be of type Automation to create releases
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }}
SLACK_CHANGELOG_WEBHOOK_URL: ${{ secrets.SLACK_CHANGELOG_WEBHOOK_URL }}
run: |
echo "scope=@kiwicom" > ~/.npmrc
echo "access=public" >> ~/.npmrc
Expand Down
48 changes: 24 additions & 24 deletions scripts/post-changelog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ import gitDiffParser from "gitdiff-parser";
const CHANNEL = "orbit-react";
const COLOR_CORE = "#00A58E";
const PACKAGES = ["orbit-components", "orbit-tailwind-preset", "orbit-design-tokens"];
const SLACK_API_POST_RELEASE_MESSAGE = "https://slack.com/api/chat.postMessage";
const SLACK_CHANGELOG_WEBHOOK_URL = process.env.SLACK_CHANGELOG_WEBHOOK_URL ?? "";

function getTitle(pkg) {
return `New ${pkg} release 🚀`;
}

const apiRequest = async ({ method = "GET", url, body }) =>
fetch(url, {
method,
body,
headers: {
"Content-Type": "application/json; charset=utf-8",
Authorization: `Bearer ${process.env.SLACK_TOKEN}`,
Accept: "application/json",
},
});
async function sendToWebhook({ content, webhookUrl }) {
await fetch(webhookUrl, {
method: "POST",
body: JSON.stringify(content),
})
.then(res => {
if (res.status !== 200) {
throw new Error(`${res.status} ${res.statusText}`);
}
})
.catch(err => {
console.error(err);
process.exit(1);
});
}

function format(str, package_, prefix = "@kiwicom") {
const output = str
Expand Down Expand Up @@ -58,19 +62,17 @@ function changelogPath(package_) {
async function postSlackNotification(changelog, package_) {
try {
$.verbose = false;
const res = await apiRequest({
url: SLACK_API_POST_RELEASE_MESSAGE,
method: "POST",
body: JSON.stringify({
channel: CHANNEL,
const res = await sendToWebhook({
webhookUrl: SLACK_CHANGELOG_WEBHOOK_URL,
content: {
attachments: [
{
title: getTitle(package_),
text: changelog,
color: COLOR_CORE,
},
],
}),
},
});
return res;
} catch (err) {
Expand All @@ -81,17 +83,15 @@ async function postSlackNotification(changelog, package_) {
return undefined;
}

async function configureSlackToken() {
async function configureWebhookURL() {
try {
dotenv.config({
allowEmptyValues: true,
example: ".env.example",
});
} catch (err) {
if (/SLACK_TOKEN/g.test(err.message)) {
throw new Error(
"Slack token is missing in the .env file, please add it.\nLearn how to create one: https://slack.com/intl/en-cz/help/articles/215770388-Create-and-regenerate-API-tokens",
);
if (/SLACK_CHANGELOG_WEBHOOK_URL/g.test(err.message)) {
throw new Error("SLACK_CHANGELOG_WEBHOOK_URL is not set");
}
}
}
Expand All @@ -113,7 +113,7 @@ async function publishChangelog(package_) {
if (argv.dry) {
console.info(formattedChangelog);
} else {
await configureSlackToken();
await configureWebhookURL();
await postSlackNotification(slackifiedChangelog, package_);
}
} catch (err) {
Expand Down
Loading