Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented Pizza GitHub action #1

Merged
merged 11 commits into from
Aug 30, 2024
Merged
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:
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
pizza-action:
runs-on: ubuntu-latest
steps:
- name: Pizza Action
uses: open-sauced/pizza-action@v1.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
command: "generate codeowners ./"
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
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 }}
108 changes: 47 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,56 @@
const core = require("@actions/core");
const github = require("@actions/github");
const fs = require("fs");
const exec = require("@actions/exec");
const fs = require("fs").promises;
const path = require("path");

function stripQuotes(input) {
const regex = /[^\s"']+|"([^"]*)"|'([^']*)'/g;
let args = [];
let match;

while ((match = regex.exec(input))) {
if (match[1]) args.push(match[1]); // Was quoted with "
else if (match[2]) args.push(match[2]); // Was quoted with '
else args.push(match[0]); // Was not quoted
}

return args;
}

async function run() {
try {
const token = core.getInput("github-token");
const pizzaArgs = core.getInput("pizza-args");

// Run the pizza CLI command using the given pizza arguments
const { execSync } = require("child_process");
execSync(`pizza ${pizzaCommand}`);

// 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");
console.log("Raw pizza-args:", pizzaArgs);

const args = stripQuotes(pizzaArgs);
console.log("Stripped args:", args);

// Create a temporary bash script
const scriptContent = `#!/bin/bash
set -e
pizza ${args.join(" ")}
`;
console.log("Bash script content:", scriptContent);

const scriptPath = path.join(process.cwd(), "run_pizza.sh");
await fs.writeFile(scriptPath, scriptContent);
await fs.chmod(scriptPath, "0755"); // Make the script executable

// Execute the bash script
console.log("Executing bash script...");
await exec.exec(`bash ${scriptPath}`);

// Remove the temporary script
await fs.unlink(scriptPath);

// Add and commit changes
await exec.exec("git config --global user.name github-actions");
await exec.exec("git config --global user.email github-actions@github.com");
await exec.exec("git add .");
await exec.exec('git commit -m "Auto-commit from OpenSauced Pizza Action"');
await exec.exec("git push");
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
console.error("Error:", error);
core.setFailed(error.message);
}
}
Expand Down
Loading