11require 'open3'
22
3- RED = "\033 [31m"
4- GREEN = "\033 [32m"
5- RESET = "\033 [0m"
3+ RED = "\033 [31m" . freeze
4+ GREEN = "\033 [32m" . freeze
5+ RESET = "\033 [0m" . freeze
66
7+ # Let's ignore rubocop here until such a time as we either muzzle it
8+ # or get these helpers more centralized and tested.
9+ # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
710def run_command ( cmd , silent : true , print_command : false , report_status : false )
8- puts "#{ GREEN } Running #{ cmd } #{ RESET } " if print_command
11+ cmd_string = cmd . is_a? ( String ) ? cmd : cmd . join ( ' ' )
12+ puts "#{ GREEN } Running #{ cmd_string } #{ RESET } " if print_command
913 output = ''
10- Open3 . popen2e ( cmd ) do |_stdin , stdout_stderr , thread |
14+ Open3 . popen2e ( * Array ( cmd ) ) do |_stdin , stdout_stderr , thread |
1115 stdout_stderr . each do |line |
1216 puts line unless silent
1317 output += line
1418 end
1519 exitcode = thread . value . exitstatus
1620 unless exitcode . zero?
17- err = "#{ RED } Command failed! Command: #{ cmd } , Exit code: #{ exitcode } "
21+ err = "#{ RED } Command failed! Command: #{ cmd_string } , Exit code: #{ exitcode } "
1822 # Print details if we were running silent
1923 err += "\n Output:\n #{ output } " if silent
2024 err += RESET
@@ -24,6 +28,7 @@ def run_command(cmd, silent: true, print_command: false, report_status: false)
2428 end
2529 output . chomp
2630end
31+ # rubocop:enable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
2732
2833Dir . glob ( File . join ( 'tasks/**/*.rake' ) ) . each { |file | load file }
2934
4449
4550desc "verify that commit messages match CONTRIBUTING.md requirements"
4651task ( :commits ) do
47- # This rake task looks at the summary from every commit from this branch not
48- # in the branch targeted for a PR. This is accomplished by using the
49- # TRAVIS_COMMIT_RANGE environment variable, which is present in travis CI and
50- # populated with the range of commits the PR contains. If not available, this
51- # falls back to `main..HEAD` as a next best bet as `main` is unlikely to
52- # ever be absent.
53- #
54- # When we move to GH actions, use `GITHUB_BASE_REF` to resolve the merge base
52+ # Use `GITHUB_BASE_REF` to resolve the merge base
5553 # ref, which is the common ancestor between the base branch and PR. Then do
56- # git log for all of the commits in `HEAD` that are not in the base ref
57- #
58- # baseref = %x{git merge-base HEAD $GITHUB_BASE_REF}
59- # commits = "#{baseref}..HEAD"
60- commits = ENV [ 'TRAVIS_COMMIT_RANGE' ] . nil? ? 'main..HEAD' : ENV [ 'TRAVIS_COMMIT_RANGE' ] . sub ( /\. \. \. / , '..' )
61- %x{git log --no-merges --pretty=%s #{ commits } } . each_line do |commit_summary |
62- error_message = <<-HEREDOC
63- \n \n \n \t This commit summary didn't match CONTRIBUTING.md guidelines:\n \
64- \n \t \t #{ commit_summary } \n \
65- \t The commit summary (i.e. the first line of the commit message) should start with one of:\n \
66- \t \t (docs)\n \
67- \t \t (maint)\n \
68- \t \t (packaging)\n \
69- \t \t (<ANY PUBLIC JIRA TICKET>)\n \
70- \n \t This test for the commit summary is case-insensitive.\n \n \n
54+ # git log for all of the commits in `HEAD` that are not in the base ref.
55+ github_base_ref = ENV . fetch ( 'GITHUB_BASE_REF' , 'main' )
56+ github_base_ref = 'main' if github_base_ref . empty?
57+ # There's still something odd here. After action/checkout, 'main' by itself raises
58+ # objections from git like: `fatal: Not a valid object name main`.
59+ # but 'origin/main' works. However, if github_base_ref were a sha,
60+ # origin/SHA would fail as a reference...
61+ git_merge_base_cmd = [ 'git' , 'merge-base' , 'HEAD' , "origin/#{ github_base_ref } " ]
62+ baseref = run_command ( git_merge_base_cmd )
63+ commits = "#{ baseref } ..HEAD"
64+ git_log_cmd = [ 'git' , 'log' , '--no-merges' , '--pretty=%s' , commits ]
65+ commit_lines = run_command ( git_log_cmd , silent : false , print_command : true )
66+
67+ commit_lines . each_line do |commit_summary |
68+ error_message = <<~HEREDOC
69+ \n \n \n \t This commit summary didn't match CONTRIBUTING.md guidelines:
70+ \n \t \t #{ commit_summary }
71+ \t The commit summary (i.e. the first line of the commit message) should start with one of:
72+ \t \t (docs)
73+ \t \t (maint)
74+ \t \t (packaging)
75+ \t \t (<gh-#>) (An existing github ticket ref)
76+ \n \t This test for the commit summary is case-insensitive.\n
7177 HEREDOC
7278
73- if /^\( (maint|doc|docs|packaging|pa-\d +)\) |revert|bumping|merge|promoting/i . match ( commit_summary ) . nil?
74- ticket = commit_summary . match ( /^\( ([[:alpha:]]+-[[:digit:]]+)\) .*/ )
75- if ticket . nil?
76- raise error_message
77- else
78- require 'net/http'
79- require 'uri'
80- uri = URI . parse ( "https://tickets.puppetlabs.com/browse/#{ ticket [ 1 ] } " )
81- response = Net ::HTTP . get_response ( uri )
82- if response . code != "200"
83- raise error_message
84- end
85- end
79+ commit_format_regex = /
80+ ^\( (?:maint|doc|docs|packaging|(([[:alpha:]]+)-(\d +)))\) |
81+ revert|bumping|merge|promoting
82+ /xi
83+ match = commit_summary . match ( commit_format_regex )
84+ raise error_message if match . nil?
85+
86+ ticket = match [ 1 ]
87+ next if ticket . nil?
88+ project = match [ 2 ]
89+ # Could be an old PUP- or other Jira, and while there is still a
90+ # https://puppet.atlassian.net archive, determining that a
91+ # particular ticket does not exist is more involved that getting a
92+ # 200 on a call like https://puppet.atlassian.net/browse/PUP-1234
93+ next if project != 'gh'
94+ ticket_number = match [ 3 ]
95+
96+ require 'net/http'
97+ require 'uri'
98+ uri = URI . parse ( "https://github.com/openvoxproject/openvox-agent/issues/#{ ticket_number } " )
99+ response = Net ::HTTP . get_response ( uri )
100+ if response . code != "200"
101+ ticket_error = "\t #{ RED } Did not find a ticket matching #{ ticket } at #{ uri } #{ RESET } \n \n "
102+ raise error_message + ticket_error
86103 end
87104 end
105+ puts "#{ GREEN } All commit messages match the guidelines!#{ RESET } "
88106end
0 commit comments