Skip to content

Pagination fixes #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions examples/deployments.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,43 @@ github = Octokit.client(access_token: ENV.fetch("GITHUB_TOKEN"))
github.auto_paginate = true
github.per_page = 100

repo = "owner/repo"

puts "################ list deployments ################"
deployments = github.deployments(repo, {"environment" => "production"})
deployments.records.each do |deployment|
puts "deployment.id: #{deployment.id}"
puts "deployment.sha: #{deployment.sha}"
puts "deployment.description: #{deployment.description}"
puts "deployment.payload: #{deployment.payload}"
puts "deployment.environment: #{deployment.environment}"
end

puts "################ create_deployment ################"
deployment = github.create_deployment("owner/repo", "main", description: "Deploying from octokit.cr")
deployment = JSON.parse(deployment)
puts deployment.to_pretty_json
deployment = github.create_deployment(repo, "main", description: "Deploying from octokit.cr", payload: { "foo" => "bar" })
puts "created deployment: #{deployment.id}"

puts "################ get deployment ################"
deployment_id = deployment["id"].as_i64
the_deployment = github.deployment("owner/repo", deployment_id)
puts JSON.parse(the_deployment).to_pretty_json
deployment_id = deployment.id
the_deployment = github.deployment(repo, deployment_id)
puts "deployment.id: #{the_deployment.id}"
puts "deployment.sha: #{the_deployment.sha}"
puts "deployment.payload: #{the_deployment.payload}"
puts "deployment.payload['foo']: #{the_deployment.payload["foo"]}"
puts "deployment.environment: #{the_deployment.environment}"

puts "################ create_deployment_status ################"
github.create_deployment_status("owner/repo", deployment_id, "in_progress")
result = github.create_deployment_status("owner/repo", deployment_id, "success")
puts JSON.parse(result).to_pretty_json
github.create_deployment_status(repo, deployment_id, "in_progress")
result = github.create_deployment_status(repo, deployment_id, "success")
result.id

puts "################ get deployment status ################"
status = github.deployment_status(repo, deployment_id, result.id)
puts "status.state: #{status.state}"

puts "################ list deployment statuses ################"
statuses = github.list_deployment_statuses("owner/repo", deployment_id)
data = JSON.parse(statuses.records.to_json)
puts data.to_pretty_json
statuses = github.list_deployment_statuses(repo, deployment_id)
statuses.records.each do |s|
puts "status.id: #{s.id}"
end
```
27 changes: 27 additions & 0 deletions examples/reactions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Reactions

This example shows how you can interact with GitHub reactions using the `octokit.cr` library:

```crystal
require "octokit"

github = Octokit.client(access_token: ENV.fetch("GITHUB_TOKEN"))
github.auto_paginate = true
github.per_page = 100

# Get all the comments on a given issue
issue_comments = github.issue_comments("grantbirki/actions-sandbox", 126)

# Grab the first comment
first_issue_comment = issue_comments.records.first

# Get all the reactions on the first comment
reactions = github.issue_comment_reactions("grantbirki/actions-sandbox", first_issue_comment.id)

