diff --git a/playbooks/propose_update.yaml b/playbooks/propose_update.yaml index c7df943b..1e9fc70e 100644 --- a/playbooks/propose_update.yaml +++ b/playbooks/propose_update.yaml @@ -80,7 +80,8 @@ vars: doc_label: "{{ doc.label }}" apply_doc_patch_repository: "{{ doc.repository }}" - apply_doc_patch_patch_file: "{{ ansible_user_dir }}/{{ doc.label }}.patch" + apply_doc_patch_diff_patch_file: "{{ ansible_user_dir }}/{{ doc.label }}.diff.patch" + apply_doc_patch_git_patch_file: "{{ ansible_user_dir }}/{{ doc.label }}.git.patch" apply_doc_patch_target_location: "{{ doc.project_location }}" loop: "{{ docs }}" loop_control: diff --git a/roles/apply_doc_patch/defaults/main.yaml b/roles/apply_doc_patch/defaults/main.yaml index 2f9fd362..46b966c7 100644 --- a/roles/apply_doc_patch/defaults/main.yaml +++ b/roles/apply_doc_patch/defaults/main.yaml @@ -4,3 +4,4 @@ apply_doc_patch_pr_title: | Changes to {{ doc_label }} from doc-exports#{{ zuul.change }} apply_doc_patch_pr_body_file: "{{ ansible_user_dir }}/{{ doc_label }}_pr_body.txt" apply_doc_patch_pr_label: "autoPR" +apply_doc_patch_reject_file: "{{ ansible_user_dir }}/{{ doc_label }}_rej" diff --git a/roles/apply_doc_patch/tasks/main.yaml b/roles/apply_doc_patch/tasks/main.yaml index ef60492b..d5f27a78 100644 --- a/roles/apply_doc_patch/tasks/main.yaml +++ b/roles/apply_doc_patch/tasks/main.yaml @@ -1,22 +1,34 @@ --- -- name: Check patch presense +- name: Check diff patch presense ansible.builtin.stat: - path: "{{ apply_doc_patch_patch_file }}" - register: "patch_stat" + path: "{{ apply_doc_patch_diff_patch_file }}" + register: "diff_patch_stat" -- name: Clone Repository - no_log: true +- name: Check git patch presense + ansible.builtin.stat: + path: "{{ apply_doc_patch_git_patch_file }}" + register: "git_patch_stat" + +- name: Clone {{ apply_doc_patch_repository }} Repository + # no_log: true command: "git clone https://x-access-token:{{ github_token }}@github.com/{{ apply_doc_patch_repository }} {{ doc_label }}" args: chdir: "{{ ansible_user_dir }}" - when: "patch_stat.stat.exists" + when: "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" -- name: Try to apply patch - command: "patch -p1 -N -f -i {{ apply_doc_patch_patch_file }}" +- name: Try to apply git patch + command: "git apply --reject --ignore-space-change {{ apply_doc_patch_git_patch_file }}" args: - chdir: "{{ ansible_user_dir }}/{{ doc_label }}/{{ apply_doc_patch_target_location }}" + chdir: "{{ ansible_user_dir }}/{{ doc_label }}" ignore_errors: true - when: "patch_stat.stat.exists" + when: "git_patch_stat.stat.exists" + +# - name: Try to apply diff patch +# command: "patch -p1 -N -f -i {{ apply_doc_patch_patch_file }}" +# args: +# chdir: "{{ ansible_user_dir }}/{{ doc_label }}/{{ apply_doc_patch_target_location }}" +# ignore_errors: true +# when: "diff_patch_stat.stat.exists" - name: Find patch backup and reject files find: @@ -25,28 +37,51 @@ use_regex: true recurse: true register: rej_files - when: "patch_stat.stat.exists" + when: "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" + +- name: Prepare patch remainder comment file + template: + dest: "{{ apply_doc_patch_reject_file}}" + src: "patch_remainder_comment.txt.j2" + when: + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" + - "rej_files.matched > 0" + +- name: Construct patch remainder + shell: "cat {{ item.path }} >> {{ apply_doc_patch_reject_file}}" + loop: "{{ rej_files.files }}" + when: + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" + - "rej_files.matched > 0" + +- name: Finish patch remainder comment file + shell: "echo '```' >> {{ apply_doc_patch_reject_file}}" + when: + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" + - "rej_files.matched > 0" - name: Drop all interim patch files command: "find . \\( -name '*.rst.orig' -or -name '*.rst.rej' \\) -exec rm {} \\;" args: chdir: "{{ ansible_user_dir }}/{{ doc_label }}/{{ apply_doc_patch_target_location }}" register: drop_failed - when: "patch_stat.stat.exists" + when: + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" + - "rej_files.matched > 0" - name: Perform git diff to see changes command: "git diff" register: diff_output args: chdir: "{{ ansible_user_dir }}/{{ doc_label }}/{{ apply_doc_patch_target_location }}" - when: "patch_stat.stat.exists" + when: "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" - name: Checkout branch command: "git checkout -b {{ apply_doc_patch_branch_name }}" args: chdir: "{{ ansible_user_dir }}/{{ doc_label }}" when: - - "patch_stat.stat.exists" + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" - "diff_output.stdout != ''" - name: Stage files @@ -54,7 +89,7 @@ args: chdir: "{{ ansible_user_dir }}/{{ doc_label }}" when: - - "patch_stat.stat.exists" + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" - "diff_output.stdout != ''" # Maybe put some reference in the commit message @@ -63,7 +98,7 @@ args: chdir: "{{ ansible_user_dir }}/{{ doc_label }}" when: - - "patch_stat.stat.exists" + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" - "diff_output.stdout != ''" - name: Push changes @@ -71,7 +106,7 @@ args: chdir: "{{ ansible_user_dir }}/{{ doc_label }}" when: - - "patch_stat.stat.exists" + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" - "diff_output.stdout != ''" # Gists are not available by GitHub App, do not try it now @@ -89,7 +124,7 @@ dest: "{{ apply_doc_patch_pr_body_file }}" src: "pr_body.txt.j2" when: - - "patch_stat.stat.exists" + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" - "diff_output.stdout != ''" - name: Search existing Pull Request @@ -98,7 +133,7 @@ chdir: "{{ ansible_user_dir }}/{{ doc_label }}" register: "existing_pr" when: - - "patch_stat.stat.exists" + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" - "diff_output.stdout != ''" - name: Open Pull Request @@ -106,6 +141,15 @@ args: chdir: "{{ ansible_user_dir }}/{{ doc_label }}" when: - - "patch_stat.stat.exists" + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" - "diff_output.stdout != ''" - "existing_pr.stdout == '[]'" + +- name: Post info about apply failures + command: "{{ apply_doc_patch_gh }} pr comment -F {{ apply_doc_patch_reject_file }}" + args: + chdir: "{{ ansible_user_dir }}/{{ doc_label }}" + when: + - "git_patch_stat.stat.exists or diff_patch_stat.stat.exists" + - "diff_output.stdout != ''" + - "rej_files.matched > 0" diff --git a/roles/apply_doc_patch/templates/patch_remainder_comment.txt.j2 b/roles/apply_doc_patch/templates/patch_remainder_comment.txt.j2 new file mode 100644 index 00000000..bad6dc5c --- /dev/null +++ b/roles/apply_doc_patch/templates/patch_remainder_comment.txt.j2 @@ -0,0 +1,9 @@ +I tried my best, but could not apply the patch as is, therefore I will report you what I could not apply. + +BTW, if in the diff you see removed line equal to the added line it may be caused by invisible trailing spaces. This is safe to ignore. + +In most cases content of the patch remainder need to be carefully analyzed and necessary changes applied manually (i.e. by pushing additional commits to the PR once original PR in the doc-exports#{{ zuul.change }} repository closes or making another PR into {{ apply_doc_patch_branch_name }}). + +Following is the patch remainder: + +```