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

Add python api based ff cleanup demo #630

Merged
merged 1 commit into from
Nov 7, 2023
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
21 changes: 21 additions & 0 deletions demo/feature_flag_cleanup_using_py_api/java/SampleClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2023 Uber Technologies, Inc.
//
// <p>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
// <p>http://www.apache.org/licenses/LICENSE-2.0
//
// <p>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.

class SampleJava {

public void sampleMethod(ExperimentInterface exp) {
if (exp.isTreated("SAMPLE_STALE_FLAG")) {
System.out.println("SAMPLE_STALE_FLAG is enabled");
} else {
System.out.println("SAMPLE_STALE_FLAG is disabled");
}
}
}
53 changes: 53 additions & 0 deletions demo/stale_feature_flag_cleanup_demos_using_py_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from os.path import join, dirname, getmtime, exists
from polyglot_piranha import Rule, RuleGraph, execute_piranha, PiranhaArguments
import logging
from logging import info

feature_flag_dir = join(dirname(__file__), "feature_flag_cleanup_using_py_api")


def run_java_ff_demo():
info("Running the stale feature flag cleanup demo for Java")

directory_path = join(feature_flag_dir, "java")
modified_file_path = join(directory_path, "SampleClass.java")

update_is_treated = Rule(
name="update_is_treated",
# :[e] matches any node (its like a hole) and @flag_name refers to the substitution
query='cs :[e].isTreated("@flag_name")',
replace_node="*",
# @treated refers to the substitution
replace="@treated",
groups={"replace_expression_with_boolean_literal"},
# This is a list of holes that need to be filled in for the rule to be applied
holes={"treated", "flag_name"}
)

args = PiranhaArguments(
"java",
paths_to_codebase=[directory_path],
substitutions={"flag_name": "SAMPLE_STALE_FLAG", "treated": "true"},
rule_graph=RuleGraph(rules=[update_is_treated], edges=[])
)

output_summaries = execute_piranha(args)

old_mtime = getmtime(modified_file_path)

assert len(output_summaries) == 1

for summary in output_summaries:
assert len(summary.rewrites) > 0

new_mtime = getmtime(modified_file_path)

assert old_mtime < new_mtime


FORMAT = "%(levelname)s %(name)s %(asctime)-15s %(filename)s:%(lineno)d %(message)s"
logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.DEBUG)

run_java_ff_demo()
print("Completed running the stale feature flag cleanup demos")
Loading