Skip to content

Commit

Permalink
ci: 简化流程
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnAngela committed Dec 15, 2023
1 parent fcd64f7 commit 17f91d7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 67 deletions.
13 changes: 3 additions & 10 deletions .github/workflows/postCommit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,14 @@ jobs:
- name: Check environment
run: 'npx eslint --env-info && echo ---- && echo "stylelint: "$(npx stylelint --version) && echo ---- && echo "v8r: "$(npx v8r --version) && echo ---- && mkdir -vp .cache && echo ".cache:" && ls -lhA .cache'
- name: Run eslint
run: npx eslint --cache --cache-strategy content --cache-location ".cache/" --ext js --exit-on-fatal-error --format ./scripts/modules/eslint-formatter-gha.cjs --max-warnings 0 ./src
run: npx eslint --cache --cache-strategy content --cache-location ".cache/" --ext js --exit-on-fatal-error --format ./scripts/modules/eslint-formatter-gha.cjs --max-warnings 0 ./src || bash infoAboutFailure.sh
- name: Run stylelint
run: npx stylelint --cache --cache-strategy content --cache-location ".cache/" --formatter github --max-warnings 0 "src/**/*.css"
run: npx stylelint --cache --cache-strategy content --cache-location ".cache/" --formatter github --max-warnings 0 "src/**/*.css" || bash infoAboutFailure.sh
- name: Run v8r
run: echo "::group::v8r output" && npx v8r && echo "::endgroup::"
run: echo "::group::v8r output" && npx v8r && echo "::endgroup::" || bash infoAboutFailure.sh 1
- name: Check .mailmap
if: needs.postCommit.result == 'success'
run: node scripts/emailmapChecker/index.js
- name: Infomation about linter test failure
if: failure()
run: |
echo "如果代码检查失败,你可以通过以下方式查看错误原因:"
echo "1. 如果本次提交属于 Pull Request,你可以在该 PR 的 【Files changed】 标签页查看错误原因;"
echo "2. 如果本次提交属于直接推送 commit,你可以在该 commit 页查看错误原因。"
echo "你可能需要下拉到对应页面的最底部来查看未被修改的文件的错误原因。"
webhook:
runs-on: ubuntu-latest
needs:
Expand Down
7 changes: 7 additions & 0 deletions infoAboutFailure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
echo "如果代码检查失败,你可以通过以下方式查看错误原因:"
echo "1. 如果本次提交属于 Pull Request,你可以在该 PR 的 【Files changed】 标签页查看错误原因;"
echo "2. 如果本次提交属于直接推送 commit,你可以在该 commit 页查看错误原因。"
echo "你可能需要下拉到对应页面的最底部来查看未被修改的文件的错误原因。"
if [ $# -eq 1 ]; then
exit $1
fi
60 changes: 3 additions & 57 deletions scripts/postCommit/push.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import console from "../modules/console.js";
console.info("Initialization done.");
import { startGroup, endGroup, setOutput } from "@actions/core";
import { startGroup, endGroup } from "@actions/core";
import git from "../modules/git.js";
import { isInGithubActions, isPullRequest } from "../modules/octokit.js";
import { isInGithubActions } from "../modules/octokit.js";
import readWorkflowEvent from "../modules/workflowEvent.js";

const contentConfigs = [
"src/gadgets/Gadgets-definition-list.yaml",
".github/linter test/action.yaml",
".vscode/json-schemas/gadget-definition.json",
".browserslistrc",
"package-lock.json",
];
/**
* @type {(files: string[]) => boolean}
*/
const detectContentChanged = (files) => files.filter((file) => file.startsWith("src/") || contentConfigs.includes(file) || /^\.[^./]+\.yaml$/.test(file)).length > 0;
if (!isInGithubActions) {
console.info("Not running in github actions, exit.");
process.exit(0);
Expand All @@ -33,56 +21,14 @@ const changedFiles = before && after ? (await git.raw(["diff-tree", "-c", "-r",
startGroup("changedFiles:");
console.info(changedFiles);
endGroup();
/**
* @return {never}
*/
const triggerLinterTest = (force = false) => {
if (!detectContentChanged(changedFiles.split("\n")) && !force) {
console.info("Nothing need to lint, exit.");
process.exit(0);
}
const { commits, head_commit } = GITHUB_EVENT;
const allCommits = Array.isArray(commits) ? commits : [];
if (head_commit && !allCommits.map(({ id }) => id).includes(head_commit.id)) {
allCommits.push(head_commit);
}
const foundCommits = allCommits.map((commit) => {
commit.author.email = commit.author.email.toLowerCase();
commit.committer.email = commit.committer.email.toLowerCase();
return commit;
});
startGroup("Found commits:");
console.info(foundCommits);
endGroup();
setOutput("commits", JSON.stringify(foundCommits));
setOutput("linterTest", "true");
console.info('Exposed outputs "commits" and "linterTest" , exit.');
console.info("Done.");
process.exit(0);
};
if (isPullRequest) {
console.info("Running in github actions, but in pull request event, skip checking unpushed commits...");
triggerLinterTest();
}
console.info("Running in github actions, start to check unpushed commits...");
const unpushedCommits = (await git.raw(["cherry", "-v"])).trim();
if (unpushedCommits.length === 0) {
console.info("No unpushed commit.");
triggerLinterTest();
process.exit(0);
}
console.info("Found unpushed commits:", unpushedCommits.split("\n"));
console.info("Pulling new commits...");
console.info("Successfully pulled the commits:", await git.pull(undefined, undefined, ["--rebase"]));
console.info("Pushing these commits...");
console.info("Successfully pushed the commits:", await git.push());
console.info("process.env.changedFiles:", process.env.changedFiles);
/**
* @type {string[] | undefined}
*/
const changedFilesFromEnv = JSON.parse(process.env.changedFiles || "[]");
console.info("changedFilesFromEnv:", changedFilesFromEnv);
if (!Array.isArray(changedFilesFromEnv) || changedFilesFromEnv.length === 0) {
console.info("Unable to get changed files.");
triggerLinterTest();
}
triggerLinterTest(detectContentChanged(changedFilesFromEnv));

0 comments on commit 17f91d7

Please sign in to comment.