in tools/azure-rest-api-specs-examples-automation/automation/main.py [0:0]
def process_release(operation: OperationConfiguration, sdk: SdkConfiguration, release: Release, report: Report):
# process per release
logging.info(f"Processing release: {release.tag}")
tmp_root_path = path.join(root_path, tmp_folder)
os.makedirs(tmp_root_path, exist_ok=True)
tmp_path = tempfile.mkdtemp(prefix="tmp", dir=tmp_root_path)
logging.info(f"Work directory: {tmp_path}")
try:
example_repo_path = path.join(tmp_path, tmp_example_folder)
sdk_repo_path = path.join(tmp_path, tmp_sdk_folder)
spec_repo_path = path.join(tmp_root_path, tmp_spec_folder)
# checkout azure-rest-api-specs-examples repo
cmd = ["git", "clone", "--quiet", "--depth", "1", operation.sdk_examples_repository, example_repo_path]
logging.info(f"Checking out repository: {operation.sdk_examples_repository}")
logging.info("Command line: " + " ".join(cmd))
subprocess.check_call(cmd, cwd=tmp_path)
# checkout sdk repo
cmd = [
"git",
"clone",
"-c",
"advice.detachedHead=false",
"--quiet",
"--depth",
"1",
"--branch",
release.tag,
sdk.repository,
sdk_repo_path,
]
logging.info(f"Checking out repository: {sdk.repository}")
logging.info("Command line: " + " ".join(cmd))
subprocess.check_call(cmd, cwd=tmp_path)
# prepare input.json
input_json_path = path.join(tmp_path, "input.json")
output_json_path = path.join(tmp_path, "output.json")
with open(input_json_path, "w", encoding="utf-8") as f_out:
input_json = {
"specsPath": spec_repo_path,
"sdkExamplesPath": example_repo_path,
"sdkPath": sdk_repo_path,
"tempPath": tmp_path,
"release": {"tag": release.tag, "package": release.package, "version": release.version},
}
logging.info(f"Input JSON for worker: {input_json}")
json.dump(input_json, f_out, indent=2)
# run script
logging.info(f"Running worker: {sdk.script.run}")
start = time.perf_counter()
subprocess.check_call([sdk.script.run, input_json_path, output_json_path], cwd=root_path)
end = time.perf_counter()
logging.info(f"Worker ran: {str(timedelta(seconds=end-start))}")
# parse output.json
release_name = release.tag
succeeded = True
files = []
if path.isfile(output_json_path):
with open(output_json_path, "r", encoding="utf-8") as f_in:
output = json.load(f_in)
logging.info(f"Output JSON from worker: {output}")
release_name = output["name"]
succeeded = "succeeded" == output["status"]
files = output["files"]
if not succeeded:
report.statuses[release.tag] = "failed at worker"
report.aggregated_error.errors.append(RuntimeError(f"Worker failed for release tag: {release.tag}"))
return
# commit and create pull request
# check for new examples
cmd = ["git", "status", "--porcelain"]
logging.info("Command line: " + " ".join(cmd))
output = subprocess.check_output(cmd, cwd=example_repo_path)
if len(output) == 0:
logging.info(f"No change to repository: {example_repo_path}")
report.statuses[release.tag] = "succeeded, no change"
else:
output_str = str(output, "utf-8")
logging.info(f"git status:\n{output_str}")
# git add
cmd = ["git", "add", "--all"]
logging.info("Command line: " + " ".join(cmd))
subprocess.check_call(cmd, cwd=example_repo_path)
# find added/modified files
cmd = ["git", "status", "--porcelain"]
logging.info("Command line: " + " ".join(cmd))
output = subprocess.check_output(cmd, cwd=example_repo_path)
output_str = str(output, "utf-8")
changed_files = [file.strip()[3:] for file in output_str.splitlines()]
# git checkout new branch
branch = f"automation-examples_{sdk.name}_{release.tag}_{operation.build_id}"
cmd = ["git", "checkout", "-b", branch]
logging.info("Command line: " + " ".join(cmd))
subprocess.check_call(cmd, cwd=example_repo_path)
# git commit
title = f"[Automation] Collect examples from {sdk.name}#{release.tag}"
logging.info(f"git commit: {title}")
cmd = ["git", "-c", "user.name=azure-sdk", "-c", "user.email=azuresdk@microsoft.com", "commit", "-m", title]
logging.info("Command line: " + " ".join(cmd))
subprocess.check_call(cmd, cwd=example_repo_path)
# git push
remote_uri = "https://" + github_token + "@" + operation.sdk_examples_repository[len("https://") :]
cmd = ["git", "push", remote_uri, branch]
# do not print this as it contains token
# logging.info('Command line: ' + ' '.join(cmd))
subprocess.check_call(cmd, cwd=example_repo_path)
try:
# create github pull request
head = f"{operation.repository_owner}:{branch}"
repo = GitHubRepository(operation.repository_owner, operation.repository_name, github_token)
pull_number = repo.create_pull_request(title, head, "main")
repo.add_label(pull_number, ["auto-merge"])
except Exception as e:
logging.error(f"Error: {e}")
report.statuses[release.tag] = "failed to create pull request"
report.aggregated_error.errors.append(e)
return
try:
if operation.persist_data:
# commit changes to database
commit_database(release_name, sdk.language, release, files)
except Exception as e:
logging.error(f"Error: {e}")
report.statuses[release.tag] = "failed to update database"
report.aggregated_error.errors.append(e)
return
report.statuses[release.tag] = f"succeeded, {len(changed_files)} files changed, pull number {pull_number}"
except subprocess.CalledProcessError as e:
logging.error(f"Call error: {e}")
report.statuses[release.tag] = "failed to invoke git"
report.aggregated_error.errors.append(e)
finally:
if clean_tmp_dir:
shutil.rmtree(tmp_path, ignore_errors=True)