From 3fd5a3fdc9e58e08f87bcc2f9f24983b82fc17ed Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Apr 2022 23:37:13 +0300 Subject: [PATCH 1/9] test sending random slack notification --- src/develop_actions.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index 8a42f36..42ebb8c 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -25,7 +25,6 @@ const run = async () => { head: branch_name, } ); - console.log("compare commit", compare_commits?.data?.commits); let commits = ""; @@ -37,7 +36,9 @@ const run = async () => { !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; }); // fetch pr from branch_name to staging to check if it exists @@ -55,7 +56,7 @@ const run = async () => { type: "header", text: { type: "plain_text", - text: `:sparkles: PR was created from ${branch_name} to satging`, + text: `:sparkles: PR was created from ${branch_name} to staging`, emoji: true, }, }, @@ -63,7 +64,7 @@ const run = async () => { type: "context", elements: [ { - text: `commits`, + text: commits, type: "mrkdwn", }, ], From 07241952e0fdf365502e267b1f04887d51bbab6f Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Apr 2022 23:38:00 +0300 Subject: [PATCH 2/9] test sending random slack notification --- src/develop_actions.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index 42ebb8c..b41fe7a 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -52,26 +52,24 @@ const run = async () => { const options = { blocks: [ - { - type: "header", - text: { - type: "plain_text", - text: `:sparkles: PR was created from ${branch_name} to staging`, - emoji: true, - }, - }, + // { + // type: "header", + // text: { + // type: "plain_text", + // text: `:sparkles: PR was created from ${branch_name} to staging`, + // emoji: true, + // }, + // }, { type: "context", elements: [ { - text: commits, type: "mrkdwn", + text: `:sparkles: PR was created from ${branch_name} to staging`, + emoji: true, }, ], }, - { - type: "divider", - }, ], }; From ce1d86b2ddc08afd9025a705e336d53222e48ce6 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Apr 2022 23:58:18 +0300 Subject: [PATCH 3/9] test update pr --- src/develop_actions.js | 115 ++++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 42 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index b41fe7a..3e00a1e 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -8,14 +8,16 @@ const octokit = github.getOctokit(GITHUB_TOKEN); const { context = {} } = github; const run = async () => { - try { - const branch_name = context.payload?.head_commit?.message - ?.split("from")[1] - .split("\n")[0] - ?.split("/") - .slice(1) - .join("/"); + const branch_name = context.payload?.head_commit?.message + ?.split("from")[1] + .split("\n")[0] + ?.split("/") + .slice(1) + .join("/"); + // fetching commits + let commits = ""; + try { const compare_commits = await octokit.request( `GET /repos/${context.payload?.repository?.full_name}/compare/staging...${branch_name}`, { @@ -28,6 +30,11 @@ const run = async () => { let commits = ""; + if (compare_commits?.data?.commits?.length === 0) { + commits = ""; + return; + } + compare_commits?.data?.commits?.forEach((e, i) => { if ( !e?.commit?.message.includes("Merge") && @@ -44,35 +51,33 @@ const run = async () => { // fetch pr from branch_name to staging to check if it exists // if pr exists, update // if not create + } catch (error) { + console.log(error?.message); + } - if (compare_commits?.data?.commits?.length === 0) { - console.log("no changes"); - return; - } - + // attempt to create PR + try { const options = { blocks: [ - // { - // type: "header", - // text: { - // type: "plain_text", - // text: `:sparkles: PR was created from ${branch_name} to staging`, - // emoji: true, - // }, - // }, + { + type: "header", + text: { + type: "plain_text", + text: ``, + emoji: true, + }, + }, { type: "context", elements: [ { type: "mrkdwn", - text: `:sparkles: PR was created from ${branch_name} to staging`, - emoji: true, + text: `<:sparkles: PR was created from ${branch_name} to staging>`, }, ], }, ], }; - const createpr = await octokit.request( `POST /repos/${context.payload?.repository?.full_name}/pulls`, { @@ -85,32 +90,58 @@ const run = async () => { } ); if (createpr?.data) { - 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); - }); + console.log("pr created successfully"); + // 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); + // }); } else { // update existing pr console.log("pr exists"); - axios - .post( - SLACK_WEBHOOK_URL, - JSON.stringify(`PR from ${branch_name} to staging already exist`) - ) - .then((response) => { - console.log("SUCCEEDED: Sent slack webhook", response.data); - }) - .catch((error) => { - console.log("FAILED: Send slack webhook", error); - }); + // axios + // .post( + // SLACK_WEBHOOK_URL, + // JSON.stringify(`PR from ${branch_name} to staging already exist`) + // ) + // .then((response) => { + // console.log("SUCCEEDED: Sent slack webhook", response.data); + // }) + // .catch((error) => { + // console.log("FAILED: Send slack webhook", error); + // }); } } catch (error) { console.log("error", error?.message); } + + // update PR + try { + // fetching existing PR + const existing_pr = await this.octokit.rest.pulls.list({ + owner: context.payload?.repository?.owner?.login, + repo: context.payload?.repository?.name, + state: "open", + head: branch_name, + base: "staging", + }); + console.log("existing PR", existing_pr); + // update pr + await this.octokit.rest.pulls.update({ + pull_number: existing_pr?.data[0].number, + owner: context.payload?.repository?.owner?.login, + repo: context.payload?.repository?.name, + title: branch_name, + body: commits, + head: branch_name, + base: "staging", + }); + } catch (error) { + console.log("error", error?.message); + } }; run(); From e07a19c3b8366f5814c584ca34b5cc2b223fe988 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 23 Apr 2022 00:00:22 +0300 Subject: [PATCH 4/9] test update pr --- src/develop_actions.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index 3e00a1e..bd1a9b8 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -121,7 +121,7 @@ const run = async () => { // update PR try { // fetching existing PR - const existing_pr = await this.octokit.rest.pulls.list({ + const existing_pr = await octokit.rest.pulls.list({ owner: context.payload?.repository?.owner?.login, repo: context.payload?.repository?.name, state: "open", @@ -130,7 +130,7 @@ const run = async () => { }); console.log("existing PR", existing_pr); // update pr - await this.octokit.rest.pulls.update({ + 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, @@ -139,6 +139,7 @@ const run = async () => { head: branch_name, base: "staging", }); + console.log(update_pr?.data); } catch (error) { console.log("error", error?.message); } From 4f102fb4f843500a88b27e58779ca7836bc4b92c Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 23 Apr 2022 00:05:05 +0300 Subject: [PATCH 5/9] test update pr --- src/develop_actions.js | 92 ++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index bd1a9b8..d6a1629 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -57,16 +57,8 @@ const run = async () => { // attempt to create PR try { - const options = { + let options = { blocks: [ - { - type: "header", - text: { - type: "plain_text", - text: ``, - emoji: true, - }, - }, { type: "context", elements: [ @@ -90,29 +82,15 @@ const run = async () => { } ); if (createpr?.data) { - console.log("pr created successfully"); - // 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); - // }); - } else { - // update existing pr - console.log("pr exists"); - // axios - // .post( - // SLACK_WEBHOOK_URL, - // JSON.stringify(`PR from ${branch_name} to staging already exist`) - // ) - // .then((response) => { - // console.log("SUCCEEDED: Sent slack webhook", response.data); - // }) - // .catch((error) => { - // console.log("FAILED: Send slack webhook", error); - // }); + 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; } } catch (error) { console.log("error", error?.message); @@ -120,6 +98,19 @@ const run = async () => { // update PR try { + const options = { + blocks: [ + { + type: "context", + elements: [ + { + type: "mrkdwn", + text: `<:sparkles: PR was updated from ${branch_name} to staging>`, + }, + ], + }, + ], + }; // fetching existing PR const existing_pr = await octokit.rest.pulls.list({ owner: context.payload?.repository?.owner?.login, @@ -128,18 +119,31 @@ const run = async () => { head: branch_name, base: "staging", }); - console.log("existing PR", existing_pr); - // 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, - title: branch_name, - body: commits, - head: branch_name, - base: "staging", - }); - console.log(update_pr?.data); + + 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, + title: branch_name, + body: commits, + head: branch_name, + base: "staging", + }); + } + 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; + } } catch (error) { console.log("error", error?.message); } From 0010d22507ea4b5325f153a0fb3ea2ddc4a8a01a Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 23 Apr 2022 00:07:33 +0300 Subject: [PATCH 6/9] test update pr --- src/develop_actions.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index d6a1629..2621da1 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -131,18 +131,18 @@ const run = async () => { head: branch_name, base: "staging", }); - } - 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; + 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; + } } } catch (error) { console.log("error", error?.message); From aeb67ceba393f77e1e01b87d25e8bff9220b83d8 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 23 Apr 2022 00:12:47 +0300 Subject: [PATCH 7/9] finalize develop branch actions --- src/develop_actions.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index 2621da1..08567fb 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -59,12 +59,20 @@ const run = async () => { 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: `<:sparkles: PR was created from ${branch_name} to staging>`, + text: `> :sparkles: PR was created from ${branch_name} to staging`, }, ], }, @@ -100,12 +108,20 @@ const run = async () => { 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: `<:sparkles: PR was updated from ${branch_name} to staging>`, + text: `> :sparkles: PR was updated from ${branch_name} to staging`, }, ], }, @@ -150,6 +166,3 @@ const run = async () => { }; 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##*/} From 812ff25bb2a9f5201497fc2010b3e45b6b82e724 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 23 Apr 2022 00:15:39 +0300 Subject: [PATCH 8/9] finalize develop branch actions --- .github/workflows/develop_actions.yml | 1 + action.yml | 3 +++ src/develop_actions.js | 19 ++++++++----------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.github/workflows/develop_actions.yml b/.github/workflows/develop_actions.yml index 6b361ec..6dd1eff 100644 --- a/.github/workflows/develop_actions.yml +++ b/.github/workflows/develop_actions.yml @@ -19,3 +19,4 @@ jobs: with: GITHUB_TOKEN: ${{secrets.TOKEN}} SLACK_WEBHOOK_URL: ${{secrets.SLACK_WEBHOOK_URL}} + DESTINATION_BRANCH: staging diff --git a/action.yml b/action.yml index f5270f6..f5917a0 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/src/develop_actions.js b/src/develop_actions.js index 08567fb..abda7fb 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -4,6 +4,7 @@ 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; @@ -19,11 +20,11 @@ const run = async () => { let commits = ""; try { const compare_commits = await octokit.request( - `GET /repos/${context.payload?.repository?.full_name}/compare/staging...${branch_name}`, + `GET /repos/${context.payload?.repository?.full_name}/compare/${DESTINATION_BRANCH}...${branch_name}`, { owner: context.payload?.repository?.owner?.login, repo: context.payload?.repository?.name, - base: "staging", + base: DESTINATION_BRANCH, head: branch_name, } ); @@ -47,10 +48,6 @@ const run = async () => { ? "> " + e.commit.message : commits + "\n\n" + "> " + e.commit.message; }); - - // fetch pr from branch_name to staging to check if it exists - // if pr exists, update - // if not create } catch (error) { console.log(error?.message); } @@ -72,7 +69,7 @@ const run = async () => { elements: [ { type: "mrkdwn", - text: `> :sparkles: PR was created from ${branch_name} to staging`, + text: `> :sparkles: PR was created from ${branch_name} to ${DESTINATION_BRANCH}`, }, ], }, @@ -86,7 +83,7 @@ const run = async () => { title: branch_name, body: commits, head: branch_name, - base: "staging", + base: DESTINATION_BRANCH, } ); if (createpr?.data) { @@ -121,7 +118,7 @@ const run = async () => { elements: [ { type: "mrkdwn", - text: `> :sparkles: PR was updated from ${branch_name} to staging`, + text: `> :sparkles: PR was updated from ${branch_name} to ${DESTINATION_BRANCH}`, }, ], }, @@ -133,7 +130,7 @@ const run = async () => { repo: context.payload?.repository?.name, state: "open", head: branch_name, - base: "staging", + base: DESTINATION_BRANCH, }); if (existing_pr?.data) { @@ -145,7 +142,7 @@ const run = async () => { title: branch_name, body: commits, head: branch_name, - base: "staging", + base: DESTINATION_BRANCH, }); if (update_pr.data) { // send slack notification From 35a285bb0dff2493acea59bfb2d00d55f0115da9 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 23 Apr 2022 00:18:46 +0300 Subject: [PATCH 9/9] finalize develop branch actions --- src/develop_actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index abda7fb..1d1d03d 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -69,7 +69,7 @@ const run = async () => { elements: [ { type: "mrkdwn", - text: `> :sparkles: PR was created from ${branch_name} to ${DESTINATION_BRANCH}`, + text: `> Pull request was created from ${branch_name} to ${DESTINATION_BRANCH}`, }, ], }, @@ -118,7 +118,7 @@ const run = async () => { elements: [ { type: "mrkdwn", - text: `> :sparkles: PR was updated from ${branch_name} to ${DESTINATION_BRANCH}`, + text: `> Pull request was updated from ${branch_name} to ${DESTINATION_BRANCH}`, }, ], },