Skip to content
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
15 changes: 12 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,25 @@ jobs:
strategy:
matrix:
os:
- "ubuntu-2004"
- "almalinux-8"
- "almalinux-9"
- "amazonlinux-2023"
- "centos-stream-9"
- "debian-12"
- "fedora-latest"
- "rockylinux-8"
- "rockylinux-9"
- "ubuntu-2204"
- "ubuntu-2404"
suite:
- "default"
fail-fast: false

steps:
- name: Check out code
uses: actions/checkout@v7
- name: Install Chef
uses: actionshub/chef-install@6.0.0
- name: Install Cinc Workstation
uses: sous-chefs/.github/.github/actions/install-workstation@9.0.1
- name: Dokken
uses: actionshub/test-kitchen@3.0.0
env:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/conventional-commits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ name: conventional-commits

jobs:
conventional-commits:
permissions:
pull-requests: read
uses: sous-chefs/.github/.github/workflows/conventional-commits.yml@9.0.1
2 changes: 1 addition & 1 deletion .github/workflows/copilot-setup-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Install Chef
uses: actionshub/chef-install@main
- name: Install cookbooks
run: berks install
run: chef install Policyfile.rb
2 changes: 2 additions & 0 deletions .github/workflows/prevent-file-change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ name: prevent-file-change

jobs:
prevent-file-change:
permissions:
pull-requests: write
uses: sous-chefs/.github/.github/workflows/prevent-file-change.yml@9.0.1
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require:
- cookstyle
7 changes: 0 additions & 7 deletions Berksfile

This file was deleted.

9 changes: 9 additions & 0 deletions Policyfile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

name 'github'

run_list 'test::default'
named_run_list :default, 'test::default'

cookbook 'github', path: '.'
cookbook 'test', path: 'test/fixtures/cookbooks/test'
67 changes: 8 additions & 59 deletions kitchen.dokken.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
driver:
name: dokken
privileged: true
chef_version: <%= ENV['CHEF_VERSION'] || 'current' %>
chef_image: cincproject/cinc
chef_version: <%= ENV['CHEF_VERSION'] || 'latest' %>

transport: { name: dokken }
provisioner: { name: dokken }
provisioner:
name: dokken
product_name: cinc
chef_binary: /opt/cinc/bin/cinc-client

platforms:
- name: almalinux-8
Expand All @@ -22,36 +26,11 @@ platforms:
image: dokken/amazonlinux-2023
pid_one_command: /usr/lib/systemd/systemd

- name: centos-7
driver:
image: dokken/centos-7
pid_one_command: /usr/lib/systemd/systemd

- name: centos-stream-8
driver:
image: dokken/centos-stream-8
pid_one_command: /usr/lib/systemd/systemd

- name: centos-stream-9
driver:
image: dokken/centos-stream-9
pid_one_command: /usr/lib/systemd/systemd

- name: debian-9
driver:
image: dokken/debian-9
pid_one_command: /bin/systemd

- name: debian-10
driver:
image: dokken/debian-10
pid_one_command: /bin/systemd

- name: debian-11
driver:
image: dokken/debian-11
pid_one_command: /bin/systemd

- name: debian-12
driver:
image: dokken/debian-12
Expand All @@ -62,26 +41,6 @@ platforms:
image: dokken/fedora-latest
pid_one_command: /usr/lib/systemd/systemd

- name: opensuse-leap-15
driver:
image: dokken/opensuse-leap-15
pid_one_command: /usr/lib/systemd/systemd

- name: oraclelinux-7
driver:
image: dokken/oraclelinux-7
pid_one_command: /usr/lib/systemd/systemd

- name: oraclelinux-8
driver:
image: dokken/oraclelinux-8
pid_one_command: /usr/lib/systemd/systemd

- name: oraclelinux-9
driver:
image: dokken/oraclelinux-9
pid_one_command: /usr/lib/systemd/systemd

- name: rockylinux-8
driver:
image: dokken/rockylinux-8
Expand All @@ -92,22 +51,12 @@ platforms:
image: dokken/rockylinux-9
pid_one_command: /usr/lib/systemd/systemd

- name: ubuntu-18.04
driver:
image: dokken/ubuntu-18.04
pid_one_command: /bin/systemd

- name: ubuntu-20.04
driver:
image: dokken/ubuntu-20.04
pid_one_command: /bin/systemd

- name: ubuntu-22.04
driver:
image: dokken/ubuntu-22.04
pid_one_command: /bin/systemd

- name: ubuntu-23.04
- name: ubuntu-24.04
driver:
image: dokken/ubuntu-23.04
image: dokken/ubuntu-24.04
pid_one_command: /bin/systemd
14 changes: 11 additions & 3 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ driver:

provisioner:
name: chef_infra
product_name: chef
product_name: cinc
enforce_idempotency: true
multiple_converge: 2
deprecations_as_errors: true
chef_license: accept-no-persist

verifier:
name: inspec

platforms:
- name: ubuntu-20.04
- name: almalinux-8
- name: almalinux-9
- name: amazonlinux-2023
- name: centos-stream-9
- name: debian-12
- name: fedora-latest
- name: rockylinux-8
- name: rockylinux-9
- name: ubuntu-22.04
- name: ubuntu-24.04

suites:
- name: default
Expand Down
2 changes: 2 additions & 0 deletions libraries/github_archive.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'open-uri'
require 'uri'
require 'fileutils'
Expand Down
23 changes: 18 additions & 5 deletions libraries/github_asset.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'net/http'
require 'open-uri'
require 'uri'
Expand All @@ -6,6 +8,15 @@

