Module: Reviewlette

Defined in:
lib/reviewlette/version.rb,
lib/reviewlette/database.rb,
lib/reviewlette/graph_gen.rb,
lib/reviewlette/vacations.rb,
lib/reviewlette/exceptions.rb,
lib/reviewlette/github_connection.rb,
lib/reviewlette/trello_connection.rb,
lib/reviewlette.rb

Defined Under Namespace

Classes: AlreadyAssignedException, Database, GithubConnection, Graphgenerator, NoTrelloCardException, TrelloConnection, Vacations

Constant Summary collapse

VERSION =
'0.0.6'
TRELLO_CONFIG1 =
YAML.load_file("#{File.dirname(__FILE__)}/../config/.trello.yml")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#boardObject

Returns the value of attribute board.



20
21
22
# File 'lib/reviewlette.rb', line 20

def board
  @board
end

#dbObject

Returns the value of attribute db.



20
21
22
# File 'lib/reviewlette.rb', line 20

def db
  @db
end

#github_connectionObject

Returns the value of attribute github_connection.



20
21
22
# File 'lib/reviewlette.rb', line 20

def github_connection
  @github_connection
end

#repoObject

Returns the value of attribute repo.



20
21
22
# File 'lib/reviewlette.rb', line 20

def repo
  @repo
end

#trello_connectionObject

Returns the value of attribute trello_connection.



20
21
22
# File 'lib/reviewlette.rb', line 20

def trello_connection
  @trello_connection
end

Class Method Details

.add_reviewer_on_githubObject

Adds Assignee on GitHub.



172
173
174
# File 'lib/reviewlette.rb', line 172

def add_reviewer_on_github
  @github_connection.add_assignee(@repo, @number, @title, @body, @githubname) if @number && @githubname
end

.add_to_trello_cardObject

Adds Reviewer on Trello Card.



181
182
183
# File 'lib/reviewlette.rb', line 181

def add_to_trello_card
  @trello_connection.add_reviewer_to_card(@reviewer, @card)
end

.comment_on_errorObject

TODO: Generic Error message.



121
122
123
# File 'lib/reviewlette.rb', line 121

def comment_on_error
  @trello_connection.comment_on_card("Skipped Issue #{@number} because everyone on the team is assigned to the card", @card)
end

.comment_on_githubObject

Comments on GitHub.



177
178
179
# File 'lib/reviewlette.rb', line 177

def comment_on_github
  @github_connection.comment_on_issue(@repo, @number, @githubname, @card.url) if @number && @githubname
end

.comment_on_trelloObject

Comments on Trello Card.



186
187
188
189
# File 'lib/reviewlette.rb', line 186

def comment_on_trello
  @full_comment = '@' + @trelloname + ' will review ' + 'https://github.com/'+ @repo+'/issues/'+@number.to_s
  @trello_connection.comment_on_card(@full_comment, @card) if @full_comment
end

.evaluate_vacations(reviewer) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reviewlette.rb', line 85

def evaluate_vacations(reviewer)
  parse_vacations.each do |check|
    check[1] = check[0] unless check[1] # Rewrite if statement with catch to prevent this error?
    if (check[0]..check[1]).cover?(Date.today)
      @db.set_vacation_flag(reviewer, 'true')
      break
    else
      @db.set_vacation_flag(reviewer, 'false') # hopefully not to_bool?
    end
  end
end

.fetch_branchObject

gets the branchname from github pullrequest



103
104
105
106
107
# File 'lib/reviewlette.rb', line 103

def fetch_branch
  pr_id = @pullreq_ids.values.index(@number)
  branch_name = @github_connection.get_branch_name(pr_id, @repo)
  branch_name.split('_').last.to_i
end

.find_card(title) ⇒ Object

Finds card based on title of Github Issue. Or by branch name if title does not include the trello number. Happens if the pullrequest consists of only one commit.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/reviewlette.rb', line 64

def find_card(title)
  begin
    match_pr_id_with_issue_id
    @id = title.split('_').last.to_i
    @id = fetch_branch if @id == 0 && !(@pullreq_ids.values.index(@number)).nil?
    raise NoTrelloCardException, "Could not find a Trello card. Found #{title.split('_').last} instead, maybe the naming of your pullrequest is wrong? And/or you dont have a branch?" if @id == 0
    true
  rescue NoTrelloCardException => e
    @logger.error e.message
    puts (e.message)
    false
  end
end

.find_idObject

Finds a sets card.



