Skip to content

Commit 189111c

Browse files
authored
Merge pull request #46 from runwaylab/octokit-updates
Octokit updates
2 parents b75d5c5 + 5c346a2 commit 189111c

19 files changed

+32
-38
lines changed

β€Žacceptance/logs/expected.log

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ INFO: πŸ“– loading runway configuration
33
DEBUG: attempting to load config from acceptance/config/config.yml
44
INFO: βœ… loaded configuration successfully
55
INFO: 🚚 2 projects loaded
6-
INFO: πŸ›« starting runway - version: v0.2.5
6+
INFO: πŸ›« starting runway - version: v0.2.6
77
INFO: πŸ“¦ starting project project-1
88
INFO: πŸ• scheduling event with interval 3s for project-1
99
INFO: πŸ“¦ starting project project-2

β€Žshard.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ shards:
3434

3535
octokit:
3636
git: https://github.com/octokit-cr/octokit.cr.git
37-
version: 0.3.1
37+
version: 0.4.0
3838

3939
retriable:
4040
git: https://github.com/sija/retriable.cr.git

β€Žshard.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: runway
2-
version: 0.2.5
2+
version: 0.2.6
33

44
authors:
55
- GrantBirki
@@ -15,7 +15,7 @@ license: MIT
1515
dependencies:
1616
octokit:
1717
github: octokit-cr/octokit.cr
18-
version: ~> 0.3.1
18+
version: ~> 0.4.0
1919
emoji:
2020
github: veelenga/emoji.cr
2121
version: ~> 0.5.0

β€Žsrc/runway/events/github_deployment.cr

+25-31
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class GitHubDeployment < BaseEvent
1111
@github = Runway::GitHub.new(log)
1212
@deployment_filter = (@event.deployment_filter.try(&.to_i) || 1)
1313
@repo = @event.repo.not_nil!
14-
@timezone = Runway::TimeHelpers.timezone(@event.schedule.timezone)
1514
@success = "success"
1615
@failure = "failure"
1716
end
@@ -42,9 +41,9 @@ class GitHubDeployment < BaseEvent
4241
# create a deployment status
4342
result = @github.create_deployment_status(@repo, deployment_id, status)
4443

45-
@log.debug { "deployment status result: #{JSON.parse(result).to_pretty_json}" } if Runway::VERBOSE
44+
@log.debug { "deployment status result: #{result.to_pretty_json}" } if Runway::VERBOSE
4645

47-
raise "Unexpected deployment status result" unless JSON.parse(result)["state"] == @success
46+
raise "Unexpected deployment status result" unless result.state == @success
4847

4948
# logs about the deployment status
5049
@log.info { Emoji.emojize(":white_check_mark: successfully completed deployment for #{@repo} in the #{@event.environment} environment") } if status == @success unless Runway::QUIET
@@ -54,7 +53,7 @@ class GitHubDeployment < BaseEvent
5453
rescue error : Exception
5554
@log.error { "error handling deployment event: #{error.message} - attempting to set a 'failure' statue on the deployment" }
5655
result = @github.create_deployment_status(@repo, payload.id.to_s.to_i64.not_nil!, @failure)
57-
@log.debug { "deployment status result (on error): #{JSON.parse(result).to_pretty_json}" }
56+
@log.debug { "deployment status result (on error): #{result.to_pretty_json}" }
5857
return payload
5958
end
6059

@@ -86,20 +85,18 @@ class GitHubDeployment < BaseEvent
8685
# this should already have been done by the GitHub API, but we'll do it again out of extra caution
8786
# @param deployments [JSON::Any] the deployments to filter
8887
# @return [Array] the filtered deployments
89-
protected def filter_deployments(deployments : JSON::Any) : Array
90-
deployments.as_a.select do |deployment|
91-
deployment["environment"] == @event.environment
88+
protected def filter_deployments(deployments : Array(Octokit::Models::Deployment)) : Array(Octokit::Models::Deployment)
89+
deployments.select do |deployment|
90+
deployment.environment == @event.environment
9291
end
9392
end
9493

9594
# sort deployments by created_at date with the most recent first
9695
# uses the deployment_filter attribute to only grab the X most recent deployments
9796
# @param deployments [Array] the deployments to sort
9897
# @return [Array] the sorted deployments
99-
protected def sort_deployments(deployments : Array) : Array
100-
deployments = deployments.sort_by do |deployment|
101-
Time.parse(deployment["created_at"].as_s, "%FT%T%z", @timezone)
102-
end.reverse!
98+
protected def sort_deployments(deployments : Array(Octokit::Models::Deployment)) : Array(Octokit::Models::Deployment)
99+
deployments = deployments.sort_by(&.created_at).reverse!
103100

104101
# only grab the X most recent deployments (based on event.filters.deployments)
105102
return deployments.first(@deployment_filter)
@@ -108,22 +105,20 @@ class GitHubDeployment < BaseEvent
108105
# A helper method to find the most recent deployment with an "in_progress" status and return it (if it exists)
109106
# @param deployments [Array] the deployments to search through
110107
# @return [JSON::Any] the deployment with an "in_progress" status or nil if it doesn't exist
111-
protected def find_in_progress_deployment(deployments : Array) : JSON::Any?
108+
protected def find_in_progress_deployment(deployments : Array) : Octokit::Models::Deployment | Nil
112109
# loop through all filtered deployments and get their deployment statuses
113110
# the first deployment to have an "in_progress" status will be the one we're looking for
114111
# however, the "in_progress" status must be the most recent status for the deployment or we'll ignore it
115112
deployments.each do |deployment|
116-
deployment_id = deployment["id"].to_s.to_i
113+
deployment_id = deployment.id.to_i32
117114
statuses = @github.list_deployment_statuses(@event.repo.not_nil!, deployment_id, per_page: 100)
118-
statuses = JSON.parse(statuses.records.to_json).as_a
115+
statuses = statuses.records
119116

