Class: FlakyTestTracker::Storage::GitHubIssueStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/flaky_test_tracker/storage/github_issue_storage.rb

Overview

Store Test on the GitHub Issue of a GitHub repository.

Constant Summary collapse

DEFAULT_LABELS =
["flaky test"].freeze
DEFAULT_TITLE_TEMPLATE =
"Flaky test <%= test.reference %>"
DEFAULT_BODY_TEMPLATE =
<<~ERB
  ### Reference
  <%= test.reference %>

  ### Description
  <i><%= test.description %></i>

  ### Exception
  <pre><%= test.exception %></pre>

  ### Failed at
  <%= test.finished_at %>

  ### Number occurrences
  <%= test.number_occurrences %>

  ### Location
  [<%= test.location %>](<%= test.source_location_url %>)
ERB

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, repository:, labels:, title_rendering:, body_rendering:, serializer:) ⇒ GitHubIssueStorage

Returns a new instance of GitHubIssueStorage.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 73

def initialize(
  client:,
  repository:,
  labels:,
  title_rendering:,
  body_rendering:,
  serializer:
)
  @client = client
  @repository = repository
  @labels = labels
  @title_rendering = title_rendering
  @body_rendering = body_rendering
  @serializer = serializer
end

Instance Attribute Details

#body_rendering#output

Template rendering used to create GitHub Issue body.

Returns:

  • (#output)

    the current value of body_rendering



17
18
19
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 17

def body_rendering
  @body_rendering
end

#client#issue #create_issue #update_issue #close_issue

GitHub client.

Returns:

  • (#issue #create_issue #update_issue #close_issue)

    the current value of client



17
18
19
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 17

def client
  @client
end

#labelsArray<String>

GitHub Issue labels used to store tests.

Returns:

  • (Array<String>)

    the current value of labels



17
18
19
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 17

def labels
  @labels
end

#repositoryString

GitHub repository name with organization name or user name. Ex: "masterT/flaky_test_tracker".

Returns:

  • (String)

    the current value of repository



17
18
19
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 17

def repository
  @repository
end

#serializer#serialize, #deserialize

The serializer used to store the Test in the GitHub Issue body.

Returns:

  • (#serialize, #deserialize)

    the current value of serializer



17
18
19
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 17

def serializer
  @serializer
end

#title_rendering#output

Template rendering used to create GitHub Issue title.

Returns:

  • (#output)

    the current value of title_rendering



17
18
19
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 17

def title_rendering
  @title_rendering
end

Class Method Details

.build(client:, repository:, labels: DEFAULT_LABELS, title_template: DEFAULT_TITLE_TEMPLATE, body_template: DEFAULT_BODY_TEMPLATE, serializer: FlakyTestTracker::Serializers::TestHTMLSerializer.new) ⇒ Object

Parameters:

  • client (Hash)

    The options used to initialize Octokit::Client. It will be merged with {auto_paginate:true}.

  • repository (String)

    GitHub repository name with organization name or user name. Ex: "masterT/flaky_test_tracker".

  • labels (Array<String>) (defaults to: DEFAULT_LABELS)

    GitHub Issue labels used to store tests.

  • title_template (String) (defaults to: DEFAULT_TITLE_TEMPLATE)

    ERB Template used to render the GitHub Issue title. The variable "test" will be bind to the ERB template which represent stored Test.

  • body_template (String) (defaults to: DEFAULT_BODY_TEMPLATE)

    ERB Template used to render the GitHub Issue body. The variable "test" will be bind to the ERB template which represent stored Test.

  • serializer (#serialize #deserialize) (defaults to: FlakyTestTracker::Serializers::TestHTMLSerializer.new)

    Serializer used to serialize/deserialize Test. This will be prepend to the GitHub Issue body.

See Also:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 53

def self.build(
  client:,
  repository:,
  labels: DEFAULT_LABELS,
  title_template: DEFAULT_TITLE_TEMPLATE,
  body_template: DEFAULT_BODY_TEMPLATE,
  serializer: FlakyTestTracker::Serializers::TestHTMLSerializer.new
)
  new(
    client: Octokit::Client.new(client.merge(auto_paginate: true)),
    repository: repository,
    labels: labels,
    title_rendering: FlakyTestTracker::Rendering.new(template: title_template),
    body_rendering: FlakyTestTracker::Rendering.new(template: body_template),
    serializer: serializer
  )
end

Instance Method Details

#allArray<Test>

Returns:



90
91
92
93
94
95
96
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 90

def all
  # See https://octokit.github.io/octokit.rb/Octokit/Client/Issues.html#list_issues-instance_method
  client
    .list_issues(repository, { labels: labels.join(",") })
    .map { |github_issue| to_model(github_issue) }
    .compact
end

#create(test_input) ⇒ Test

Returns:



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 99

def create(test_input)
  test = FlakyTestTracker::Test.new(test_input.serializable_hash)
  # See https://octokit.github.io/octokit.rb/Octokit/Client/Issues.html#create_issue-instance_method
  github_issue = client.create_issue(
    repository,
    render_title(test: test),
    render_body(test: test),
    { labels: labels.join(",") }
  )
  to_model(github_issue)
end

#delete(id) ⇒ Test

Returns:



127
128
129
130
131
132
133
134
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 127

def delete(id)
  # See https://octokit.github.io/octokit.rb/Octokit/Client/Issues.html#close_issue-instance_method
  github_issue = client.close_issue(
    repository,
    id
  )
  to_model(github_issue)
end

#update(id, test_input) ⇒ Test

Returns:



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/flaky_test_tracker/storage/github_issue_storage.rb', line 112

def update(id, test_input)
  test = FlakyTestTracker::Test.new(test_input.serializable_hash)
  # See https://octokit.github.io/octokit.rb/Octokit/Client/Issues.html#update_issue-instance_method
  state = test.resolved? ? "closed" : "open"
  github_issue = client.update_issue(
    repository,
    id,
    render_title(test: test),
    render_body(test: test),
    { labels: labels, state: state }
  )
  to_model(github_issue)
end