module GithubCB
class Asset
module JsonSerialization
private

def sawyer_options
super.merge(serializer: Sawyer::Serializer.json)
end
end
private_constant :JsonSerialization

class << self
def asset_cache
::File.join(Chef::Config[:file_cache_path], 'github_assets')
Expand Down Expand Up @@ -38,15 +49,17 @@ def initialize(fqrn, options = {})
def asset_url(options)
require 'octokit'

Octokit.configure do |c|
c.login = options[:user]
c.access_token = options[:token]
end
# Some embedded Ruby runtimes expose Yajl without Sawyer's dump/load API.
client = Octokit::Client.new(
login: options[:user] || false,
access_token: options[:token] || false,
connection_options: Octokit.connection_options.dup
).extend(JsonSerialization)

page = 1
release = nil
loop do
releases = Octokit.releases(fqrn, per_page: 100, page: page)
releases = client.releases(fqrn, per_page: 100, page: page)
release = releases.find { |r| r.tag_name == tag_name }
break if release || releases.empty?

Expand Down
2 changes: 2 additions & 0 deletions libraries/github_errors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module GithubCB
class GHError < StandardError; end

Expand Down
2 changes: 2 additions & 0 deletions resources/archive.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

unified_mode true

default_action :extract
Expand Down
2 changes: 2 additions & 0 deletions resources/asset.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

unified_mode true

default_action :download
Expand Down
2 changes: 2 additions & 0 deletions resources/deploy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

unified_mode true

default_action :deploy
Expand Down
2 changes: 2 additions & 0 deletions spec/libraries/github_archive_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# require 'spec_helper'
#
# describe GithubCB::Archive do
Expand Down
47 changes: 34 additions & 13 deletions spec/libraries/github_asset_spec.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,81 @@
# frozen_string_literal: true

require 'spec_helper'
require 'octokit'
require_relative '../../libraries/github_asset'

RSpec.describe GithubCB::Asset do
subject(:asset) { described_class.new(repository, release: 'v1.2.3', name: 'example.zip') }

let(:repository) { 'example/project' }
let(:configuration) { Struct.new(:login, :access_token).new }
let(:client) { instance_double(Octokit::Client) }
let(:download_url) { 'https://api.github.com/repos/example/project/releases/assets/123' }
let(:release_asset) { double('release asset', name: 'example.zip', rels: { self: double(href: download_url) }) }
let(:assets_response) { double(data: [release_asset]) }
let(:release) { double('release', tag_name: 'v1.2.3', rels: { assets: double(get: assets_response) }) }
let(:other_release) { double('other release', tag_name: 'v2.0.0') }

before do
allow(Octokit).to receive(:configure).and_yield(configuration)
allow(Octokit::Client).to receive(:new).and_return(client)
end

it 'returns a matching first-page asset without requesting more pages' do
expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 1).once.and_return([release])
expect(Octokit::Client).to receive(:new).with(hash_including(login: 'example-user', access_token: 'example-token')).and_return(client)
expect(client).to receive(:releases).with(repository, per_page: 100, page: 1).once.and_return([release])

expect(asset.asset_url(user: 'example-user', token: 'example-token')).to eq(download_url)
expect(configuration.login).to eq('example-user')
expect(configuration.access_token).to eq('example-token')
end

it 'uses JSON when the embedded Sawyer default cannot provide a serializer' do
allow(Sawyer::Serializer).to receive(:any_json).and_raise(NameError, 'Yajl does not implement dump')
allow(Octokit).to receive(:instance_variable_get).and_call_original
allow(Octokit).to receive(:instance_variable_get).with(:@login).and_return('other-user')
allow(Octokit).to receive(:instance_variable_get).with(:@access_token).and_return('other-token')
allow(Octokit::Client).to receive(:new).and_wrap_original do |constructor, options|
real_client = constructor.call(options)
allow(real_client).to receive(:releases) do
expect(real_client.login).to be_falsey
expect(real_client.access_token).to be_falsey
expect(real_client.agent.decode_body('{"version":"v1.2.3"}')).to eq(version: 'v1.2.3')
[release]
end
real_client
end

expect(asset.asset_url({})).to eq(download_url)
end

it 'finds an older release on a later page' do
expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release])
expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_return([release])
expect(client).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release])
expect(client).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_return([release])

expect(asset.asset_url({})).to eq(download_url)
end

it 'raises ReleaseNotFound after reaching an empty page' do
expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release])
expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_return([])
expect(client).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release])
expect(client).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_return([])

expect { asset.asset_url({}) }.to raise_error(GithubCB::ReleaseNotFound, 'release not found')
end

it 'raises AssetNotFound when the matching release has no matching asset' do
allow(Octokit).to receive(:releases).and_return([release])
allow(client).to receive(:releases).and_return([release])
allow(assets_response).to receive(:data).and_return([])

expect { asset.asset_url({}) }.to raise_error(GithubCB::AssetNotFound, 'asset not found')
end

it 'propagates errors from a later release page' do
expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release])
expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_raise(Octokit::Unauthorized)
expect(client).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release])
expect(client).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_raise(Octokit::Unauthorized)

expect { asset.asset_url({}) }.to raise_error(Octokit::Unauthorized)
end

it 'does not discard an authenticated draft or prerelease returned by the API' do
allow(release).to receive_messages(draft: true, prerelease: true)
allow(Octokit).to receive(:releases).and_return([release])
allow(client).to receive(:releases).and_return([release])

expect(asset.asset_url(user: 'example-user', token: 'example-token')).to eq(download_url)
end
Expand Down
Loading
Loading