# Methods that return paginated results will return a Paginator object. This object has a `records` method that returns the actual results. Records in this case will be an Array of `Reaction` objects
reactions.records.each do |reaction|
puts reaction.content # --> "+1" (example)
puts reaction.user.login # --> "octocat" (example)
puts reaction.id # --> 123456 (example)
end
```
10 changes: 9 additions & 1 deletion script/zipper
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# COLORS
OFF='\033[0m'
GREEN='\033[0;32m'

# set the working directory to the root of the project
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
VENDOR_DIR="$DIR/vendor"
Expand Down Expand Up @@ -48,12 +52,16 @@ echo "$shards" | while IFS= read -r shard; do

echo '{"name": "'$name'", "version": "'$version'", "repository": "'$owner/$repo'"}' > "$TRASHDIR/$name-$version.shard/metadata.json"

# remove the vendor/ and lib/ dirs if they exist as it will bloat the zip and they are not needed with this vendoring strategy
rm -rf "$TRASHDIR/$name-$version.shard/shard/$name/vendor" 2>/dev/null || true
rm -rf "$TRASHDIR/$name-$version.shard/shard/$name/lib" 2>/dev/null || true

# Change to the temporary directory and zip the shard and cache directories
(cd "$TRASHDIR/$name-$version.shard" && zip -q -r "$TRASHDIR/$name-$version.zip" shard cache metadata.json -x "*.shard.vendor.cache.sha256")

# Move the zip to the cache
mkdir -p "$SHARDS_CACHED"
mv "$TRASHDIR/$name-$version.zip" "$SHARDS_CACHED/$name-$version.shard"

# echo "cached $name $version"
echo -e "${GREEN}cached${OFF} $name $version"
done
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: octokit
version: 0.3.1
version: 0.4.0

authors:
- Grant Birkinbine
Expand Down
46 changes: 26 additions & 20 deletions src/octokit/client/deployments.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ module Octokit
# :nodoc:
alias Repository = Models::Repository

# :nodoc:
alias Deployment = Models::Deployment

# :nodoc:
alias DeploymentStatus = Models::DeploymentStatus

# :nodoc:
VALID_STATES = %w[error failure inactive in_progress queued pending success]

Expand All @@ -28,10 +34,11 @@ module Octokit
# Fetch a single deployment for a repository
#
# ```
# Octokit.deployment("monsalisa/app", 123456)
# deployment = Octokit.deployment("monsalisa/app", 123456)
# puts deployment.id
# ```
def deployment(repo : String, deployment_id : Int64, **options)
get("#{Repository.path(repo)}/deployments/#{deployment_id}", options)
def deployment(repo : String, deployment_id : Int64, **options) : Deployment
Deployment.from_json(get("#{Repository.path(repo)}/deployments/#{deployment_id}", **options))
end

# List deployments for a repository
Expand All @@ -51,15 +58,15 @@ module Octokit
# Octokit.deployments("monalisa/app", {"environment" => "production"})
# ```
# An alias method exists for `deployments` called `list_deployments` which can be used interchangeably
def deployments(repo : String, params : Hash(String, String) = {} of String => String, **options)
def deployments(repo : String, params : Hash(String, String) = {} of String => String, **options) : Paginator(Deployment)
query_string = params.map { |k, v| "#{URI.encode_path(k)}=#{URI.encode_path(v)}" }.join("&")
url = "#{Repository.path(repo)}/deployments"
url += "?#{query_string}" unless query_string.empty?
get(url, options)
paginate(Deployment, url, **options)
end

# Alias for `deployments`
def list_deployments(repo : String, params : Hash(String, String) = {} of String => String, **options)
def list_deployments(repo : String, params : Hash(String, String) = {} of String => String, **options) : Paginator(Deployment)
deployments(repo, params, **options)
end

Expand Down Expand Up @@ -91,7 +98,7 @@ module Octokit
transient_environment : Bool = false,
production_environment : Bool = true,
**options
)
) : Deployment
options = options.merge({
json: {
ref: ref,
Expand All @@ -106,7 +113,7 @@ module Octokit
},
})

post("#{Repository.path(repo)}/deployments", options)
Deployment.from_json(post("#{Repository.path(repo)}/deployments", options))
end

# Delete a Deployment
Expand All @@ -125,8 +132,8 @@ module Octokit
# ```
# Octokit.delete_deployment("monalisa/app", 123456)
# ```
def delete_deployment(repo : String, deployment_id : Int64, **options)
delete("#{Repository.path(repo)}/deployments/#{deployment_id}", options)
def delete_deployment(repo : String, deployment_id : Int64, **options) : Bool
boolean_from_response(:delete, "#{Repository.path(repo)}/deployments/#{deployment_id}", **options)
end

# Get a deployment status
Expand All @@ -139,8 +146,8 @@ module Octokit
# ```
# Octokit.deployment_status("monalisa/app", 1234567890, 1234567890)
# ```
def deployment_status(repo : String, deployment_id : Int64, status_id : Int64, **options)
get("#{Repository.path(repo)}/deployments/#{deployment_id}/statuses/#{status_id}", options)
def deployment_status(repo : String, deployment_id : Int64, status_id : Int64, **options) : DeploymentStatus
DeploymentStatus.from_json(get("#{Repository.path(repo)}/deployments/#{deployment_id}/statuses/#{status_id}", **options))
end

# List all statuses for a Deployment
Expand All @@ -166,17 +173,16 @@ module Octokit
# ```
#
# Returns an array of deployment statuses
def deployment_statuses(repo : String, deployment_id : Int64, **options) : Paginator(Octokit::Models::DeploymentStatus)
def deployment_statuses(repo : String, deployment_id : Int64, **options) : Paginator(DeploymentStatus)
paginate(
Octokit::Models::DeploymentStatus,
"#{Repository.path(repo)}/deployments/#{deployment_id}" + "/statuses",
start_page: options[:page]?,
per_page: options[:per_page]?
DeploymentStatus,
"#{Repository.path(repo)}/deployments/#{deployment_id}/statuses",
**options
)
end

