Skip to content

Conversation

Fix3dP0int
Copy link

@Fix3dP0int Fix3dP0int commented Jun 17, 2025

What kind of change does this PR introduce?

feature

What is the current behavior?

The current serve uses Go’s built-in http package, which lacks modern features. And It fails to correctly aggregate the total score, and parameters and details cannot be retrieved properly.

What is the new behavior (if this is a feature change)?**

This PR refactors the serve component by migrating the original CLI-based parameter input to a RESTful API interface. Additionally, I replaced the native net/http logic with the chi router, which is lightweight yet expressive and well-suited for modular HTTP services in Go.

  • Tests for the changes have been added (for bug fixes/features)

Which issue(s) this PR fixes

Related to #4627

@Fix3dP0int Fix3dP0int changed the title ✨ feat: Refactor scorecard serve cmd (#4627) ✨ feat: Refactor scorecard serve cmd Jun 17, 2025
@spencerschrock
Copy link
Member

Don't have time to look in-depth, but wanted to make one comment:

The current serve uses Go’s built-in http package, which lacks modern features

What features specifically? Go 1.22 made good strides at least for the routing
https://go.dev/blog/routing-enhancements

@Fix3dP0int
Copy link
Author

Sorry. I'm unfamiliar with new features of Go. It seems that chi isn't necessary. I will revert it. Thx.

@Fix3dP0int Fix3dP0int marked this pull request as ready for review June 22, 2025 16:45
@Fix3dP0int Fix3dP0int requested a review from a team as a code owner June 22, 2025 16:45
@Fix3dP0int Fix3dP0int requested review from spencerschrock and raghavkaul and removed request for a team June 22, 2025 16:45
Copy link
Member

@spencerschrock spencerschrock left a comment

Choose a reason for hiding this comment

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

At a high level this looks like what we want, an http wrapper around the CLI. You said MCP will be built on top of this API, so is it matching what you need for that?

There are a few things that need changed, which I've left individual comments on. The linter also has some thoughts with this file.

cmd/serve.go Outdated
Comment on lines 63 to 64
PolicyFile string `json:"policy_file,omitempty"`
}
Copy link
Member

Choose a reason for hiding this comment

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

In the CLI, policy file is a local file. How do we expect to pass a policy file to a server, with a URI?

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, I'm late. I rarely use this policy file. I think that it should be a cmd parameter rather than a server parameter, so now I've temporarily removed it.

cmd/serve.go Outdated
Comment on lines 115 to 116
if s.opts.LogLevel == "" {
s.opts.LogLevel = "info"
Copy link
Member

Choose a reason for hiding this comment

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

log.InfoLevel is a constant with this value.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks. I have fixed it.

cmd/serve.go Outdated
Comment on lines 97 to 101
// Set options
s.opts.Repo = req.Repo
s.opts.Local = req.Local
s.opts.NPM = req.NPM
s.opts.PyPI = req.PyPI
Copy link
Member

Choose a reason for hiding this comment

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

Right now we have one options.Options, which is passed in serveCmd, and then this one copy is modified for each request. This seems like a race condition.

we should create a new struct for each request. either through options.New, or manually if we want to avoid the env var parsing.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah. options.New would be a better choice. Have fixed it.

cmd/serve.go Outdated
Comment on lines 121 to 124
} else if s.opts.FileMode != options.FileModeArchive && s.opts.FileMode != options.FileModeGit {
http.Error(w, fmt.Sprintf("unsupported file mode: %s", s.opts.FileMode), http.StatusBadRequest)
return
}
Copy link
Member

Choose a reason for hiding this comment

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

this validation is already covered by the s.opts.Validate call below

Copy link
Author

Choose a reason for hiding this comment

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

OK. It has been removed.

cmd/serve.go Outdated
Comment on lines 154 to 159
if s.opts.Local != "" {
repo, err = localdir.MakeLocalDirRepo(s.opts.Local)
if err != nil {
http.Error(w, fmt.Sprintf("making local dir: %v", err), http.StatusInternalServerError)
return
}
Copy link
Member

Choose a reason for hiding this comment

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

do we expect people to pass the serve command repos which are local to the server?

Copy link
Author

Choose a reason for hiding this comment

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

This error also stems from my naive approach of simply copying the logic from root.go. When the server starts up, the repo should be a parameter provided by the client's request. Now local would be not supported.

cmd/serve.go Outdated
Comment on lines 183 to 184
enabledChecks, err := policy.GetEnabled(pol, s.opts.Checks(), requiredRequestTypes)
stdlog.Printf("DEBUG: enabledChecks = %#v", enabledChecks)
Copy link
Member

Choose a reason for hiding this comment

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

should this be removed? was this for your testing?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah. Sorry for my carelessness. Done removing.

cmd/serve.go Outdated
Comment on lines 227 to 228
Details: s.opts.ShowDetails,
Annotations: false,
Copy link
Member

Choose a reason for hiding this comment

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

we would probably want ShowAnnotations as a query parameter.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I've added ShowAnnotations as a query parameter now.

cmd/serve.go Outdated
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
stdlog.Printf("%s %s %s", r.Method, r.URL, time.Since(start))

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
Copy link

codecov bot commented Jun 24, 2025

Codecov Report

Attention: Patch coverage is 0% with 211 lines in your changes missing coverage. Please review.

Project coverage is 67.71%. Comparing base (353ed60) to head (3060b2a).
Report is 198 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4665      +/-   ##
==========================================
+ Coverage   66.80%   67.71%   +0.91%     
==========================================
  Files         230      249      +19     
  Lines       16602    19044    +2442     
==========================================
+ Hits        11091    12896    +1805     
- Misses       4808     5289     +481     
- Partials      703      859     +156     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

github-actions bot commented Jul 5, 2025

This pull request has been marked stale because it has been open for 10 days with no activity

@github-actions github-actions bot added Stale and removed Stale labels Jul 5, 2025
Copy link

This pull request has been marked stale because it has been open for 10 days with no activity

@github-actions github-actions bot added the Stale label Jul 17, 2025
@github-actions github-actions bot removed the Stale label Jul 27, 2025
Copy link

github-actions bot commented Aug 8, 2025

This pull request has been marked stale because it has been open for 10 days with no activity

@github-actions github-actions bot added the Stale label Aug 8, 2025
@spencerschrock
Copy link
Member

Back from vacation, going through my backlog from before and reopenning PRs that went stale waiting for me.

@spencerschrock spencerschrock reopened this Sep 3, 2025
@github-actions github-actions bot removed the Stale label Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants