fa-harness-tools 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 421353286e74cdc70badf595a611fc8d9a34a5f14e4341cacac88bbba11cee19
4
- data.tar.gz: f80a236d16ea035736191b65c2138abec2cff6c126d36b930b3866e01d50489d
3
+ metadata.gz: 51253c635049f814f42fcac831fd937ed713254a29547cf5915fc2861848a52e
4
+ data.tar.gz: ed786f440ef207a1f2d6b4c920d82f673c1726c81571f4d0cec692f02a2c42a4
5
5
  SHA512:
6
- metadata.gz: d610913dbd0979c1d6d5a19a04fe68b511042e333fbd4a616919855fb1d34dca130ea10e1e6a5d58d0579ad86fd796f9e2983a19ae222ddd2dea9b1015bbd229
7
- data.tar.gz: 0fa44445386572aa266ef138f1bcc1bda3b84ea8b0fe6f98cfd86f17d58c0e5f045650c9b19bbc7309018f1cb9e0237f3e07f34f01e75b0110009d20a26035c3
6
+ metadata.gz: 3cb5bc18ffb6355a629de71c8220f851e12a85814e59549f8cfd7b259d4425d4422f81e0d85dc2e80e05e9fa76c5d5b3dfb12fe161fc61a9b87550dbd61300af
7
+ data.tar.gz: 73877cc9224058d573a41b3ab1b34e67d0fe8bbf06c17a408b37f646d552d7b627fab219d2b042654297b9ccb9dae09fbc4f1d833a6fae3e798b3603970fc320
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fa-harness-tools (1.1.0)
4
+ fa-harness-tools (1.2.0)
5
+ fugit (~> 1.3)
5
6
  octokit (~> 4.0)
6
7
  pastel (~> 0.7)
7
8
  tzinfo (~> 2.0)
@@ -15,8 +16,13 @@ GEM
15
16
  concurrent-ruby (1.1.6)
16
17
  diff-lcs (1.3)
17
18
  equatable (0.6.1)
19
+ et-orbi (1.2.4)
20
+ tzinfo
18
21
  faraday (1.0.1)
19
22
  multipart-post (>= 1.2, < 3)
23
+ fugit (1.3.5)
24
+ et-orbi (~> 1.1, >= 1.1.8)
25
+ raabro (~> 1.1)
20
26
  multipart-post (2.1.1)
21
27
  octokit (4.18.0)
22
28
  faraday (>= 0.9)
@@ -25,6 +31,7 @@ GEM
25
31
  equatable (~> 0.6)
26
32
  tty-color (~> 0.5)
27
33
  public_suffix (4.0.5)
34
+ raabro (1.3.1)
28
35
  rake (13.0.1)
29
36
  rspec (3.9.0)
30
37
  rspec-core (~> 3.9.0)
@@ -60,4 +67,4 @@ DEPENDENCIES
60
67
  timecop (~> 0.9)
61
68
 
62
69
  BUNDLED WITH
63
- 1.16.4
70
+ 1.17.2
@@ -3,11 +3,32 @@
3
3
  require "fa-harness-tools"
4
4
  require "optparse"
5
5
 
6
- options = {}
6
+ options = {
7
+ schedules: [],
8
+ timezone: "Europe/London",
9
+ }
7
10
  OptionParser.new do |opts|
8
11
  opts.banner = "Usage: check-schedule [options]"
12
+
13
+ opts.on("--schedule CRON", "Schedule defines a window in which a deployment can take place. Accepts cron syntax, e.g. '* 9-15 * * mon-thu'") do |v|
14
+ options[:schedules] << v
15
+ end
16
+
17
+ opts.on("--timezone TIMEZONE", "Specify the timezone which the schedule will be checked in, e.g. 'Europe/London'") do |v|
18
+ options[:timezone] = v
19
+ end
9
20
  end.parse!
10
21
 
11
- result = FaHarnessTools::CheckSchedule.new.verify?
22
+ def schedules(options)
23
+ if options[:schedules].length == 0
24
+ options[:schedules] = [
25
+ "* 9-15 * * mon-thu",
26
+ "* 9-11 * * fri",
27
+ ]
28
+ end
29
+ options[:schedules].map { |schedule| FaHarnessTools::Schedule.new(schedule: schedule.to_s) }
30
+ end
31
+
32
+ result = FaHarnessTools::CheckSchedule.new(timezone: options[:timezone], schedules: schedules(options)).verify?
12
33
 
