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

Refactor worker and action functions to include CloseCheckParam #33

Merged
9 commits merged into from
Nov 7, 2023
Merged

Conversation

jill64
Copy link
Owner

@jill64 jill64 commented Nov 7, 2023

Refactor worker and action functions to include CloseCheckParam and update Ghost and GhostPayload types. Import CloseCheckParam and update actions function. Add appkit to payload and revoke installation access tokens. This PR improves the functionality of the worker and action functions by including CloseCheckParam and updating the Ghost and GhostPayload types. It also adds appkit to the payload and revokes installation access tokens for improved security.

Summary by CodeRabbit

  • New Features
    • Introduced enhanced error handling and asynchronous operations in the actions function.
    • Added a new function to handle the closing of check runs based on the result.
  • Refactor
    • Modified the workers function to include additional logic for creating check runs and handling errors.
    • Removed the action function from various modules, leaving only the worker function.
  • Bug Fixes
    • Fixed the check run status setting in the event of an error in the app.action call.
  • Documentation
    • Updated the GhostPayload type and the WorkerContext interface to reflect changes in the codebase.

@ghost ghost enabled auto-merge November 7, 2023 09:55
Copy link

coderabbitai bot commented Nov 7, 2023

Walkthrough

The changes primarily revolve around the modification of function signatures and the introduction of asynchronous operations in the Ghost module. The action function has been removed from several objects, and error handling logic has been enhanced. The WorkerContext interface and GhostPayload type have been updated, and a new createCheckRun method has been introduced.

Changes

File(s) Change Summary
packages/action/src/index.ts Introduced a new parameter appkit and added logic to revoke the installation access token.
packages/ghost/actions.ts Added imports, modified the actions function to handle asynchronous operations, and added error handling logic.
packages/ghost/workers.ts Significant changes to the workers function, including additional logic for creating check runs and handling errors.
packages/types/Ghost.ts, packages/types/GhostPayload.ts, packages/types/WorkerContext.ts Updated types and interfaces, including the addition of a new createCheckRun method.
packages/ghost/app/build.ts, packages/ghost/app/closer.ts, packages/ghost/app/docs.ts, packages/ghost/app/format.ts, packages/ghost/app/lint.ts, packages/ghost/app/merge.ts, packages/ghost/app/release.ts Removed the action function from various objects in the Ghost module.
packages/ghost/app/deploy.ts Modified function signatures, replaced check run creation logic, and updated return values based on status and exitCode.

Poem

🍂 As the leaves fall, so do the old ways,
🐇 Hopping forward, we embrace the change of days.
🎉 On this day, in history's page,
🚀 Sputnik 2 launched, a new space age.
📚 Just like Laika, brave and bright,
🌟 Our code takes flight, into the night.
🌍 From every culture, every land,
🤝 Together we code, hand in hand.
🎈 So let's celebrate, let's make some noise,
🥳 For every change, every deploy!


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link

cloudflare-workers-and-pages bot commented Nov 7, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: c506efb
Status: ✅  Deploy successful!
Preview URL: https://22dc2923.wraith-ci.pages.dev
Branch Preview URL: https://dev.wraith-ci.pages.dev

View logs

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 275c9ca and f768f25.
Files selected for processing (6)
  • packages/action/src/index.ts (2 hunks)
  • packages/ghost/actions.ts (2 hunks)
  • packages/ghost/workers.ts (1 hunks)
  • packages/types/Ghost.ts (1 hunks)
  • packages/types/GhostPayload.ts (1 hunks)
  • packages/types/WorkerContext.ts (1 hunks)
Additional comments: 14
packages/types/GhostPayload.ts (1)
  • 1-3: The check_run_id property is now required in the GhostPayload type. Ensure that all instances where GhostPayload is used are updated to include this property.
packages/types/Ghost.ts (2)
  • 1-1: The new import statement for CloseCheckParam is correctly added.

  • 8-8: The return type of the action function has been changed to Promise<CloseCheckParam>. Ensure that all implementations of this function are updated to return a Promise<CloseCheckParam>.

packages/types/WorkerContext.ts (2)
  • 8-8: The head_sha property has been added. Ensure that all instances where WorkerContext is used have been updated to handle this new property.

  • 12-12: The createCheckRun method has been added. Ensure that all instances where WorkerContext is used have been updated to handle this new method.

packages/action/src/index.ts (3)
  • 8-11: The function action now accepts a new parameter appkit. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 29-30: The installation access token is being revoked for both octokit and appkit. This is a good security practice as it minimizes the risk of token misuse.

  • 28-31: The Promise.all function is used to execute multiple promises concurrently. This is a good practice for improving performance. However, if one promise fails, Promise.all fails with the reason of the first promise that rejected. Make sure to handle any potential errors that might occur during the execution of these promises.

