Skip to content

Commit

Permalink
fix: run prettier in orbit github action (#2056)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance authored Nov 8, 2024
1 parent 1d4e811 commit 4b6e176
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 249 deletions.
54 changes: 53 additions & 1 deletion .github/workflows/add-orbit-chain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ jobs:
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.ref_name }}

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
node-version: '20'
cache: 'yarn'

- name: Install node_modules
Expand All @@ -34,10 +35,61 @@ jobs:
- name: Build scripts
run: yarn workspace scripts build

- name: Get issue details
id: issue
uses: actions/github-script@v7
with:
script: |
const issue = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: parseInt(process.env.ISSUE_NUMBER)
});
const chainName = issue.data.title.match(/Add Orbit chain\s+(\S+)/i)?.[1] || 'Orbit chain';
return chainName;
result-encoding: string
env:
ISSUE_NUMBER: ${{ inputs.issue_number }}

- name: Generate branch name
id: branch
run: |
timestamp=$(date +%s)
echo "name=feat/add-orbit-chain-${{ inputs.issue_number }}-${timestamp}" >> $GITHUB_OUTPUT
- name: Setup git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Create branch
run: git checkout -b ${{ steps.branch.outputs.name }}

- name: Run addOrbitChain script
run: yarn workspace scripts add-orbit-chain ../../packages/arb-token-bridge-ui/src/util/orbitChainsData.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ github.event.issue.number || inputs.issue_number }}
NEXT_PUBLIC_INFURA_KEY: ${{ secrets.NEXT_PUBLIC_INFURA_KEY }}
NEXT_PUBLIC_INFURA_KEY_HOLESKY: ${{ secrets.NEXT_PUBLIC_INFURA_KEY_HOLESKY }}

- name: Format files
run: yarn prettier --write "packages/arb-token-bridge-ui/src/**/*.{ts,tsx,js,jsx,json}"

- name: Commit changes
run: |
git add packages/arb-token-bridge-ui/src/**/*
git add packages/arb-token-bridge-ui/public/**/*
git status
git commit -m "feat: add ${{ steps.issue.outputs.result }}"
git push origin ${{ steps.branch.outputs.name }}
- name: Create Pull Request
run: |
gh pr create \
--title "feat: add Orbit chain ${{ steps.issue.outputs.result }}" \
--body "Automated pull request to add ${{ steps.issue.outputs.result }} to the bridge. Closes #${{ inputs.issue_number }}" \
--base master \
--head ${{ steps.branch.outputs.name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
144 changes: 1 addition & 143 deletions packages/scripts/src/addOrbitChain/github.ts
Original file line number Diff line number Diff line change
@@ -1,154 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { context, getOctokit } from "@actions/github";
import { Issue } from "./schemas";

const github = getOctokit(process.env.GITHUB_TOKEN || "");

const getIssue = async (issueNumber: string): Promise<Issue> => {
export const getIssue = async (issueNumber: string): Promise<Issue> => {
const response = await github.rest.issues.get({
...context.repo,
issue_number: Number(issueNumber),
});
return response.data as Issue;
};

const getContent = async (
branchName: string,
imageSavePath: string
): Promise<any> => {
return github.rest.repos.getContent({
...context.repo,
path: `packages/arb-token-bridge-ui/public/${imageSavePath}`,
ref: branchName,
});
};

export const updateContent = async (
branchName: string,
imageSavePath: string,
imageBuffer: Buffer,
sha?: string
) => {
return github.rest.repos.createOrUpdateFileContents({
...context.repo,
path: `packages/arb-token-bridge-ui/public/${imageSavePath}`,
message: "Add new image",
content: imageBuffer.toString("base64"),
branch: branchName,
sha: sha, // Include the sha if the file exists, undefined otherwise
});
};

const createBranch = async (branchName: string): Promise<void> => {
try {
// Check if the branch already exists
await github.rest.git.getRef({
...context.repo,
ref: `heads/${branchName}`,
});
console.log(`Branch ${branchName} already exists. Using existing branch.`);
} catch (error) {
// If the branch doesn't exist, create it
if ((error as any).status === 404) {
await github.rest.git.createRef({
...context.repo,
ref: `refs/heads/${branchName}`,
sha: context.sha,
});
console.log(`Created new branch: ${branchName}`);
} else {
// If there's any other error, throw it
throw error;
}
}
};

const commitChanges = async (
branchName: string,
targetJsonPath: string,
fileContents: string
): Promise<void> => {
// Convert the relative path to a repository-relative path
const repoPath = targetJsonPath.replace(/^\.\.\//, "");

try {
const { data } = await github.rest.repos.getContent({
...context.repo,
path: repoPath,
ref: branchName,
});

await github.rest.repos.createOrUpdateFileContents({
...context.repo,
path: repoPath,
message: `Update ${repoPath} with new chain`,
content: Buffer.from(fileContents).toString("base64"),
sha: (data as { sha: string }).sha,
branch: branchName,
});
} catch (error: any) {
if (error.status === 404) {
// File doesn't exist, create it
await github.rest.repos.createOrUpdateFileContents({
...context.repo,
path: repoPath,
message: `Create ${repoPath} with new chain`,
content: Buffer.from(fileContents).toString("base64"),
branch: branchName,
});
} else {
throw error;
}
}
};

const createPullRequest = async (
branchName: string,
chainName: string,
issueUrl: string
): Promise<void> => {
// Get the default branch of the repository
const { data: repo } = await github.rest.repos.get({
...context.repo,
});
const defaultBranch = repo.default_branch;

await github.rest.pulls.create({
...context.repo,
title: `feat: add Orbit chain - ${chainName}`,
head: branchName,
base: defaultBranch,
body: `Automated pull request to add ${chainName} to the bridge. Closes ${issueUrl}.`,
});
};

export { commitChanges, createBranch, createPullRequest, getContent, getIssue };

export const saveImageToGitHub = async (
branchName: string,
imageSavePath: string,
imageBuffer: Buffer
): Promise<void> => {
try {
// Check if the file already exists in the repository
let sha: string | undefined;
try {
const { data } = await getContent(branchName, imageSavePath);

if ("sha" in data) {
sha = data.sha;
}
} catch (error) {
// File doesn't exist, which is fine
console.log(`File ${imageSavePath} doesn't exist in the repository yet.`);
}

// Update or create the file in the repository
await updateContent(branchName, imageSavePath, imageBuffer, sha);
console.log(`Successfully saved image to GitHub at ${imageSavePath}`);
} catch (error) {
console.error("Error saving image to GitHub:", error);
throw error;
}
};
19 changes: 1 addition & 18 deletions packages/scripts/src/addOrbitChain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import {
handleImages,
createAndValidateOrbitChain,
updateAndValidateOrbitChainsList,
commitChangesAndCreatePR,
setOutputs,
runPrettier,
} from "./transforms";

/**
Expand All @@ -32,21 +29,7 @@ export async function addOrbitChain(targetJsonPath: string): Promise<void> {
nativeTokenLogoPath
);

const updatedOrbitChainsList = await updateAndValidateOrbitChainsList(
orbitChain,
targetJsonPath
);

await runPrettier(targetJsonPath);

await commitChangesAndCreatePR(
branchName,
targetJsonPath,
updatedOrbitChainsList,
orbitChain
);

setOutputs(branchName, orbitChain, targetJsonPath);
await updateAndValidateOrbitChainsList(orbitChain, targetJsonPath);
} catch (error) {
core.setFailed(`Error in addOrbitChain: ${error}`);
throw error;
Expand Down
Loading

0 comments on commit 4b6e176

Please sign in to comment.