Skip to content
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

GitHub Enterprise feature #2287

Merged
merged 4 commits into from
Oct 25, 2021
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
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,61 @@ Next, create a Github Access token. You also do this from the "Applications" sec
GITHUB_ACCESS_TOKEN=[Token you just generated through Github]
```

## Connecting your Github Enterprise Account

There are a couple features that depend on GitHub Enterprise integration (CLA signing, some quality metrics in Fieri). You can set up an integration for your development environment by creating an application with your Github Enterprise account. To do this:

1. Log into your Github Enterprise account if you aren't already.
2. Click on your username in the upper right hand corner.
3. Click the "User settings" in the vertical menu on the right corner. This will bring you to your Public Profile page.
4. Click on "Developer settings" in the vertical menu on the left hand side
5. At the top of the screen you'll see a section labeled "Developer settings" with text "OAuth App" Click on this.
6. Click on button "new OAuth App". This will bring you to Register a new OAuth application page.
7. Name your application whatever you like (I use "testing-supermarket-app"), the set the homepage url as http://localhost:3000 (or whatever localhost domain that you use). Also set the Authorization callback URL to http://localhost:3000/auth/github/callback (or your localhost domain of choice).
8. Click the "Register application" button.
9. Open up the .env.development file in your local copy of the Supermarket repo. Replace these values:

```
GITHUB_KEY=YOUR_GITHUB_KEY
GITHUB_SECRET=YOUR_GITHUB_SECRET
GITHUB_ENTERPRISE_URL=YOUR_GITHUB_ENTERPRISE_URL
GITHUB_CLIENT_OPTION_SITE=YOUR_GITHUB_ENTERPRISE_SITE
GITHUB_CLIENT_OPTION_AUTHORIZE_URL=YOUR_GITHUB_ENTERPRISE_AUTHORIZE_URL
GITHUB_CLIENT_OPTION_ACCESS_TOKEN_URL=YOUR_GITHUB_ENTERPRISE_ACCESS_TOKEN_URL
```

with these values:

```
GITHUB_KEY=[Your new application's client ID]
GITHUB_SECRET=[Your new application's client secret]
GITHUB_ENTERPRISE_URL=[Your GitHub Enterprise URL]
GITHUB_CLIENT_OPTION_SITE=YOURGITHUBENTERPRISEURL/api/v3
GITHUB_CLIENT_OPTION_AUTHORIZE_URL=YOURGITHUBENTERPRISEURL/login/oauth/authorize
GITHUB_CLIENT_OPTION_ACCESS_TOKEN_URL=YOURGITHUBENTERPRISEURL/login/oauth/access_token
```

Next, create a Github Access token. You also do this from the "Developer settings" section.

1. Click on "Personal access tokens". This will bring you to Personal access tokens page.
2. Look at the "Personal access tokens section heading." Click on the "Generate new token" button.
3. When prompted, enter your Github password.
4. Enter whatever you like for the Token description, I use "testing-supermarket"
5. Leave the scopes at the defaults
6. Click the "Generate token" button
7. Copy the token it generates and put it somewhere safe!
8. Open up your .env.development file again and replace this value:

```
GITHUB_ACCESS_TOKEN=YOUR_GITHUB_ACCESS_TOKEN
```

with this value:

```
GITHUB_ACCESS_TOKEN=[Token you just generated through Github]
```

## Tests

Requirements for tests: PhantomJS 1.8, Node
Expand Down
5 changes: 5 additions & 0 deletions omnibus/cookbooks/omnibus-supermarket/attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@
default['supermarket']['api_item_limit'] = 100
default['supermarket']['rails_log_to_stdout'] = true
default['supermarket']['fips_enabled'] = nil
default['supermarket']['github_url'] = nil
default['supermarket']['github_enterprise_url'] = nil
default['supermarket']['github_client_option_authorize_url'] = nil
default['supermarket']['github_client_option_site'] = nil
default['supermarket']['github_client_option_access_token_url'] = nil

# Allow owners to remove their cookbooks, cookbook versions, or tools.
# Added as a step towards implementing RFC072 Artifact Yanking
Expand Down
3 changes: 1 addition & 2 deletions omnibus/cookbooks/omnibus-supermarket/recipes/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@
end
end

case node['platform_family']
when 'suse'
if platform_family?('suse')
execute 'special permission change for SLES OS' do
# unicorn needs different permission to run rails in SLES
command "chmod 4755 -R #{node['supermarket']['install_directory']}/embedded/bin/ruby"
Expand Down
5 changes: 5 additions & 0 deletions src/supermarket/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
GITHUB_KEY=YOUR_GITHUB_KEY
GITHUB_SECRET=YOUR_GITHUB_SECRET
GITHUB_ACCESS_TOKEN=YOUR_GITHUB_ACCESS_TOKEN
GITHUB_URL=https://github.com/
GITHUB_ENTERPRISE_URL=YOUR_GITHUB_ENTERPRISE_URL
GITHUB_CLIENT_OPTION_SITE=YOUR_GITHUB_ENTERPRISE_SITE
GITHUB_CLIENT_OPTION_AUTHORIZE_URL=YOUR_GITHUB_ENTERPRISE_AUTHORIZE_URL
GITHUB_CLIENT_OPTION_ACCESS_TOKEN_URL=YOUR_GITHUB_ENTERPRISE_ACCESS_TOKEN_URL
CHEF_OAUTH2_APP_ID=YOUR_CHEF_OAUTH2_APP_ID
CHEF_OAUTH2_SECRET=YOUR_CHEF_OAUTH2_SECRET
CHEF_OAUTH2_VERIFY_SSL=false
Expand Down
24 changes: 24 additions & 0 deletions src/supermarket/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,28 @@ def flash_message_class_for(name)
"warning" => "warning",
}.fetch(name)
end

#
# Returns a github user's profile url
#
# @param name [String]
#
# @return [String]
#

def github_profile_url(username)
path = ENV["GITHUB_ENTERPRISE_URL"].presence || ENV["GITHUB_URL"]
path += "/" unless path.end_with?("/")
path + username
end

#
# Returns a github account type
#
# @return [String]
#

def github_account_type
ENV["GITHUB_ENTERPRISE_URL"].present? ? "GitHub Enterprise" : "GitHub"
end
end
4 changes: 2 additions & 2 deletions src/supermarket/app/views/profile/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<% if Feature.active?(:github) %>
<div class="content" id="github-accounts">
<p>Linking your account lets other cookbook shoppers find you on GitHub for feedback, collaboration, and kudos. The link requests access only to your GitHub account's public information.</p>
<p>Linking your account lets other cookbook shoppers find you on <%= github_account_type %> for feedback, collaboration, and kudos. The link requests access only to your <%= github_account_type %> account's public information.</p>
<table>
<% current_user.accounts.for('github').each do |account| %>
<tr>
Expand All @@ -62,7 +62,7 @@
<% end %>
</table>

<%= link_to '<i class="fa fa-github"></i> Connect GitHub Account'.html_safe, auth_path(:github), method: :post, class: 'button radius', rel: 'connect_github' %>
<%= link_to "<i class='fa fa-github'></i> Connect #{ github_account_type } Account".html_safe, auth_path(:github), method: :post, class: 'button radius', rel: 'connect_github', title: "Connect with #{ github_account_type } Account"%>
</div>
<% end %>
<div class="content" id="collaborations">
Expand Down
5 changes: 3 additions & 2 deletions src/supermarket/app/views/users/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
</li>
<% end %>
<% end %>


<% @user.accounts.for(:github).each do |account| %>
<li class="has-tip" title="<%= posessivize(@user.name) %> GitHub Username">
<a href="https://github.com/<%= account.username %>" target="_blank" rel="noopener"><i class="fa fa-github"></i> <%= account.username %></a>
<li class="has-tip" title="<%= posessivize(@user.name) %> <%= github_account_type %> Username">
<a href="<%= github_profile_url(account.username) %>" target="_blank" rel="noopener"><i class="fa fa-github"></i> <%= account.username %></a>
</li>
<% end %>

Expand Down
13 changes: 12 additions & 1 deletion src/supermarket/config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@

# Configure middleware used by OmniAuth
Rails.application.config.middleware.use(OmniAuth::Builder) do
github_client_options = {}

if ENV["GITHUB_CLIENT_OPTION_SITE"].present? &&
ENV["GITHUB_CLIENT_OPTION_AUTHORIZE_URL"].present? &&
ENV["GITHUB_CLIENT_OPTION_ACCESS_TOKEN_URL"].present?
github_client_options[:site] = ENV["GITHUB_CLIENT_OPTION_SITE"]
github_client_options[:authorize_url] = ENV["GITHUB_CLIENT_OPTION_AUTHORIZE_URL"]
github_client_options[:token_url] = ENV["GITHUB_CLIENT_OPTION_ACCESS_TOKEN_URL"]
end

provider(
:github,
ENV["GITHUB_KEY"],
ENV["GITHUB_SECRET"]
ENV["GITHUB_SECRET"],
{ client_options: github_client_options }
)

# Use an alternate URL for the Chef OAuth2 service if one is provided
Expand Down
38 changes: 38 additions & 0 deletions src/supermarket/spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,42 @@
expect(flash_message_class_for("warning")).to eql("warning")
end
end

context "when using github account" do

before do
ENV["GITHUB_ENTERPRISE_URL"] = ""
ENV["GITHUB_URL"] = "https://github.com"
end

describe "#github_profile_url" do
it "should return a user's profile url" do
expect(github_profile_url("johndoe")).to eql("https://github.com/johndoe")
end
end

describe "#github_account_type" do
it "should return github account type" do
expect(github_account_type).to eql("GitHub")
end
end
end

context "when using github enterprise account" do
before do
ENV["GITHUB_ENTERPRISE_URL"] = "https://example.com"
ENV["GITHUB_URL"] = ""
end
describe "#github_profile_url" do
it "should return a user's profile url" do
expect(github_profile_url("johndoe")).to eql("https://example.com/johndoe")
end
end

describe "#github_account_type" do
it "should return github account type" do
expect(github_account_type).to eql("GitHub Enterprise")
end
end
end
end
65 changes: 65 additions & 0 deletions src/supermarket/spec/views/profile/edit.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "spec_helper"

describe "profile/edit.html.erb" do

context "page with github enterprise account content" do

let(:user) { create(:user) }

before do
allow(view).to receive(:policy) do |record|
Pundit.policy(user, record)
end
allow(view).to receive(:current_user).and_return(user)
assign(:user, user)
ENV["GITHUB_ENTERPRISE_URL"] = "https://example.com"
end

it "should have GitHub Enterprise content on edit profile page" do
render
content = "Linking your account lets other cookbook shoppers find you on GitHub Enterprise for feedback, collaboration, and kudos. The link requests access only to your GitHub Enterprise account's public information."
expect(rendered).to have_selector("p", text: content)
end

it "should have Connect GitHub Account button text" do
render
expect(rendered).to have_selector("a", text: "Connect GitHub Enterprise Account")
end

it "should have Connect GitHub Account button title" do
render
expect(rendered).to have_selector("a[title='Connect with GitHub Enterprise Account']")
end
end

context "page with github account content" do

let(:user) { create(:user) }

before do
allow(view).to receive(:policy) do |record|
Pundit.policy(user, record)
end
allow(view).to receive(:current_user).and_return(user)
assign(:user, user)
ENV["GITHUB_ENTERPRISE_URL"] = ""
ENV["GITHUB_URL"] = "https://github.com"
end

it "should have GitHub content on edit profile page" do
render
content = "Linking your account lets other cookbook shoppers find you on GitHub for feedback, collaboration, and kudos. The link requests access only to your GitHub account's public information."
expect(rendered).to have_selector("p", text: content)
end

it "should have Connect GitHub Account button text" do
render
expect(rendered).to have_selector("a", text: "Connect GitHub Account")
end

it "should have Connect GitHub Account button title" do
render
expect(rendered).to have_selector("a[title='Connect with GitHub Account']")
end
end
end