Class: Soba::Services::WorkflowBlockingChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/soba/services/workflow_blocking_checker.rb

Constant Summary collapse

ACTIVE_LABELS =
%w(
  soba:queued
  soba:planning
  soba:ready
  soba:doing
  soba:reviewing
  soba:revising
).freeze
INTERMEDIATE_LABELS =
%w(
  soba:review-requested
  soba:requires-changes
  soba:done
  soba:merged
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(github_client:, logger: nil) ⇒ WorkflowBlockingChecker

Returns a new instance of WorkflowBlockingChecker.



24
25
26
27
# File 'lib/soba/services/workflow_blocking_checker.rb', line 24

def initialize(github_client:, logger: nil)
  @github_client = github_client
  @logger = logger || SemanticLogger["WorkflowBlockingChecker"]
end

Instance Attribute Details

#github_clientObject (readonly)

Returns the value of attribute github_client.



22
23
24
# File 'lib/soba/services/workflow_blocking_checker.rb', line 22

def github_client
  @github_client
end

#loggerObject (readonly)

Returns the value of attribute logger.



22
23
24
# File 'lib/soba/services/workflow_blocking_checker.rb', line 22

def logger
  @logger
end

Instance Method Details

#blocking?(repository, issues:, except_issue_number: nil) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/soba/services/workflow_blocking_checker.rb', line 29

def blocking?(repository, issues:, except_issue_number: nil)
  !blocking_issues(repository, issues: issues, except_issue_number: except_issue_number).empty?
end

#blocking_issues(repository, issues:, except_issue_number: nil) ⇒ Object



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/soba/services/workflow_blocking_checker.rb', line 33

def blocking_issues(repository, issues:, except_issue_number: nil)
  # 引数で渡されたissuesからACTIVE_LABELSまたはINTERMEDIATE_LABELSを持つものを検出
  # except_issue_numberが指定されている場合は、そのissueを除外
  blocking = issues.select do |issue|
    if except_issue_number && issue.number == except_issue_number
      next false
    end

    issue.labels.any? do |label|
      label_name = label.is_a?(Hash) ? label[:name] : label.name
      ACTIVE_LABELS.include?(label_name) || INTERMEDIATE_LABELS.include?(label_name)
    end
  end

  logger&.debug("Found #{blocking.size} blocking issues with ACTIVE_LABELS or INTERMEDIATE_LABELS")
  blocking.each do |issue|
    labels = issue.labels.map { |l| l.is_a?(Hash) ? l[:name] : l.name }.
      select { |n| ACTIVE_LABELS.include?(n) || INTERMEDIATE_LABELS.include?(n) }
    logger&.debug("Issue ##{issue.number}: #{labels.join(', ')}")
  end

  blocking.compact.uniq { |issue| issue.number }
end

#blocking_reason(repository, issues:, except_issue_number: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/soba/services/workflow_blocking_checker.rb', line 57

def blocking_reason(repository, issues:, except_issue_number: nil)
  blocking = blocking_issues(repository, issues: issues, except_issue_number: except_issue_number)
  return nil if blocking.empty?

  issue = blocking.first
  label = issue.labels.find do |l|
    label_name = l.is_a?(Hash) ? l[:name] : l.name
    ACTIVE_LABELS.include?(label_name) || INTERMEDIATE_LABELS.include?(label_name)
  end
  return nil unless label

  label_name = label.is_a?(Hash) ? label[:name] : label.name
  "Issue ##{issue.number}#{label_name} のため、新しいワークフローの開始をスキップしました"
end