diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-16 18:09:35 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-16 18:09:35 +0000 |
commit | 3a9076e0a4c28af9a1a40ed5e181b70fb1b659de (patch) | |
tree | 3d531c50bec43c1845c7d74f76442b7a6f49849f /lib/gitlab/global_id.rb | |
parent | 2d8454515e7b631a8f39a6415c86154d6c62841c (diff) | |
download | gitlab-ce-3a9076e0a4c28af9a1a40ed5e181b70fb1b659de.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/global_id.rb')
-rw-r--r-- | lib/gitlab/global_id.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/gitlab/global_id.rb b/lib/gitlab/global_id.rb index cc82b6c5897..e8a6006dce1 100644 --- a/lib/gitlab/global_id.rb +++ b/lib/gitlab/global_id.rb @@ -2,6 +2,8 @@ module Gitlab module GlobalId + CoerceError = Class.new(ArgumentError) + def self.build(object = nil, model_name: nil, id: nil, params: nil) if object model_name ||= object.class.name @@ -10,5 +12,20 @@ module Gitlab ::URI::GID.build(app: GlobalID.app, model_name: model_name, model_id: id, params: params) end + + def self.as_global_id(value, model_name: nil) + case value + when GlobalID + value + when URI::GID + GlobalID.new(value) + when Integer + raise CoerceError, 'Cannot coerce Integer' unless model_name.present? + + GlobalID.new(::Gitlab::GlobalId.build(model_name: model_name, id: value)) + else + raise CoerceError, "Invalid ID. Cannot coerce instances of #{value.class}" + end + end end end |