Skip to content

Commit

Permalink
Fixes for spec-folder-path (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
grant0417 authored May 30, 2024
1 parent 5b6e0e1 commit 64cd0f9
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 266 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ jobs:

- name: Set Node.js
uses: actions/setup-node@v4
with:
node-version-file: .node-version

- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 8
run_install: true

- name: Rebuild the dist/ directory
Expand Down
36 changes: 14 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ jobs:

- name: Set Node.js
uses: actions/setup-node@v4
with:
node-version-file: .node-version

- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 8
run_install: true

- name: Rebuild the dist/ directory
Expand All @@ -44,13 +45,10 @@ jobs:
repo-org: withfig
pr-body: "Integration test (simple-spec)"
diff-based-versioning: false
- name: Close Pull Request
uses: peter-evans/close-pull@v2
with:
token: ${{ secrets.FIG_BOT_TOKEN }}
pull-request-number: ${{ steps.simple-spec.outputs.pr-number }}
comment: "Closing created PR"
delete-branch: true
- name: Close Pull
run: gh pr close --comment "Closing created PR" --delete-branch ${{ steps.simple-spec.outputs.pr-number }}
env:
GH_TOKEN: ${{ secrets.FIG_BOT_TOKEN }}

test-diff-based-versioning:
if: startsWith(github.head_ref, 'auto-update') == false
Expand All @@ -70,13 +68,10 @@ jobs:
pr-body: "Integration test (diff-based-versioning)"
diff-based-versioning: true
new-spec-version: 1.1.0
- name: Close Pull Request
uses: peter-evans/close-pull@v2
with:
token: ${{ secrets.FIG_BOT_TOKEN }}
pull-request-number: ${{ steps.diff-based-versioning.outputs.pr-number }}
comment: "Closing created PR"
delete-branch: true
- name: Close Pull
run: gh pr close --comment "Closing created PR" --delete-branch ${{ steps.diff-based-versioning.outputs.pr-number }}
env:
GH_TOKEN: ${{ secrets.FIG_BOT_TOKEN }}

test-spec-folder:
if: startsWith(github.head_ref, 'auto-update') == false
Expand All @@ -94,10 +89,7 @@ jobs:
repo-name: push-to-fig-autocomplete-action
repo-org: withfig
pr-body: "Integration test (spec-folder)"
- name: Close Pull Request
uses: peter-evans/close-pull@v2
with:
token: ${{ secrets.FIG_BOT_TOKEN }}
pull-request-number: ${{ steps.spec-folder.outputs.pr-number }}
comment: "Closing created PR"
delete-branch: true
- name: Close Pull
run: gh pr close --comment "Closing created PR" --delete-branch ${{ steps.spec-folder.outputs.pr-number }}
env:
GH_TOKEN: ${{ secrets.FIG_BOT_TOKEN }}
426 changes: 213 additions & 213 deletions dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"description": "Action to automatically open a new PR to the withfig/autocomplete repo",
"main": "lib/main.js",
"packageManager": "[email protected]",
"scripts": {
"format": "prettier --write '**/*.ts'",
"format-check": "prettier --check '**/*.ts'",
Expand Down
14 changes: 8 additions & 6 deletions src/autocomplete-repo-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class AutocompleteRepoManager {
};
}

autocompleteRepoString() {
return `${this.autocompleteRepo.owner}/${this.autocompleteRepo.repo}`;
}

constructor(
autocompleteRepo: Repo,
autocompleteDefaultBranch: string,
Expand Down Expand Up @@ -65,6 +69,7 @@ export class AutocompleteRepoManager {
core.info(`Created a new branch on the fork: refs/heads/${branchName}`);

// create new blob, new tree, commit everything and update PR branch
core.startGroup("Creating file blobs");
const blobs = [];
for (const fileOrFolder of localSpecFileOrFolder) {
const stats = await stat(fileOrFolder.localPath);
Expand All @@ -80,6 +85,7 @@ export class AutocompleteRepoManager {
);
}
}
core.endGroup();

const newTree = await this.octokit.rest.git.createTree({
...fork,
Expand Down Expand Up @@ -255,9 +261,7 @@ export class AutocompleteRepoManager {
async cloneFile(repoFilepath: string, destinationPath: string) {
core.startGroup("Starting to clone file...");
core.info(
`Cloning ${repoFilepath} from repo: ${JSON.stringify(
this.autocompleteRepo,
)} into ${destinationPath}`,
`Cloning ${repoFilepath} from repo: ${this.autocompleteRepoString()} into ${destinationPath}`,
);
let fileData;
try {
Expand Down Expand Up @@ -302,9 +306,7 @@ export class AutocompleteRepoManager {
async cloneSpecFolder(repoFolderPath: string, destinationFolderPath: string) {
core.startGroup("Starting to clone folder...");
core.info(
`Cloning ${repoFolderPath} from repo: ${JSON.stringify(
this.autocompleteRepo,
)} into ${destinationFolderPath}`,
`Cloning ${repoFolderPath} from repo: ${this.autocompleteRepoString()} into ${destinationFolderPath}`,
);
let folderData;
try {
Expand Down
8 changes: 8 additions & 0 deletions src/git-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as github from "@actions/github";
import * as core from "@actions/core";
import * as path from "node:path";
import type { Blob, FileOrFolder, Octokit, Repo } from "./types";
import { readFile, readdir } from "node:fs/promises";
import { sleep } from "./utils";

export async function getDefaultBranch(
octokit: Octokit,
Expand All @@ -15,13 +17,19 @@ export async function createFileBlob(
repo: Repo,
filePath: FileOrFolder,
): Promise<Blob> {
core.info(`Creating blob for ${filePath.localPath}`);
const newBlob = await octokit.rest.git.createBlob({
...repo,
content: await readFile(filePath.localPath, {
encoding: "utf8",
}),
encoding: "utf-8",
});
core.info(`Created blob for ${filePath.localPath}: ${newBlob.data.sha}`);

// sleep for 1 second to avoid rate limiting
await sleep(1000);

return {
path: filePath.repoPath,
sha: newBlob.data.sha,
Expand Down
24 changes: 19 additions & 5 deletions src/lint-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,27 @@ import * as core from "@actions/core";
import { execFileAsync, execFileAsyncWithLogs } from "./utils";
import { writeFile } from "node:fs/promises";

let hasInstalledDependencies = false;
async function installDependencies(cwd: string) {
if (hasInstalledDependencies) {
return;
}

core.startGroup("Installing dependencies");
await execFileAsync(
"npm",
["i", "@fig/eslint-config-autocomplete@latest", "eslint@8"],
{ cwd },
);
core.endGroup();

hasInstalledDependencies = true;
}

// TODO: find a way to have shared configs for all autocomplete tools)
async function runEslintOnPath(p: string, cwd: string) {
await installDependencies(cwd);

core.startGroup(`Started running eslint on spec: ${path.join(cwd, p)}`);
await writeFile(path.join(cwd, ".browserslistrc"), "safari >=11\nedge >=79", {
encoding: "utf8",
Expand All @@ -16,11 +35,6 @@ async function runEslintOnPath(p: string, cwd: string) {
encoding: "utf8",
},
);
await execFileAsync(
"npm",
["i", "@fig/eslint-config-autocomplete@latest", "eslint@8"],
{ cwd },
);
await execFileAsyncWithLogs(
"npx",
[
Expand Down
33 changes: 18 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,27 @@ async function run(): Promise<void> {

for (const file of await readdir(newSpecFolderPath)) {
if (existsSync(path.join(localSpecFolder, file))) {
core.startGroup(`Merging ${file}`);
await mergeSpecs(
path.join(localSpecFolder, file),
path.join(newSpecFolderPath, file),
path.join(newSpecFolderPath, file),
TMP_FOLDER,
{
skipLintAndFormat: true,
},
);
} else {
await lintAndFormatSpec(
path.join(newSpecFolderPath, file),
TMP_FOLDER,
);
core.endGroup();
}
}
} else {
for (const file of await readdir(newSpecFolderPath)) {
lintAndFormatSpec(path.join(newSpecFolderPath, file), TMP_FOLDER);
}
await uploadFolderPathArtifact(
`new-spec-folder-${randomUUID()}`,
newSpecFolderPath,
);
}

await lintAndFormatSpec(newSpecFolderPath, TMP_FOLDER);
await uploadFolderPathArtifact(
`new-spec-folder-${randomUUID()}`,
newSpecFolderPath,
);

localSpecFileOrFolder.push({
repoPath: `src/${autocompleteSpecName}`,
localPath: newSpecFolderPath,
Expand Down Expand Up @@ -219,8 +217,8 @@ async function run(): Promise<void> {
);

if (commitHasDiff) {
// skip 500ms because github returns a validation error otherwise (commit is sync)
await timeout(500);
// skip 1s because github returns a validation error otherwise (commit is sync)
await timeout(1000);
// create a PR from the branch with changes
const createdPRNumber =
await autocompleteRepoManager.createAutocompleteRepoPR(
Expand All @@ -238,6 +236,11 @@ async function run(): Promise<void> {
(error as Error).stack
}`,
);
if (error instanceof Error || typeof error === "string") {
core.setFailed(error);
} else {
core.setFailed(`${error}`);
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function mergeSpecs(
newSpecFilepath: string,
mergedSpecFilepath: string,
cwd: string,
{ skipLintAndFormat }: { skipLintAndFormat?: boolean } | undefined = {},
): Promise<void> {
const integration = core.getInput("integration") as PresetName;
core.startGroup("Merge specs");
Expand All @@ -24,7 +25,9 @@ export async function mergeSpecs(
prettifyOutput: false,
});
await writeFile(mergedSpecFilepath, mergedSpecContent, { encoding: "utf8" });
await lintAndFormatSpec(mergedSpecFilepath, cwd);
if (!skipLintAndFormat) {
await lintAndFormatSpec(mergedSpecFilepath, cwd);
}
core.endGroup();
}

Expand Down Expand Up @@ -79,3 +82,7 @@ export async function mkdirIfNotExists(
await mkdir(...args);
}
}

export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

0 comments on commit 64cd0f9

Please sign in to comment.