Sha256: 56246c35dc4d240f6bc00020ccabacdd85e86c8939ede0e026d88ce8c0ddb7bd

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true
module Shipit
  class UpdateGithubLastDeployedRefJob < BackgroundJob
    queue_as :default

    # We do not prefix 'refs/' because Octokit methods will do this automatically.
    BRANCH_REF_PREFIX = 'heads'
    DEPLOY_PREFIX = "#{Shipit.app_name.downcase}-deploy"

    def perform(stack)
      stack_sha = stack.last_successful_deploy_commit&.sha
      return unless stack_sha

      environment = stack.environment
      stack_ref = create_full_ref(environment)
      client = stack.github_api

      full_repo_name = stack.github_repo_name

      update_or_create_ref(client: client, repo_name: full_repo_name, ref: stack_ref, new_sha: stack_sha)
    end

    private

    def create_full_ref(stack_environment)
      [BRANCH_REF_PREFIX, DEPLOY_PREFIX, stack_environment].join("/")
    end

    def create_ref(client:, repo_name:, ref:, sha:)
      client.create_ref(repo_name, ref, sha)
    end

    def update_or_create_ref(client:, repo_name:, ref:, new_sha:)
      client.update_ref(repo_name, ref, new_sha)
    rescue Octokit::UnprocessableEntity => e
      error_msg = e.message
      if error_msg.include?("Reference does not exist")
        create_ref(client: client, repo_name: repo_name, ref: ref, sha: new_sha)
      else
        raise
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
shipit-engine-0.39.0 app/jobs/shipit/update_github_last_deployed_ref_job.rb
shipit-engine-0.38.0 app/jobs/shipit/update_github_last_deployed_ref_job.rb
shipit-engine-0.37.0 app/jobs/shipit/update_github_last_deployed_ref_job.rb
shipit-engine-0.36.1 app/jobs/shipit/update_github_last_deployed_ref_job.rb
shipit-engine-0.36.0 app/jobs/shipit/update_github_last_deployed_ref_job.rb
shipit-engine-0.35.1 app/jobs/shipit/update_github_last_deployed_ref_job.rb
shipit-engine-0.35.0 app/jobs/shipit/update_github_last_deployed_ref_job.rb
shipit-engine-0.34.0 app/jobs/shipit/update_github_last_deployed_ref_job.rb