13
34
  exit result ? 0 : 1
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_runtime_dependency "pastel", "~> 0.7"
31
31
  spec.add_runtime_dependency "tzinfo", "~> 2.0"
32
32
  spec.add_runtime_dependency "tzinfo-data", "~> 1.0"
33
+ spec.add_runtime_dependency "fugit", "~> 1.3"
33
34
 
34
35
  spec.add_development_dependency "bundler", "~> 1.0"
35
36
  spec.add_development_dependency "rake", "~> 13.0"
@@ -3,10 +3,12 @@ require "fa-harness-tools/check_branch_protection"
3
3
  require "fa-harness-tools/check_forward_deploy"
4
4
  require "fa-harness-tools/check_recent_deploy"
5
5
  require "fa-harness-tools/check_schedule"
6
+ require "fa-harness-tools/schedule"
6
7
  require "fa-harness-tools/github_client"
7
8
  require "fa-harness-tools/harness_context"
8
9
  require "fa-harness-tools/version"
9
10
 
10
11
  module FaHarnessTools
11
12
  LookupError = Class.new(StandardError)
13
+ InvalidScheduleError = Class.new(StandardError)
12
14
  end
@@ -3,14 +3,13 @@ require "tzinfo"
3
3
 
4
4
  module FaHarnessTools
5
5
  # Check against the time of day so you can restrict deploying to sensible
6
- # hours. Uses local London time by default.
7
- #
8
- # Restricts to Mon-Thu from 9am to 4pm, Fri from 9am to 12pm.
6
+ # hours.
9
7
  class CheckSchedule
10
- def initialize(timezone: "Europe/London")
8
+ def initialize(timezone:, schedules:)
11
9
  tz = TZInfo::Timezone.get(timezone)
12
10
  @timezone = timezone
13
11
  @now = tz.to_local(Time.now.utc)
12
+ @schedules = schedules
14
13
  @logger = CheckLogger.new(
15
14
  name: "Check deployment schedule",
16
15
  description: "Only allow deployments within certain times of the day",
@@ -22,19 +21,16 @@ module FaHarnessTools
22
21
  @logger.info("operating in the #{@timezone} timezone")
23
22
  @logger.info("local time is #{@now}")
24
23
 
25
- permitted = false
26
- case @now.wday
27
- when 1..4
28
- @logger.info("deployments are allowed between 9am to 4pm today (Mon-Thu)")
29
- permitted = true if @now.hour >= 9 && @now.hour < 16
30
- when 5
31
- @logger.info("deployments are allowed between 9am to 12pm today (Fri)")
32
- permitted = true if @now.hour >= 9 && @now.hour < 12
24
+ permitted = @schedules.any? do |schedule|
25
+ can_run = schedule.can_run?(time: @now)
26
+ @logger.info("deployments are allowed due to the following schedule: #{schedule.to_s}") if can_run
27
+ can_run
33
28
  end
34
29
 
35
30
  if permitted
36
31
  @logger.pass "inside the deployment schedule"
37
32
  else
33
+ @logger.info "failed to match any schedule #{@schedules.map(&:to_s).join(", ")}"
38
34
  @logger.fail "outside the deployment schedule"
39
35
  end
40
36
  end
@@ -0,0 +1,22 @@
1
+ require 'fugit'
2
+
3
+ module FaHarnessTools
4
+ # Creates a schedule which can be used to check if a change
5
+ # should be deployed.
6
+ class Schedule
7
+ def initialize(schedule:)
8
+ @schedule = schedule
9
+ @cron_schedule = Fugit.parse(schedule)
10
+ raise InvalidScheduleError, "'#{schedule}' can not be parsed" unless @cron_schedule
11
+ end
12
+
13
+ def can_run?(time:)
14
+ return false unless @cron_schedule.day_match?(time)
15
+ return @cron_schedule.hour_match?(time)
16
+ end
17
+
18
+ def to_s
19
+ return @schedule
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module FaHarnessTools
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fa-harness-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - FreeAgent
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-25 00:00:00.000000000 Z
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octokit
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fugit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -165,6 +179,7 @@ files:
165
179
  - lib/fa-harness-tools/check_schedule.rb
166
180
  - lib/fa-harness-tools/github_client.rb
167
181
  - lib/fa-harness-tools/harness_context.rb
182
+ - lib/fa-harness-tools/schedule.rb
168
183
  - lib/fa-harness-tools/version.rb
169
184
  homepage: https://github.com/fac/fa-harness-tools
170
185
  licenses: