tenseleyflow/shithub / 90c7bfb

Browse files

profile: satisfy policy boundary lint

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
90c7bfb1cf66e0e9214898ba7f80b583edec27d6
Parents
d933bdc
Tree
8fce3ad

3 changed files

StatusFile+-
M internal/web/handlers/profile/overview.go 14 5
M internal/web/handlers/profile/profile_test.go 8 2
M internal/web/handlers/profile/stars_tab.go 1 1
internal/web/handlers/profile/overview.gomodified
@@ -122,6 +122,8 @@ type profileContributionRepo struct {
122122
 	Repo                  reposdb.Repo
123123
 	OwnerSlug             string
124124
 	AllowIdentityFallback bool
125
+	IsPrivate             bool
126
+	IsProfileOwnedPublic  bool
125127
 }
126128
 
127129
 func (h *Handlers) visibleUserRepos(ctx context.Context, userID int64, viewer middleware.CurrentUser) []reposdb.Repo {
@@ -296,7 +298,7 @@ func (h *Handlers) contributionCalendar(ctx context.Context, user usersdb.User,
296298
 		if !source.Repo.CreatedAt.Valid || created.Before(windowStart) || !created.Before(windowEnd) {
297299
 			continue
298300
 		}
299
-		if source.Repo.OwnerUserID.Int64 != user.ID || source.Repo.Visibility != reposdb.RepoVisibilityPublic {
301
+		if !source.IsProfileOwnedPublic {
300302
 			continue
301303
 		}
302304
 		activity.addCreatedRepo(created, source)
@@ -370,7 +372,7 @@ func newProfileActivityBuilder() *profileActivityBuilder {
370372
 }
371373
 
372374
 func (b *profileActivityBuilder) addCommit(day time.Time, source profileContributionRepo) {
373
-	isPrivate := source.Repo.Visibility == reposdb.RepoVisibilityPrivate
375
+	isPrivate := source.IsPrivate
374376
 	fullName := source.OwnerSlug + "/" + source.Repo.Name
375377
 	url := "/" + url.PathEscape(source.OwnerSlug) + "/" + url.PathEscape(source.Repo.Name)
376378
 	if isPrivate {
@@ -600,15 +602,15 @@ func (h *Handlers) addProfileThreadActivity(ctx context.Context, user usersdb.Us
600602
 	}
601603
 	deps := policy.Deps{Pool: h.d.Pool}
602604
 	for _, row := range rows {
603
-		if row.Visibility == issuesdb.RepoVisibilityPrivate && !user.IncludePrivateContributions {
604
-			continue
605
-		}
606605
 		ref := policy.RepoRef{
607606
 			ID:          row.RepoID,
608607
 			OwnerUserID: row.OwnerUserID.Int64,
609608
 			OwnerOrgID:  row.OwnerOrgID.Int64,
610609
 			Visibility:  string(row.Visibility),
611610
 		}
611
+		if ref.IsPrivate() && !user.IncludePrivateContributions {
612
+			continue
613
+		}
612614
 		if !policy.IsVisibleTo(ctx, deps, actor, ref) {
613615
 			continue
614616
 		}
@@ -658,6 +660,8 @@ func (h *Handlers) profileContributionRepos(ctx context.Context, user usersdb.Us
658660
 			Repo:                  repo,
659661
 			OwnerSlug:             ownerSlug,
660662
 			AllowIdentityFallback: allowIdentityFallback,
663
+			IsPrivate:             repoRef.IsPrivate(),
664
+			IsProfileOwnedPublic:  isProfileOwnedPublicRepo(repo, repoRef, user.ID),
661665
 		})
662666
 	}
663667
 
@@ -697,6 +701,11 @@ func (h *Handlers) profileContributionRepos(ctx context.Context, user usersdb.Us
697701
 	return out
698702
 }
699703
 
704
+func isProfileOwnedPublicRepo(repo reposdb.Repo, ref policy.RepoRef, userID int64) bool {
705
+	ownerUserID := repo.OwnerUserID.Int64
706
+	return ownerUserID == userID && ref.IsPublic()
707
+}
708
+
700709
 func selectedContributionYear(query url.Values, currentYear int) int {
701710
 	for _, key := range []string{"year", "from"} {
702711
 		raw := strings.TrimSpace(query.Get(key))
internal/web/handlers/profile/profile_test.gomodified
@@ -640,8 +640,14 @@ func TestProfile_ContributionActivityIncludesCommitsReposIssuesAndPulls(t *testi
640640
 			t.Errorf("missing %q in body: %s", want, body)
641641
 		}
642642
 	}
643
-	if strings.Contains(body, strconv.FormatInt(oldRepoID, 10)) {
644
-		t.Fatalf("test template leaked implementation ids unexpectedly: %s", body)
643
+	for _, leak := range []string{
644
+		fmt.Sprintf("repo_id=%d", oldRepoID),
645
+		fmt.Sprintf(`data-repo-id="%d"`, oldRepoID),
646
+		fmt.Sprintf("RepoID:%d", oldRepoID),
647
+	} {
648
+		if strings.Contains(body, leak) {
649
+			t.Fatalf("test template leaked implementation id marker %q unexpectedly: %s", leak, body)
650
+		}
645651
 	}
646652
 }
647653
 
internal/web/handlers/profile/stars_tab.gomodified
@@ -107,7 +107,7 @@ func (h *Handlers) serveStarsTab(w http.ResponseWriter, r *http.Request, user us
107107
 			URL:             "/" + url.PathEscape(ownerName) + "/" + url.PathEscape(row.RepoName),
108108
 			Description:     row.Description,
109109
 			Visibility:      string(row.Visibility),
110
-			IsPrivate:       row.Visibility == socialdb.RepoVisibilityPrivate,
110
+			IsPrivate:       ref.IsPrivate(),
111111
 			StarCount:       row.StarCount,
112112
 			PrimaryLanguage: language,
113113
 			LanguageColor:   template.CSS(orgLanguageColor(language)), //nolint:gosec // server-side constant map.