Skip to content

Commit

Permalink
chore: add prettier config (#157)
Browse files Browse the repository at this point in the history
* feat: add prettier config

* feat: format files and prettier check
  • Loading branch information
mandarini committed Jun 17, 2024
1 parent bcc7f3a commit 5ba8bac
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 130 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Check Formatting

on:
pull_request:
branches:
- main

jobs:
check-format:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18

- name: Install
run: yarn --frozen-lockfile

- name: Check formatting with Prettier
run: npx prettier --check .
1 change: 0 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
npx --no-install lint-staged --allow-empty

node tools/pre-commit.js

9 changes: 9 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
dist
/yarn.lock
/.vscode
/.idea
/.github
/.husky
/.yarn
/.env
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"endOfLine": "lf",
"tabWidth": 2
}
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,37 @@ jobs:
# Common names for this branch include main and master.
#
# Default: main
main-branch-name: ""
main-branch-name: ''

# Applies the derived SHAs for base and head as NX_BASE and NX_HEAD environment variables within the current Job.
#
# Default: true
set-environment-variables-for-job: ""
set-environment-variables-for-job: ''

# By default, if no successful workflow run is found on the main branch to determine the SHA, we will log a warning and use HEAD~1. Enable this option to error and exit instead.
#
# Default: false
error-on-no-successful-workflow: ""
error-on-no-successful-workflow: ''

# Fallback SHA to use if no successful workflow run is found. This can be useful in scenarios where you need a specific commit as a reference for comparison, especially in newly set up repositories or those with sparse workflow runs.
#
# Default: ""
fallback-sha: ""
fallback-sha: ''

# The type of event to check for the last successful commit corresponding to that workflow-id, e.g. push, pull_request, release etc.
#
# Default: push
last-successful-event: ""
last-successful-event: ''

# The path where your repository is. This is only required for cases where the repository code is checked out or moved to a specific path.
#
# Default: .
working-directory: ""
working-directory: ''

# The ID of the github action workflow to check for successful run or the name of the file name containing the workflow.
# E.g. 'ci.yml'. If not provided, current workflow id will be used
#
workflow-id: ""
workflow-id: ''
```

<!-- end configuration-options -->
Expand All @@ -123,8 +123,8 @@ jobs:
runs-on: ubuntu-latest
name: My Job
permissions:
contents: "read"
actions: "read"
contents: 'read'
actions: 'read'
```

<!-- end permissions-in-v2 -->
Expand Down
36 changes: 18 additions & 18 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
name: "Nx set SHAs"
description: "Derives SHAs for base and head for use in nx affected commands, optionally setting them as env variables for the current job"
name: 'Nx set SHAs'
description: 'Derives SHAs for base and head for use in nx affected commands, optionally setting them as env variables for the current job'

inputs:
main-branch-name:
description: "The name of the main branch in your repo, used as the target of PRs. E.g. main, master etc"
default: "main"
description: 'The name of the main branch in your repo, used as the target of PRs. E.g. main, master etc'
default: 'main'
set-environment-variables-for-job:
description: "Applies the derived SHAs for base and head as NX_BASE and NX_HEAD environment variables within the current Job"
default: "true"
description: 'Applies the derived SHAs for base and head as NX_BASE and NX_HEAD environment variables within the current Job'
default: 'true'
error-on-no-successful-workflow:
description: "By default, if no successful workflow is found on the main branch to determine the SHA, we will log a warning and use HEAD~1. Enable this option to error and exit instead."
default: "false"
description: 'By default, if no successful workflow is found on the main branch to determine the SHA, we will log a warning and use HEAD~1. Enable this option to error and exit instead.'
default: 'false'
fallback-sha:
description: "Fallback SHA to use if no successful workflow run is found."
description: 'Fallback SHA to use if no successful workflow run is found.'
required: false
default: ""
default: ''
last-successful-event:
description: "The type of event to check for the last successful commit corresponding to that workflow-id, e.g. push, pull_request, release etc"
default: "push"
description: 'The type of event to check for the last successful commit corresponding to that workflow-id, e.g. push, pull_request, release etc'
default: 'push'
working-directory:
description: "The directory where your repository is located"
default: "."
description: 'The directory where your repository is located'
default: '.'
workflow-id:
description: "The ID of the workflow to track or name of the file name. E.g. ci.yml. Defaults to current workflow"
description: 'The ID of the workflow to track or name of the file name. E.g. ci.yml. Defaults to current workflow'

