Skip to content

Commit

Permalink
fix(bot): apply pre-created patch instead of running npm scripts for …
Browse files Browse the repository at this point in the history
…lint (#1736)
  • Loading branch information
AlCalzone authored Feb 16, 2021
1 parent 0e209d5 commit 3e8b608
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 81 deletions.
6 changes: 4 additions & 2 deletions .github/bot-scripts/fixLintFeedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ async function main(param) {
};

let feedbackText;
if (process.env.FEEDBACK === "error") {
feedbackText = `🔨 I tried my best, but unfortunately some lint errors require manual attention.`;
if (process.env.PENDING) {
feedbackText = `🐌 Please wait for the lint check to complete, then try again.`;
} else if (process.env.FEEDBACK === "error") {
feedbackText = `❌ I tried my best, but something went wrong.`;
} else if (process.env.FEEDBACK === "unchanged") {
feedbackText = `😕 Sorry, I couldn't do anything here.`;
} else {
Expand Down
56 changes: 56 additions & 0 deletions .github/bot-scripts/getFixLintInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// <reference path="types.d.ts" />
// @ts-check

/**
* @param {{github: Github, context: Context}} param
*/
async function main(param) {
const { github, context } = param;

const options = {
owner: context.repo.owner,
repo: context.repo.repo,
};

// Get PR info
const { data: pull } = await github.pulls.get({
...options,
pull_number: context.issue.number,
});
const { data: checks } = await github.checks.listForRef({
...options,
ref: pull.head.sha,
});
// Find the correct check
const lintCheck = checks.check_runs.find(
(r) => r.conclusion === "failure" && r.name.startsWith("lint"),
);
if (!lintCheck) return undefined;

const { data: job } = await github.actions.getJobForWorkflowRun({
...options,
job_id: lintCheck.id,
});
const {
data: { artifacts },
} = await github.actions.listWorkflowRunArtifacts({
...options,
// @ts-expect-error for some reason job is `never`
run_id: job.run_id,
});

if (!artifacts.length) return undefined;

const { url } = await github.actions.downloadArtifact({
...options,
artifact_id: artifacts[0].id,
archive_format: "zip",
});

return {
repoName: pull.head.repo.full_name,
headRef: pull.head.ref,
patchUrl: url,
};
}
module.exports = main;
1 change: 1 addition & 0 deletions .github/bot-scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
checkAuthorized: (...args) => require("./checkAuthorized")(...args),
fixLintFeedback: (...args) => require("./fixLintFeedback")(...args),
getFixLintInfo: (...args) => require("./getFixLintInfo")(...args),
rebaseFeedback: (...args) => require("./rebaseFeedback")(...args),
renameCommitGetPRInfo: (...args) =>
require("./renameCommitGetPRInfo")(...args),
Expand Down
116 changes: 37 additions & 79 deletions .github/workflows/zwave-js-bot_comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ jobs:
if: |
contains(github.event.issue.html_url, '/pull/') &&
contains(github.event.comment.body, '@zwave-js-bot fix lint') &&
(github.event.comment.user.login != 'zwave-js-bot' && github.event.comment.user.login != 'zwave-js-assistant[bot]') &&
(github.event.comment.user.login == 'AlCalzone')
(github.event.comment.user.login != 'zwave-js-bot' && github.event.comment.user.login != 'zwave-js-assistant[bot]')
runs-on: [ubuntu-latest]
strategy:
Expand All @@ -104,84 +103,50 @@ jobs:
# These steps only run if the check was successful
- name: Retrieve PR information
if: steps.check-permissions.outputs.result == 'true'
id: get-pr
id: pr-info
uses: actions/github-script@v3
with:
script: |
const request = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
}
core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`)
try {
const result = await github.pulls.get(request)
return result.data
} catch (err) {
core.setFailed(`Request failed with error ${err}`)
}
const bot = require(`${process.env.GITHUB_WORKSPACE}/trusted/.github/bot-scripts/index.js`);
const result = await bot.getFixLintInfo({github, context});
console.dir(result);
return result || {pending: true};
- name: Download Lint patch
if: |
steps.check-permissions.outputs.result == 'true' &&
!fromJSON(steps.pr-info.outputs.result).pending
env:
PATCH_URL: ${{ fromJSON(steps.pr-info.outputs.result).patchUrl }}
run: |
mkdir -p patch
cd patch
curl "$PATCH_URL" -o patch.zip
unzip patch.zip
- name: Checkout pull request side by side
if: steps.check-permissions.outputs.result == 'true'
if: |
steps.check-permissions.outputs.result == 'true' &&
!fromJSON(steps.pr-info.outputs.result).pending
uses: actions/checkout@v2
with:
token: ${{secrets.BOT_TOKEN}}
repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
ref: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
repository: ${{ fromJSON(steps.pr-info.outputs.result).repoName }}
ref: ${{ fromJSON(steps.pr-info.outputs.result).headRef }}
path: untrusted

- name: Use Node.js ${{ matrix.node-version }}
if: steps.check-permissions.outputs.result == 'true'
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Get Yarn cache directory
if: steps.check-permissions.outputs.result == 'true'
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Use Yarn cache
if: steps.check-permissions.outputs.result == 'true'
uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ matrix.node-version }}-${{ hashFiles('trusted/**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
if: steps.check-permissions.outputs.result == 'true'
working-directory: ./trusted
run: yarn install --prefer-offline --frozen-lockfile

- name: Copy sources from untrusted repo
if: steps.check-permissions.outputs.result == 'true'
run: |
rm -rf ./trusted/packages
cp -r ./untrusted/packages/ ./trusted
# We need this or the linter will complain about other stuff than the checks
- name: Compile TypeScript code
if: steps.check-permissions.outputs.result == 'true'
working-directory: ./trusted
run: yarn run build

- name: Do the lint fix
if: steps.check-permissions.outputs.result == 'true'
working-directory: ./trusted
if: |
steps.check-permissions.outputs.result == 'true' &&
!fromJSON(steps.pr-info.outputs.result).pending
working-directory: ./untrusted
id: fix
run: |
# Run all lint commands and remember if one fails
LINT_RESULT=0
if ! yarn run lint:ts --fix ; then LINT_RESULT=1 ; fi
if ! yarn run lint:wotan --fix ; then LINT_RESULT=1 ; fi
if ! yarn run lint:configjson -w ; then LINT_RESULT=1 ; fi
# Copy potentially fixed sources back
rm -rf ../untrusted/packages
cp -r ./packages/ ../untrusted
# Apply the patch
if ! git apply ../patch/fix.patch ; then
echo "::set-output name=result::error"
exit 0
fi
# Check if something changed in the untrusted repo
cd ../untrusted
Expand All @@ -192,22 +157,18 @@ jobs:
git reset -- .github
git commit -m "style: fix lint"
git push
echo "::set-output name=result::ok"
else
echo "::set-output name=result::unchanged"
exit 0
fi
if [[ $LINT_RESULT == 0 ]] ; then
echo "::set-output name=result::ok"
else
echo "::set-output name=result::error"
fi
- name: Give feedback
if: steps.check-permissions.outputs.result == 'true'
uses: actions/github-script@v3
env:
FEEDBACK: ${{ steps.fix.outputs.result }}
PENDING: ${{ fromJSON(steps.pr-info.outputs.result).pending }}
with:
github-token: ${{secrets.BOT_TOKEN}}
script: |
Expand All @@ -222,8 +183,7 @@ jobs:
if: |
contains(github.event.issue.html_url, '/pull/') &&
contains(github.event.comment.body, '@zwave-js-bot rebase') &&
(github.event.comment.user.login != 'zwave-js-bot' && github.event.comment.user.login != 'zwave-js-assistant[bot]') &&
(github.event.comment.user.login == 'AlCalzone')
(github.event.comment.user.login != 'zwave-js-bot' && github.event.comment.user.login != 'zwave-js-assistant[bot]')
runs-on: [ubuntu-latest]
strategy:
Expand Down Expand Up @@ -321,8 +281,7 @@ jobs:
if: |
contains(github.event.issue.html_url, '/pull/') &&
contains(github.event.comment.body, '@zwave-js-bot rename commit') &&
(github.event.comment.user.login != 'zwave-js-bot' && github.event.comment.user.login != 'zwave-js-assistant[bot]') &&
(github.event.comment.user.login == 'AlCalzone')
(github.event.comment.user.login != 'zwave-js-bot' && github.event.comment.user.login != 'zwave-js-assistant[bot]')
runs-on: [ubuntu-latest]
strategy:
Expand Down Expand Up @@ -412,8 +371,7 @@ jobs:
if: |
contains(github.event.issue.html_url, '/issues/') &&
contains(github.event.comment.body, '@zwave-js-bot import config') &&
(github.event.comment.user.login != 'zwave-js-bot' && github.event.comment.user.login != 'zwave-js-assistant[bot]') &&
(github.event.comment.user.login == 'AlCalzone')
(github.event.comment.user.login != 'zwave-js-bot' && github.event.comment.user.login != 'zwave-js-assistant[bot]')
runs-on: [ubuntu-latest]
strategy:
Expand Down

0 comments on commit 3e8b608

Please sign in to comment.