diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..a8567d7 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,40 @@ +name: TEST + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + compile: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: | + g++ -Wall -Wextra -o test source.cpp > compilation.log 2>&1 + - uses: actions/upload-artifact@v4 + with: + name: compilation_log + path: compilation.log + - run: ./test + + test: + needs: + - compile + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: npm install + - run: npm run build + - uses: actions/download-artifact@v4 + with: + name: compilation_log + - run: node dist/index.js + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/source.cpp b/source.cpp new file mode 100644 index 0000000..12b9916 --- /dev/null +++ b/source.cpp @@ -0,0 +1,9 @@ +#include + +[[nodiscard]] int func() { return 42; } + +int main() { + int unused_variable = 42; + func(); + std::cout << "Hello, World!" << std::endl; +} diff --git a/src/index.ts b/src/index.ts index 6eb18a1..b54089f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,39 +1,21 @@ import { Octokit } from "@octokit/action"; +import { readFileSync } from "fs"; + +// if the action is triggered by not a pull request, exit +if (!process.env.GITHUB_REF?.startsWith("refs/pull/")) { + console.log("Not a pull request, exiting."); + process.exit(0); +} const octokit = new Octokit(); const [owner, repo] = process.env.GITHUB_REPOSITORY?.split("/")!; const pull_request_number = parseInt(process.env.GITHUB_REF?.split("/")[2]!); - -const { data: pullRequest } = await octokit.rest.pulls.get({ - owner, - repo, - pull_number: pull_request_number, -}); - -const { data: actions } = await octokit.rest.actions.listWorkflowRunsForRepo({ - owner, - repo, -}); - -const workflow_runs = actions.workflow_runs.filter((action) => { - return ( - action.head_branch === pullRequest.head.ref && - action.head_sha === pullRequest.head.sha && - action.status === "completed" - ); -}); - -const latest = workflow_runs.reduce((latest, action) => { - if (!latest) return action; - return new Date(action.created_at) > new Date(latest.created_at) - ? action - : latest; -}); +const compilation_output = readFileSync("compilation.log"); octokit.rest.issues.createComment({ owner, repo, issue_number: pull_request_number, - body: `The latest workflow run for this pull request is [#${latest.run_number}](${latest.html_url})`, + body: `compilation output is:\n\n\`\`\`\n${compilation_output}\n\`\`\``, });