From 681349de6007ecfd4ce18d6f4ed878c4a8562099 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Apr 2022 22:26:54 +0300 Subject: [PATCH 1/5] test commit --- res.js | 194 --------------------------------------------------------- 1 file changed, 194 deletions(-) diff --git a/res.js b/res.js index 81268c3..e69de29 100644 --- a/res.js +++ b/res.js @@ -1,194 +0,0 @@ -exports.response = { - after: "94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", - base_ref: null, - before: "cc8b67c069125765f31ee6908392e0439f0d04eb", - commits: [ - { - author: [Object], - committer: [Object], - distinct: false, - id: "49f9be33fb8cbf34c5a891db989e3de07c27b7a1", - message: "test commit", - timestamp: "2022-04-22T20:00:25+03:00", - tree_id: "969c2c9cb3ca68847fcf744a21df3672c153127d", - url: "https://github.com/bmsteven/demo/commit/49f9be33fb8cbf34c5a891db989e3de07c27b7a1", - }, - { - author: [Object], - committer: [Object], - distinct: true, - id: "94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", - message: - "Merge pull request #22 from bmsteven/test_branch\n\ntest commit", - timestamp: "2022-04-22T20:01:07+03:00", - tree_id: "969c2c9cb3ca68847fcf744a21df3672c153127d", - url: "https://github.com/bmsteven/demo/commit/94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", - }, - ], - compare: - "https://github.com/bmsteven/demo/compare/cc8b67c06912...94a28b830c5a", - created: false, - deleted: false, - forced: false, - head_commit: { - author: { - email: "48621248+bmsteven@users.noreply.github.com", - name: "Benedict Steven", - username: "bmsteven", - }, - committer: { - email: "noreply@github.com", - name: "GitHub", - username: "web-flow", - }, - distinct: true, - id: "94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", - message: "Merge pull request #22 from bmsteven/test_branch\n\ntest commit", - timestamp: "2022-04-22T20:01:07+03:00", - tree_id: "969c2c9cb3ca68847fcf744a21df3672c153127d", - url: "https://github.com/bmsteven/demo/commit/94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", - }, - pusher: { - email: "48621248+bmsteven@users.noreply.github.com", - name: "bmsteven", - }, - ref: "refs/heads/develop", - repository: { - allow_forking: true, - archive_url: - "https://api.github.com/repos/bmsteven/demo/{archive_format}{/ref}", - archived: false, - assignees_url: - "https://api.github.com/repos/bmsteven/demo/assignees{/user}", - blobs_url: "https://api.github.com/repos/bmsteven/demo/git/blobs{/sha}", - branches_url: - "https://api.github.com/repos/bmsteven/demo/branches{/branch}", - clone_url: "https://github.com/bmsteven/demo.git", - collaborators_url: - "https://api.github.com/repos/bmsteven/demo/collaborators{/collaborator}", - comments_url: - "https://api.github.com/repos/bmsteven/demo/comments{/number}", - commits_url: "https://api.github.com/repos/bmsteven/demo/commits{/sha}", - compare_url: - "https://api.github.com/repos/bmsteven/demo/compare/{base}...{head}", - contents_url: "https://api.github.com/repos/bmsteven/demo/contents/{+path}", - contributors_url: "https://api.github.com/repos/bmsteven/demo/contributors", - created_at: 1553584187, - default_branch: "master", - deployments_url: "https://api.github.com/repos/bmsteven/demo/deployments", - description: "demo repository", - disabled: false, - downloads_url: "https://api.github.com/repos/bmsteven/demo/downloads", - events_url: "https://api.github.com/repos/bmsteven/demo/events", - fork: false, - forks: 0, - forks_count: 0, - forks_url: "https://api.github.com/repos/bmsteven/demo/forks", - full_name: "bmsteven/demo", - git_commits_url: - "https://api.github.com/repos/bmsteven/demo/git/commits{/sha}", - git_refs_url: "https://api.github.com/repos/bmsteven/demo/git/refs{/sha}", - git_tags_url: "https://api.github.com/repos/bmsteven/demo/git/tags{/sha}", - git_url: "git://github.com/bmsteven/demo.git", - has_downloads: true, - has_issues: true, - has_pages: false, - has_projects: true, - has_wiki: true, - homepage: null, - hooks_url: "https://api.github.com/repos/bmsteven/demo/hooks", - html_url: "https://github.com/bmsteven/demo", - id: 177733530, - is_template: false, - issue_comment_url: - "https://api.github.com/repos/bmsteven/demo/issues/comments{/number}", - issue_events_url: - "https://api.github.com/repos/bmsteven/demo/issues/events{/number}", - issues_url: "https://api.github.com/repos/bmsteven/demo/issues{/number}", - keys_url: "https://api.github.com/repos/bmsteven/demo/keys{/key_id}", - labels_url: "https://api.github.com/repos/bmsteven/demo/labels{/name}", - language: "JavaScript", - languages_url: "https://api.github.com/repos/bmsteven/demo/languages", - license: null, - master_branch: "master", - merges_url: "https://api.github.com/repos/bmsteven/demo/merges", - milestones_url: - "https://api.github.com/repos/bmsteven/demo/milestones{/number}", - mirror_url: null, - name: "demo", - node_id: "MDEwOlJlcG9zaXRvcnkxNzc3MzM1MzA=", - notifications_url: - "https://api.github.com/repos/bmsteven/demo/notifications{?since,all,participating}", - open_issues: 0, - open_issues_count: 0, - owner: { - avatar_url: "https://avatars.githubusercontent.com/u/48621248?v=4", - email: "48621248+bmsteven@users.noreply.github.com", - events_url: "https://api.github.com/users/bmsteven/events{/privacy}", - followers_url: "https://api.github.com/users/bmsteven/followers", - following_url: - "https://api.github.com/users/bmsteven/following{/other_user}", - gists_url: "https://api.github.com/users/bmsteven/gists{/gist_id}", - gravatar_id: "", - html_url: "https://github.com/bmsteven", - id: 48621248, - login: "bmsteven", - name: "bmsteven", - node_id: "MDQ6VXNlcjQ4NjIxMjQ4", - organizations_url: "https://api.github.com/users/bmsteven/orgs", - received_events_url: - "https://api.github.com/users/bmsteven/received_events", - repos_url: "https://api.github.com/users/bmsteven/repos", - site_admin: false, - starred_url: - "https://api.github.com/users/bmsteven/starred{/owner}{/repo}", - subscriptions_url: "https://api.github.com/users/bmsteven/subscriptions", - type: "User", - url: "https://api.github.com/users/bmsteven", - }, - private: false, - pulls_url: "https://api.github.com/repos/bmsteven/demo/pulls{/number}", - pushed_at: 1650646867, - releases_url: "https://api.github.com/repos/bmsteven/demo/releases{/id}", - size: 169, - ssh_url: "git@github.com:bmsteven/demo.git", - stargazers: 0, - stargazers_count: 0, - stargazers_url: "https://api.github.com/repos/bmsteven/demo/stargazers", - statuses_url: "https://api.github.com/repos/bmsteven/demo/statuses/{sha}", - subscribers_url: "https://api.github.com/repos/bmsteven/demo/subscribers", - subscription_url: "https://api.github.com/repos/bmsteven/demo/subscription", - svn_url: "https://github.com/bmsteven/demo", - tags_url: "https://api.github.com/repos/bmsteven/demo/tags", - teams_url: "https://api.github.com/repos/bmsteven/demo/teams", - topics: [], - trees_url: "https://api.github.com/repos/bmsteven/demo/git/trees{/sha}", - updated_at: "2022-04-20T22:35:19Z", - url: "https://github.com/bmsteven/demo", - visibility: "public", - watchers: 0, - watchers_count: 0, - }, - sender: { - avatar_url: "https://avatars.githubusercontent.com/u/48621248?v=4", - events_url: "https://api.github.com/users/bmsteven/events{/privacy}", - followers_url: "https://api.github.com/users/bmsteven/followers", - following_url: - "https://api.github.com/users/bmsteven/following{/other_user}", - gists_url: "https://api.github.com/users/bmsteven/gists{/gist_id}", - gravatar_id: "", - html_url: "https://github.com/bmsteven", - id: 48621248, - login: "bmsteven", - node_id: "MDQ6VXNlcjQ4NjIxMjQ4", - organizations_url: "https://api.github.com/users/bmsteven/orgs", - received_events_url: - "https://api.github.com/users/bmsteven/received_events", - repos_url: "https://api.github.com/users/bmsteven/repos", - site_admin: false, - starred_url: "https://api.github.com/users/bmsteven/starred{/owner}{/repo}", - subscriptions_url: "https://api.github.com/users/bmsteven/subscriptions", - type: "User", - url: "https://api.github.com/users/bmsteven", - }, -}; From f2a0a5b2916d8a2be77e82215d777dd304197000 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Apr 2022 22:40:58 +0300 Subject: [PATCH 2/5] text payload --- res.js | 194 +++++++++++++++++++++++++++++++++++++++++ src/develop_actions.js | 7 +- 2 files changed, 198 insertions(+), 3 deletions(-) diff --git a/res.js b/res.js index e69de29..81268c3 100644 --- a/res.js +++ b/res.js @@ -0,0 +1,194 @@ +exports.response = { + after: "94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", + base_ref: null, + before: "cc8b67c069125765f31ee6908392e0439f0d04eb", + commits: [ + { + author: [Object], + committer: [Object], + distinct: false, + id: "49f9be33fb8cbf34c5a891db989e3de07c27b7a1", + message: "test commit", + timestamp: "2022-04-22T20:00:25+03:00", + tree_id: "969c2c9cb3ca68847fcf744a21df3672c153127d", + url: "https://github.com/bmsteven/demo/commit/49f9be33fb8cbf34c5a891db989e3de07c27b7a1", + }, + { + author: [Object], + committer: [Object], + distinct: true, + id: "94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", + message: + "Merge pull request #22 from bmsteven/test_branch\n\ntest commit", + timestamp: "2022-04-22T20:01:07+03:00", + tree_id: "969c2c9cb3ca68847fcf744a21df3672c153127d", + url: "https://github.com/bmsteven/demo/commit/94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", + }, + ], + compare: + "https://github.com/bmsteven/demo/compare/cc8b67c06912...94a28b830c5a", + created: false, + deleted: false, + forced: false, + head_commit: { + author: { + email: "48621248+bmsteven@users.noreply.github.com", + name: "Benedict Steven", + username: "bmsteven", + }, + committer: { + email: "noreply@github.com", + name: "GitHub", + username: "web-flow", + }, + distinct: true, + id: "94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", + message: "Merge pull request #22 from bmsteven/test_branch\n\ntest commit", + timestamp: "2022-04-22T20:01:07+03:00", + tree_id: "969c2c9cb3ca68847fcf744a21df3672c153127d", + url: "https://github.com/bmsteven/demo/commit/94a28b830c5af1d1a5e0a1ccd55ac9f9df8244da", + }, + pusher: { + email: "48621248+bmsteven@users.noreply.github.com", + name: "bmsteven", + }, + ref: "refs/heads/develop", + repository: { + allow_forking: true, + archive_url: + "https://api.github.com/repos/bmsteven/demo/{archive_format}{/ref}", + archived: false, + assignees_url: + "https://api.github.com/repos/bmsteven/demo/assignees{/user}", + blobs_url: "https://api.github.com/repos/bmsteven/demo/git/blobs{/sha}", + branches_url: + "https://api.github.com/repos/bmsteven/demo/branches{/branch}", + clone_url: "https://github.com/bmsteven/demo.git", + collaborators_url: + "https://api.github.com/repos/bmsteven/demo/collaborators{/collaborator}", + comments_url: + "https://api.github.com/repos/bmsteven/demo/comments{/number}", + commits_url: "https://api.github.com/repos/bmsteven/demo/commits{/sha}", + compare_url: + "https://api.github.com/repos/bmsteven/demo/compare/{base}...{head}", + contents_url: "https://api.github.com/repos/bmsteven/demo/contents/{+path}", + contributors_url: "https://api.github.com/repos/bmsteven/demo/contributors", + created_at: 1553584187, + default_branch: "master", + deployments_url: "https://api.github.com/repos/bmsteven/demo/deployments", + description: "demo repository", + disabled: false, + downloads_url: "https://api.github.com/repos/bmsteven/demo/downloads", + events_url: "https://api.github.com/repos/bmsteven/demo/events", + fork: false, + forks: 0, + forks_count: 0, + forks_url: "https://api.github.com/repos/bmsteven/demo/forks", + full_name: "bmsteven/demo", + git_commits_url: + "https://api.github.com/repos/bmsteven/demo/git/commits{/sha}", + git_refs_url: "https://api.github.com/repos/bmsteven/demo/git/refs{/sha}", + git_tags_url: "https://api.github.com/repos/bmsteven/demo/git/tags{/sha}", + git_url: "git://github.com/bmsteven/demo.git", + has_downloads: true, + has_issues: true, + has_pages: false, + has_projects: true, + has_wiki: true, + homepage: null, + hooks_url: "https://api.github.com/repos/bmsteven/demo/hooks", + html_url: "https://github.com/bmsteven/demo", + id: 177733530, + is_template: false, + issue_comment_url: + "https://api.github.com/repos/bmsteven/demo/issues/comments{/number}", + issue_events_url: + "https://api.github.com/repos/bmsteven/demo/issues/events{/number}", + issues_url: "https://api.github.com/repos/bmsteven/demo/issues{/number}", + keys_url: "https://api.github.com/repos/bmsteven/demo/keys{/key_id}", + labels_url: "https://api.github.com/repos/bmsteven/demo/labels{/name}", + language: "JavaScript", + languages_url: "https://api.github.com/repos/bmsteven/demo/languages", + license: null, + master_branch: "master", + merges_url: "https://api.github.com/repos/bmsteven/demo/merges", + milestones_url: + "https://api.github.com/repos/bmsteven/demo/milestones{/number}", + mirror_url: null, + name: "demo", + node_id: "MDEwOlJlcG9zaXRvcnkxNzc3MzM1MzA=", + notifications_url: + "https://api.github.com/repos/bmsteven/demo/notifications{?since,all,participating}", + open_issues: 0, + open_issues_count: 0, + owner: { + avatar_url: "https://avatars.githubusercontent.com/u/48621248?v=4", + email: "48621248+bmsteven@users.noreply.github.com", + events_url: "https://api.github.com/users/bmsteven/events{/privacy}", + followers_url: "https://api.github.com/users/bmsteven/followers", + following_url: + "https://api.github.com/users/bmsteven/following{/other_user}", + gists_url: "https://api.github.com/users/bmsteven/gists{/gist_id}", + gravatar_id: "", + html_url: "https://github.com/bmsteven", + id: 48621248, + login: "bmsteven", + name: "bmsteven", + node_id: "MDQ6VXNlcjQ4NjIxMjQ4", + organizations_url: "https://api.github.com/users/bmsteven/orgs", + received_events_url: + "https://api.github.com/users/bmsteven/received_events", + repos_url: "https://api.github.com/users/bmsteven/repos", + site_admin: false, + starred_url: + "https://api.github.com/users/bmsteven/starred{/owner}{/repo}", + subscriptions_url: "https://api.github.com/users/bmsteven/subscriptions", + type: "User", + url: "https://api.github.com/users/bmsteven", + }, + private: false, + pulls_url: "https://api.github.com/repos/bmsteven/demo/pulls{/number}", + pushed_at: 1650646867, + releases_url: "https://api.github.com/repos/bmsteven/demo/releases{/id}", + size: 169, + ssh_url: "git@github.com:bmsteven/demo.git", + stargazers: 0, + stargazers_count: 0, + stargazers_url: "https://api.github.com/repos/bmsteven/demo/stargazers", + statuses_url: "https://api.github.com/repos/bmsteven/demo/statuses/{sha}", + subscribers_url: "https://api.github.com/repos/bmsteven/demo/subscribers", + subscription_url: "https://api.github.com/repos/bmsteven/demo/subscription", + svn_url: "https://github.com/bmsteven/demo", + tags_url: "https://api.github.com/repos/bmsteven/demo/tags", + teams_url: "https://api.github.com/repos/bmsteven/demo/teams", + topics: [], + trees_url: "https://api.github.com/repos/bmsteven/demo/git/trees{/sha}", + updated_at: "2022-04-20T22:35:19Z", + url: "https://github.com/bmsteven/demo", + visibility: "public", + watchers: 0, + watchers_count: 0, + }, + sender: { + avatar_url: "https://avatars.githubusercontent.com/u/48621248?v=4", + events_url: "https://api.github.com/users/bmsteven/events{/privacy}", + followers_url: "https://api.github.com/users/bmsteven/followers", + following_url: + "https://api.github.com/users/bmsteven/following{/other_user}", + gists_url: "https://api.github.com/users/bmsteven/gists{/gist_id}", + gravatar_id: "", + html_url: "https://github.com/bmsteven", + id: 48621248, + login: "bmsteven", + node_id: "MDQ6VXNlcjQ4NjIxMjQ4", + organizations_url: "https://api.github.com/users/bmsteven/orgs", + received_events_url: + "https://api.github.com/users/bmsteven/received_events", + repos_url: "https://api.github.com/users/bmsteven/repos", + site_admin: false, + starred_url: "https://api.github.com/users/bmsteven/starred{/owner}{/repo}", + subscriptions_url: "https://api.github.com/users/bmsteven/subscriptions", + type: "User", + url: "https://api.github.com/users/bmsteven", + }, +}; diff --git a/src/develop_actions.js b/src/develop_actions.js index f75b47a..be3086a 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -16,12 +16,13 @@ const run = async () => { .slice(1) .join("/"); + // context + console.log(context.payload); console.log("branch name", branch_name); console.log("full name", context.payload?.full_name); console.log("owner", context.payload?.owner?.login); console.log("repo", context.payload?.repository?.name); - console.log("commits", context.payload?.commits); let commits = ""; context.payload?.commits?.forEach((e, i) => { @@ -32,10 +33,10 @@ const run = async () => { !e.message.includes("Skip") ) commits = - commits + i !== 0 ? "> " + e.message : "\n\n" + "> " + e.message; + i === 0 ? "> " + e.message : commits + "\n\n" + "> " + e.message; }); - console.log("formatted commits", commits); + console.log(commits); const createpr = await octokit.request( `POST /repos/${context.payload?.full_name}/pulls`, From 593070519d20e2df3315af818826b20348d5c545 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Apr 2022 22:45:10 +0300 Subject: [PATCH 3/5] texst pr to staging --- src/develop_actions.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/develop_actions.js b/src/develop_actions.js index be3086a..baf701a 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -16,13 +16,6 @@ const run = async () => { .slice(1) .join("/"); - // context - console.log(context.payload); - console.log("branch name", branch_name); - console.log("full name", context.payload?.full_name); - console.log("owner", context.payload?.owner?.login); - console.log("repo", context.payload?.repository?.name); - let commits = ""; context.payload?.commits?.forEach((e, i) => { @@ -36,12 +29,10 @@ const run = async () => { i === 0 ? "> " + e.message : commits + "\n\n" + "> " + e.message; }); - console.log(commits); - const createpr = await octokit.request( - `POST /repos/${context.payload?.full_name}/pulls`, + `POST /repos/${context.payload?.repository?.full_name}/pulls`, { - owner: context.payload?.owner?.login, + owner: context.payload?.repository?.owner?.login, repo: context.payload?.repository?.name, title: branch_name, body: commits, From 523fb03e134382e90b1e15db8fa58f4cdf59baf8 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Apr 2022 22:49:28 +0300 Subject: [PATCH 4/5] test sending slack notification --- .github/workflows/develop_actions.yml | 1 + src/develop_actions.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/.github/workflows/develop_actions.yml b/.github/workflows/develop_actions.yml index f6e8c1e..6b361ec 100644 --- a/.github/workflows/develop_actions.yml +++ b/.github/workflows/develop_actions.yml @@ -18,3 +18,4 @@ jobs: - uses: ./ with: GITHUB_TOKEN: ${{secrets.TOKEN}} + SLACK_WEBHOOK_URL: ${{secrets.SLACK_WEBHOOK_URL}} diff --git a/src/develop_actions.js b/src/develop_actions.js index baf701a..bf7644e 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -4,6 +4,7 @@ const core = require("@actions/core"); // const response = require("../res"); const GITHUB_TOKEN = core.getInput("GITHUB_TOKEN"); +const SLACK_WEBHOOK_URL = core.getInput("SLACK_WEBHOOK_URL"); const octokit = github.getOctokit(GITHUB_TOKEN); const { context = {} } = github; @@ -41,6 +42,21 @@ const run = async () => { } ); console.log("createPr", createpr?.data); + if (createpr?.data) { + axios + .post( + `${SLACK_WEBHOOK_URL}`, + `PR from ${branch_name} to staging was created successfully` + ) + .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")); + }); + } } catch (error) { console.log("error", error?.message); } From befe2a3d86461242f82c9fd8559e5c5fb33b4166 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 22 Apr 2022 23:00:04 +0300 Subject: [PATCH 5/5] fix fetching slack secret --- action.yml | 3 +++ src/develop_actions.js | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 6d290fe..f5270f6 100644 --- a/action.yml +++ b/action.yml @@ -6,6 +6,9 @@ inputs: GITHUB_TOKEN: description: 'GitHub Token' required: true + SLACK_WEBHOOK_URL: + description: 'Slack webhook' + required: true runs: using: 'node16' main: './src/develop_actions.js' diff --git a/src/develop_actions.js b/src/develop_actions.js index bf7644e..721f153 100644 --- a/src/develop_actions.js +++ b/src/develop_actions.js @@ -1,7 +1,6 @@ const axios = require("axios"); const github = require("@actions/github"); const core = require("@actions/core"); -// const response = require("../res"); const GITHUB_TOKEN = core.getInput("GITHUB_TOKEN"); const SLACK_WEBHOOK_URL = core.getInput("SLACK_WEBHOOK_URL"); @@ -57,6 +56,16 @@ const run = async () => { reject(new Error("FAILED: Send slack webhook")); }); } + const compare_commits = await octokit.request( + `GET /repos/${context.payload?.repository?.full_name}/compare/staging...${branch_name}`, + { + owner: context.payload?.repository?.owner?.login, + repo: context.payload?.repository?.name, + base: "staging", + head: branch_name, + } + ); + console.log(compare_commits); } catch (error) { console.log("error", error?.message); }