140
141
142
143
144
145
146
147
148
# File 'lib/reviewlette.rb', line 140

def find_id
  if @id != 0
    @card = @trello_connection.find_card_by_id(@id)
    true
  else
    @logger.warn("Id not found, skipping Issue #{@title} with number #{@number}")
    false
  end
end

.get_available_reposObject



57
58
59
# File 'lib/reviewlette.rb', line 57

def get_available_repos
  @repos = Reviewlette::GithubConnection::GITHUB_CONFIG['repo']
end

.get_unassigned_github_issuesObject

Gets [Array(String, String)] all GitHub Issues that are not assigned to someone.



126
127
128
# File 'lib/reviewlette.rb', line 126

def get_unassigned_github_issues
  @issues = @github_connection.list_issues(@repo).select{|issue| !issue[:assignee]}
end

.match_pr_id_with_issue_idObject

Matches Pull Request IDs with the respective Order of PullRequests to call them and get the branch name.



111
112
113
114
115
116
117
118
# File 'lib/reviewlette.rb', line 111

def match_pr_id_with_issue_id
  @pullreq_ids = {}
  @counter = 0
  @github_connection.list_pulls(@repo).each do |x|
    @pullreq_ids[@counter] = x.number
    @counter +=1
  end
end

.move_to_listObject

TODO: More generic ‘Done’ and ‘in Review’ are not present everywhere



192
193
194
195
196
197
198
199
200
# File 'lib/reviewlette.rb', line 192

def move_to_list
  if @github_connection.pull_merged?(@repo, @id)
    @column = @trello_connection.find_column('Done')
    @trello_connection.move_card_to_list(@card, @column)
  else
    @column = @trello_connection.find_column('In review')
    @trello_connection.move_card_to_list(@card, @column)
  end
end

.parse_vacationsObject



97
98
99
100
# File 'lib/reviewlette.rb', line 97

def parse_vacations
  split = @vacations.map { |x| x.split(' - ') }
  split.map { |x| x.map { |b| Date.parse(b) } }
end

.set_reviewerObject

Selects and sets reviewer.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/reviewlette.rb', line 151

def set_reviewer
  begin
    while !(@reviewer)
      @reviewer = @trello_connection.determine_reviewer(@card) if @card
    end
    @trelloname = @reviewer.username
    puts "Selected #{@reviewer.username}"
    return true
  rescue AlreadyAssignedException => e
    @logger.warn("Skipped Issue #{@card.short_id} because #{e.message}")
    puts ("Skipped Issue #{@card.short_id} because #{e.message}")
    return false
  end
end

.setupObject

Sets instance variables.



131
132
133
134
135
136
137
# File 'lib/reviewlette.rb', line 131

def setup
  @logger = Logger.new('review.log')
  @db = Reviewlette::Database.new
  @github_connection = Reviewlette::GithubConnection.new
  @trello_connection = Reviewlette::TrelloConnection.new
  @board = Trello::Board.find(TRELLO_CONFIG1['board_id'])
end

.spinObject

Execute logic.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/reviewlette.rb', line 27

def spin
  setup
  get_available_repos.each do |repo|
    @repo = repo
    get_unassigned_github_issues.each do |issue|
      @number = issue[:number]
      @title = issue[:title]
      @body = issue[:body]
      update_vacations
      
      if find_card(@title.to_s) and find_id
        if set_reviewer
          transform_name
          add_reviewer_on_github
          comment_on_github
          # add_to_trello_card
          comment_on_trello
          move_to_list
          @db.add_pr_to_db(@title, @reviewer.username)
          @reviewer = nil
          Reviewlette::Graphgenerator.new.write_to_graphs('graph.html',Reviewlette::Graphgenerator.new.model_graphs(Reviewlette::Database.new.conscruct_line_data.to_json, Reviewlette::Database.new.conscruct_graph_struct.to_json, 'Donut'))
        else
          comment_on_error
        end
      end
      puts 'No new issues.' unless @issues.present?
    end
  end
end

.transform_nameObject

Get Trelloname from configfile.



167
168
169
# File 'lib/reviewlette.rb', line 167

def transform_name
  @githubname = @db.find_gh_name_by_trello_name(@trelloname)
end

.update_vacationsObject



78
79
80
81
82
83
# File 'lib/reviewlette.rb', line 78

def update_vacations
  @db.get_users_tel_entries.each do |name|
    @vacations = Reviewlette::Vacations.find_vacations(name) #tel_name
    evaluate_vacations(name)
  end
end