Skip to content

Extract duplicate expires preprocessing logic into shared helper - #14899

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

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

Conversation

CopilotAI commented Feb 11, 2026

Copy link
Copy Markdown
Contributor

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:=falseifconfigData!=nil {
ifexpires, exists:=configData["expires"]; exists {
expiresInt:=parseExpiresFromConfig(configData)
ifexpiresInt==-1 {
expiresDisabled=trueconfigData["expires"] =0
} elseifexpiresInt>0 {
configData["expires"] =expiresInt
} else {
configData["expires"] =0
}
log.Printf("Parsed expires value %v to %d hours (disabled=%t)", expires, expiresInt, expiresDisabled)
}
}
// AfterexpiresDisabled:=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:=falseifconfigData!=nil {
ifexpires, exists:=configData["expires"]; exists {
expiresInt:=parseExpiresFromConfig(configData)
ifexpiresInt==-1 { ... } elseifexpiresInt>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>
CopilotAI changed the title [WIP] Refactor duplicate expires preprocessing in issue and discussion creationExtract duplicate expires preprocessing logic into shared helperFeb 11, 2026
CopilotAI requested a review from pelikhanFebruary 11, 2026 06:30
@pelikhan
pelikhan marked this pull request as ready for review February 11, 2026 06:31
CopilotAI review requested due to automatic review settings February 11, 2026 06:31

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

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.

FileDescription
pkg/workflow/config_helpers.goAdded preprocessExpiresField() helper function with complete documentation, proper nil handling for logger parameter, and logic identical to original implementation
pkg/workflow/create_issue.goReplaced 20-line duplicate preprocessing block with single call to preprocessExpiresField(configData, createIssueLog)
pkg/workflow/create_discussion.goReplaced 20-line duplicate preprocessing block with single call to preprocessExpiresField(configData, discussionLog)
pkg/workflow/config_parsing_helpers_test.goAdded 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
Copy Markdown
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 mainFeb 11, 2026
158 checks passed
@pelikhan
pelikhan deleted the copilot/refactor-expires-preprocessing branch February 11, 2026 06:41
Sign up for freeto 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

3 participants

@pelikhan