# alias for `deployment_statuses`
def list_deployment_statuses(repo : String, deployment_id : Int64, **options) : Paginator(Octokit::Models::DeploymentStatus)
def list_deployment_statuses(repo : String, deployment_id : Int64, **options) : Paginator(DeploymentStatus)
deployment_statuses(repo, deployment_id, **options)
end

Expand Down Expand Up @@ -208,7 +214,7 @@ module Octokit
environment_url : String = "",
auto_inactive : Bool = true,
**options
)
) : DeploymentStatus
raise "ArgumentError: state must be one of error, failure, inactive, in_progress, queued, pending, success" unless VALID_STATES.includes?(state)

options = options.merge({
Expand All @@ -223,7 +229,7 @@ module Octokit
},
})

post("#{Repository.path(repo)}/deployments/#{deployment_id}" + "/statuses", options)
DeploymentStatus.from_json(post("#{Repository.path(repo)}/deployments/#{deployment_id}/statuses", options))
end
end
end
Expand Down
50 changes: 26 additions & 24 deletions src/octokit/client/reactions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ module Octokit
# :nodoc:
alias Reaction = Models::Reaction

# :nodoc:
alias Reactions = Models::Reactions

# List reactions for a commit comment
#
# **See Also:**
Expand All @@ -33,8 +30,8 @@ module Octokit
# ```
# Octokit.commit_comment_reactions("monsalisa/app", 123456)
# ```
def commit_comment_reactions(repo : String, id : Int64, **options)
get("#{Repository.path(repo)}/comments/#{id}/reactions", options)
def commit_comment_reactions(repo : String, id : Int64, **options) : Paginator(Reaction)
paginate(Reaction, "#{Repository.path(repo)}/comments/#{id}/reactions", **options)
end

# Create a reaction for a commit comment
Expand All @@ -47,7 +44,7 @@ module Octokit
# ```
# Octokit.create_commit_comment_reaction("monsalisa/app", 123456, "+1")
# ```
def create_commit_comment_reaction(repo : String, id : Int64, reaction : String, **options)
def create_commit_comment_reaction(repo : String, id : Int64, reaction : String, **options) : Reaction
options = options.merge({json: {content: reaction}})
Reaction.from_json(post("#{Repository.path(repo)}/comments/#{id}/reactions", options))
end
Expand All @@ -62,8 +59,8 @@ module Octokit
# ```
# Octokit.issue_reactions("monsalisa/app", 123456)
# ```
def issue_reactions(repo : String, number : Int64, **options)
get("#{Repository.path(repo)}/issues/#{number}/reactions", options)
def issue_reactions(repo : String, number : Int64, **options) : Paginator(Reaction)
paginate(Reaction, "#{Repository.path(repo)}/issues/#{number}/reactions", **options)
end

# Create reaction for an issue
Expand All @@ -76,7 +73,7 @@ module Octokit
# ```
# Octokit.create_issue_reaction("monsalisa/app", 123456, "heart")
# ```
def create_issue_reaction(repo : String, number : Int64, reaction : String, **options)
def create_issue_reaction(repo : String, number : Int64, reaction : String, **options) : Reaction
options = options.merge({json: {content: reaction}})
Reaction.from_json(post("#{Repository.path(repo)}/issues/#{number}/reactions", options))
end
Expand All @@ -89,10 +86,15 @@ module Octokit
# **Examples:**
#
# ```
# Octokit.issue_comment_reactions("monsalisa/app", 987654)
# reactions = Octokit.issue_comment_reactions("monsalisa/app", 987654)
# reactions.records.each do |reaction|
# puts reaction.content # --> "+1" (example)
# puts reaction.user.login # --> "octocat" (example)
# puts reaction.id # --> 271694398 (example)
# end
# ```
def issue_comment_reactions(repo : String, id : Int64, **options)
get("#{Repository.path(repo)}/issues/comments/#{id}/reactions", options)
def issue_comment_reactions(repo : String, id : Int64, **options) : Paginator(Reaction)
paginate(Reaction, "#{Repository.path(repo)}/issues/comments/#{id}/reactions", **options)
end