120117
# sort statuses by created_at date with the most recent first
121-
statuses = statuses.sort_by do |status|
122-
Time.parse(status["created_at"].as_s, "%FT%T%z", @timezone)
123-
end.reverse!
118+
statuses = statuses.sort_by(&.created_at).reverse!
124119

125120
# if the most recent status is "in_progress", we have our deployment
126-
if statuses.first["state"] == "in_progress"
121+
if statuses.first.state == "in_progress"
127122
@log.debug { "found an in_progress deployment for #{@repo} in the #{@event.environment} environment" }
128123
return deployment
129124
end
@@ -135,17 +130,17 @@ class GitHubDeployment < BaseEvent
135130

136131
# set the payload attributes based on the detected deployment
137132
# @param payload [Payload] the payload object to set attributes on
138-
# @param detected_deployment [JSON::Any] the detected deployment to get attributes from
133+
# @param detected_deployment [Octokit::Models::Deployment] the detected deployment to get attributes from
139134
# @return [Payload] the payload object with attributes set
140-
protected def set_payload_attributes(payload : Payload, detected_deployment : JSON::Any) : Payload
141-
payload.id = detected_deployment["id"].to_s.not_nil!
142-
payload.environment = detected_deployment.try(&.["environment"]).try(&.to_s) || nil
143-
payload.created_at = detected_deployment.try(&.["created_at"]).try(&.to_s) || nil
144-
payload.updated_at = detected_deployment.try(&.["updated_at"]).try(&.to_s) || nil
145-
payload.description = detected_deployment.try(&.["description"]).try(&.to_s) || nil
146-
payload.user = detected_deployment.try(&.["creator"]).try(&.["login"]).try(&.to_s) || nil
147-
payload.sha = detected_deployment.try(&.["sha"]).try(&.to_s) || nil
148-
payload.ref = detected_deployment.try(&.["ref"]).try(&.to_s) || nil
135+
protected def set_payload_attributes(payload : Payload, detected_deployment : Octokit::Models::Deployment) : Payload
136+
payload.id = detected_deployment.id.to_s
137+
payload.environment = detected_deployment.environment
138+
payload.created_at = detected_deployment.created_at.to_s
139+
payload.updated_at = detected_deployment.updated_at.to_s
140+
payload.description = detected_deployment.description
141+
payload.user = detected_deployment.creator.login
142+
payload.sha = detected_deployment.sha
143+
payload.ref = detected_deployment.ref
149144
payload.status = "in_progress"
150145
payload.ship_it = true
151146
return payload
@@ -165,9 +160,8 @@ class GitHubDeployment < BaseEvent
165160
# It then sorts the deployments as well
166161
# @param deployments [String] the deployments raw JSON response
167162
# @return [Array] the parsed, filtered, and sorted deployments
168-
protected def parse_and_filter_deployments(deployments : String) : Array
169-
deployments = JSON.parse(deployments)
170-
deployments = filter_deployments(deployments)
163+
protected def parse_and_filter_deployments(deployments : Octokit::Connection::Paginator(Octokit::Models::Deployment)) : Array
164+
deployments = filter_deployments(deployments.records)
171165
sort_deployments(deployments)
172166
end
173167
end

β€Žsrc/runway/services/github.cr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Runway
1515
@miniumum_rate_limit = ENV.fetch("GITHUB_MINIMUM_RATE_LIMIT", "10").to_s.to_i
1616
end
1717

18-
def create_deployment_status(repo : String, deployment_id : Int64, status : String) : String
18+
def create_deployment_status(repo : String, deployment_id : Int64, status : String) : Octokit::Models::DeploymentStatus
1919
Retriable.retry do
2020
check_rate_limit!
2121
@client.create_deployment_status(repo, deployment_id, status)
@@ -29,7 +29,7 @@ module Runway
2929
end
3030
end
3131

32-
def deployments(repo : String, environment : String) : String
32+
def deployments(repo : String, environment : String) : Octokit::Connection::Paginator(Octokit::Models::Deployment)
3333
Retriable.retry do
3434
check_rate_limit!
3535
@client.deployments(repo, {"environment" => environment})

β€Žsrc/version.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Runway
2-
VERSION = "v0.2.5"
2+
VERSION = "v0.2.6"
33
end
-4.55 KB
Binary file not shown.
-2.83 KB
Binary file not shown.
-413 Bytes
Binary file not shown.
-980 Bytes
Binary file not shown.
-478 Bytes
Binary file not shown.
-3.29 KB
Binary file not shown.
-583 Bytes
Binary file not shown.
-743 Bytes
Binary file not shown.
-2.6 KB
Binary file not shown.
-1.41 KB
Binary file not shown.
-960 Bytes
Binary file not shown.

0 commit comments

Comments
Β (0)