Skip to content

Commit

Permalink
Add Github team slug to view_repo_ownership (#831)
Browse files Browse the repository at this point in the history
* Add migration to update view_repo_ownership

- Add team slug
- Add repo short name

Co-authored-by: Natasha <[email protected]>

* Run migrate dev command

These changes were automatically created by running ```npm -w cli start migrate -- --stage DEV`

Co-authored-by: Natasha <[email protected]>

* Update docs to describe Prisma client generation

Co-authored-by: Natasha <[email protected]>

* Update migration to make column names clearer

Co-authored-by: Natasha <[email protected]>
Co-authored-by: Akash Askoolum <[email protected]>
Co-authored-by: TJ Silver <[email protected]>

* Fix TypeScript errors arising from generating new Prisma client

Co-authored-by: Natasha <[email protected]>
Co-authored-by: Akash Askoolum <[email protected]>
Co-authored-by: TJ Silver <[email protected]>

---------

Co-authored-by: Natasha <[email protected]>
Co-authored-by: Natasha <[email protected]>
Co-authored-by: Akash Askoolum <[email protected]>
Co-authored-by: TJ Silver <[email protected]>
5 people authored Mar 6, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 4c25477 commit 38e1700
Showing 8 changed files with 40 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/database-migrations.md
Original file line number Diff line number Diff line change
@@ -26,6 +26,12 @@ Once you have a draft migration, apply it via:
npm -w cli start migrate -- --stage DEV
```

You can then generate a new version of the Prisma Client via:

```bash
npx -w common prisma generate
```

## Applying a migration to CODE or PROD
Prerequisite:
1. You have an approved Pull Request
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
drop view if exists view_repo_ownership;

create view view_repo_ownership as
select ght.id as "github_team_id"
, ght.name as "github_team_name"
, ght.slug as "github_team_slug"
, tr.name as "short_repo_name"
, tr.full_name as "full_repo_name"
, tr.role_name
, tr.archived
, gtt.team_name as "galaxies_team"
, gtt.team_contact_email
from github_team_repositories tr
join github_teams ght on tr.team_id = ght.id
left join galaxies_teams_table gtt on ght.slug = gtt.team_primary_github_team
where tr.role_name = 'admin';
7 changes: 4 additions & 3 deletions packages/common/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -631,14 +631,15 @@ model guardian_github_actions_usage {
view view_repo_ownership {
github_team_id BigInt
github_team_name String
repo_name String
full_name String
github_team_slug String
short_repo_name String
full_repo_name String
role_name String
archived Boolean
galaxies_team String?
team_contact_email String?
@@unique([github_team_id, full_name])
@@unique([github_team_name, full_repo_name])
}

view aws_accounts {
5 changes: 3 additions & 2 deletions packages/common/prisma/views/public/view_repo_ownership.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
SELECT
ght.id AS github_team_id,
ght.name AS github_team_name,
tr.full_name AS repo_name,
tr.full_name,
ght.slug AS github_team_slug,
tr.name AS short_repo_name,
tr.full_name AS full_repo_name,
tr.role_name,
tr.archived,
gtt.team_name AS galaxies_team,
Original file line number Diff line number Diff line change
@@ -6,10 +6,11 @@ import type { Team } from '../../types';
import { createBranchProtectionEvents } from './branch-protection';

const nullOwner: view_repo_ownership = {
full_name: '',
full_repo_name: '',
github_team_id: BigInt(0),
github_team_name: '',
repo_name: '',
github_team_slug: '',
short_repo_name: '',
role_name: '',
archived: false,
galaxies_team: null,
@@ -40,9 +41,10 @@ describe('Team slugs should be findable for every team associated with a repo',

const teamOneOwner: view_repo_ownership = {
...nullOwner,
full_name: repo,
full_repo_name: repo,
github_team_id: BigInt(1),
github_team_name: 'Team One',
github_team_slug: teamOne.slug,
};

const actual = createBranchProtectionEvents(
2 changes: 1 addition & 1 deletion packages/repocop/src/remediation/shared-utilities.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ export function findContactableOwners(
allRepoOwners: view_repo_ownership[],
teams: Team[],
): string[] {
const owners = allRepoOwners.filter((owner) => owner.full_name === repo);
const owners = allRepoOwners.filter((owner) => owner.full_repo_name === repo);
const teamSlugs = owners
.map((owner) => findTeamSlugFromId(owner.github_team_id, teams))
.filter((slug): slug is string => !!slug);
Original file line number Diff line number Diff line change
@@ -27,8 +27,9 @@ const date = new Date('2021-01-01');
const ownershipRecord: view_repo_ownership = {
github_team_name: teamName,
github_team_id: teamId,
repo_name: removeRepoOwner(fullName),
full_name: fullName,
github_team_slug: teamSlug,
short_repo_name: removeRepoOwner(fullName),
full_repo_name: fullName,
role_name: '',
archived: false,
galaxies_team: null,
@@ -52,7 +53,7 @@ const anotherOwnershipRecord: view_repo_ownership = {
...ownershipRecord,
github_team_name: anotherTeam.name,
github_team_id: anotherTeam.id,
full_name: anotherFullName,
full_repo_name: anotherFullName,
};

const anotherRepocopRuleEvaluation: repocop_github_repository_rules = {
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ function getOwningRepos(

const resultsOwnedByTeam = reposOwnedByTeam
.map((repo) => {
return results.find((result) => result.fullName === repo.full_name);
return results.find((result) => result.fullName === repo.full_repo_name);
})
.filter((result): result is EvaluationResult => result !== undefined);

0 comments on commit 38e1700

Please sign in to comment.