Skip to content

Commit

Permalink
Merge pull request #1435 from jmeridth/jm/generalized-github-ci
Browse files Browse the repository at this point in the history
feat: Add new CustomCIWithGithub CI Source Type
  • Loading branch information
orta committed Apr 21, 2023
2 parents 949dc67 + caa4bdb commit 093c31a
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Add /github/workspace to git safe.directory [@hiro-flank](https://github.com/hiro-flank) [#1427](https://github.com/danger/danger/pull/1427)
* Fixes issue where a comment is posted to Bitbucket Cloud even when everything is green. [@SalvatoreT](https://github.com/SalvatoreT) [#1299](https://github.com/danger/danger/issues/1299)
* Add Ruby 3.2 to test matrix [@mataku](https://github.com/mataku) [#1434](https://github.com/danger/danger/pull/1434)
* Add CI Source of CustomCiWithGithub - [@jmeridth](https://github.com/jmeridth] [#1435](https://github.com/danger/danger/pull/1435)

<!-- Your comment above here -->

Expand Down
45 changes: 45 additions & 0 deletions lib/danger/ci_source/custom_ci_with_github.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "danger/request_sources/github/github"

module Danger
# ### CI Setup
#
# Custom CI with GitHub
#
# This CI source is for custom, most likely internal, CI systems that are use GitHub as source control.
# An example could be argo-workflows or tekton hosted in your own Kubernetes cluster.
#
# The following environment variables are required:
# - `CUSTOM_CI_WITH_GITHUB` - Set to any value to indicate that this is a custom CI with GitHub
#
# ### Token Setup
#
# #### GitHub
# As you own the setup, it's up to you to add the environment variable for the `DANGER_GITHUB_API_TOKEN`.
#
class CustomCIWithGithub < CI
def self.validates_as_ci?(env)
env.key? "CUSTOM_CI_WITH_GITHUB"
end

def self.validates_as_pr?(env)
value = env["GITHUB_EVENT_NAME"]
["pull_request", "pull_request_target"].include?(value)
end

def supported_request_sources
@supported_request_sources ||= [Danger::RequestSources::GitHub]
end

def initialize(env)
self.repo_slug = env["GITHUB_REPOSITORY"]
pull_request_event = JSON.parse(File.read(env["GITHUB_EVENT_PATH"]))
self.pull_request_id = pull_request_event["number"]
self.repo_url = pull_request_event["repository"]["clone_url"]

# if environment variable DANGER_GITHUB_API_TOKEN is not set, use env GITHUB_TOKEN
if (env.key? "CUSTOM_CI_WITH_GITHUB") && (!env.key? "DANGER_GITHUB_API_TOKEN")
env["DANGER_GITHUB_API_TOKEN"] = env["GITHUB_TOKEN"]
end
end
end
end
1 change: 1 addition & 0 deletions spec/lib/danger/ci_sources/ci_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"Danger::Codemagic",
"Danger::Codeship",
"Danger::Concourse",
"Danger::CustomCIWithGithub",
"Danger::DotCi",
"Danger::Drone",
"Danger::GitHubActions",
Expand Down
93 changes: 93 additions & 0 deletions spec/lib/danger/ci_sources/custom_ci_with_github_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require "danger/ci_source/custom_ci_with_github"

RSpec.describe Danger::CustomCIWithGithub do
let(:valid_env) do
{
"CUSTOM_CI_WITH_GITHUB" => "true",
"GITHUB_EVENT_NAME" => "pull_request",
"GITHUB_REPOSITORY" => "danger/danger",
"GITHUB_EVENT_PATH" => File.expand_path("../../../fixtures/ci_source/pull_request_event.json", __dir__)
}
end

let(:invalid_env) do
{}
end

let(:source) { described_class.new(valid_env) }

context "with GitHub" do
describe ".validates_as_ci?" do
it "validates when required env variables are set" do
expect(described_class.validates_as_ci?(valid_env)).to be true
end

it "doesn't validate when require env variables are not set" do
expect(described_class.validates_as_ci?(invalid_env)).to be false
end
end

describe ".validates_as_pr?" do
it "validates if `GITHUB_EVENT_NAME` is 'pull_request" do
valid_env["GITHUB_EVENT_NAME"] = "pull_request"
expect(described_class.validates_as_pr?(valid_env)).to be true
end

it "validates if `GITHUB_EVENT_NAME` is 'pull_request_target" do
valid_env["GITHUB_EVENT_NAME"] = "pull_request_target"
expect(described_class.validates_as_pr?(valid_env)).to be true
end

it "doesn't validate if `GITHUB_EVENT_NAME` is 'push'" do
valid_env["GITHUB_EVENT_NAME"] = "push"
expect(described_class.validates_as_pr?(valid_env)).to be false
end

it "doesn't validate if `GITHUB_EVENT_NAME` is missing" do
valid_env["GITHUB_EVENT_NAME"] = nil
expect(described_class.validates_as_pr?(valid_env)).to be false
end
end
end

describe "#new" do
describe "repo slug" do
it "gets out a repo slug" do
expect(source.repo_slug).to eq("danger/danger")
end
end

describe "pull request id" do
it "get out a pull request id" do
expect(source.pull_request_id).to eq 1
end
end

describe "repo url" do
it "get out a repo url" do
expect(source.repo_url).to eq "https://github.com/Codertocat/Hello-World.git"
end
end

describe "without DANGER_GITHUB_API_TOKEN" do
it "override by GITHUB_TOKEN if GITHUB_TOKEN is not empty" do
valid_env["GITHUB_TOKEN"] = "github_token"
source
expect(valid_env["DANGER_GITHUB_API_TOKEN"]).to eq("github_token")
end

it "doesn't override if DANGER_GITHUB_API_TOKEN is not empty" do
valid_env["DANGER_GITHUB_API_TOKEN"] = "danger_github_api_token"
valid_env["GITHUB_TOKEN"] = "github_token"
source
expect(valid_env["DANGER_GITHUB_API_TOKEN"]).to eq("danger_github_api_token")
end
end
end

describe "#supported_request_sources" do
it "supports GitHub" do
expect(source.supported_request_sources).to include(Danger::RequestSources::GitHub)
end
end
end

0 comments on commit 093c31a

Please sign in to comment.