Skip to content

Commit d49a5e9

Browse files
authored
Merge branch 'main' into jetbrains-icons
2 parents 74cd390 + 4ebf490 commit d49a5e9

File tree

80 files changed

+2155
-771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2155
-771
lines changed

coderd/apidoc/docs.go

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apikey.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (api *API) postToken(rw http.ResponseWriter, r *http.Request) {
9191
TokenName: tokenName,
9292
})
9393
if err != nil {
94-
if database.IsUniqueViolation(err, database.UniqueIndexApiKeyName) {
94+
if database.IsUniqueViolation(err, database.UniqueIndexAPIKeyName) {
9595
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
9696
Message: fmt.Sprintf("A token with name %q already exists.", tokenName),
9797
Validations: []codersdk.ValidationError{{

coderd/database/errors.go

+22
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ func IsUniqueViolation(err error, uniqueConstraints ...UniqueConstraint) bool {
3737
return false
3838
}
3939

40+
// IsForeignKeyViolation checks if the error is due to a foreign key violation.
41+
// If one or more specific foreign key constraints are given as arguments,
42+
// the error must be caused by one of them. If no constraints are given,
43+
// this function returns true for any foreign key violation.
44+
func IsForeignKeyViolation(err error, foreignKeyConstraints ...ForeignKeyConstraint) bool {
45+
var pqErr *pq.Error
46+
if errors.As(err, &pqErr) {
47+
if pqErr.Code.Name() == "foreign_key_violation" {
48+
if len(foreignKeyConstraints) == 0 {
49+
return true
50+
}
51+
for _, fc := range foreignKeyConstraints {
52+
if pqErr.Constraint == string(fc) {
53+
return true
54+
}
55+
}
56+
}
57+
}
58+
59+
return false
60+
}
61+
4062
// IsQueryCanceledError checks if the error is due to a query being canceled.
4163
func IsQueryCanceledError(err error) bool {
4264
var pqErr *pq.Error

coderd/database/foreign_key_constraint.go

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/unique_constraint.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/httpapi/url.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package httpapi
22

33
import (
44
"fmt"
5+
"hash/crc32"
56
"net"
67
"regexp"
78
"strings"
@@ -18,6 +19,8 @@ var (
1819
nameRegex))
1920

2021
validHostnameLabelRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
22+
23+
crcTable = crc32.MakeTable(crc32.IEEE)
2124
)
2225

2326
// ApplicationURL is a parsed application URL hostname.
@@ -39,7 +42,12 @@ func (a ApplicationURL) String() string {
3942
_, _ = appURL.WriteString(a.WorkspaceName)
4043
_, _ = appURL.WriteString("--")
4144
_, _ = appURL.WriteString(a.Username)
42-
return appURL.String()
45+
hostname := appURL.String()
46+
47+
if len(hostname) < 64 { // max length for the subdomain level
48+
return hostname
49+
}
50+
return fmt.Sprintf("app-%08x", crc32.Checksum([]byte(hostname), crcTable))
4351
}
4452

4553
// ParseSubdomainAppURL parses an ApplicationURL from the given subdomain. If

coderd/httpapi/url_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func TestApplicationURLString(t *testing.T) {
4242
},
4343
Expected: "8080--agent--workspace--user",
4444
},
45+
{
46+
Name: "LongAppName",
47+
URL: httpapi.ApplicationURL{
48+
AppSlugOrPort: "0123456789012345678901234567890123456789",
49+
AgentName: "agent",
50+
WorkspaceName: "workspace",
51+
Username: "user",
52+
},
53+
Expected: "app-90667f72",
54+
},
4555
}
4656

4757
for _, c := range testCases {

0 commit comments

Comments
 (0)