Skip to content
Snippets Groups Projects
Commit 8fe1b87b authored by Igor Drozdov's avatar Igor Drozdov 2️⃣
Browse files

Merge branch 'feat/graphql-branch-rules--default-branch' into 'master'

Add isDefault to BranchRule type objects in GraphQL endpoint

See merge request !98852



Merged-by: Igor Drozdov's avatarIgor Drozdov <idrozdov@gitlab.com>
Approved-by: default avatarAmy Qualls <aqualls@gitlab.com>
Approved-by: default avatarPedro Pombeiro <noreply@pedro.pombei.ro>
Approved-by: default avatarNikola Milojevic <nmilojevic@gitlab.com>
Co-authored-by: default avatarJoe Woodward <jwoodward@gitlab.com>
parents 20b51679 bbbcb9b6
No related branches found
No related tags found
1 merge request!98852Add isDefault to BranchRule type objects in GraphQL endpoint
Pipeline #657111368 passed
......@@ -13,6 +13,13 @@ class BranchRuleType < BaseObject
null: false,
description: 'Branch name, with wildcards, for the branch rules.'
field :is_default,
type: GraphQL::Types::Boolean,
null: false,
method: :default_branch?,
calls_gitaly: true,
description: "Check if this branch rule protects the project's default branch."
field :branch_protection,
type: Types::BranchRules::BranchProtectionType,
null: false,
......
......@@ -95,6 +95,10 @@ def allow_multiple?(type)
def self.downcase_humanized_name
name.underscore.humanize.downcase
end
def default_branch?
name == project.default_branch
end
end
ProtectedBranch.prepend_mod_with('ProtectedBranch')
......@@ -10258,6 +10258,7 @@ List of branch rules for a project, grouped by branch name.
| ---- | ---- | ----------- |
| <a id="branchrulebranchprotection"></a>`branchProtection` | [`BranchProtection!`](#branchprotection) | Branch protections configured for this branch rule. |
| <a id="branchrulecreatedat"></a>`createdAt` | [`Time!`](#time) | Timestamp of when the branch rule was created. |
| <a id="branchruleisdefault"></a>`isDefault` | [`Boolean!`](#boolean) | Check if this branch rule protects the project's default branch. |
| <a id="branchrulename"></a>`name` | [`String!`](#string) | Branch name, with wildcards, for the branch rules. |
| <a id="branchruleupdatedat"></a>`updatedAt` | [`Time!`](#time) | Timestamp of when the branch rule was last updated. |
 
......@@ -10,6 +10,7 @@
let(:fields) do
%i[
name
isDefault
branch_protection
created_at
updated_at
......
......@@ -435,4 +435,28 @@
expect(described_class.downcase_humanized_name).to eq 'protected branch'
end
end
describe '.default_branch?' do
before do
allow(subject.project).to receive(:default_branch).and_return(branch)
end
context 'when the name matches the default branch' do
let(:branch) { subject.name }
it { is_expected.to be_default_branch }
end
context 'when the name does not match the default branch' do
let(:branch) { "#{subject.name}qwerty" }
it { is_expected.not_to be_default_branch }
end
context 'when a wildcard name matches the default branch' do
let(:branch) { "#{subject.name}*" }
it { is_expected.not_to be_default_branch }
end
end
end
......@@ -21,27 +21,24 @@
let(:branch_rules_data) { graphql_data_at('project', 'branchRules', 'edges') }
let(:variables) { { path: project.full_path } }
let(:fields) do
<<~QUERY
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
cursor
node {
#{all_graphql_fields_for('branch_rules'.classify)}
}
}
QUERY
end
# fields must use let as the all_graphql_fields_for also configures some spies
let(:fields) { all_graphql_fields_for('BranchRule') }
let(:query) do
<<~GQL
query($path: ID!, $n: Int, $cursor: String) {
project(fullPath: $path) {
branchRules(first: $n, after: $cursor) { #{fields} }
branchRules(first: $n, after: $cursor) {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
cursor
node {
#{fields}
}
}
}
}
}
GQL
......@@ -55,7 +52,9 @@
it_behaves_like 'a working graphql query'
it { expect(branch_rules_data).to be_empty }
it 'hides branch rules data' do
expect(branch_rules_data).to be_empty
end
end
context 'when the user does have read_protected_branch abilities' do
......@@ -66,12 +65,17 @@
it_behaves_like 'a working graphql query'
it 'includes a name' do
it 'returns branch rules data' do
expect(branch_rules_data.dig(0, 'node', 'name')).to be_present
end
it 'includes created_at and updated_at' do
expect(branch_rules_data.dig(0, 'node', 'isDefault')).to be(true).or be(false)
expect(branch_rules_data.dig(0, 'node', 'branchProtection')).to be_present
expect(branch_rules_data.dig(0, 'node', 'createdAt')).to be_present
expect(branch_rules_data.dig(0, 'node', 'updatedAt')).to be_present
expect(branch_rules_data.dig(1, 'node', 'name')).to be_present
expect(branch_rules_data.dig(1, 'node', 'isDefault')).to be(true).or be(false)
expect(branch_rules_data.dig(1, 'node', 'branchProtection')).to be_present
expect(branch_rules_data.dig(1, 'node', 'createdAt')).to be_present
expect(branch_rules_data.dig(1, 'node', 'updatedAt')).to be_present
end
......@@ -82,16 +86,16 @@
{ path: project.full_path, n: branch_rule_limit, cursor: last_cursor }
end
it_behaves_like 'a working graphql query' do
it 'only returns N branch_rules' do
expect(branch_rules_data.size).to eq(branch_rule_limit)
expect(has_next_page).to be_truthy
expect(has_prev_page).to be_falsey
post_graphql(query, current_user: current_user, variables: next_variables)
expect(branch_rules_data.size).to eq(branch_rule_limit)
expect(has_next_page).to be_falsey
expect(has_prev_page).to be_truthy
end
it_behaves_like 'a working graphql query'
it 'returns pagination information' do
expect(branch_rules_data.size).to eq(branch_rule_limit)
expect(has_next_page).to be_truthy
expect(has_prev_page).to be_falsey
post_graphql(query, current_user: current_user, variables: next_variables)
expect(branch_rules_data.size).to eq(branch_rule_limit)
expect(has_next_page).to be_falsey
expect(has_prev_page).to be_truthy
end
context 'when no limit is provided' do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment