Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: labels from first invocation were being applied to future calls #251

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions packages/label-sync/src/label-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,25 @@ interface Repos {
// from the local copy. We are using the `PushEvent` to detect the change,
// meaning the file running in cloud will be older than the one on master.
let labelsCache: Labels;
async function getLabels(github: GitHubAPI, repoPath: string) {
async function getLabels(github: GitHubAPI, repoPath: string): Promise<Labels> {
if (!labelsCache) {
await refreshLabels(github, repoPath);
}
return labelsCache;
const labels = {
labels: labelsCache.labels.slice(0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of a labels.slice(0)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this creates a copy of the labels so that we don't end up populating additional labels in the cache each time we access labels.

creating a cache means that the labels are cached between invocations of cloud functions.

} as Labels;
const apiLabelsRes = await handler.getApiLabels(repoPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little worried here. Isn't this essentially completely bypassing the cache? Will this end up calling out to GCS constantly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only called once per function invocation, which only occurs rarely, e.g., when a new repo is created, or a label is deleted.

I think we're going to need to get rid of the push functionality any ways, because it takes like an hour to backfill all labels, and the maximum execution time of a cloud function is 9 minutes.

apiLabelsRes.apis.forEach(api => {
labels.labels.push({
name: api.github_label,
description: `Issues related to the ${api.display_name} API.`,
color: createHash('md5')
.update(api.api_shortname)
.digest('hex')
.slice(0, 6),
});
});
return labels;
}

async function refreshLabels(github: GitHubAPI, repoPath: string) {
Expand All @@ -60,18 +74,6 @@ async function refreshLabels(github: GitHubAPI, repoPath: string) {
labelsCache = JSON.parse(
Buffer.from(data.content as string, 'base64').toString('utf8')
);

const apiLabelsRes = await handler.getApiLabels(repoPath);
apiLabelsRes.apis.forEach(api => {
labelsCache.labels.push({
name: api.github_label,
description: `Issues related to the ${api.display_name} API.`,
color: createHash('md5')
.update(api.api_shortname)
.digest('hex')
.slice(0, 6),
});
});
}

function handler(app: Application) {
Expand Down