Skip to content

[jsweep] Clean add_labels.cjs - #7634

Closed
github-actions[bot] wants to merge 2 commits into
mainfrom
jsweep/clean-add-labels-cjs-b57fd79e35bd24c5
Closed

[jsweep] Clean add_labels.cjs#7634
github-actions[bot] wants to merge 2 commits into
mainfrom
jsweep/clean-add-labels-cjs-b57fd79e35bd24c5

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Summary

This PR cleans up pkg/workflow/js/add_labels.cjs by applying modern JavaScript patterns and simplifying the code structure.

Context

This is a github-script context file that runs within GitHub Actions using the actions/github-script action. It has access to core, github, and context as global objects.

Improvements Made

1. Optional Chaining for Error Checking

  • Before: labelsResult.error && labelsResult.error.includes("No valid labels")
  • After: labelsResult.error?.includes("No valid labels")

2. Simplified core.summary Calls

  • Before: Multi-line nested .addRaw() calls with separate string arguments
  • After: Direct chaining with template literals in .addRaw().write()

3. Modern Error Handling

  • Before: error instanceof Error ? error.message : String(error)
  • After: error?.message ?? String(error)

4. Cleaner Code Structure

  • Removed unnecessary nested function call arguments
  • Direct method chaining for better readability
  • Consistent use of optional chaining and nullish coalescing

Changes Overview

- if (labelsResult.error && labelsResult.error.includes("No valid labels")) {
+ if (labelsResult.error?.includes("No valid labels")) {

- await core.summary.addRaw(`...`).write();
+ await core.summary.addRaw(`...`).write();

- const errorMessage = error instanceof Error ? error.message : String(error);
+ const errorMessage = error?.message ?? String(error);

Testing

The file has comprehensive test coverage in add_labels.test.cjs with 40+ test cases covering:

  • Environment variable validation
  • Context validation (issue/PR events)
  • Label parsing and validation
  • GitHub API integration
  • Edge cases (whitespace, duplicates, sanitization, length limits)
  • Target configuration

All existing tests should continue to pass as the logic remains unchanged.

Benefits

  • Cleaner code: Reduced nesting and simplified structure
  • Modern JavaScript: Uses ES6+ features (optional chaining, nullish coalescing)
  • Better readability: More concise without sacrificing clarity
  • Maintained functionality: Zero changes to business logic

Part of jsweep initiative: Daily JavaScript unbloating for cleaner, more maintainable code.

AI generated by jsweep - JavaScript Unbloater

@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot format, lint, recompile

Copilot AI commented Dec 25, 2025

Copy link
Copy Markdown
Contributor

@pelikhan I've opened a new pull request, #7639, to work on those changes. Once the pull request is ready, I'll request review from you.

@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot you need to modify the files in actions/setup

Copilot AI commented Dec 25, 2025

Copy link
Copy Markdown
Contributor

@pelikhan I've opened a new pull request, #7640, to work on those changes. Once the pull request is ready, I'll request review from you.

@pelikhan pelikhan closed this Dec 25, 2025
@pelikhan
pelikhan deleted the jsweep/clean-add-labels-cjs-b57fd79e35bd24c5 branch August 18, 2026 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants