Skip to content

Commit

Permalink
feat: implemented Pizza GitHub action
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline committed Aug 29, 2024
1 parent 0528b27 commit bb4f832
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 175 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For more information about the pizza-cli. check out the OpenSauced [pizza-cli](h
To use this action, you need to add the following to a GitHub Actions workflow file. The YAML snippet below uses the command to update your CODEOWNERS file in your repository, but replace it with whatever pizza-cli command you want to run.

```yaml
name: Runs the OpenSauced Pizza CLI
name: OpenSauced Pizza Action

on:
schedule:
Expand All @@ -20,14 +20,14 @@ on:
workflow_dispatch: # Allow manual triggering

jobs:
update-dev-card:
pizza-action:
runs-on: ubuntu-latest
steps:
- name: Pizza Action
uses: open-sauced/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
command: "generate codeowners ./"
pizza-args: "generate codeowners ./"
```
We suggest you add this to a workflow file in the `.github/workflows` directory of your repository and call it something like `pizza-action.yml`.
Expand Down
5 changes: 2 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ runs:
working-directory: ${{ github.action_path }}

- name: Install pizza-cli
run: go install github.com/open-sauced/pizza-cli@latest
run: npm i -g pizza
shell: bash
working-directory: ${{ github.action_path }}

- name: Run action
run: node ${{ github.action_path }}/index.js
shell: bash
working-directory: ${{ github.action_path }}
env:
INPUT_GITHUB-TOKEN: ${{ inputs.github-token }}
INPUT_PIZZA-ARGS: ${{ inputs.pizza-args }}
69 changes: 10 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,20 @@
const core = require("@actions/core");
const github = require("@actions/github");
const fs = require("fs");
const exec = require("@actions/exec");

async function run() {
try {
const token = core.getInput("github-token");
const pizzaArgs = core.getInput("pizza-args");
const args = pizzaArgs.split(/\s+/).filter((arg) => arg.length > 0);

// Run the pizza CLI command using the given pizza arguments
const { execSync } = require("child_process");
execSync(`pizza ${pizzaCommand}`);
console.log("Running Pizza CLI with args:", args);
await exec.exec("pizza", [pizzaArgs]);

// Commit and push changes
const octokit = github.getOctokit(token);
const { repo, owner } = github.context.repo;

const { data: refData } = await octokit.rest.git.getRef({
owner,
repo,
ref: `heads/${github.context.ref.replace("refs/heads/", "")}`,
});

const { data: commitData } = await octokit.rest.git.getCommit({
owner,
repo,
commit_sha: refData.object.sha,
});

const { data: blobData } = await octokit.rest.git.createBlob({
owner,
repo,
content: fs.readFileSync(outputPath, { encoding: "base64" }),
encoding: "base64",
});

const { data: treeData } = await octokit.rest.git.createTree({
owner,
repo,
base_tree: commitData.tree.sha,
tree: [
{
path: outputPath,
mode: "100644",
type: "blob",
sha: blobData.sha,
},
],
});

const { data: newCommitData } = await octokit.rest.git.createCommit({
owner,
repo,
message: `Automated pizza update for command ${pizzaCommand}`,
tree: treeData.sha,
parents: [commitData.sha],
});

await octokit.rest.git.updateRef({
owner,
repo,
ref: `heads/${github.context.ref.replace("refs/heads/", "")}`,
sha: newCommitData.sha,
});

console.log("Changes committed and pushed successfully");
// Add and commit changes
await exec.exec("git config --global user.name github-actions");
await exec.exec("git config --global user.email [email protected]");
await exec.exec("git add .");
await exec.exec('git commit -m "Auto-commit from OpenSauced Pizza Action"');
await exec.exec("git push");
} catch (error) {
core.setFailed(error.message);
}
Expand Down
Loading

0 comments on commit bb4f832

Please sign in to comment.