From 91a4e1af01dc9406bab99d08ed4cf6755b13a1a5 Mon Sep 17 00:00:00 2001 From: Miguel Savignano Date: Sat, 5 Dec 2020 13:38:55 +0100 Subject: [PATCH 1/7] check_commit --- lib/check_commit.rb | 23 +++++++++++++++++++++++ lib/command.rb | 10 ++++++++++ lib/index.rb | 1 + spec/check_commit_spec.rb | 6 ++++++ 4 files changed, 40 insertions(+) create mode 100644 lib/check_commit.rb create mode 100644 lib/command.rb create mode 100644 spec/check_commit_spec.rb diff --git a/lib/check_commit.rb b/lib/check_commit.rb new file mode 100644 index 0000000..d1474bc --- /dev/null +++ b/lib/check_commit.rb @@ -0,0 +1,23 @@ +require_relative './command' + +class CheckCommit + include Command + + def initialize(commit_sha) + @commit_sha = commit_sha + end + + def in_branch?(branch_name) + contains_in_branches.include?(branch_name) + end + + def contains_in_branches + response = execute_branch_contains + return nil if command_error?(response) + command_to_list(response) + end + + def execute_branch_contains + `git branch -r --contains #{@commit_sha}` + end +end diff --git a/lib/command.rb b/lib/command.rb new file mode 100644 index 0000000..7641769 --- /dev/null +++ b/lib/command.rb @@ -0,0 +1,10 @@ +module Command + + def command_error?(response) + return nil if response == "" + end + + def command_to_list(response = "") + response.split("\n").map(&:strip) + end +end diff --git a/lib/index.rb b/lib/index.rb index ab8d379..548be56 100644 --- a/lib/index.rb +++ b/lib/index.rb @@ -1,6 +1,7 @@ require 'json' require 'octokit' require_relative './services/merge_branch_service' +require_relative './check_commit' def presence(value) return nil if value == "" diff --git a/spec/check_commit_spec.rb b/spec/check_commit_spec.rb new file mode 100644 index 0000000..50386db --- /dev/null +++ b/spec/check_commit_spec.rb @@ -0,0 +1,6 @@ +require 'pry' +require_relative '../lib/check_commit' + +commit_sha = '9f7b8529087fc8ea011a37b1c768960235438795' + +puts CheckCommit.new(commit_sha).in_branch?('origin/master') From 2bc0b7dda548663711eed3a951c3dc1126a72ea7 Mon Sep 17 00:00:00 2001 From: Miguel Savignano Date: Sat, 5 Dec 2020 19:21:02 +0100 Subject: [PATCH 2/7] label merged_branch --- .gitignore | 1 + README.md | 21 +++++++++++++++ lib/check_commit.rb | 23 ---------------- lib/command.rb | 10 ------- lib/index.rb | 4 ++- lib/services/label_merged_branch_service.rb | 29 +++++++++++++++++++++ lib/services/merge_branch_service.rb | 2 ++ spec/check_commit_spec.rb | 6 ----- 8 files changed, 56 insertions(+), 40 deletions(-) delete mode 100644 lib/check_commit.rb delete mode 100644 lib/command.rb create mode 100644 lib/services/label_merged_branch_service.rb delete mode 100644 spec/check_commit_spec.rb diff --git a/.gitignore b/.gitignore index 4c49bd7..64fed38 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +test diff --git a/README.md b/README.md index 0531574..58d9de5 100644 --- a/README.md +++ b/README.md @@ -84,3 +84,24 @@ jobs: target_branch: 'develop' github_token: ${{ github.token }} ``` + +### Check merged branch + +```yaml +name: Label merged branch +on: + push: + branches: + - 'staging' +jobs: + merge-branch: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + + - name: check merge in staging + uses: devmasx/merge-branch@v1.3.1 + with: + type: 'label-merged' + github_token: ${{ github.token }} +``` diff --git a/lib/check_commit.rb b/lib/check_commit.rb deleted file mode 100644 index d1474bc..0000000 --- a/lib/check_commit.rb +++ /dev/null @@ -1,23 +0,0 @@ -require_relative './command' - -class CheckCommit - include Command - - def initialize(commit_sha) - @commit_sha = commit_sha - end - - def in_branch?(branch_name) - contains_in_branches.include?(branch_name) - end - - def contains_in_branches - response = execute_branch_contains - return nil if command_error?(response) - command_to_list(response) - end - - def execute_branch_contains - `git branch -r --contains #{@commit_sha}` - end -end diff --git a/lib/command.rb b/lib/command.rb deleted file mode 100644 index 7641769..0000000 --- a/lib/command.rb +++ /dev/null @@ -1,10 +0,0 @@ -module Command - - def command_error?(response) - return nil if response == "" - end - - def command_to_list(response = "") - response.split("\n").map(&:strip) - end -end diff --git a/lib/index.rb b/lib/index.rb index 548be56..99740b5 100644 --- a/lib/index.rb +++ b/lib/index.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'json' require 'octokit' require_relative './services/merge_branch_service' -require_relative './check_commit' +require_relative './services/label_merged_branch_service' def presence(value) return nil if value == "" diff --git a/lib/services/label_merged_branch_service.rb b/lib/services/label_merged_branch_service.rb new file mode 100644 index 0000000..0e46dcb --- /dev/null +++ b/lib/services/label_merged_branch_service.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class LabelMergedBranchService + def initialize(client, sha:, label_name:, repo:) + @client = client + @commit_sha = sha + @label_name = label_name + @repo = repo + end + + def run + all_commits = @client.commits(@repo, @sha) + if is_merged_commit?(all_commits.first) + sha_commit = all_commits[1].sha + end + + pull_requests = @client.pull_requests(@repo, :state => 'open') + if pull_request = pull_requests.find{ |item| sha_commit == item.head.sha } + puts "Create label #{@label_name} on #{pull_request.number}" + @client.add_labels_to_an_issue(@repo, pull_request.number, [@label_name]) + else + puts "No pull request found" + end + end + + def is_merged_commit?(commit) + commit.parents.count >= 2 + end +end diff --git a/lib/services/merge_branch_service.rb b/lib/services/merge_branch_service.rb index cfe7b41..57559ae 100644 --- a/lib/services/merge_branch_service.rb +++ b/lib/services/merge_branch_service.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class MergeBrachService attr_reader :inputs, :event diff --git a/spec/check_commit_spec.rb b/spec/check_commit_spec.rb deleted file mode 100644 index 50386db..0000000 --- a/spec/check_commit_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'pry' -require_relative '../lib/check_commit' - -commit_sha = '9f7b8529087fc8ea011a37b1c768960235438795' - -puts CheckCommit.new(commit_sha).in_branch?('origin/master') From 68b9d3f5c6763c72edb615dff16070027e5608f5 Mon Sep 17 00:00:00 2001 From: Miguel Savignano Date: Sat, 5 Dec 2020 19:39:10 +0100 Subject: [PATCH 3/7] new type service in index --- README.md | 9 +++++++-- lib/constans.rb | 5 +++++ lib/index.rb | 27 ++++++++++++++++++--------- lib/services/merge_branch_service.rb | 5 ++--- 4 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 lib/constans.rb diff --git a/README.md b/README.md index 58d9de5..ec41438 100644 --- a/README.md +++ b/README.md @@ -87,8 +87,12 @@ jobs: ### Check merged branch +Set label merge in staging on Pull request. + +Run workflow on staging branch, this workflow check if the `GITHUB_SHA` have a pull request, and add the label 'merged-in-staging'. + ```yaml -name: Label merged branch +name: Set label on merged branch on: push: branches: @@ -102,6 +106,7 @@ jobs: - name: check merge in staging uses: devmasx/merge-branch@v1.3.1 with: - type: 'label-merged' + type: 'merged-label' + label_name: merged-in-staging github_token: ${{ github.token }} ``` diff --git a/lib/constans.rb b/lib/constans.rb new file mode 100644 index 0000000..3a738d6 --- /dev/null +++ b/lib/constans.rb @@ -0,0 +1,5 @@ +module Constans + TYPE_LABELED = "labeled".freeze + TYPE_NOW = "now".freeze + TYPE_MERGED_LABEL = "merged-label".freeze +end diff --git a/lib/index.rb b/lib/index.rb index 99740b5..9178f52 100644 --- a/lib/index.rb +++ b/lib/index.rb @@ -2,6 +2,7 @@ require 'json' require 'octokit' +require_relative './constans' require_relative './services/merge_branch_service' require_relative './services/label_merged_branch_service' @@ -16,18 +17,26 @@ def presence(value) @github_token = presence(ENV['INPUT_GITHUB_TOKEN']) || presence(ENV['GITHUB_TOKEN']) inputs = { - type: presence(ENV['INPUT_TYPE']) || MergeBrachService::TYPE_LABELED, # labeled | comment | now + type: presence(ENV['INPUT_TYPE']) || Constans::TYPE_LABELED, label_name: ENV['INPUT_LABEL_NAME'], target_branch: ENV['INPUT_TARGET_BRANCH'] } -MergeBrachService.validate_inputs!(inputs) -service = MergeBrachService.new(inputs, @event) - -if service.valid? - @client = Octokit::Client.new(access_token: @github_token) - @client.merge(@repository, inputs[:target_branch], @head_to_merge) - puts "Finish merge branch to #{inputs[:target_branch]}" +@client = Octokit::Client.new(access_token: @github_token) +if inputs[:type] == Constans::TYPE_MERGED_LABEL + LabelMergedBranchService.new(@client, { + sha: @head_to_merge, + label_name: inputs[:label_name], + repo: @repository + }).run else - puts 'Skip' + MergeBrachService.validate_inputs!(inputs) + service = MergeBrachService.new(inputs, @event) + + if service.valid? + @client.merge(@repository, inputs[:target_branch], @head_to_merge) + puts "Finish merge branch to #{inputs[:target_branch]}" + else + puts 'Skip' + end end diff --git a/lib/services/merge_branch_service.rb b/lib/services/merge_branch_service.rb index 57559ae..7b83256 100644 --- a/lib/services/merge_branch_service.rb +++ b/lib/services/merge_branch_service.rb @@ -1,10 +1,9 @@ # frozen_string_literal: true +require_relative '../constans' class MergeBrachService attr_reader :inputs, :event - - TYPE_LABELED = "labeled".freeze - TYPE_NOW = "now".freeze + include Constans def self.validate_inputs!(target_branch:, type:, label_name:) raise "Invalid type" unless [TYPE_LABELED, TYPE_NOW].include?(type) From 4bd3008bb63f77bf4406399e4271d0fdb5f58df3 Mon Sep 17 00:00:00 2001 From: Miguel Savignano Date: Sat, 5 Dec 2020 20:37:39 +0100 Subject: [PATCH 4/7] fix find pull request --- lib/services/label_merged_branch_service.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/services/label_merged_branch_service.rb b/lib/services/label_merged_branch_service.rb index 0e46dcb..ef2483b 100644 --- a/lib/services/label_merged_branch_service.rb +++ b/lib/services/label_merged_branch_service.rb @@ -9,18 +9,23 @@ def initialize(client, sha:, label_name:, repo:) end def run - all_commits = @client.commits(@repo, @sha) + create_label(find_pull_request) + end + + def find_pull_request + all_commits = @client.commits(@repo, @commit_sha) if is_merged_commit?(all_commits.first) - sha_commit = all_commits[1].sha + commit_sha = all_commits[1].sha + # puts "Real commit (no merge) #{commit_sha}" end pull_requests = @client.pull_requests(@repo, :state => 'open') - if pull_request = pull_requests.find{ |item| sha_commit == item.head.sha } - puts "Create label #{@label_name} on #{pull_request.number}" - @client.add_labels_to_an_issue(@repo, pull_request.number, [@label_name]) - else - puts "No pull request found" - end + pull_requests&.find{ |item| commit_sha == item.head.sha } + end + + def create_label(pull_request) + puts "Create label #{@label_name} on #{pull_request.number}" + @client.add_labels_to_an_issue(@repo, pull_request.number, [@label_name]) end def is_merged_commit?(commit) From 4bf968161058f81d495936d427ac61b7d145ff4f Mon Sep 17 00:00:00 2001 From: Miguel Savignano Date: Sun, 6 Dec 2020 01:48:27 +0100 Subject: [PATCH 5/7] create check run --- lib/services/label_merged_branch_service.rb | 48 +++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/services/label_merged_branch_service.rb b/lib/services/label_merged_branch_service.rb index ef2483b..52e7ac0 100644 --- a/lib/services/label_merged_branch_service.rb +++ b/lib/services/label_merged_branch_service.rb @@ -1,33 +1,65 @@ # frozen_string_literal: true class LabelMergedBranchService + CHECK_BASE_NAME = "merge-branch" + def initialize(client, sha:, label_name:, repo:) @client = client - @commit_sha = sha + @sha = sha @label_name = label_name @repo = repo end def run - create_label(find_pull_request) + if pull_request + create_label + else + puts "Not Pull request found" + end + create_check_run end - def find_pull_request - all_commits = @client.commits(@repo, @commit_sha) + def commit_sha + return @commit_sha if @commit_sha + + all_commits = @client.commits(@repo, @sha) if is_merged_commit?(all_commits.first) - commit_sha = all_commits[1].sha - # puts "Real commit (no merge) #{commit_sha}" + @commit_sha = all_commits[1].sha + # puts "Commit sha (no merge) #{@commit_sha}" + else + @commit_sha = @sha end + end + def pull_request + return @pull_request if @pull_request + + all_commits = @client.commits(@repo, @sha) pull_requests = @client.pull_requests(@repo, :state => 'open') - pull_requests&.find{ |item| commit_sha == item.head.sha } + @pull_request = pull_requests&.find{ |item| commit_sha == item.head.sha } end - def create_label(pull_request) + def create_label puts "Create label #{@label_name} on #{pull_request.number}" @client.add_labels_to_an_issue(@repo, pull_request.number, [@label_name]) end + def create_check_run + url = "/repos/#{@repo}/check-runs" + @client.post(url, { + name: "#{CHECK_BASE_NAME}: #{@label_name}", + head_sha: commit_sha, + status: 'completed', + started_at: Time.now.iso8601, + conclusion: "success", + output: { + title: "#{CHECK_BASE_NAME}: #{@label_name}", + summary: "#{CHECK_BASE_NAME}: #{@label_name}", + annotations: [] + } + }) + end + def is_merged_commit?(commit) commit.parents.count >= 2 end From 30e98ccbe852b88743ac128f5e7eb5f50b6a8393 Mon Sep 17 00:00:00 2001 From: Miguel Savignano Date: Sun, 6 Dec 2020 11:04:25 +0100 Subject: [PATCH 6/7] update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec41438..be0e9d6 100644 --- a/README.md +++ b/README.md @@ -87,9 +87,9 @@ jobs: ### Check merged branch -Set label merge in staging on Pull request. +Assign label to pull request merged in another branch. -Run workflow on staging branch, this workflow check if the `GITHUB_SHA` have a pull request, and add the label 'merged-in-staging'. +Run workflow on staging branch, this workflow check if the `GITHUB_SHA` have a pull request, and assign the label 'merged-in-staging' and create a check-run in the merged commit. ```yaml name: Set label on merged branch From ca1abea21a98af2f2b3ea3844e3f0ec115340ac2 Mon Sep 17 00:00:00 2001 From: Miguel Savignano Date: Sun, 6 Dec 2020 11:06:18 +0100 Subject: [PATCH 7/7] rename file --- lib/index.rb | 2 +- ..._merged_branch_service.rb => check_merged_branch_service.rb} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename lib/services/{label_merged_branch_service.rb => check_merged_branch_service.rb} (98%) diff --git a/lib/index.rb b/lib/index.rb index 9178f52..117321e 100644 --- a/lib/index.rb +++ b/lib/index.rb @@ -4,7 +4,7 @@ require 'octokit' require_relative './constans' require_relative './services/merge_branch_service' -require_relative './services/label_merged_branch_service' +require_relative './services/check_merged_branch_service' def presence(value) return nil if value == "" diff --git a/lib/services/label_merged_branch_service.rb b/lib/services/check_merged_branch_service.rb similarity index 98% rename from lib/services/label_merged_branch_service.rb rename to lib/services/check_merged_branch_service.rb index 52e7ac0..8666196 100644 --- a/lib/services/label_merged_branch_service.rb +++ b/lib/services/check_merged_branch_service.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class LabelMergedBranchService +class CheckMergedBranchService CHECK_BASE_NAME = "merge-branch" def initialize(client, sha:, label_name:, repo:)