From 77b6bfdccb067fab8bcd222ab5fd7f22cdee1366 Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 23 Aug 2022 10:09:51 +0200 Subject: [PATCH 1/5] `create_commit` cmd support deletions --- src/doc_builder/commands/push.py | 60 +++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/doc_builder/commands/push.py b/src/doc_builder/commands/push.py index 97696b4cc..7dc20f943 100644 --- a/src/doc_builder/commands/push.py +++ b/src/doc_builder/commands/push.py @@ -60,6 +60,48 @@ def create_additions(library_name: str) -> List[Dict]: return additions +def create_deletions(repo_id: str, library_name: str, token: str) -> List[Dict]: + """ + Given `repo_id/library_name` path, returns [FileDeletion!]!: [{path: "some_path"}, ...] + see more here: https://docs.github.com/en/graphql/reference/input-objects#filechanges + """ + # GET doc-build-dev/transformers + res = requests.get( + f"https://api.github.com/repos/{repo_id}/git/trees/heads/main", headers={"Authorization": f"bearer {token}"} + ) + if res.status_code != 200: + raise Exception(f"create_deletions failed (GET tree root): {res.message}") + json = res.json() + node = next(filter(lambda node: node["path"] == library_name, json["tree"]), None) + url = node["url"] + + # GET doc-build-dev/transformers/pr_xyz + root_folder = Path(library_name) + doc_version_folder = next(root_folder.glob("*")).relative_to(root_folder) + doc_version_folder = str(doc_version_folder) + res = requests.get(url, headers={"Authorization": f"bearer {token}"}) + if res.status_code != 200: + raise Exception(f"create_deletions failed (GET tree root/{repo_id}): {res.message}") + json = res.json() + node = next(filter(lambda node: node["path"] == doc_version_folder, json["tree"]), None) + if node is None: + # there is no need to delete since the path does not exist + return [] + url = node["url"] + + # GET doc-build-dev/transformers/pr_xyz/**/* + res = requests.get(f"{url}?recursive=true", headers={"Authorization": f"bearer {token}"}) + if res.status_code != 200: + raise Exception(f"create_deletions failed (GET tree root/{repo_id}/{doc_version_folder}): {res.message}") + json = res.json() + tree = json["tree"] + deletions = [ + {"path": f"{library_name}/{doc_version_folder}/{node['path']}"} for node in tree if node["type"] == "blob" + ] + + return deletions + + MAX_CHUNK_LEN = 3e7 # 30 Megabytes @@ -95,6 +137,7 @@ def create_additions_chunks(additions: List[Dict]) -> List[List[Dict]]: mutation ( $repo_id: String! $additions: [FileAddition!]! + $deletions: [FileDeletion!]! $head_oid: GitObjectID! $commit_msg: String! ) { @@ -102,7 +145,7 @@ def create_additions_chunks(additions: List[Dict]) -> List[List[Dict]]: input: { branch: { repositoryNameWithOwner: $repo_id, branchName: "main" } message: { headline: $commit_msg } - fileChanges: { additions: $additions } + fileChanges: { additions: $additions, deletions: $deletions } expectedHeadOid: $head_oid } ) { @@ -112,7 +155,9 @@ def create_additions_chunks(additions: List[Dict]) -> List[List[Dict]]: """ -def create_commit(client: Client, repo_id: str, additions: List[Dict], token: str, commit_msg: str): +def create_commit( + client: Client, repo_id: str, additions: List[Dict], deletions: List[Dict], token: str, commit_msg: str +): """ Commits additions and/or deletions to a repository using Github GraphQL mutation `createCommitOnBranch` see more here: https://docs.github.com/en/graphql/reference/mutations#createcommitonbranch @@ -122,6 +167,7 @@ def create_commit(client: Client, repo_id: str, additions: List[Dict], token: st head_oid = get_head_oid(repo_id, token) params = { "additions": additions, + "deletions": deletions, "repo_id": repo_id, "head_oid": head_oid, "commit_msg": commit_msg, @@ -143,7 +189,9 @@ def push_command(args): number_of_retries = args.n_retries n_seconds_sleep = 5 - # commit file additions + # file deletions + deletions = create_deletions(args.doc_build_repo_id, args.library_name, args.token) + # file additions time_start = time() additions = create_additions(args.library_name) time_end = time() @@ -158,9 +206,13 @@ def push_command(args): url="https://api.github.com/graphql", headers={"Authorization": f"bearer {args.token}"}, verify=True ) with Client(transport=transport, fetch_schema_from_transport=True, execute_timeout=None) as gql_client: + # commit file deletions + create_commit( + gql_client, args.doc_build_repo_id, [], deletions, args.token, f"Clean before: {args.commit_msg}" + ) # commit push chunks additions for i, additions in enumerate(additions_chunks): - create_commit(gql_client, args.doc_build_repo_id, additions, args.token, args.commit_msg) + create_commit(gql_client, args.doc_build_repo_id, additions, [], args.token, args.commit_msg) print(f"Committed additions chunk: {i+1}/{len(additions_chunks)}") break except Exception as e: From 430383fd7809f79f57428059e03155efecdf1063 Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 23 Aug 2022 10:16:15 +0200 Subject: [PATCH 2/5] debug change should be removed --- .github/workflows/build_pr_documentation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_pr_documentation.yml b/.github/workflows/build_pr_documentation.yml index 40d1fd33d..93347253c 100644 --- a/.github/workflows/build_pr_documentation.yml +++ b/.github/workflows/build_pr_documentation.yml @@ -95,7 +95,7 @@ jobs: run: | pip uninstall -y doc-builder cd doc-builder - git pull origin main + git pull origin create_commit_deletions pip install . cd .. From d0b51e9de4d344fb09d0edc4d50beff6879c842c Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 23 Aug 2022 11:41:19 +0200 Subject: [PATCH 3/5] Find deleted files --- src/doc_builder/commands/push.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/doc_builder/commands/push.py b/src/doc_builder/commands/push.py index 7dc20f943..780a4097e 100644 --- a/src/doc_builder/commands/push.py +++ b/src/doc_builder/commands/push.py @@ -95,8 +95,15 @@ def create_deletions(repo_id: str, library_name: str, token: str) -> List[Dict]: raise Exception(f"create_deletions failed (GET tree root/{repo_id}/{doc_version_folder}): {res.message}") json = res.json() tree = json["tree"] + + deletions = [] + built_docs_path = Path(f"{library_name}/{doc_version_folder}").absolute() + built_docs_files = [x for x in built_docs_path.glob("**/*") if x.is_file()] + built_docs_files_relative = set([str(f.relative_to(built_docs_path)) for f in built_docs_files]) deletions = [ - {"path": f"{library_name}/{doc_version_folder}/{node['path']}"} for node in tree if node["type"] == "blob" + {"path": f"{library_name}/{doc_version_folder}/{node['path']}"} + for node in tree + if node["type"] == "blob" and node["path"] not in built_docs_files_relative ] return deletions @@ -208,7 +215,7 @@ def push_command(args): with Client(transport=transport, fetch_schema_from_transport=True, execute_timeout=None) as gql_client: # commit file deletions create_commit( - gql_client, args.doc_build_repo_id, [], deletions, args.token, f"Clean before: {args.commit_msg}" + gql_client, args.doc_build_repo_id, [], deletions, args.token, f"Deleteions: {args.commit_msg}" ) # commit push chunks additions for i, additions in enumerate(additions_chunks): From 6a15aac883fe6a7b3033567b971a1909ae231680 Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 23 Aug 2022 12:23:15 +0200 Subject: [PATCH 4/5] revert dev/debug change --- .github/workflows/build_pr_documentation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_pr_documentation.yml b/.github/workflows/build_pr_documentation.yml index 93347253c..40d1fd33d 100644 --- a/.github/workflows/build_pr_documentation.yml +++ b/.github/workflows/build_pr_documentation.yml @@ -95,7 +95,7 @@ jobs: run: | pip uninstall -y doc-builder cd doc-builder - git pull origin create_commit_deletions + git pull origin main pip install . cd .. From dd6718a1232b03a928023ca155d5894d96c58623 Mon Sep 17 00:00:00 2001 From: Mishig Davaadorj Date: Tue, 23 Aug 2022 12:27:44 +0200 Subject: [PATCH 5/5] Add comment --- src/doc_builder/commands/push.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/doc_builder/commands/push.py b/src/doc_builder/commands/push.py index 780a4097e..09284dec1 100644 --- a/src/doc_builder/commands/push.py +++ b/src/doc_builder/commands/push.py @@ -65,7 +65,7 @@ def create_deletions(repo_id: str, library_name: str, token: str) -> List[Dict]: Given `repo_id/library_name` path, returns [FileDeletion!]!: [{path: "some_path"}, ...] see more here: https://docs.github.com/en/graphql/reference/input-objects#filechanges """ - # GET doc-build-dev/transformers + # 1. find url for `doc-build-dev/{library_name}` ex: doc-build-dev/accelerate res = requests.get( f"https://api.github.com/repos/{repo_id}/git/trees/heads/main", headers={"Authorization": f"bearer {token}"} ) @@ -75,7 +75,7 @@ def create_deletions(repo_id: str, library_name: str, token: str) -> List[Dict]: node = next(filter(lambda node: node["path"] == library_name, json["tree"]), None) url = node["url"] - # GET doc-build-dev/transformers/pr_xyz + # 2. find url for `doc-build-dev/{library_name}/{doc_version}` ex: doc-build-dev/accelerate/pr_365 root_folder = Path(library_name) doc_version_folder = next(root_folder.glob("*")).relative_to(root_folder) doc_version_folder = str(doc_version_folder) @@ -89,17 +89,19 @@ def create_deletions(repo_id: str, library_name: str, token: str) -> List[Dict]: return [] url = node["url"] - # GET doc-build-dev/transformers/pr_xyz/**/* + # 3. list paths in `doc-build-dev/{library_name}/{doc_version}/**/*` ex: doc-build-dev/accelerate/pr_365/**/* res = requests.get(f"{url}?recursive=true", headers={"Authorization": f"bearer {token}"}) if res.status_code != 200: raise Exception(f"create_deletions failed (GET tree root/{repo_id}/{doc_version_folder}): {res.message}") json = res.json() tree = json["tree"] - deletions = [] + # 4. list paths in currently built doc folder built_docs_path = Path(f"{library_name}/{doc_version_folder}").absolute() built_docs_files = [x for x in built_docs_path.glob("**/*") if x.is_file()] built_docs_files_relative = set([str(f.relative_to(built_docs_path)) for f in built_docs_files]) + + # 5. deletions = set difference between step 3 & 4 deletions = [ {"path": f"{library_name}/{doc_version_folder}/{node['path']}"} for node in tree