|
| 1 | +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples |
| 2 | +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help |
| 3 | +on: [push, pull_request] |
| 4 | + |
| 5 | +name: lint-changed-warnings |
| 6 | + |
| 7 | +permissions: read-all |
| 8 | + |
| 9 | +jobs: |
| 10 | + redundancy-check: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + outputs: |
| 13 | + redundant: ${{ steps.check.outputs.redundant }} |
| 14 | + steps: |
| 15 | + - name: Check if this branch is a part of a PR |
| 16 | + id: check |
| 17 | + if: github.event_name != 'pull_request' |
| 18 | + run: | |
| 19 | + import requests |
| 20 | + import os |
| 21 | + import json |
| 22 | + headers = {'Authorization': 'token ' + '${{ secrets.GITHUB_TOKEN }}'} |
| 23 | + prs = requests.get("https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${{ github.ref_name }}", |
| 24 | + headers = headers) |
| 25 | + if len(prs.json()) > 0: open(os.getenv("GITHUB_OUTPUT"), 'a').write("redundant=1\n") |
| 26 | + shell: python |
| 27 | + |
| 28 | + lint-changed-warnings: |
| 29 | + needs: redundancy-check |
| 30 | + if: needs.redundancy-check.outputs.redundant == false |
| 31 | + runs-on: ubuntu-latest |
| 32 | + env: |
| 33 | + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + steps: |
| 35 | + - name: Check out HEAD |
| 36 | + uses: actions/checkout@v4 |
| 37 | + with: |
| 38 | + path: head |
| 39 | + |
| 40 | + - name: Select BASE |
| 41 | + run: | |
| 42 | + if [[ ${{ github.event_name }} == "pull_request" ]] |
| 43 | + then # PR -> base branch |
| 44 | + echo "BASEREF=${{ github.event.pull_request.base.ref }}" >> "$GITHUB_ENV" |
| 45 | + elif [[ ${{ github.ref_name }} == ${{ github.event.repository.default_branch }} ]] |
| 46 | + then # default branch -> previous push |
| 47 | + echo "BASEREF=${{ github.event.before }}" >> "$GITHUB_ENV" |
| 48 | + else # otherwise -> default branch |
| 49 | + echo "BASEREF=${{ github.event.repository.default_branch }}" >> "$GITHUB_ENV" |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Obtain the diff for PR |
| 53 | + if: github.event_name == 'pull_request' |
| 54 | + run: | |
| 55 | + import requests |
| 56 | + import json |
| 57 | + headers = {'Authorization': 'token ' + '${{ secrets.GITHUB_TOKEN }}'} |
| 58 | + files = requests.get("https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files", headers = headers) |
| 59 | + open('diff.json', 'w').write(files.text) |
| 60 | + open('files.list', 'w').write('\n'.join([f['filename'] for f in files.json()])) |
| 61 | + shell: python |
| 62 | + |
| 63 | + - name: Obtain the diff for other push |
| 64 | + if: github.event_name != 'pull_request' |
| 65 | + run: | |
| 66 | + import requests |
| 67 | + import json |
| 68 | + headers = {'Authorization': 'token ' + '${{ secrets.GITHUB_TOKEN }}'} |
| 69 | + diff = requests.get("https://api.github.com/repos/${{ github.repository }}/compare/${{ env.BASEREF }}...${{ github.ref_name }}", headers = headers) |
| 70 | + json.dump(diff.json()['files'], open('diff.json', 'w')) |
| 71 | + open('files.list', 'w').write('\n'.join([f['filename'] for f in diff.json()['files']])) |
| 72 | + shell: python |
| 73 | + |
| 74 | + - name: Check out BASE |
| 75 | + uses: actions/checkout@v4 |
| 76 | + with: |
| 77 | + path: base |
| 78 | + ref: ${{ env.BASEREF }} |
| 79 | + |
| 80 | + - uses: r-lib/actions/setup-r@v2 |
| 81 | + |
| 82 | + - uses: r-lib/actions/setup-r-dependencies@v2 |
| 83 | + with: |
| 84 | + extra-packages: | |
| 85 | + any::lintr |
| 86 | + needs: check |
| 87 | + working-directory: head |
| 88 | + |
| 89 | + - name: Add lintr options |
| 90 | + run: | |
| 91 | + cat('\noptions(lintr.linter_file = ".lintr")\n', file = "~/.Rprofile", append = TRUE) |
| 92 | + shell: Rscript {0} |
| 93 | + |
| 94 | + - name: Lint both versions |
| 95 | + run: | |
| 96 | + files <- scan("files.list", character(), sep = "\n") |
| 97 | + base_files <- list.files("base", recursive = TRUE) |
| 98 | + base_exclude <- setdiff(base_files, files) |
| 99 | + base <- lintr::lint_package("base", exclusions = base_exclude) |
| 100 | + writeLines(capture.output(base), "lintr.base.txt") |
| 101 | +
|
| 102 | + head_files <- list.files("head", recursive = TRUE) |
| 103 | + head_exclude <- setdiff(head_files, files) |
| 104 | + head <- lintr::lint_package("head", exclusions = head_exclude) |
| 105 | + writeLines(capture.output(head), "lintr.head.txt") |
| 106 | + shell: Rscript {0} |
| 107 | + env: |
| 108 | + LINTR_ERROR_ON_LINT: false |
| 109 | + |
| 110 | + - name: Run cpplint |
| 111 | + run: | |
| 112 | + set +e |
| 113 | + pipx run cpplint --version |
| 114 | + pipx run cpplint --recursive base 2> cpplint.base.txt |
| 115 | + pipx run cpplint --recursive head 2> cpplint.head.txt |
| 116 | + set -e |
| 117 | +
|
| 118 | + # The first -e deletes base/ and head/ from the file path, the |
| 119 | + # second rearranges the message into GitHub's format. |
| 120 | + sed -i -r -e 's!^[^/]+/!!' -e 's!^([^:]+):([0-9]+):!::warning file=\1,line=\2::!' cpplint.base.txt cpplint.head.txt |
| 121 | +
|
| 122 | + - name: Save only unique warnings |
| 123 | + run: | |
| 124 | + import json |
| 125 | + import re |
| 126 | + import sys |
| 127 | + import os.path |
| 128 | + sys.path.append(os.path.join('head', '.github', 'workflows')) |
| 129 | + from changed_warnings import filter_messages, split_lines |
| 130 | +
|
| 131 | + patches = {f['filename']:f['patch'] for f in json.load(open('diff.json'))} |
| 132 | +
|
| 133 | + lintr_re = re.compile('^::warning file=(?P<file>.+),line=(?P<line>[0-9]+),col=(?P<col>[0-9]+)::') |
| 134 | + o = filter_messages(open("lintr.base.txt"), open("lintr.head.txt"), patches, |
| 135 | + lintr_re, lambda l, o, n: l.replace(f',line={o},', f',line={n},')) |
| 136 | + split_lines(o, 'lintr-') |
| 137 | +
|
| 138 | + cpplint_re = re.compile('^::warning file=(?P<file>.+),line=(?P<line>[0-9]+)::') |
| 139 | + o = filter_messages(open("cpplint.base.txt"), open("cpplint.head.txt"), patches, |
| 140 | + cpplint_re, lambda l, o, n: l.replace(f',line={o}::', f',line={n}::')) |
| 141 | + split_lines(o, 'cpplint-') |
| 142 | + shell: python |
| 143 | + |
| 144 | + - name: Print lintr warnings 1 |
| 145 | + if: ${{ hashFiles('lintr-1.txt') != '' }} |
| 146 | + run: cat "lintr-1.txt" |
| 147 | + |
| 148 | + - name: Print lintr warnings 2 |
| 149 | + if: ${{ hashFiles('lintr-2.txt') != '' }} |
| 150 | + run: cat "lintr-2.txt" |
| 151 | + |
| 152 | + - name: Print lintr warnings 3 |
| 153 | + if: ${{ hashFiles('lintr-3.txt') != '' }} |
| 154 | + run: cat "lintr-3.txt" |
| 155 | + |
| 156 | + - name: Print lintr warnings 4 |
| 157 | + if: ${{ hashFiles('lintr-4.txt') != '' }} |
| 158 | + run: cat "lintr-4.txt" |
| 159 | + |
| 160 | + - name: Print lintr warnings 5 |
| 161 | + if: ${{ hashFiles('lintr-5.txt') != '' }} |
| 162 | + run: cat "lintr-5.txt" |
| 163 | + |
| 164 | + - name: Print cpplint warnings 1 |
| 165 | + if: ${{ hashFiles('cpplint-1.txt') != '' }} |
| 166 | + run: cat "cpplint-1.txt" |
| 167 | + |
| 168 | + - name: Print cpplint warnings 2 |
| 169 | + if: ${{ hashFiles('cpplint-2.txt') != '' }} |
| 170 | + run: cat "cpplint-2.txt" |
| 171 | + |
| 172 | + - name: Print cpplint warnings 3 |
| 173 | + if: ${{ hashFiles('cpplint-3.txt') != '' }} |
| 174 | + run: cat "cpplint-3.txt" |
| 175 | + |
| 176 | + - name: Print cpplint warnings 4 |
| 177 | + if: ${{ hashFiles('cpplint-4.txt') != '' }} |
| 178 | + run: cat "cpplint-4.txt" |
| 179 | + |
| 180 | + - name: Print cpplint warnings 5 |
| 181 | + if: ${{ hashFiles('cpplint-5.txt') != '' }} |
| 182 | + run: cat "cpplint-5.txt" |
0 commit comments