packages/ghost/actions.ts (3)
  • 1-5: Ensure that the newly added imports are being used correctly and that they are necessary for the code. Also, verify that the imported modules are installed in your project dependencies.

  • 8-11: The actions function has been modified to handle asynchronous operations. Ensure that all calls to this function throughout the codebase have been updated to handle the Promise returned by this function.

  • 55-82: Error handling has been added to the actions function. Ensure that the error messages are meaningful and helpful for debugging. Also, verify that the core.setFailed function is being used correctly.

packages/ghost/workers.ts (3)
  • 5-7: The function signature has been updated to exclude 'createCheckRun' and 'head_sha' from the WorkerContext. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 36-55: The 'attempt' function is used to handle potential errors. This is a good practice as it improves the robustness of the code.

  • 60-60: The 'Promise.all' function is used to handle multiple promises. This is a good practice as it improves the performance of the code by allowing multiple operations to be performed concurrently.

Comment on lines 30 to 53
const closeCheckRun = async (param: CloseCheckParam) => {
const { status, result } = data

if (status === 'error') {
return
}

const check_run_id = result.check_run_id

if (!check_run_id) {
return
}

const outputs =
typeof param === 'string' ? { conclusion: param } : param

await octokit.rest.checks.update({
check_run_id,
owner,
repo,
details_url,
...outputs
})
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The closeCheckRun function is defined within the actions function. This could lead to the function being redefined every time actions is called, which is not efficient. Consider moving closeCheckRun outside of actions to improve performance.

+ const closeCheckRun = async (param: CloseCheckParam, data: any, octokit: any, owner: any, repo: any, details_url: any) => {
- const closeCheckRun = async (param: CloseCheckParam) => {
    const { status, result } = data
    // ...
    await octokit.rest.checks.update({
      check_run_id,
      owner,
      repo,
      details_url,
      ...outputs
    })
  }

- export const actions = (
+ export const actions = async (
    context: Omit<ActionContext, 'data'> & { data: WraithPayload | Error }
  ) => {
    // ...
    try {
      const result = await app.action?.({
        ...context,
        data
      })
      if (!result) {
        return
      }
-     await closeCheckRun(result)
+     await closeCheckRun(result, data, octokit, owner, repo, details_url)
    } catch (e) {
      // ...
    }
  }

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
const closeCheckRun = async (param: CloseCheckParam) => {
const { status, result } = data
if (status === 'error') {
return
}
const check_run_id = result.check_run_id
if (!check_run_id) {
return
}
const outputs =
typeof param === 'string' ? { conclusion: param } : param
await octokit.rest.checks.update({
check_run_id,
owner,
repo,
details_url,
...outputs
})
}
const closeCheckRun = async (param: CloseCheckParam, data: any, octokit: any, owner: any, repo: any, details_url: any) => {
const { status, result } = data
if (status === 'error') {
return
}
const check_run_id = result.check_run_id
if (!check_run_id) {
return
}
const outputs =
typeof param === 'string' ? { conclusion: param } : param
await octokit.rest.checks.update({
check_run_id,
owner,
repo,
details_url,
...outputs
})
}
export const actions = async (
context: Omit<ActionContext, 'data'> & { data: WraithPayload | Error }
) => {
// ...
try {
const result = await app.action?.({
...context,
data
})
if (!result) {
return
}
await closeCheckRun(result, data, octokit, owner, repo, details_url)
} catch (e) {
// ...
}
}

Comment on lines +11 to +16
const head_sha =
'pull_request' in payload
? payload.pull_request.head.sha
: 'after' in payload
? payload.after
: null
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining the 'head_sha' value is not clear. Consider adding comments to explain the logic behind these checks.

Comment on lines +18 to +34
const createCheckRun = async (name: string) => {
if (!(head_sha && Number(head_sha) !== 0)) {
return 0
}

const {
data: { id }
} = await installation.kit.rest.checks.create({
repo,
owner,
name,
await attempt(
async () => {
const result = await app.worker(context)
return result
? {
status: 'success' as const,
result
}
: null
},
(e, o) => ({
status: 'error' as const,
result: e ?? new Error(String(o))
})
)
] as const
)
head_sha,
status: 'in_progress'
})

return id
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'createCheckRun' function is defined within the 'workers' function. This could lead to the function being redefined every time 'workers' is called, which could impact performance. Consider moving 'createCheckRun' outside of 'workers' to avoid this.

+ const createCheckRun = async (name: string, head_sha: string, installation: any, repo: any, owner: any) => {
+   if (!(head_sha && Number(head_sha) !== 0)) {
+     return 0
+   }
+   const {
+     data: { id }
+   } = await installation.kit.rest.checks.create({
+     repo,
+     owner,
+     name,
+     head_sha,
+     status: 'in_progress'
+   })
+   return id
+ }
- const createCheckRun = async (name: string) => {
-   if (!(head_sha && Number(head_sha) !== 0)) {
-     return 0
-   }
-   const {
-     data: { id }
-   } = await installation.kit.rest.checks.create({
-     repo,
-     owner,
-     name,
-     head_sha,
-     status: 'in_progress'
-   })
-   return id
- }

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
const createCheckRun = async (name: string) => {
if (!(head_sha && Number(head_sha) !== 0)) {
return 0
}
const {
data: { id }
} = await installation.kit.rest.checks.create({
repo,
owner,
name,
await attempt(
async () => {
const result = await app.worker(context)
return result
? {
status: 'success' as const,
result
}
: null
},
(e, o) => ({
status: 'error' as const,
result: e ?? new Error(String(o))
})
)
] as const
)
head_sha,
status: 'in_progress'
})
return id
}
const createCheckRun = async (name: string, head_sha: string, installation: any, repo: any, owner: any) => {
if (!(head_sha && Number(head_sha) !== 0)) {
return 0
}
const {
data: { id }
} = await installation.kit.rest.checks.create({
repo,
owner,
name,
head_sha,
status: 'in_progress'
})
return id
}

