Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/develop_actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jobs:
with:
GITHUB_TOKEN: ${{secrets.TOKEN}}
SLACK_WEBHOOK_URL: ${{secrets.SLACK_WEBHOOK_URL}}
DESTINATION_BRANCH: staging
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ inputs:
SLACK_WEBHOOK_URL:
description: 'Slack webhook'
required: true
DESTINATION_BRANCH:
description: 'destination branch'
required: true
runs:
using: 'node16'
main: './src/develop_actions.js'
Expand Down
146 changes: 117 additions & 29 deletions src/develop_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,77 @@ const core = require("@actions/core");

const GITHUB_TOKEN = core.getInput("GITHUB_TOKEN");
const SLACK_WEBHOOK_URL = core.getInput("SLACK_WEBHOOK_URL");
const DESTINATION_BRANCH = core.getInput("DESTINATION_BRANCH");
const octokit = github.getOctokit(GITHUB_TOKEN);
const { context = {} } = github;

const run = async () => {
const branch_name = context.payload?.head_commit?.message
?.split("from")[1]
.split("\n")[0]
?.split("/")
.slice(1)
.join("/");

// fetching commits
let commits = "";
try {
const branch_name = context.payload?.head_commit?.message
?.split("from")[1]
.split("\n")[0]
?.split("/")
.slice(1)
.join("/");
const compare_commits = await octokit.request(
`GET /repos/${context.payload?.repository?.full_name}/compare/${DESTINATION_BRANCH}...${branch_name}`,
{
owner: context.payload?.repository?.owner?.login,
repo: context.payload?.repository?.name,
base: DESTINATION_BRANCH,
head: branch_name,
}
);

let commits = "";

context.payload?.commits?.forEach((e, i) => {
if (compare_commits?.data?.commits?.length === 0) {
commits = "";
return;
}

compare_commits?.data?.commits?.forEach((e, i) => {
if (
!e.message.includes("Merge") &&
!e.message.includes("Merged") &&
!e.message.includes("skip") &&
!e.message.includes("Skip")
!e?.commit?.message.includes("Merge") &&
!e?.commit?.message.includes("Merged") &&
!e?.commit?.message.includes("skip") &&
!e?.commit?.message.includes("Skip")
)
commits =
i === 0 ? "> " + e.message : commits + "\n\n" + "> " + e.message;
i === 0
? "> " + e.commit.message
: commits + "\n\n" + "> " + e.commit.message;
});
} catch (error) {
console.log(error?.message);
}

// attempt to create PR
try {
let options = {
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: ":sparkles: New notification sent from github actions",
emoji: true,
},
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: `> Pull request was created from ${branch_name} to ${DESTINATION_BRANCH}`,
},
],
},
],
};
const createpr = await octokit.request(
`POST /repos/${context.payload?.repository?.full_name}/pulls`,
{
Expand All @@ -37,41 +83,83 @@ const run = async () => {
title: branch_name,
body: commits,
head: branch_name,
base: "staging",
base: DESTINATION_BRANCH,
}
);
console.log("createPr", createpr?.data);
if (createpr?.data) {
axios
.post(
`${SLACK_WEBHOOK_URL}`,
`PR from ${branch_name} to staging was created successfully`
)
.post(SLACK_WEBHOOK_URL, JSON.stringify(options))
.then((response) => {
console.log("SUCCEEDED: Sent slack webhook", response.data);
resolve(response.data);
})
.catch((error) => {
console.log("FAILED: Send slack webhook", error);
reject(new Error("FAILED: Send slack webhook"));
});
return;
}
const compare_commits = await octokit.request(
`GET /repos/${context.payload?.repository?.full_name}/compare/staging...${branch_name}`,
{
} catch (error) {
console.log("error", error?.message);
}

// update PR
try {
const options = {
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: ":sparkles: New notification sent from github actions",
emoji: true,
},
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: `> Pull request was updated from ${branch_name} to ${DESTINATION_BRANCH}`,
},
],
},
],
};
// fetching existing PR
const existing_pr = await octokit.rest.pulls.list({
owner: context.payload?.repository?.owner?.login,
repo: context.payload?.repository?.name,
state: "open",
head: branch_name,
base: DESTINATION_BRANCH,
});

if (existing_pr?.data) {
// update pr
const update_pr = await octokit.rest.pulls.update({
pull_number: existing_pr?.data[0].number,
owner: context.payload?.repository?.owner?.login,
repo: context.payload?.repository?.name,
base: "staging",
title: branch_name,
body: commits,
head: branch_name,
base: DESTINATION_BRANCH,
});
if (update_pr.data) {
// send slack notification
axios
.post(SLACK_WEBHOOK_URL, JSON.stringify(options))
.then((response) => {
console.log("SUCCEEDED: Sent slack webhook", response.data);
})
.catch((error) => {
console.log("FAILED: Send slack webhook", error);
});
return;
}
);
console.log(compare_commits);
}
} catch (error) {
console.log("error", error?.message);
}
};

run();

// $GITHUB_REF would look like (refs/pull/33/merge), $GITHUB_HEAD_REF would just store the source branch name while $GITHUB_BASE_REF would represent RP destination branch. Maybe you can update your answer to fallback to $GITHUB_HEAD_REF
// ${GITHUB_REF##*/}