-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from gentlementlegen/feat/cron-job
- Loading branch information
Showing
19 changed files
with
321 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,10 @@ on: | |
command: | ||
description: "Command" | ||
|
||
env: | ||
APP_ID: ${{ secrets.APP_ID }} | ||
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} | ||
|
||
jobs: | ||
compute: | ||
environment: ${{ github.ref == 'refs/heads/main' && 'main' || 'development' }} | ||
|
@@ -42,3 +46,22 @@ jobs: | |
|
||
- name: Watch Activity | ||
uses: ./ | ||
|
||
- name: Get GitHub App token | ||
if: env.APP_ID != '' && env.APP_PRIVATE_KEY != '' | ||
uses: actions/create-github-app-token@v1 | ||
id: app-token | ||
with: | ||
app-id: ${{ env.APP_ID }} | ||
private-key: ${{ env.APP_PRIVATE_KEY }} | ||
|
||
- name: Commit updated DB | ||
uses: swinton/[email protected] | ||
env: | ||
GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }} | ||
with: | ||
files: | | ||
db.json | ||
commit-message: "chore: [skip ci] update db.json" | ||
# We save to the default branch since CRON cannot access any other branch | ||
ref: ${{ github.event.repository.default_branch }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: Cron | ||
|
||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: "1 0 * * *" | ||
|
||
env: | ||
APP_ID: ${{ secrets.APP_ID }} | ||
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }} | ||
|
||
jobs: | ||
run-cron: | ||
environment: ${{ github.ref == 'refs/heads/main' && 'main' || 'development' }} | ||
name: Run Cron | ||
runs-on: ubuntu-latest | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
steps: | ||
- uses: oven-sh/setup-bun@v2 | ||
|
||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ github.event.workflow_run.head_branch || github.ref }} | ||
|
||
- name: Install packages | ||
run: bun install --frozen-lockfile | ||
|
||
- name: Get GitHub App token | ||
if: env.APP_ID != '' && env.APP_PRIVATE_KEY != '' | ||
uses: actions/create-github-app-token@v1 | ||
id: app-token | ||
with: | ||
app-id: ${{ env.APP_ID }} | ||
private-key: ${{ env.APP_PRIVATE_KEY }} | ||
|
||
- name: Run CRON job | ||
run: | | ||
bun src/cron/index.ts | ||
env: | ||
GITHUB_TOKEN: ${{ steps.app-token.outputs.token || github.token }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ cypress/screenshots | |
/tests/http/http-client.private.env.json | ||
.wrangler | ||
test-dashboard.md | ||
db.json |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { JSONFilePreset } from "lowdb/node"; | ||
import path from "node:path"; | ||
|
||
export const DB_FILE_NAME = "db.json"; | ||
|
||
export interface DbComment { | ||
commentId: number; | ||
issueNumber: number; | ||
} | ||
|
||
export interface DbIssues { | ||
[repo: string]: DbComment[]; | ||
} | ||
|
||
export default await JSONFilePreset<DbIssues>(path.join(process.env.GITHUB_WORKSPACE || import.meta.dirname || "", DB_FILE_NAME), {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Octokit } from "@octokit/rest"; | ||
import { Logs } from "@ubiquity-os/ubiquity-os-logger"; | ||
import db from "./database-handler"; | ||
|
||
async function main() { | ||
const logger = new Logs(process.env.LOG_LEVEL ?? "info"); | ||
const octokit = new Octokit({ | ||
auth: process.env.GITHUB_TOKEN, | ||
}); | ||
const fileContent = db.data; | ||
for (const [key, value] of Object.entries(fileContent)) { | ||
try { | ||
logger.info(`Triggering update`, { | ||
key, | ||
value, | ||
}); | ||
const [owner, repo] = key.split("/"); | ||
const comment = value.pop(); | ||
if (!comment) { | ||
logger.error(`No comment was found for repository ${key}`); | ||
continue; | ||
} | ||
const { | ||
data: { body = "" }, | ||
} = await octokit.rest.issues.getComment({ | ||
owner, | ||
repo, | ||
comment_id: comment.commentId, | ||
issue_number: comment.issueNumber, | ||
}); | ||
const newBody = body + `\n<!-- daemon-disqualifier update ${Date().toLocaleString()} -->`; | ||
logger.debug(`Update comment ${comment.commentId}`, { newBody }); | ||
await octokit.rest.issues.updateComment({ | ||
owner, | ||
repo, | ||
comment_id: comment.commentId, | ||
issue_number: comment.issueNumber, | ||
body: newBody, | ||
}); | ||
} catch (e) { | ||
logger.error("Failed to update the comment", { key, value, e }); | ||
} | ||
} | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ContextPlugin } from "../types/plugin-input"; | ||
import db from "./database-handler"; | ||
|
||
export async function updateCronState(context: ContextPlugin) { | ||
await db.update((data) => { | ||
for (const key of Object.keys(data)) { | ||
if (!data[key].length) { | ||
delete data[key]; | ||
} | ||
} | ||
return data; | ||
}); | ||
|
||
if (!process.env.GITHUB_REPOSITORY) { | ||
context.logger.error("Can't update the Action Workflow state as GITHUB_REPOSITORY is missing from the env."); | ||
return; | ||
} | ||
|
||
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); | ||
|
||
if (Object.keys(db.data).length) { | ||
context.logger.verbose("Enabling cron.yml workflow."); | ||
await context.octokit.rest.actions.enableWorkflow({ | ||
owner, | ||
repo, | ||
workflow_id: "cron.yml", | ||
}); | ||
} else { | ||
context.logger.verbose("Disabling cron.yml workflow."); | ||
await context.octokit.rest.actions.disableWorkflow({ | ||
owner, | ||
repo, | ||
workflow_id: "cron.yml", | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.