Import LeetCode problems from a pasted link on the site
Test and publish the site / test (push) Successful in 1m9s
Test and publish the site / publish (push) Successful in 1m31s

The problem list's admin row gains a link box beside "Today's LeetCode":
POST /leetcode/import takes {url} and does what `usaco new
<leetcode-link>` does, through the same lib/leetcode.py run and "Start
<slug>" commit as the daily question, then opens the solve page. A
problem already in the repo is only opened.

leetcode.Slug reads the link as slug_of in lib/leetcode.py does: full
links with /description/ and query strings, www., no scheme, or a bare
slug. Anything else is a 400.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 13:02:11 +01:00
co-authored by Claude Opus 5.5
parent ccf4e4dd74
commit 3a8f651a6f
8 changed files with 179 additions and 23 deletions
+28 -3
View File
@@ -27,8 +27,6 @@ func (s *Server) leetcode() LeetCode {
// startDaily is `usaco daily`: it imports today's LeetCode daily question
// into solving/ and commits it, and answers its name for the page to open.
// A daily question already in the repo (started yesterday from the CLI, or
// solved months ago) is only named, never imported again.
func (s *Server) startDaily(ctx *gin.Context) {
c, cancel := context.WithTimeout(ctx.Request.Context(), 60*time.Second)
defer cancel()
@@ -44,6 +42,33 @@ func (s *Server) startDaily(ctx *gin.Context) {
ctx.JSON(http.StatusBadGateway, gin.H{"error": "LeetCode named the daily question " + slug + ", which is not a usable name"})
return
}
s.startLeetCode(ctx, c, lc, slug)
}
// startImport is `usaco new <leetcode-link>`: the same import, for the
// problem a pasted link names.
func (s *Server) startImport(ctx *gin.Context) {
var body struct {
URL string `json:"url"`
}
if err := ctx.ShouldBindJSON(&body); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid JSON"})
return
}
slug, err := leetcode.Slug(body.URL)
if err != nil || !validName.MatchString(slug) {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "not a LeetCode problem link"})
return
}
c, cancel := context.WithTimeout(ctx.Request.Context(), 60*time.Second)
defer cancel()
s.startLeetCode(ctx, c, s.leetcode(), slug)
}
// startLeetCode imports slug into solving/, commits it as "Start <slug>" and
// answers {name, created}. A problem already in the repo (started yesterday
// from the CLI, or solved months ago) is only named, never imported again.
func (s *Server) startLeetCode(ctx *gin.Context, c context.Context, lc LeetCode, slug string) {
if idx := s.Index(); idx != nil {
if _, ok := idx.Problems[slug]; ok {
ctx.JSON(http.StatusOK, gin.H{"name": slug, "created": false})
@@ -51,7 +76,7 @@ func (s *Server) startDaily(ctx *gin.Context) {
}
}
created := false
err = s.Repo.Mutate(c, "Start "+slug, func(dir string) error {
err := s.Repo.Mutate(c, "Start "+slug, func(dir string) error {
if _, err := findOnDisk(dir, slug); err == nil {
created = false
return nil
+63
View File
@@ -84,3 +84,66 @@ func TestDailyImportFails(t *testing.T) {
t.Fatalf("daily = %d %s, commits %v", rec.Code, rec.Body, repo.msg)
}
}
func TestImportLink(t *testing.T) {
r, repo, _ := setup(t)
lc := &fakeLeetCode{}
repo.srv.LeetCode = lc
body := `{"url":"https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2"}`
if rec := do(r, "POST", "/usaco/api/leetcode/import", body, false); rec.Code != 401 {
t.Fatalf("non-admin = %d, want 401", rec.Code)
}
rec := do(r, "POST", "/usaco/api/leetcode/import", body, true)
var got dailyReply
json.Unmarshal(rec.Body.Bytes(), &got)
if rec.Code != 200 || got != (dailyReply{"two-sum", true}) {
t.Fatalf("import = %d %s", rec.Code, rec.Body)
}
if len(repo.msg) != 1 || repo.msg[0] != "Start two-sum" {
t.Fatalf("commits = %v", repo.msg)
}
if _, ok := repo.srv.Index().Problems["two-sum"]; !ok {
t.Fatal("two-sum is not in the index after the import")
}
rec = do(r, "POST", "/usaco/api/leetcode/import", body, true)
got = dailyReply{}
json.Unmarshal(rec.Body.Bytes(), &got)
if rec.Code != 200 || got != (dailyReply{"two-sum", false}) || lc.imports != 1 {
t.Fatalf("second import = %d %s after %d imports", rec.Code, rec.Body, lc.imports)
}
}
func TestImportExisting(t *testing.T) {
r, repo, _ := setup(t)
lc := &fakeLeetCode{}
repo.srv.LeetCode = lc
rec := do(r, "POST", "/usaco/api/leetcode/import", `{"url":"https://leetcode.com/problems/moocast/"}`, true)
if rec.Code != 200 || lc.imports != 0 || len(repo.msg) != 0 {
t.Fatalf("import = %d %s, %d imports, commits %v", rec.Code, rec.Body, lc.imports, repo.msg)
}
}
func TestImportBadLink(t *testing.T) {
r, repo, _ := setup(t)
lc := &fakeLeetCode{}
repo.srv.LeetCode = lc
for _, body := range []string{`{"url":"https://codeforces.com/problemset/problem/1/A"}`, `{"url":""}`, `not json`} {
if rec := do(r, "POST", "/usaco/api/leetcode/import", body, true); rec.Code != 400 {
t.Fatalf("import %s = %d %s, want 400", body, rec.Code, rec.Body)
}
}
if lc.imports != 0 || len(repo.msg) != 0 {
t.Fatalf("%d imports, commits %v", lc.imports, repo.msg)
}
}
func TestImportFails(t *testing.T) {
r, repo, _ := setup(t)
repo.srv.LeetCode = &fakeLeetCode{err: errors.New("LeetCode has no problem 'nope'")}
rec := do(r, "POST", "/usaco/api/leetcode/import", `{"url":"https://leetcode.com/problems/nope/"}`, true)
if rec.Code != 502 || len(repo.msg) != 0 {
t.Fatalf("import = %d %s, commits %v", rec.Code, rec.Body, repo.msg)
}
}
+3 -2
View File
@@ -54,8 +54,8 @@ type Server struct {
// Statements reads a statement from the problem's judge; nil means the
// real judges.
Statements Fetcher
// LeetCode starts the daily question; nil means the real API and the
// clone's lib/leetcode.py.
// LeetCode starts the daily question and pasted links; nil means the
// real API and the clone's lib/leetcode.py.
LeetCode LeetCode
// Transcriber turns review recordings into text; nil when
// TRANSCRIBE_API_KEY is not set, which turns reviews off.
@@ -118,6 +118,7 @@ func (s *Server) Routes(api *gin.RouterGroup) {
admin.POST("/problems/:name/evaluate", s.startEvaluate)
admin.GET("/problems/:name/evaluate", s.evaluateStatus)
admin.POST("/leetcode/daily", s.startDaily)
admin.POST("/leetcode/import", s.startImport)
api.GET("/concepts", s.listConcepts)
api.GET("/concepts/:slug", s.getConcept)
admin.POST("/concepts", s.startConcept)