# Create reaction for an issue comment
Expand Down Expand Up @@ -120,8 +122,8 @@ module Octokit
# ```
# Octokit.delete_issue_comment_reaction("monsalisa/app", 987654, 123)
# ```
def delete_issue_comment_reaction(repo : String, comment_id : Int64, reaction_id : Int64, **options)
boolean_from_response(:delete, "#{Repository.path(repo)}/issues/comments/#{comment_id}/reactions/#{reaction_id}", options)
def delete_issue_comment_reaction(repo : String, comment_id : Int64, reaction_id : Int64, **options) : Bool
boolean_from_response(:delete, "#{Repository.path(repo)}/issues/comments/#{comment_id}/reactions/#{reaction_id}", **options)
end

# List reactions for a pull request review comment
Expand All @@ -134,8 +136,8 @@ module Octokit
# ```
# Octokit.pull_request_review_comment_reactions("monsalisa/app", 555111)
# ```
def pull_request_review_comment_reactions(repo : String, id : Int64, **options)
get("#{Repository.path(repo)}/pulls/comments/#{id}/reactions", options)
def pull_request_review_comment_reactions(repo : String, id : Int64, **options) : Paginator(Reaction)
paginate(Reaction, "#{Repository.path(repo)}/pulls/comments/#{id}/reactions", **options)
end

# Create reaction for a pull request review comment
Expand All @@ -148,7 +150,7 @@ module Octokit
# ```
# Octokit.create_pull_request_review_comment_reaction("monsalisa/app", 555111, "hooray")
# ```
def create_pull_request_review_comment_reaction(repo : String, id : Int64, reaction : String, **options)
def create_pull_request_review_comment_reaction(repo : String, id : Int64, reaction : String, **options) : Reaction
options = options.merge({json: {content: reaction}})
Reaction.from_json(post("#{Repository.path(repo)}/pulls/comments/#{id}/reactions", options))
end
Expand All @@ -163,8 +165,8 @@ module Octokit
# ```
# Octokit.delete_issue_reaction("monsalisa/app", 987654, 123)
# ```
def delete_issue_reaction(repo : String, issue_id : Int64, reaction_id : Int64, **options)
boolean_from_response(:delete, "#{Repository.path(repo)}/issues/#{issue_id}/reactions/#{reaction_id}", options)
def delete_issue_reaction(repo : String, issue_id : Int64, reaction_id : Int64, **options) : Bool
boolean_from_response(:delete, "#{Repository.path(repo)}/issues/#{issue_id}/reactions/#{reaction_id}", **options)
end

# List reactions for a release
Expand All @@ -177,8 +179,8 @@ module Octokit
# ```
# Octokit.release_reactions("monsalisa/app", 987654)
# ```
def release_reactions(repo : String, release_id : Int64, **options)
get("#{Repository.path(repo)}/releases/#{release_id}/reactions", options)
def release_reactions(repo : String, release_id : Int64, **options) : Paginator(Reaction)
paginate(Reaction, "#{Repository.path(repo)}/releases/#{release_id}/reactions", **options)
end

# Create reaction for a release
Expand All @@ -191,7 +193,7 @@ module Octokit
# ```
# Octokit.create_release_reaction("monsalisa/app", 987654, "heart")
# ```
def create_release_reaction(repo : String, release_id : Int64, reaction : String, **options)
def create_release_reaction(repo : String, release_id : Int64, reaction : String, **options) : Reaction
options = options.merge({json: {content: reaction}})
Reaction.from_json(post("#{Repository.path(repo)}/releases/#{release_id}/reactions", options))
end
Expand All @@ -206,8 +208,8 @@ module Octokit
# ```
# Octokit.delete_release_reaction("monsalisa/app", 987654, 123)
# ```
def delete_release_reaction(repo : String, release_id : Int64, reaction_id : Int64, **options)
boolean_from_response(:delete, "#{Repository.path(repo)}/releases/#{release_id}/reactions/#{reaction_id}", options)
def delete_release_reaction(repo : String, release_id : Int64, reaction_id : Int64, **options) : Bool
boolean_from_response(:delete, "#{Repository.path(repo)}/releases/#{release_id}/reactions/#{reaction_id}", **options)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion src/octokit/models/reactions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module Octokit
id: Int64,
user: User,
node_id: String,

content: String
)
end
Expand Down
Loading
Loading