Class: Github
- Inherits:
-
Object
- Object
- Github
- Defined in:
- lib/github.rb
Instance Method Summary collapse
- #check_environment ⇒ Object
-
#extract_blocks(pr) ⇒ Object
used to extract blocks of information from a PR body.
-
#extract_linked_issues(pr) ⇒ Object
used to extract linked issues from the description follow githubs linking issue linking matchers.
-
#format(stories) ⇒ Object
formats the output of stories.
- #get_release_notes(gh_pr_ids) ⇒ Object
- #get_release_notes_array(gh_pr_ids) ⇒ Object
- #get_repo_name ⇒ Object
- #get_stories(repo_name, array_of_pr_ids) ⇒ Object
- #get_story(repo_name, pr_id = false) ⇒ Object
- #pulls_url(repo_name, pr_id) ⇒ Object
Instance Method Details
#check_environment ⇒ Object
10 11 12 13 14 15 |
# File 'lib/github.rb', line 10 def check_environment if GITHUB_USERNAME == nil || GITHUB_TOKEN == nil raise "environment variables not set. Please check that you have the following set...\ \nGITHUB_USERNAME\nGITHUB_TOKEN" end end |
#extract_blocks(pr) ⇒ Object
used to extract blocks of information from a PR body
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/github.rb', line 61 def extract_blocks(pr) body = pr['body'] return pr if body.nil? blocks = body.scan(/## ?((?:(?!##).)*)/m) return pr if blocks.nil? pr['blocks'] = [] blocks.each do |block| pr['blocks'] << block.first unless block.first.empty? end pr end |
#extract_linked_issues(pr) ⇒ Object
used to extract linked issues from the description follow githubs linking issue linking matchers
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/github.rb', line 75 def extract_linked_issues(pr) body = pr['body'] # UGH, this happens when a story doesn't have any description return pr if body.nil? regex = /(?:close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved|connect(?:s)?(?:ed)?\s(?:to)?)\s?+((\w*\/\w*)?#(\d+))/im linked_issues = body.scan(regex) return pr if linked_issues.nil? pr['linked_issues'] = [] linked_issues.each do |linked_issue| pr['linked_issues'] << get_story(linked_issue[0]) unless linked_issue[0].nil? end pr end |
#format(stories) ⇒ Object
formats the output of stories
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/github.rb', line 91 def format(stories) result_string = "" stories.each do |story| result_string += "#{story['number']}:" result_string += "#{story['title']}" result_string += "\n" end # clean up any troublesome chars result_string.gsub!('`', ' ') || result_string end |
#get_release_notes(gh_pr_ids) ⇒ Object
23 24 25 26 |
# File 'lib/github.rb', line 23 def get_release_notes gh_pr_ids stories = get_release_notes_array gh_pr_ids result_string = format stories end |
#get_release_notes_array(gh_pr_ids) ⇒ Object
28 29 30 31 32 |
# File 'lib/github.rb', line 28 def get_release_notes_array gh_pr_ids check_environment repo_name = get_repo_name get_stories repo_name, gh_pr_ids end |
#get_repo_name ⇒ Object
17 18 19 20 21 |
# File 'lib/github.rb', line 17 def get_repo_name cmd = "git remote -v |grep origin" repo_info = `#{cmd}` repo_info.scan(/\:(.*\/.*)\.git/).uniq.flatten.first end |
#get_stories(repo_name, array_of_pr_ids) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/github.rb', line 51 def get_stories(repo_name, array_of_pr_ids) stories = [] return [] unless array_of_pr_ids array_of_pr_ids.each do |id| stories<< get_story(repo_name, id) end stories end |
#get_story(repo_name, pr_id = false) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/github.rb', line 38 def get_story(repo_name, pr_id = false) # if story url like "user/repo_name#123" unless pr_id story = repo_name.split '#' repo_name = story[0].empty? ? get_repo_name : story[0] pr_id = story[1] end pr = HTTParty.get( pulls_url(repo_name, pr_id), HEADERS ).parsed_response return {"id" => story_id, "name" => "PR not found in github"} if pr.nil? || pr["code"] == "error" pr = extract_linked_issues pr pr = extract_blocks pr end |
#pulls_url(repo_name, pr_id) ⇒ Object
34 35 36 |
# File 'lib/github.rb', line 34 def pulls_url(repo_name, pr_id) "#{GITHUB_BASE_URL}/repos/#{repo_name}/issues/#{pr_id}?access_token=#{GITHUB_TOKEN}" end |