rosette-core 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +26 -0
  3. data/History.txt +3 -0
  4. data/README.md +94 -0
  5. data/Rakefile +18 -0
  6. data/lib/rosette/core.rb +110 -0
  7. data/lib/rosette/core/branch_utils.rb +152 -0
  8. data/lib/rosette/core/commands.rb +139 -0
  9. data/lib/rosette/core/commands/errors.rb +17 -0
  10. data/lib/rosette/core/commands/git/commit_command.rb +65 -0
  11. data/lib/rosette/core/commands/git/diff_base_command.rb +301 -0
  12. data/lib/rosette/core/commands/git/diff_command.rb +188 -0
  13. data/lib/rosette/core/commands/git/diff_entry.rb +44 -0
  14. data/lib/rosette/core/commands/git/fetch_command.rb +27 -0
  15. data/lib/rosette/core/commands/git/repo_snapshot_command.rb +40 -0
  16. data/lib/rosette/core/commands/git/show_command.rb +70 -0
  17. data/lib/rosette/core/commands/git/snapshot_command.rb +50 -0
  18. data/lib/rosette/core/commands/git/status_command.rb +128 -0
  19. data/lib/rosette/core/commands/git/with_non_merge_ref.rb +48 -0
  20. data/lib/rosette/core/commands/git/with_ref.rb +92 -0
  21. data/lib/rosette/core/commands/git/with_refs.rb +92 -0
  22. data/lib/rosette/core/commands/git/with_repo_name.rb +50 -0
  23. data/lib/rosette/core/commands/git/with_snapshots.rb +45 -0
  24. data/lib/rosette/core/commands/queuing/enqueue_commit_command.rb +37 -0
  25. data/lib/rosette/core/commands/queuing/requeue_commit_command.rb +46 -0
  26. data/lib/rosette/core/commands/translations/export_command.rb +257 -0
  27. data/lib/rosette/core/commands/translations/translation_lookup_command.rb +66 -0
  28. data/lib/rosette/core/commands/translations/with_locale.rb +47 -0
  29. data/lib/rosette/core/configurator.rb +160 -0
  30. data/lib/rosette/core/error_reporters/buffered_error_reporter.rb +96 -0
  31. data/lib/rosette/core/error_reporters/error_reporter.rb +31 -0
  32. data/lib/rosette/core/error_reporters/nil_error_reporter.rb +25 -0
  33. data/lib/rosette/core/error_reporters/printing_error_reporter.rb +58 -0
  34. data/lib/rosette/core/error_reporters/raising_error_reporter.rb +27 -0
  35. data/lib/rosette/core/errors.rb +93 -0
  36. data/lib/rosette/core/extractor/commit_log.rb +33 -0
  37. data/lib/rosette/core/extractor/commit_log_status.rb +57 -0
  38. data/lib/rosette/core/extractor/commit_processor.rb +109 -0
  39. data/lib/rosette/core/extractor/extractor.rb +72 -0
  40. data/lib/rosette/core/extractor/extractor_config.rb +74 -0
  41. data/lib/rosette/core/extractor/locale.rb +118 -0
  42. data/lib/rosette/core/extractor/phrase.rb +76 -0
  43. data/lib/rosette/core/extractor/phrase/phrase_index_policy.rb +108 -0
  44. data/lib/rosette/core/extractor/phrase/phrase_to_hash.rb +33 -0
  45. data/lib/rosette/core/extractor/repo_config.rb +339 -0
  46. data/lib/rosette/core/extractor/serializer_config.rb +55 -0
  47. data/lib/rosette/core/extractor/static_extractor.rb +44 -0
  48. data/lib/rosette/core/extractor/translation.rb +44 -0
  49. data/lib/rosette/core/extractor/translation/translation_to_hash.rb +28 -0
  50. data/lib/rosette/core/git/diff_finder.rb +131 -0
  51. data/lib/rosette/core/git/ref.rb +116 -0
  52. data/lib/rosette/core/git/repo.rb +378 -0
  53. data/lib/rosette/core/path_matcher_factory.rb +330 -0
  54. data/lib/rosette/core/resolvers/extractor_id.rb +37 -0
  55. data/lib/rosette/core/resolvers/integration_id.rb +37 -0
  56. data/lib/rosette/core/resolvers/preprocessor_id.rb +38 -0
  57. data/lib/rosette/core/resolvers/resolver.rb +115 -0
  58. data/lib/rosette/core/resolvers/serializer_id.rb +37 -0
  59. data/lib/rosette/core/snapshots/cached_head_snapshot_factory.rb +51 -0
  60. data/lib/rosette/core/snapshots/cached_snapshot_factory.rb +67 -0
  61. data/lib/rosette/core/snapshots/head_snapshot_factory.rb +58 -0
  62. data/lib/rosette/core/snapshots/repo_config_path_filter.rb +83 -0
  63. data/lib/rosette/core/snapshots/snapshot_factory.rb +184 -0
  64. data/lib/rosette/core/string_utils.rb +23 -0
  65. data/lib/rosette/core/translation_status.rb +81 -0
  66. data/lib/rosette/core/validators.rb +18 -0
  67. data/lib/rosette/core/validators/commit_validator.rb +62 -0
  68. data/lib/rosette/core/validators/commits_validator.rb +32 -0
  69. data/lib/rosette/core/validators/encoding_validator.rb +32 -0
  70. data/lib/rosette/core/validators/locale_validator.rb +37 -0
  71. data/lib/rosette/core/validators/repo_validator.rb +33 -0
  72. data/lib/rosette/core/validators/serializer_validator.rb +37 -0
  73. data/lib/rosette/core/validators/validator.rb +31 -0
  74. data/lib/rosette/core/version.rb +8 -0
  75. data/lib/rosette/data_stores.rb +11 -0
  76. data/lib/rosette/data_stores/errors.rb +26 -0
  77. data/lib/rosette/data_stores/phrase_status.rb +59 -0
  78. data/lib/rosette/integrations.rb +12 -0
  79. data/lib/rosette/integrations/errors.rb +15 -0
  80. data/lib/rosette/integrations/integratable.rb +58 -0
  81. data/lib/rosette/integrations/integration.rb +23 -0
  82. data/lib/rosette/preprocessors.rb +11 -0
  83. data/lib/rosette/preprocessors/errors.rb +14 -0
  84. data/lib/rosette/preprocessors/preprocessor.rb +48 -0
  85. data/lib/rosette/queuing.rb +14 -0
  86. data/lib/rosette/queuing/commits.rb +19 -0
  87. data/lib/rosette/queuing/commits/commit_conductor.rb +90 -0
  88. data/lib/rosette/queuing/commits/commit_job.rb +93 -0
  89. data/lib/rosette/queuing/commits/commits_queue_configurator.rb +60 -0
  90. data/lib/rosette/queuing/commits/extract_stage.rb +46 -0
  91. data/lib/rosette/queuing/commits/fetch_stage.rb +51 -0
  92. data/lib/rosette/queuing/commits/finalize_stage.rb +76 -0
  93. data/lib/rosette/queuing/commits/phrase_storage_granularity.rb +20 -0
  94. data/lib/rosette/queuing/commits/push_stage.rb +91 -0
  95. data/lib/rosette/queuing/commits/stage.rb +96 -0
  96. data/lib/rosette/queuing/job.rb +74 -0
  97. data/lib/rosette/queuing/queue.rb +28 -0
  98. data/lib/rosette/queuing/queue_configurator.rb +76 -0
  99. data/lib/rosette/queuing/worker.rb +30 -0
  100. data/lib/rosette/serializers.rb +10 -0
  101. data/lib/rosette/serializers/serializer.rb +98 -0
  102. data/lib/rosette/tms.rb +9 -0
  103. data/lib/rosette/tms/repository.rb +95 -0
  104. data/rosette-core.gemspec +24 -0
  105. data/spec/core/branch_utils_spec.rb +110 -0
  106. data/spec/core/commands/git/commit_command_spec.rb +60 -0
  107. data/spec/core/commands/git/diff_command_spec.rb +263 -0
  108. data/spec/core/commands/git/fetch_command_spec.rb +61 -0
  109. data/spec/core/commands/git/repo_snapshot_command_spec.rb +72 -0
  110. data/spec/core/commands/git/show_command_spec.rb +128 -0
  111. data/spec/core/commands/git/snapshot_command_spec.rb +86 -0
  112. data/spec/core/commands/git/status_command_spec.rb +154 -0
  113. data/spec/core/commands/queuing/enqueue_commit_command_spec.rb +34 -0
  114. data/spec/core/commands/queuing/requeue_commit_command_spec.rb +46 -0
  115. data/spec/core/commands/translations/export_command_spec.rb +113 -0
  116. data/spec/core/commands/translations/translation_lookup_command_spec.rb +58 -0
  117. data/spec/core/configurator_spec.rb +47 -0
  118. data/spec/core/error_reporters/buffered_error_reporter_spec.rb +61 -0
  119. data/spec/core/error_reporters/nil_error_reporter_spec.rb +16 -0
  120. data/spec/core/error_reporters/printing_error_reporter_spec.rb +60 -0
  121. data/spec/core/extractor/commit_log_status_spec.rb +216 -0
  122. data/spec/core/extractor/commit_processor_spec.rb +68 -0
  123. data/spec/core/extractor/extractor_config_spec.rb +47 -0
  124. data/spec/core/extractor/extractor_spec.rb +26 -0
  125. data/spec/core/extractor/locale_spec.rb +92 -0
  126. data/spec/core/extractor/phrase/phrase_index_policy_spec.rb +116 -0
  127. data/spec/core/extractor/phrase/phrase_to_hash_spec.rb +18 -0
  128. data/spec/core/extractor/repo_config_spec.rb +147 -0
  129. data/spec/core/extractor/translation/translation_to_hash_spec.rb +25 -0
  130. data/spec/core/git/diff_finder_spec.rb +74 -0
  131. data/spec/core/git/ref_spec.rb +118 -0
  132. data/spec/core/git/repo_spec.rb +216 -0
  133. data/spec/core/path_matcher_factory_spec.rb +139 -0
  134. data/spec/core/resolvers/extractor_id_spec.rb +47 -0
  135. data/spec/core/resolvers/integration_id_spec.rb +47 -0
  136. data/spec/core/resolvers/preprocessor_id_spec.rb +47 -0
  137. data/spec/core/resolvers/serializer_id_spec.rb +47 -0
  138. data/spec/core/snapshots/snapshot_factory_spec.rb +145 -0
  139. data/spec/core/string_utils_spec.rb +19 -0
  140. data/spec/core/translation_status_spec.rb +91 -0
  141. data/spec/core/validators/commit_validator_spec.rb +40 -0
  142. data/spec/core/validators/encoding_validator_spec.rb +30 -0
  143. data/spec/core/validators/locale_validator_spec.rb +31 -0
  144. data/spec/core/validators/repo_validator_spec.rb +30 -0
  145. data/spec/core/validators/serializer_validator_spec.rb +31 -0
  146. data/spec/integrations/integratable_spec.rb +58 -0
  147. data/spec/queuing/commits/commit_conductor_spec.rb +71 -0
  148. data/spec/queuing/commits/commit_job_spec.rb +87 -0
  149. data/spec/queuing/commits/extract_stage_spec.rb +68 -0
  150. data/spec/queuing/commits/fetch_stage_spec.rb +101 -0
  151. data/spec/queuing/commits/finalize_stage_spec.rb +88 -0
  152. data/spec/queuing/commits/push_stage_spec.rb +145 -0
  153. data/spec/queuing/commits/stage_spec.rb +80 -0
  154. data/spec/queuing/job_spec.rb +33 -0
  155. data/spec/queuing/queue_configurator_spec.rb +44 -0
  156. data/spec/spec_helper.rb +90 -0
  157. data/spec/test_helpers/fake_commit_stage.rb +17 -0
  158. metadata +257 -0
