Skip to content

Commit

Permalink
Restore updateBranchProtections method for metrics purposes
Browse files Browse the repository at this point in the history
We've decided to keep collecting metrics for `commitsToRepositoryWithBranchProtections`, which requires us to store data on protected branches in a repository
  • Loading branch information
kuychaco committed Nov 7, 2019
1 parent a5b0312 commit 03d17de
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
32 changes: 32 additions & 0 deletions app/src/lib/stores/app-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
getAccountForEndpoint,
getDotComAPIEndpoint,
IAPIOrganization,
IAPIBranch,
IAPIRepository,
} from '../api'
import { shell } from '../app-shell'
Expand Down Expand Up @@ -3271,6 +3272,37 @@ export class AppStore extends TypedBaseStore<IAppState> {
return updatedRepository
}

private async updateBranchProtectionsFromAPI(repository: Repository) {
if (
repository.gitHubRepository === null ||
repository.gitHubRepository.dbID === null
) {
return
}

const { owner, name } = repository.gitHubRepository

const account = getAccountForEndpoint(
this.accounts,
repository.gitHubRepository.endpoint
)

if (account === null) {
return
}

const api = API.fromAccount(account)

const branches = enableBranchProtectionChecks()
? await api.fetchProtectedBranches(owner.login, name)
: new Array<IAPIBranch>()

await this.repositoriesStore.updateBranchProtections(
repository.gitHubRepository,
branches
)
}

private async matchGitHubRepository(
repository: Repository
): Promise<IMatchedGitHubRepository | null> {
Expand Down
68 changes: 67 additions & 1 deletion app/src/lib/stores/repositories-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import {
RepositoriesDatabase,
IDatabaseGitHubRepository,
IDatabaseOwner,
IDatabaseProtectedBranch,
} from '../databases/repositories-database'
import { Owner } from '../../models/owner'
import { GitHubRepository } from '../../models/github-repository'
import { Repository } from '../../models/repository'
import { fatalError } from '../fatal-error'
import { IAPIRepository } from '../api'
import { IAPIRepository, IAPIBranch } from '../api'
import { BaseStore } from './base-store'
import { enableBranchProtectionChecks } from '../feature-flag'

/** The store for local repositories. */
export class RepositoriesStore extends BaseStore {
Expand Down Expand Up @@ -453,6 +455,70 @@ export class RepositoriesStore extends BaseStore {
)
}

/** Add or update the branch protections associated with a GitHub repository. */

public async updateBranchProtections(
gitHubRepository: GitHubRepository,
protectedBranches: ReadonlyArray<IAPIBranch>
): Promise<void> {
if (!enableBranchProtectionChecks()) {
return
}

const dbID = gitHubRepository.dbID
if (!dbID) {
return fatalError(
'`updateBranchProtections` can only update a GitHub repository for a repository which has been added to the database.'
)
}

await this.db.transaction('rw', this.db.protectedBranches, async () => {
// This update flow is organized into two stages:
//
// - update the in-memory cache
// - update the underyling database state
//
// This should ensure any stale values are not being used, and avoids
// the need to query the database while the results are in memory.

const prefix = getKeyPrefix(dbID)

for (const key of this.protectionEnabledForBranchCache.keys()) {
// invalidate any cached entries belonging to this repository
if (key.startsWith(prefix)) {
this.protectionEnabledForBranchCache.delete(key)
}
}

const branchRecords = protectedBranches.map<IDatabaseProtectedBranch>(
b => ({
repoId: dbID,
name: b.name,
})
)

// update cached values to avoid database lookup
for (const item of branchRecords) {
const key = getKey(dbID, item.name)
this.protectionEnabledForBranchCache.set(key, true)
}

await this.db.protectedBranches
.where('repoId')
.equals(dbID)
.delete()

const protectionsFound = branchRecords.length > 0
this.branchProtectionSettingsFoundCache.set(dbID, protectionsFound)

if (branchRecords.length > 0) {
await this.db.protectedBranches.bulkAdd(branchRecords)
}
})

this.emitUpdate()
}

/**
* Set's the last time the repository was checked for pruning
*
Expand Down

0 comments on commit 03d17de

Please sign in to comment.