Skip to content

Extract duplicate expires preprocessing logic into shared helper#14899

Merged
pelikhan merged 2 commits intomainfrom
copilot/refactor-expires-preprocessing
Feb 11, 2026
Merged

Extract duplicate expires preprocessing logic into shared helper#14899
pelikhan merged 2 commits intomainfrom
copilot/refactor-expires-preprocessing

Conversation

Copy link
Contributor

Copilot AI commented Feb 11, 2026

parseIssuesConfig and parseDiscussionsConfig contained identical ~20 line blocks for expires field preprocessing (parsing, normalization to hours, disablement handling, logging).

Changes

  • Added preprocessExpiresField() helper in pkg/workflow/config_helpers.go

    • Handles int (days), string ("48h", "7d"), and bool (false for disablement)
    • Normalizes to hours and updates configData["expires"] in-place
    • Returns bool indicating explicit disablement
  • Updated parseIssuesConfig and parseDiscussionsConfig

    • Replaced duplicate blocks with single helper call
    • Behavior unchanged
// Before (duplicated in both files)
expiresDisabled := false
if configData != nil {
    if expires, exists := configData["expires"]; exists {
        expiresInt := parseExpiresFromConfig(configData)
        if expiresInt == -1 {
            expiresDisabled = true
            configData["expires"] = 0
        } else if expiresInt > 0 {
            configData["expires"] = expiresInt
        } else {
            configData["expires"] = 0
        }
        log.Printf("Parsed expires value %v to %d hours (disabled=%t)", expires, expiresInt, expiresDisabled)
    }
}

// After
expiresDisabled := preprocessExpiresField(configData, log)

Net: -38 lines duplicate code, +34 lines shared helper, +118 lines tests

Original prompt

This section details on the original issue you should resolve

<issue_title>Duplicate Code: Expires Preprocessing in create-issue and create-discussion</issue_title>
<issue_description># 🔍 Duplicate Code Detected: Expires preprocessing in create-issue/create-discussion

Analysis of commit c4416a7

Assignee: @copilot

Summary

The expires preprocessing block in parseIssuesConfig and parseDiscussionsConfig is effectively identical (parses expires, handles false disablement, normalizes to hours, logs). This is >10 lines duplicated in two core config parsers.

Duplication Details

Pattern: Expires normalization and disablement parsing

  • Severity: Medium
  • Occurrences: 2
  • Locations:
    • pkg/workflow/create_issue.go:37
    • pkg/workflow/create_discussion.go:40
  • Code Sample:
// Pre-process the expires field (convert to hours before unmarshaling)
expiresDisabled := false
if configData != nil {
    if expires, exists := configData["expires"]; exists {
        expiresInt := parseExpiresFromConfig(configData)
        if expiresInt == -1 { ... } else if expiresInt > 0 { ... } else { ... }
        log.Printf("Parsed expires value %v to %d hours (disabled=%t)", expires, expiresInt, expiresDisabled)
    }
}

Impact Analysis

  • Maintainability: Changes to expires parsing logic must be made in two places, risking drift.
  • Bug Risk: If one parser is updated and the other is not, behavior diverges between issue and discussion workflows.
  • Code Bloat: Small but repeated block in a core config path.

Refactoring Recommendations

  1. Extract a shared helper

    • Extract to: pkg/workflow/expiration_helpers.go (or similar) and return (expires int, disabled bool).
    • Estimated effort: ~1–2 hours.
    • Benefits: Single source of truth for expires parsing and logging.
  2. Standardize logging

    • Accept a logger in the helper to keep caller-specific prefixing consistent.
    • Estimated effort: ~0.5 hour.
    • Benefits: Consistent logging format and fewer call sites to update.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 6 (create/update issue/discussion/pull_request)
  • Detection Method: Serena semantic code analysis + pattern matching
  • Commit: c4416a7
  • Analysis Date: February 11, 2026

AI generated by Duplicate Code Detector

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…elper

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicate expires preprocessing in issue and discussion creation Extract duplicate expires preprocessing logic into shared helper Feb 11, 2026
Copilot AI requested a review from pelikhan February 11, 2026 06:30
@pelikhan pelikhan marked this pull request as ready for review February 11, 2026 06:31
Copilot AI review requested due to automatic review settings February 11, 2026 06:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR successfully extracts duplicate expires field preprocessing logic from parseIssuesConfig and parseDiscussionsConfig into a shared helper function preprocessExpiresField() in config_helpers.go. The refactoring eliminates ~38 lines of duplicate code by consolidating identical logic for parsing, normalizing, and handling the expires configuration field.

Changes:

  • Added preprocessExpiresField() helper function that handles expires field parsing (integers, strings, boolean false), normalization to hours, in-place updates to configData["expires"], and logging
  • Updated parseIssuesConfig and parseDiscussionsConfig to use the new helper, replacing ~20 lines of duplicate code in each function with a single function call
  • Added comprehensive unit tests (118 lines) covering all scenarios: valid formats (integers, string specs), explicit disablement (false), invalid values, edge cases (nil configData, missing field)

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
pkg/workflow/config_helpers.go Added preprocessExpiresField() helper function with complete documentation, proper nil handling for logger parameter, and logic identical to original implementation
pkg/workflow/create_issue.go Replaced 20-line duplicate preprocessing block with single call to preprocessExpiresField(configData, createIssueLog)
pkg/workflow/create_discussion.go Replaced 20-line duplicate preprocessing block with single call to preprocessExpiresField(configData, discussionLog)
pkg/workflow/config_parsing_helpers_test.go Added comprehensive TestPreprocessExpiresField with 10 test cases covering all value types, edge cases, and validation of both return value and in-place mutation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Contributor

🔍 PR Triage Results

Category: refactor | Risk: medium | Priority: 45/100

Scores Breakdown

  • Impact: 24/50 - Code quality improvement, reduces technical debt (-38 duplicate lines), improves maintainability
  • Urgency: 8/30 - Recent PR (0.3h old), CI pending, refactoring can wait but improves code quality
  • Quality: 13/20 - Excellent description with code examples (+5), includes comprehensive tests (+3), ready for review (+5)

📋 Recommended Action: batch_review

Rationale: Good refactoring PR with medium priority. Can be efficiently reviewed alongside other refactoring PRs.

Batch: batch-refactor-001 - Similar refactoring PRs for consolidated review

Review Focus:

  • Verify behavior unchanged (expires field processing)
  • Confirm test coverage adequate
  • Check error handling consistency

Triaged by PR Triage Agent on 2026-02-11

AI generated by PR Triage Agent

@pelikhan pelikhan merged commit ca36bd7 into main Feb 11, 2026
158 checks passed
@pelikhan pelikhan deleted the copilot/refactor-expires-preprocessing branch February 11, 2026 06:41
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.

Duplicate Code: Expires Preprocessing in create-issue and create-discussion

2 participants

Comments