@@ -0,0 +1,60 @@
1
+ # encoding: UTF-8
2
+
3
+ module Rosette
4
+ module Queuing
5
+ module Commits
6
+
7
+ # Configuration specific to the "commits" queue that processes commits.
8
+ #
9
+ # @!attribute [r] name
10
+ # @return [String]
11
+ # @!attribute [r] diff_point
12
+ # @return [String] the diff point to use when computing phrase diffs.
13
+ # Defaults to "master". Used only when +phrase_storage_granularity+
14
+ # is set to [Rosette::Queuing::Commits::PhraseStorageGranularity::BRANCH].
15
+ # @!attribute [r] phrase_storage_granularity
16
+ # @return [String] determines which set of phrases to push to the TMS.
17
+ # Must be one of the constant values in
18
+ # [Rosette::Queuing::Commits::PhraseStorageGranularity]
19
+ #
20
+ # @see Rosette::Queuing::Commits::PhraseStorageGranularity
21
+ class CommitsQueueConfigurator
22
+ DEFAULT_DIFF_POINT = 'master'
23
+ DEFAULT_PHRASE_GRANULARITY = PhraseStorageGranularity::COMMIT
24
+
25
+ attr_reader :name, :diff_point, :phrase_storage_granularity
26
+
27
+ # Creates a new configurator and sets up a few defaults.
28
+ #
29
+ # @param [String] name The name of the queue.
30
+ # @return [CommitsQueueConfigurator]
31
+ def initialize(name)
32
+ @name = name
33
+ @diff_point = DEFAULT_DIFF_POINT
34
+ @phrase_storage_granularity = DEFAULT_PHRASE_GRANULARITY
35
+ end
36
+
37
+ # Sets the phrase storage granularity, i.e. the method used to determine
38
+ # which set of phrases should get uploaded to the TMS.
39
+ #
40
+ # @param [String] granularity One of the constant values in
41
+ # [Rosette::Queuing::Commits::PhraseStorageGranularity].
42
+ # @return [void]
43
+ def set_phrase_storage_granularity(granularity)
44
+ @phrase_storage_granularity = granularity
45
+ end
46
+
47
+ # Sets the diff point to use when computing phrase diffs. Note that this
48
+ # value is only important when +phrase_storage_granularity+ is set to
49
+ # [Rosette::Queuing::Commits::PhraseStorageGranularity::BRANCH].
50
+ #
51
+ # @param [String] new_diff_point The diff point to set.
52
+ # @return [void]
53
+ def set_diff_point(new_diff_point)
54
+ @diff_point = new_diff_point
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: UTF-8
2
+
3
+ module Rosette
4
+ module Queuing
5
+ module Commits
6
+
7
+ # Extracts phrases from the given commit and stores them in the datastore.
8
+ #
9
+ # @see RepoConfig
10
+ class ExtractStage < Stage
11
+ accepts PhraseStatus::FETCHED
12
+
13
+ # Executes this stage and updates the commit log with an +EXTRACTED+
14
+ # status. Uses [Rosette::Core::Commands::CommitCommand] under the hood.
15
+ # If the commit no longer exists in the git repository, the commit log
16
+ # will be updated with a status of +MISSING+.
17
+ #
18
+ # @return [void]
19
+ def execute!
20
+ logger.info("Extracting phrases for #{commit_log.commit_id}")
21
+
22
+ Rosette::Core::Commands::CommitCommand.new(rosette_config)
23
+ .set_repo_name(repo_config.name)
24
+ .set_ref(commit_log.commit_id)
25
+ .execute
26
+
27
+ commit_log.extract
28
+ commit_log.commit_datetime = Time.at(rev_commit.getCommitTime)
29
+
30
+ logger.info("Finished extracting phrases for #{commit_log.commit_id}")
31
+ rescue Java::OrgEclipseJgitErrors::MissingObjectException => e
32
+ commit_log.missing
33
+ ensure
34
+ save_commit_log
35
+ end
36
+
37
+ protected
38
+
39
+ def rev_commit
40
+ @rev_commit ||= repo_config.repo.get_rev_commit(commit_log.commit_id)
41
+ end
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+
3
+ java_import 'org.eclipse.jgit.api.Git'
4
+
5
+ module Rosette
6
+ module Queuing
7
+ module Commits
8
+
9
+ # Fetches the repository. This should be the first commit processing stage
10
+ # since it ensures the given commit is available in the repository for
11
+ # processing.
12
+ #
13
+ # @see RepoConfig
14
+ class FetchStage < Stage
15
+ accepts PhraseStatus::NOT_SEEN, PhraseStatus::NOT_FOUND
16
+
17
+ # Executes this stage and updates the commit log with a +FETCHED+
18
+ # status. Performs a git fetch.
19
+ #
20
+ # @return [void]
21
+ def execute!
22
+ logger.info("Fetching git repository #{repo_config.name}")
23
+
24
+ git.fetch
25
+ .setRemote('origin')
26
+ .setRemoveDeletedRefs(true)
27
+ .call
28
+
29
+ commit_log.fetch
30
+
31
+ # git won't know about the commit before a fetch, which is why branch
32
+ # name is set in this stage and not when the commit is first enqueued
33
+ commit_log.branch_name = Rosette::Core::BranchUtils.derive_branch_name(
34
+ commit_log.commit_id, repo_config.repo
35
+ )
36
+
37
+ save_commit_log
38
+
39
+ logger.info("Finished fetching git repository #{repo_config.name}")
40
+ end
41
+
42
+ private
43
+
44
+ def git
45
+ @git ||= Git.new(repo_config.repo.jgit_repo)
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,76 @@
1
+ # encoding: UTF-8
2
+
3
+ module Rosette
4
+ module Queuing
5
+ module Commits
6
+
7
+ # Performs cleanup tasks for the given commit as defined by the configured
8
+ # translation management system.
9
+ #
10
+ # @see RepoConfig
11
+ class FinalizeStage < Stage
12
+ accepts PhraseStatus::PUSHED
13
+
14
+ # The number of seconds to wait in between consecutive pulls. This value
15
+ # will be passed to the queue implementation, as delay is handled at the
16
+ # queue layer.
17
+ CONSECUTIVE_FINALIZE_DELAY_MIN = 10 * 60 # 10 minutes
18
+ CONSECUTIVE_FINALIZE_DELAY_MAX = 45 * 60 # 45 minutes
19
+
20
+ # Executes this stage and updates the commit log. If the commit has been
21
+ # fully translated, the commit log will be updated with a +FINALIZED+
22
+ # status, and the +finalize+ method will be called on the translation
23
+ # management system. If the commit has not been fully translated, the
24
+ # commit log's status won't be updated and +finalize+ will not be
25
+ # called. In both cases, commit log locale entries will be updated to
26
+ # track translation progress.
27
+ #
28
+ # @return [void]
29
+ def execute!
30
+ logger.info("Finalizing commit #{commit_log.commit_id}")
31
+
32
+ status = repo_config.tms.status(commit_log.commit_id)
33
+
34
+ repo_config.locales.each do |locale|
35
+ locale_code = locale.code
36
+
37
+ rosette_config.datastore.add_or_update_commit_log_locale(
38
+ commit_log.commit_id, locale_code, status.locale_count(locale_code)
39
+ )
40
+ end
41
+
42
+ if status.fully_translated?
43
+ repo_config.tms.finalize(commit_log.commit_id)
44
+ commit_log.finalize
45
+ save_commit_log
46
+ end
47
+
48
+ logger.info("Finished finalizing commit #{commit_log.commit_id}")
49
+ end
50
+
51
+ # Converts this stage to a job that can be enqueued. This method should
52
+ # be called after +#execute!+, meaning the commit log has been updated
53
+ # to the next status in the pipeline. If that next status also happens
54
+ # to be PUSHED, this method adds a delay to avoid finalizing too often.
55
+ # If the chosen queue implementation does not support delays, setting
56
+ # this value should be a safe no-op (i.e. have no adverse side-effects).
57
+ #
58
+ # @return [CommitJob]
59
+ def to_job
60
+ super.tap do |job|
61
+ if commit_log.status == PhraseStatus::PUSHED
62
+ job.set_delay(random_delay)
63
+ end
64
+ end
65
+ end
66
+
67
+ protected
68
+
69
+ def random_delay
70
+ rand(CONSECUTIVE_FINALIZE_DELAY_MIN..CONSECUTIVE_FINALIZE_DELAY_MAX)
71
+ end
72
+ end
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+
3
+ module Rosette
4
+ module Queuing
5
+ module Commits
6
+
7
+ # Provides a set of constants that specify how to determine the set of
8
+ # phrases to push to a translation management system (TMS).
9
+ class PhraseStorageGranularity
10
+ # Push only the phrases that were added or changed in single commits.
11
+ COMMIT = 'COMMIT'
12
+
13
+ # Push all the phrases contained by each git branch, regardless of the
14
+ # order of commits.
15
+ BRANCH = 'BRANCH'
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,91 @@
1
+ # encoding: UTF-8
2
+
3
+ module Rosette
4
+ module Queuing
5
+ module Commits
6
+
7
+ # Saves (or "pushes") a set of phrases from the given commit to the
8
+ # configured translation management system (TMS).
9
+ #
10
+ # @see RepoConfig
11
+ class PushStage < Stage
12
+ accepts PhraseStatus::EXTRACTED
13
+
14
+ # Executes this stage and updates the commit log. If the commit queue
15
+ # phrase storage granularity is set to +COMMIT+, only the phrases added
16
+ # or modified in the given commit will get pushed to the TMS. If the
17
+ # granularity is instead set to +BRANCH+, the full phrase diff between
18
+ # the commit's branch and the configured diff point (usually master)
19
+ # will get pushed to the TMS. If the commit or diff contains no phrases,
20
+ # this method doesn't push anything but will still update the commit log
21
+ # with a +FINALIZED+ status. If the commit no longer exists in the git
22
+ # repository, the commit log will be updated with a status of +MISSING+.
23
+ #
24
+ # @return [void]
25
+ def execute!
26
+ logger.info("Pushing commit #{commit_log.commit_id}")
27
+
28
+ if phrases.size > 0
29
+ commit_log.phrase_count = phrases.size
30
+ repo_config.tms.store_phrases(phrases, commit_log.commit_id)
31
+ commit_log.push
32
+ else
33
+ commit_log.finalize!
34
+ end
35
+
36
+ logger.info("Finished pushing commit #{commit_log.commit_id}")
37
+ rescue Java::OrgEclipseJgitErrors::MissingObjectException => ex
38
+ commit_log.missing
39
+ ensure
40
+ save_commit_log
41
+ end
42
+
43
+ protected
44
+
45
+ def granularity
46
+ case queue_config.phrase_storage_granularity
47
+ when PhraseStorageGranularity::BRANCH
48
+ if commit_log.branch_name
49
+ PhraseStorageGranularity::BRANCH
50
+ else
51
+ PhraseStorageGranularity::COMMIT
52
+ end
53
+ else
54
+ queue_config.phrase_storage_granularity
55
+ end
56
+ end
57
+
58
+ def phrases
59
+ @phrases ||= case granularity
60
+ when PhraseStorageGranularity::COMMIT
61
+ phrases_from(diff_for_commit)
62
+ when PhraseStorageGranularity::BRANCH
63
+ phrases_from(diff_for_branch)
64
+ end
65
+ end
66
+
67
+ def diff_for_commit
68
+ Rosette::Core::Commands::ShowCommand.new(rosette_config)
69
+ .set_repo_name(repo_config.name)
70
+ .set_commit_id(commit_log.commit_id)
71
+ .set_strict(false)
72
+ .execute
73
+ end
74
+
75
+ def diff_for_branch
76
+ Rosette::Core::Commands::DiffCommand.new(rosette_config)
77
+ .set_repo_name(repo_config.name)
78
+ .set_head_ref(commit_log.branch_name)
79
+ .set_diff_point_ref(queue_config.diff_point)
80
+ .set_strict(false)
81
+ .execute
82
+ end
83
+
84
+ def phrases_from(diff)
85
+ (diff[:added] + diff[:modified]).map(&:phrase)
86
+ end
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: UTF-8
2
+
3
+ module Rosette
4
+ module Queuing
5
+ module Commits
6
+
7
+ # Base class for stages of the commit processing pipeline.
8
+ #
9
+ # @!attribute [r] rosette_config
10
+ # @return [Configurator] the Rosette config to use.
11
+ # @!attribute [r] repo_config
12
+ # @return [RepoConfig] the repo config to use.
13
+ # @!attribute [r] logger
14
+ # @return [Logger] the logger to log messages to.
15
+ # @!attribute [r] commit_log
16
+ # @return [CommitLog] the commit log to process.
17
+ class Stage
18
+ PhraseStatus = Rosette::DataStores::PhraseStatus
19
+
20
+ class << self
21
+ # Sets the +PhraseStatus+es this stage can handle.
22
+ #
23
+ # @param [Array<String>] statuses A splatted list of statuses.
24
+ # @return [void]
25
+ def accepts(*statuses)
26
+ @_accepts_statuses = statuses
27
+ end
28
+
29
+ # Returns true if this stage accepts the given commit log (i.e. can
30
+ # process it), false otherwise.
31
+ #
32
+ # @param [CommitLog] commit_log The commit log to check.
33
+ # @return [Boolean]
34
+ def accepts?(commit_log)
35
+ @_accepts_statuses.include?(commit_log.status)
36
+ end
37
+
38
+ # If given a commit log that this stage accepts, returns an instance
39
+ # of this stage class. If the stage does not accept the commit log,
40
+ # returns +nil+.
41
+ #
42
+ # @param [CommitLog] commit_log The commit log to wrap.
43
+ # @param [Configurator] rosette_config The Rosette config to pass to
44
+ # the stage instance.
45
+ # @param [Logger] logger The logger to pass to the stage instance.
46
+ # @return [Stage] an instance of this stage or +nil+.
47
+ def for_commit_log(commit_log, rosette_config, logger)
48
+ if accepts?(commit_log)
49
+ repo_config = rosette_config.get_repo(commit_log.repo_name)
50
+ new(rosette_config, repo_config, logger, commit_log)
51
+ end
52
+ end
53
+ end
54
+
55
+ attr_reader :rosette_config, :repo_config, :logger, :commit_log
56
+
57
+ # Creates a new instance of this stage.
58
+ #
59
+ # @param [Configurator] rosette_config The Rosette config to use.
60
+ # @param [RepoConfig] repo_config The repo config to use.
61
+ # @param [Logger] logger The logger to log messages to.
62
+ # @param [CommitLog] commit_log The commit log to process.
63
+ # @return [Stage]
64
+ def initialize(rosette_config, repo_config, logger, commit_log)
65
+ @rosette_config = rosette_config
66
+ @repo_config = repo_config
67
+ @logger = logger
68
+ @commit_log = commit_log
69
+ end
70
+
71
+ # Converts this stage to a job that can be enqueued.
72
+ #
73
+ # @return [CommitJob]
74
+ def to_job
75
+ CommitJob.from_stage(self)
76
+ end
77
+
78
+ protected
79
+
80
+ def save_commit_log
81
+ rosette_config.datastore.add_or_update_commit_log(
82
+ commit_log.repo_name, commit_log.commit_id,
83
+ commit_log.commit_datetime, commit_log.status,
84
+ commit_log.phrase_count, commit_log.branch_name
85
+ )
86
+ end
87
+
88
+ def queue_config
89
+ @queue_config ||=
90
+ rosette_config.queue.configurator.get_queue_config('commits')
91
+ end
92
+ end
93
+
94
+ end
95
+ end
96
+ end