diff --git a/lib/github.rb b/lib/github.rb index 2a41fb6..28a6c5c 100644 --- a/lib/github.rb +++ b/lib/github.rb @@ -45,7 +45,7 @@ def self._pr_data(pr) ## Returns a list of members {id, login, name} def self.team_members(org, team_name) team = GithubGraphql.get_team_members(org, team_name) - return nil if team["data"]["organization"]["team"].nil? + return nil if team["data"]["organization"].nil? || team["data"]["organization"]["team"].nil? return team["data"]["organization"]["team"]["members"]["edges"].map { |edge| edge["node"] } end @@ -119,6 +119,14 @@ def self.parse_pull_request_url(url) return Hash[keys.zip(vals)] end + def self.parse_org_and_team(team) + keys = ["org", "team_name"] + m = team.match(/(.+)\/(.+)/) + items = m ? m.captures : ["", team] + + return Hash[keys.zip(items)] + end + def self.name_and_login(obj) if obj["name"] && !obj["name"].empty? "#{obj["name"]} (@#{obj["login"]})" diff --git a/request_review.rb b/request_review.rb index 7d7b022..fda5711 100755 --- a/request_review.rb +++ b/request_review.rb @@ -8,13 +8,17 @@ exit(1) end - if ARGV.length < 2 + if ARGV.length < 2 && !ENV["GITHUB_TEAM"] puts "Usage: #{__FILE__} " exit(2) end parsed = Github.parse_pull_request_url(ARGV[0]) - team_name = ARGV[1] + if ARGV.length > 1 + team_name = Github.parse_org_and_team(ARGV[1])["team_name"] + else + team_name = Github.parse_org_and_team(ENV["GITHUB_TEAM"])["team_name"] + end pr = Github.pull_request_by_number(parsed["org"], parsed["repo"], parsed["pr_number"].to_i) diff --git a/team_prs.rb b/team_prs.rb index c7aa00f..8a85015 100755 --- a/team_prs.rb +++ b/team_prs.rb @@ -8,14 +8,19 @@ exit(1) end - if ARGV.length < 1 + if ARGV.length < 1 && !ENV["GITHUB_TEAM"] puts "Usage: #{__FILE__} " exit(2) end - org, team = ARGV[0].match(/(.+)\/(.+)/).captures + if ARGV.length > 0 + team_name = ARGV[0] + else + team_name = ENV["GITHUB_TEAM"] + end + parsed_team = Github.parse_org_and_team(team_name) - team = Github.team_members(org, team) + team = Github.team_members(parsed_team["org"], parsed_team["team_name"]) if team.nil? $stderr.puts "Team [#{team_name}] could not be found" exit(3) @@ -25,7 +30,7 @@ puts "│   " no_prs = [] team.each_with_index do |member, i| - prs = Github.pull_requests_for_login(member["login"]).reject { |pr| pr["owner"].downcase != org.downcase } + prs = Github.pull_requests_for_login(member["login"]).reject { |pr| pr["owner"].downcase != parsed_team["org"].downcase } if !prs.empty? Github.puts_multiple_pull_requests(prs, { prefix: "│   " })