<commit_before>#   Copyright 2009 ThoughtWorks
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

require 'json'
require 'openssl'
require 'time'

class EnvironmentsController < EscController
    map('/environments')

    def index(env = nil, app = nil, key = nil)
        # Sanity check what we've got first
        if env && (not env =~ /\A[.a-zA-Z0-9_-]+\Z/)
            respond("Invalid environment name. Valid characters are ., a-z, A-Z, 0-9, _ and -", 403)
        end

        if app && (not app =~ /\A[.a-zA-Z0-9_-]+\Z/)
            respond("Invalid application name. Valid characters are ., a-z, A-Z, 0-9, _ and -", 403)
        end

        if key && (not key =~ /\A[.a-zA-Z0-9_-]+\Z/)
            respond("Invalid key name. Valid characters are ., a-z, A-Z, 0-9, _ and -", 403)
        end

        @env = env
        @app = app
        @key = key

        # Getting...
        if request.get?
            # List all environments
            if env.nil?
                listEnvs
            # List all apps in specified environment
            elsif app.nil?
                listApps
            # List keys and values for app in environment
            elsif key.nil?
                listKeys
            # We're getting value for specific key
            else
                getValue
            end

        # Copying...
        elsif request.post?
            # Undefined
            if env.nil?
                respond("Undefined", 400)
            # You're copying an env
            elsif app.nil?
                # env is the target, Location Header has the source
                copyEnv
            end

        # Creating...
        elsif request.put?
            # Undefined
            if env.nil?
                response.status = 400
            # You're creating a new env
            elsif app.nil?
                createEnv
            # You're creating a new app
            elsif key.nil?
                createApp
            # Key stuff
            else
                setValue
            end

        # Deleting...
        elsif request.delete?
            # Undefined
            if env.nil?
                response.status = 400
            # You're deleting an env
            elsif app.nil?
                delete_env
            # You're deleting an app
            elsif key.nil?
                delete_app
            # You're deleting a key
            else
                deleteKey
            end
        end
    end

    private

    #
    # Deletion
    #

    def delete_env
        respond("Not allowed to delete default environment!", 403) if @env == "default"
        get_env
        check_env_auth
        @my_env.delete
        respond("Environment '#{@env}' deleted.", 200)
    end

    def delete_app
        get_app

        if @env == "default"
            if @my_app.environments.size == 1
                @my_app.delete
                respond("Applicaton '#{@app}' deleted.", 200)
            else
                respond("Applicaton '#{@app}' is used in other environments.", 412)
            end
        else
            get_env
            check_env_auth
            @my_app.remove_environment(@my_env)
            respond("Application '#{@app}' deleted from the '#{@env}' environment.", 200)
        end
    end

    def deleteKey
        get_env
        get_app
        get_key

        if @env == "default"
            # Don't delete default if we have a value set in a non-default env
            set = false
            @myKey.app.environments.each do |appenv|
                if (not Value[:key_id => @keyId, :environment_id => appenv[:id]].nil?) and (appenv.name != "default")
                    set = true
                    break
                end
            end

            if not set
                @myKey.delete
                respond("Key '#{@key}' deleted from application '#{@app}'.", 200)
            else
                respond("Key #{@key} can't be deleted. It has non default values set.", 403)
            end
        else
            check_env_auth<commit_msg>deleteKey -> delete_key<commit_after>#   Copyright 2009 ThoughtWorks
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

require 'json'
require 'openssl'
require 'time'

class EnvironmentsController < EscController
    map('/environments')

    def index(env = nil, app = nil, key = nil)
        # Sanity check what we've got first
        if env && (not env =~ /\A[.a-zA-Z0-9_-]+\Z/)
            respond("Invalid environment name. Valid characters are ., a-z, A-Z, 0-9, _ and -", 403)
        end

        if app && (not app =~ /\A[.a-zA-Z0-9_-]+\Z/)
            respond("Invalid application name. Valid characters are ., a-z, A-Z, 0-9, _ and -", 403)
        end

        if key && (not key =~ /\A[.a-zA-Z0-9_-]+\Z/)
            respond("Invalid key name. Valid characters are ., a-z, A-Z, 0-9, _ and -", 403)
        end

        @env = env
        @app = app
        @key = key

        # Getting...
        if request.get?
            # List all environments
            if env.nil?
                listEnvs
            # List all apps in specified environment
            elsif app.nil?
                listApps
            # List keys and values for app in environment
            elsif key.nil?
                listKeys
            # We're getting value for specific key
            else
                getValue
            end

        # Copying...
        elsif request.post?
            # Undefined
            if env.nil?
                respond("Undefined", 400)
            # You're copying an env
            elsif app.nil?
                # env is the target, Location Header has the source
                copyEnv
            end

        # Creating...
        elsif request.put?
            # Undefined
            if env.nil?
                response.status = 400
            # You're creating a new env
            elsif app.nil?
                createEnv
            # You're creating a new app
            elsif key.nil?
                createApp
            # Key stuff
            else
                setValue
            end

        # Deleting...
        elsif request.delete?
            # Undefined
            if env.nil?
                response.status = 400
            # You're deleting an env
            elsif app.nil?
                delete_env
            # You're deleting an app
            elsif key.nil?
                delete_app
            # You're deleting a key
            else
                delete_key
            end
        end
    end

    private

    #
    # Deletion
    #

    def delete_env
        respond("Not allowed to delete default environment!", 403) if @env == "default"
        get_env
        check_env_auth
        @my_env.delete
        respond("Environment '#{@env}' deleted.", 200)
    end

    def delete_app
        get_app

        if @env == "default"
            if @my_app.environments.size == 1
                @my_app.delete
                respond("Applicaton '#{@app}' deleted.", 200)
            else
                respond("Applicaton '#{@app}' is used in other environments.", 412)
            end
        else
            get_env
            check_env_auth
            @my_app.remove_environment(@my_env)
            respond("Application '#{@app}' deleted from the '#{@env}' environment.", 200)
        end
    end

    def delete_key
        get_env
        get_app
        get_key

        if @env == "default"
            # Don't delete default if we have a value set in a non-default env
            set = false
            @myKey.app.environments.each do |appenv|
                if (not Value[:key_id => @keyId, :environment_id => appenv[:id]].nil?) and (appenv.name != "default")
                    set = true
                    break
                end
            end

            if not set
                @myKey.delete
                respond("Key '#{@key}' deleted from application '#{@app}'.", 200)
            else
                respond("Key #{@key} can't be deleted. It has non default values set.", 403)
            end
        else
            check_env_auth
{'fraction_non_alphanumeric': 0.08828671328671328, 'fraction_numerical': 0.013111888111888112, 'mean_word_length': 1.346923076923077, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942788', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2377, 'n_words': 824}