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 bug in github-app.ts #5039

Merged
merged 2 commits into from
Aug 3, 2021
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
28 changes: 23 additions & 5 deletions components/server/ee/src/prebuilds/github-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,37 @@ export class GithubApp {
});

app.on('installation.created', async ctx => {
const accountId: string = `${ctx.payload.installation.account.id}`;
const targetAccountName = `${ctx.payload.installation.account.login}`;
AlexTugarev marked this conversation as resolved.
Show resolved Hide resolved
const installationId = `${ctx.payload.installation.id}`;
const senderId = `${ctx.payload.sender.id}`;
const user = await this.userDB.findUserByIdentity({ authProviderId: this.env.githubAppAuthProviderId, authId: accountId });
Copy link
Member Author

Choose a reason for hiding this comment

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

accountId is the target account id, e.g. from github.com/gitpod-io. this are not valid coordinates for user accounts.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. I'd love to learn more about the differences between ctx.payload.installation.account.id and ctx.payload.sender.id.

  • From the docs you linked, I gather that ctx.payload.installation.account.id is the "account" on which the app is installed (e.g. can be an org, as you mentioned)
  • Who is ctx.payload.sender.id then? Is it the user that installed the app (e.g. a team member), or the user that triggered/caused the event? (e.g. a community contributor opening a new PR)

Copy link
Member Author

Choose a reason for hiding this comment

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

See the documentation URL at L.90.


// cf. https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#installation
const authId = `${ctx.payload.sender.id}`;

const user = await this.userDB.findUserByIdentity({ authProviderId: this.env.githubAppAuthProviderId, authId });
const userId = user ? user.id : undefined;
await this.appInstallationDB.recordNewInstallation("github", 'platform', installationId, userId, senderId);
log.debug({ userId }, "New installation recorded", { userId, platformUserId: ctx.payload.sender.id })
await this.appInstallationDB.recordNewInstallation("github", 'platform', installationId, userId, authId);
log.debug({ userId }, "New installation recorded", { userId, authId, targetAccountName })
});
app.on('installation.deleted', async ctx => {
const installationId = `${ctx.payload.installation.id}`;
await this.appInstallationDB.recordUninstallation("github", 'platform', installationId);
});

app.on('repository.renamed', async ctx => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting, where is this event type documented? I couldn't find it anywhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, that's the repository event with renamed action. See the documentation URL above.

const { action, repository, installation } = ctx.payload;
if (!installation) {
return;
}
Comment on lines +105 to +107
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: Can this actually happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

if (action === "renamed") {
const project = await this.projectDB.findProjectByInstallationId(String(installation.id))
if (project) {
project.cloneUrl = repository.clone_url;
await this.projectDB.storeProject(project);
}
}
// TODO(at): handle deleted as well
});

app.on('push', async ctx => {
await this.handlePushEvent(ctx);
});
Expand Down