outputs:
base:
Expand All @@ -36,7 +36,7 @@ outputs:
value: ${{ steps.setSHAs.outputs.noPreviousBuild }}

runs:
using: "composite"
using: 'composite'
steps:
- name: Set base and head SHAs used for nx affected
id: setSHAs
Expand Down Expand Up @@ -68,5 +68,5 @@ runs:
echo "NX_HEAD=${{ steps.setSHAs.outputs.head }}" >> $GITHUB_ENV
echo "NX_BASE and NX_HEAD environment variables have been set for the current Job"
branding:
icon: "terminal"
color: "blue"
icon: 'terminal'
color: 'blue'
66 changes: 33 additions & 33 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37868,7 +37868,7 @@ const lastSuccessfulEvent = process.argv[5];
const workingDirectory = process.argv[6];
const workflowId = process.argv[7];
const fallbackSHA = process.argv[8];
const defaultWorkingDirectory = ".";
const defaultWorkingDirectory = '.';
const ProxifiedClient = action_1.Octokit.plugin(proxyPlugin);
let BASE_SHA;
(() => __awaiter(void 0, void 0, void 0, function* () {
Expand All @@ -37877,20 +37877,20 @@ let BASE_SHA;
process.chdir(workingDirectory);
}
else {
process.stdout.write("\n");
process.stdout.write('\n');
process.stdout.write(`WARNING: Working directory '${workingDirectory}' doesn't exist.\n`);
}
}
const headResult = (0, child_process_1.spawnSync)("git", ["rev-parse", "HEAD"], {
encoding: "utf-8",
const headResult = (0, child_process_1.spawnSync)('git', ['rev-parse', 'HEAD'], {
encoding: 'utf-8',
});
const HEAD_SHA = headResult.stdout;
if ((["pull_request", "pull_request_target"].includes(eventName) &&
if ((['pull_request', 'pull_request_target'].includes(eventName) &&
!github.context.payload.pull_request.merged) ||
eventName == "merge_group") {
eventName == 'merge_group') {
try {
const mergeBaseRef = yield findMergeBaseRef();
const baseResult = (0, child_process_1.spawnSync)("git", ["merge-base", `origin/${mainBranchName}`, mergeBaseRef], { encoding: "utf-8" });
const baseResult = (0, child_process_1.spawnSync)('git', ['merge-base', `origin/${mainBranchName}`, mergeBaseRef], { encoding: 'utf-8' });
BASE_SHA = baseResult.stdout;
}
catch (e) {
Expand All @@ -37907,41 +37907,41 @@ let BASE_SHA;
return;
}
if (!BASE_SHA) {
if (errorOnNoSuccessfulWorkflow === "true") {
if (errorOnNoSuccessfulWorkflow === 'true') {
reportFailure(mainBranchName);
return;
}
else {
process.stdout.write("\n");
process.stdout.write('\n');
process.stdout.write(`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}', or the latest successful workflow was connected to a commit which no longer exists on that branch (e.g. if that branch was rebased)\n`);
if (fallbackSHA) {
BASE_SHA = fallbackSHA;
process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`);
}
else {
process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`);
process.stdout.write("\n");
process.stdout.write('\n');
process.stdout.write(`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`);
process.stdout.write("\n");
const commitCountOutput = (0, child_process_1.spawnSync)("git", ["rev-list", "--count", `origin/${mainBranchName}`], { encoding: "utf-8" }).stdout;
process.stdout.write('\n');
const commitCountOutput = (0, child_process_1.spawnSync)('git', ['rev-list', '--count', `origin/${mainBranchName}`], { encoding: 'utf-8' }).stdout;
const commitCount = parseInt(stripNewLineEndings(commitCountOutput), 10);
const LAST_COMMIT_CMD = `origin/${mainBranchName}${commitCount > 1 ? "~1" : ""}`;
const baseRes = (0, child_process_1.spawnSync)("git", ["rev-parse", LAST_COMMIT_CMD], {
encoding: "utf-8",
const LAST_COMMIT_CMD = `origin/${mainBranchName}${commitCount > 1 ? '~1' : ''}`;
const baseRes = (0, child_process_1.spawnSync)('git', ['rev-parse', LAST_COMMIT_CMD], {
encoding: 'utf-8',
});
BASE_SHA = baseRes.stdout;
}
core.setOutput("noPreviousBuild", "true");
core.setOutput('noPreviousBuild', 'true');
}
}
else {
process.stdout.write("\n");
process.stdout.write('\n');
process.stdout.write(`Found the last successful workflow run on 'origin/${mainBranchName}'\n`);
process.stdout.write(`Commit: ${BASE_SHA}\n`);
}
}
core.setOutput("base", stripNewLineEndings(BASE_SHA));
core.setOutput("head", stripNewLineEndings(HEAD_SHA));
core.setOutput('base', stripNewLineEndings(BASE_SHA));
core.setOutput('head', stripNewLineEndings(HEAD_SHA));
}))();
function reportFailure(branchName) {
core.setFailed(`
Expand All @@ -37953,7 +37953,7 @@ function reportFailure(branchName) {
- If no, then you might have changed your git history and those commits no longer exist.`);
}
function proxyPlugin(octokit) {
octokit.hook.before("request", (options) => {
octokit.hook.before('request', (options) => {
const proxy = (0, proxy_from_env_1.getProxyForUrl)(options.baseUrl);
if (proxy) {
options.request.agent = new https_proxy_agent_1.HttpsProxyAgent(proxy);
Expand All @@ -37975,7 +37975,7 @@ function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch, lastSucc
run_id,
})
.then(({ data: { workflow_id } }) => workflow_id);
process.stdout.write("\n");
process.stdout.write('\n');
process.stdout.write(`Workflow Id not provided. Using workflow '${workflow_id}'\n`);
}
// fetch all workflow runs on a given repo/branch/workflow with push and success
Expand All @@ -37984,26 +37984,26 @@ function findSuccessfulCommit(workflow_id, run_id, owner, repo, branch, lastSucc
owner,
repo,
// on some workflow runs we do not have branch property
branch: lastSuccessfulEvent === "push" ||
lastSuccessfulEvent === "workflow_dispatch"
branch: lastSuccessfulEvent === 'push' ||
lastSuccessfulEvent === 'workflow_dispatch'
? branch
: undefined,
workflow_id,
event: lastSuccessfulEvent,
status: "success",
status: 'success',
})
.then(({ data: { workflow_runs } }) => workflow_runs.map((run) => run.head_sha));
return yield findExistingCommit(octokit, branch, shas);
});
}
function findMergeBaseRef() {
return __awaiter(this, void 0, void 0, function* () {
if (eventName == "merge_group") {
if (eventName == 'merge_group') {
const mergeQueueBranch = yield findMergeQueueBranch();
return `origin/${mergeQueueBranch}`;
}
else {
return "HEAD";
return 'HEAD';
}
});
}
Expand All @@ -38016,9 +38016,9 @@ function findMergeQueueBranch() {
return __awaiter(this, void 0, void 0, function* () {
const pull_number = findMergeQueuePr();
if (!pull_number) {
throw new Error("Failed to determine PR number");
throw new Error('Failed to determine PR number');
}
process.stdout.write("\n");
process.stdout.write('\n');
process.stdout.write(`Found PR #${pull_number} from merge queue branch\n`);
const octokit = new ProxifiedClient();
const result = yield octokit.request(`GET /repos/${owner}/${repo}/pulls/${pull_number}`, { owner, repo, pull_number: +pull_number });
Expand All @@ -38044,17 +38044,17 @@ function findExistingCommit(octokit, branchName, shas) {
function commitExists(octokit, branchName, commitSha) {
return __awaiter(this, void 0, void 0, function* () {
try {
(0, child_process_1.spawnSync)("git", ["cat-file", "-e", commitSha], {
stdio: ["pipe", "pipe", null],
(0, child_process_1.spawnSync)('git', ['cat-file', '-e', commitSha], {
stdio: ['pipe', 'pipe', null],
});
// Check the commit exists in general
yield octokit.request("GET /repos/{owner}/{repo}/commits/{commit_sha}", {
yield octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', {
owner,
repo,
commit_sha: commitSha,
});
// Check the commit exists on the expected main branch (it will not in the case of a rebased main branch)
const commits = yield octokit.request("GET /repos/{owner}/{repo}/commits", {
const commits = yield octokit.request('GET /repos/{owner}/{repo}/commits', {
owner,
repo,
sha: branchName,
Expand All @@ -38071,7 +38071,7 @@ function commitExists(octokit, branchName, commitSha) {
* Strips LF line endings from given string
*/
function stripNewLineEndings(string) {
return string.replace("\n", "");
return string.replace('\n', '');
}


Expand Down
Loading

0 comments on commit 5ba8bac

Please sign in to comment.