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

Release To Main #28

Merged
merged 5 commits into from
Jul 4, 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
70 changes: 28 additions & 42 deletions .github/workflows/merged-to-main.yaml
Original file line number Diff line number Diff line change
@@ -1,68 +1,54 @@
name: Merged to Main

on:
# Triggered when a pull request is closed and merged into the main branch
# Trigger this workflow when a pull request is closed and merged into the main branch
pull_request:
branches:
- main
types:
- closed
# Manually triggered workflow with inputs for title and body
# Allow manual triggering of the workflow with a PR number input
workflow_dispatch:
inputs:
pr_number:
description: "PR Number"
description: "PR Number to use (optional for manual triggers)"
default: "200"
required: true

env:
# Slack webhook secret
# Slack webhook secret for sending release notes
CC_RELEASE_SLACK_WH: ${{ secrets.CC_RELEASE_SLACK_WH }}

jobs:
# Job to handle merged pull requests
merged_to_main:
send_release_notes:
runs-on: ubuntu-latest
environment: cc_pr_merge
# Run the job when a pull request is merged into main and not triggered manually && github.event.pull_request.merged
if: ${{ github.event_name != 'workflow_dispatch' }}
env:
# Environment variable to store pull request details
PR_NUMBER: ${{ github.event.pull_request.number }}

# This condition ensures the job runs only when a pull request is merged or the workflow is manually triggered
if: ${{ (github.event_name != 'workflow_dispatch' && github.event.pull_request.merged) || github.event_name == 'workflow_dispatch' }}

steps:
# Checkout repository action
# Step to check out the repository
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Octakit
run: |
npm i -D @octokit/rest
# Sending release notes to cc-changelog channel
- name: Sending Release Notes to cc-changelog slack channel
uses: actions/github-script@v7
with:
script: |
const {sendReleaseNotes} = require('./.github/workflows/src/send-slack.js')
sendReleaseNotes(process.env.PR_NUMBER,process.env.CC_RELEASE_SLACK_WH)
# Job to handle manually triggered workflow_dispatch events
workflow_dispatch_event:
runs-on: ubuntu-latest
environment: cc_pr_merge
# Run the job when the workflow is manually triggered
if: ${{ github.event_name == 'workflow_dispatch' }}
steps:
# Checkout repository action
- name: Check repository
uses: actions/checkout@v4
- name: Install Octakit
run: |
npm i -D @octokit/rest
# Sending release notes to cc-changelog channel

# Step to install Octokit
- name: Install Octokit
run: npm i -D @octokit/rest

# Step to set PR_NUMBER environment variable from the pull request event
- name: Set PR Number
if: ${{ github.event_name != 'workflow_dispatch' }}
run: echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV

# Step to set PR_NUMBER environment variable from the workflow dispatch input
- name: Set PR Number from Input
if: ${{ github.event_name == 'workflow_dispatch' }}
run: echo "PR_NUMBER=${{ github.event.inputs.pr_number }}" >> $GITHUB_ENV

# Step to send release notes to the cc-changelog Slack channel
- name: Sending Release Notes to cc-changelog slack channel
uses: actions/github-script@v7
# Set environment variables for repository URL, PR number,PR title, and PR description
env:
PR_NUMBER: ${{github.event.inputs.pr_number}}
with:
script: |
const {sendReleaseNotes} = require('./.github/workflows/src/send-slack.js')
sendReleaseNotes(process.env.PR_NUMBER,process.env.CC_RELEASE_SLACK_WH)
const { sendReleaseNotes } = require('./.github/workflows/src/send-slack.js')
sendReleaseNotes(process.env.PR_NUMBER, process.env.CC_RELEASE_SLACK_WH)
13 changes: 8 additions & 5 deletions .github/workflows/src/send-slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { parseTextToSlackBlocks } = require("./helpers");

// Those env variables are set by an github action automatically
const [owner, repo]=process.env.GITHUB_REPOSITORY?.split('/') || '';
const auth = process.env.GH_TOKEN;
const auth = process.env.GITHUB_TOKEN;

/**
* Function to send Slack message
Expand All @@ -11,7 +11,8 @@ const auth = process.env.GH_TOKEN;
* @returns {Promise} - Promise representing the result of the Slack message sending process
*/
const sendReleaseNotes = async (prNumber, slackWebHookURL) => {
console.log({ owner, repo, pr_number: prNumber });

console.log({ owner, repo, pr_number: prNumber, slackWebHookURL});
try {
prNumber = parseInt(prNumber);
const { Octokit } = await import("@octokit/rest");
Expand Down Expand Up @@ -42,23 +43,25 @@ const sendReleaseNotes = async (prNumber, slackWebHookURL) => {
},
];

const slackBodyBlocks = {
const slackBodyBlocks = JSON.stringify({
blocks: [...titleBlocks, ...formattedBodyBlocks],
};
});
console.log("Message", slackBodyBlocks);

// Send message to Slack webhook
const result = await fetch(slackWebHookURL, {
method: "POST",
body: JSON.stringify(slackBodyBlocks),
body: slackBodyBlocks,
headers: {
"Content-type": "application/json",
},
}).catch(console.error);

if (result.status === 200) console.log("Slack Message sent");
else {
console.log(`Slack Message not sent ${result.status}:${result.statusText}`)
}
console.log(result);
return result;
} catch (e) {
console.log(e);
Expand Down