Skip to content

Commit c96c8c8

Browse files
echarrodclaudeCopilot
authored
danger-js: Add DiffForFile method with comprehensive diff parsing, use interfaces to allow mocking (#32)
* danger-js: Add DiffForFile method with comprehensive diff parsing Add DiffForFile method to Git struct that executes git diff for a specific file. Add FileDiff and DiffLine types to represent parsed diff content. Add comprehensive test suite with 10 test cases covering various diff scenarios. AI::Created * danger-js: Convert structs to clean interfaces without suffixes Convert concrete structs (GitHub, GitLab, Settings, Git) to interfaces with clean names and internal implementations (gitImpl, gitHubImpl, gitLabImpl, settingsImpl) to improve testability and mockability while maintaining JSON marshaling compatibility. - Replace GitHubIntf/GitLabIntf/SettingsIntf with clean interface names - Add internal struct implementations with proper method implementations - Create DSLData struct for JSON unmarshaling with ToInterface() conversion method - Update danger-js.go and runner.go to use new unmarshaling pattern - Remove duplicate struct definitions from types_github.go and types_gitlab.go - All tests pass and backwards compatibility maintained 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * danger-js: Convert structs to clean interfaces and improve diff parsing - Convert concrete structs (GitHub, GitLab, Settings, Git) to clean interfaces without suffixes - Add internal implementations (gitImpl, gitHubImpl, gitLabImpl, settingsImpl) for better testability - Create DSLData struct for JSON unmarshaling with ToInterface() conversion method - Update danger-js.go and runner.go to use new unmarshaling pattern - Remove duplicate struct definitions from types_github.go and types_gitlab.go - Extract shared diff parsing logic into parseDiffContent() function to eliminate code duplication - Add DiffForFileWithRefs() method to make git commit references configurable (base/head) - Enhance line number tracking to parse actual line numbers from hunk headers (@@ syntax) - Add proper line number support to DiffLine struct - Maintain backward compatibility with existing DiffForFile() method - Uses strconv.Atoi for robust integer parsing from hunk headers - Tracks separate line counters for added vs removed lines - Line number feature is working correctly (tests show actual vs expected line numbers) - All core functionality implemented per PR feedback requirements 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * danger-js: Fix test expectations for correct line number parsing Remove Claude configuration files and update test expectations to match the correct behavior of parseDiffContent which now properly extracts line numbers from git diff hunk headers. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * danger-js: Update dangerfile to use new Git interface methods * Delete .claude/settings.local.json * danger-js: Address PR review comments - optimize performance and security * danger-js: Address PR review comments - improve security and add gitignore - Add single and double quotes to dangerous characters list in validateFilePath - Add validateGitRef function to validate baseRef and headRef parameters - Add validation for git references in DiffForFileWithRefs to prevent command injection - Add .claude/settings.local.json to .gitignore to prevent accidental commits * danger: Extract chars to vars * mr: Address comments * Update danger-js/types_danger_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 24f9751 commit c96c8c8

File tree

10 files changed

+743
-31
lines changed

10 files changed

+743
-31
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
# Local dev files
55
/go.work
66
/go.work.sum
7+
8+
# Claude Code local settings
9+
.claude/settings.local.json

build/ci/dangerfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import (
88

99
// Run is invoked by danger-go
1010
func Run(d *danger.T, pr danger.DSL) {
11-
d.Message(fmt.Sprintf("%d new files added!", len(pr.Git.CreateFiles)), "", 0)
12-
d.Message(fmt.Sprintf("%d files modified!", len(pr.Git.ModifiedFiles)), "", 0)
11+
d.Message(fmt.Sprintf("%d new files added!", len(pr.Git.GetCreatedFiles())), "", 0)
12+
d.Message(fmt.Sprintf("%d files modified!", len(pr.Git.GetModifiedFiles())), "", 0)
1313
}

cmd/danger-go/runner/runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Run() {
3838
}
3939

4040
var jsonData struct {
41-
Danger dangerJs.DSL `json:"danger"`
41+
Danger dangerJs.DSLData `json:"danger"`
4242
}
4343
err = json.Unmarshal(jsonBytes, &jsonData)
4444
if err != nil {
@@ -62,7 +62,7 @@ func Run() {
6262
}
6363

6464
d := danger.New()
65-
fn(d, jsonData.Danger)
65+
fn(d, jsonData.Danger.ToInterface())
6666
respJSON, err := d.Results()
6767
if err != nil {
6868
log.Fatalf("marshalling response: %s", err.Error())

danger-js/danger-js.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func GetPR(url string, dangerBin string) (DSL, error) {
3737
return DSL{}, fmt.Errorf("could not download DSL JSON with danger-js: %w", err)
3838
}
3939

40-
var pr DSL
41-
if err = json.Unmarshal(prJSON, &pr); err != nil {
40+
var prData DSLData
41+
if err = json.Unmarshal(prJSON, &prData); err != nil {
4242
return DSL{}, err
4343
}
44-
return pr, nil
44+
return prData.ToInterface(), nil
4545
}
4646

4747
func Process(command string, args []string) error {

0 commit comments

Comments
 (0)