-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1435 from jmeridth/jm/generalized-github-ci
feat: Add new CustomCIWithGithub CI Source Type
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |