Skip to content

Commit

Permalink
ff cleanup demo using the py api (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
ketkarameya committed Nov 7, 2023
1 parent 73f8ea6 commit 6130074
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
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")

0 comments on commit 6130074

Please sign in to comment.