@ghost ghost merged commit 82ca45c into main Nov 7, 2023
9 checks passed
@ghost ghost deleted the dev branch November 7, 2023 10:06
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f768f25 and c506efb.
Files ignored due to filter (2)
  • package.json
  • pnpm-lock.yaml
Files selected for processing (9)
  • packages/ghost/actions.ts (2 hunks)
  • packages/ghost/app/build.ts (1 hunks)
  • packages/ghost/app/closer.ts (1 hunks)
  • packages/ghost/app/deploy.ts (4 hunks)
  • packages/ghost/app/docs.ts (1 hunks)
  • packages/ghost/app/format.ts (1 hunks)
  • packages/ghost/app/lint.ts (1 hunks)
  • packages/ghost/app/merge.ts (1 hunks)
  • packages/ghost/app/release.ts (1 hunks)
Additional comments: 16
packages/ghost/app/format.ts (1)
  • 1-5: The action function has been removed from the format object. Ensure that there are no remaining calls to format.action in the codebase.
packages/ghost/app/closer.ts (1)
  • 1-5: The action function has been removed from the closer object. Ensure that this function is not being called anywhere else in the codebase.
packages/ghost/app/release.ts (1)
  • 1-5: Ensure that all references to the action function from the release object have been updated or removed throughout the codebase. Also, verify that the worker function is implemented correctly in the future, as it is currently an empty function.
packages/ghost/app/build.ts (1)
  • 1-5: The action function has been removed from the build object. Ensure that this function is not being called anywhere else in the codebase. If it is, those calls will need to be updated or removed.
packages/ghost/app/lint.ts (1)
  • 1-5: The action function has been removed from the lint object. Ensure that this change does not break any dependencies elsewhere in the codebase. Also, the worker function is currently an empty async function. If this is intentional, ensure that it is handled properly where it's called to avoid unexpected behavior.
packages/ghost/app/merge.ts (1)
  • 1-5: The action function has been removed from the merge object. Ensure that this change does not break any dependencies that might be using the action function from the merge object. Also, the worker function is currently an empty async function. If this is intentional, ignore this comment. Otherwise, you might want to add some functionality to it.
packages/ghost/app/docs.ts (1)
  • 1-5: The action function has been removed from the docs object. Ensure that this change does not break any dependencies elsewhere in the codebase. Also, the worker function is currently an empty async function. If this is intentional, ensure that it is handled properly where it's called to avoid unexpected behavior.
packages/ghost/actions.ts (5)
  • 1-5: Ensure that the new imports are used correctly and that they don't conflict with existing imports.

  • 8-10: The function signature has been updated. Make sure all calls to this function have been updated accordingly.

  • 18-85: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [11-85]

The function now handles asynchronous operations and includes error handling logic. This is a significant change in behavior and should be thoroughly tested.

  • 30-54: The new closeCheckRun function handles the closing of check runs based on the result. Ensure that this function is called correctly and that it handles all possible results.

  • 56-83: The app.action call now includes error handling and sets the check run status accordingly. This is a significant change in behavior and should be thoroughly tested.

packages/ghost/app/deploy.ts (4)
  • 3-17: The worker function signature has been updated to include createCheckRun and remove repo and owner. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 25-29: The creation of check run using installation.kit.rest.checks.create has been replaced with a call to createCheckRun method. This change improves modularity and makes the code easier to test.

  • 31-34: The action function now returns 'failure' if status is 'error'. This change improves error handling.

  • 41-55: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [44-65]

The action function now returns 'success' if exitCode is 0, otherwise it returns an object with conclusion as 'failure' and output containing error details. This change improves error handling and provides more detailed feedback in case of failure.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant