Skip to content

Commit ff76959

Browse files
AlexTugarevroboquat
authored andcommitted
[server/bitbucket] fix response codes for webhook events
so that they are not resent whenever we answer with code 5xx. cf. https://support.atlassian.com/bitbucket-cloud/docs/troubleshoot-webhooks/
1 parent 8e00078 commit ff76959

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

components/server/ee/src/prebuilds/bitbucket-app.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export class BitbucketApp {
3636
}
3737
const user = await this.findUser({ span }, secretToken);
3838
if (!user) {
39-
res.statusCode = 503;
39+
// If the webhook installer is no longer found in Gitpod's DB
40+
// we should send a UNAUTHORIZED signal.
41+
res.statusCode = 401;
4042
res.send();
4143
return;
4244
}
@@ -45,13 +47,13 @@ export class BitbucketApp {
4547
await this.handlePushHook({ span }, data, user);
4648
}
4749
} else {
48-
console.log(`Ignoring unsupported bitbucket event: ${req.header('X-Event-Key')}`);
50+
console.warn(`Ignoring unsupported bitbucket event: ${req.header('X-Event-Key')}`);
4951
}
50-
res.send('OK');
5152
} catch (err) {
52-
console.error(`Couldn't handle request.`, req.headers, req.body);
53-
console.error(err);
54-
res.sendStatus(500);
53+
console.error(`Couldn't handle request.`, err, { headers: req.headers, reqBody: req.body });
54+
} finally {
55+
// we always respond with OK, when we received a valid event.
56+
res.sendStatus(200);
5557
}
5658
});
5759
}
@@ -89,7 +91,7 @@ export class BitbucketApp {
8991
span.setTag('contextURL', contextURL);
9092
const config = await this.prebuildManager.fetchConfig({ span }, user, contextURL);
9193
if (!this.prebuildManager.shouldPrebuild(config)) {
92-
console.log('No config. No prebuild.');
94+
console.log('Bitbucket push event: No config. No prebuild.');
9395
return undefined;
9496
}
9597

@@ -159,7 +161,7 @@ export class BitbucketApp {
159161
function toData(body: BitbucketPushHook): ParsedRequestData | undefined {
160162
const branchName = body.push.changes[0]?.new?.name;
161163
const commitHash = body.push.changes[0]?.new?.target?.hash;
162-
if (!branchName || !commitHash){
164+
if (!branchName || !commitHash) {
163165
return undefined;
164166
}
165167
const result = {
@@ -169,7 +171,7 @@ function toData(body: BitbucketPushHook): ParsedRequestData | undefined {
169171
gitCloneUrl: body.repository.links.html.href + '.git'
170172
}
171173
if (!result.commitHash || !result.repoUrl) {
172-
console.error('unexpected request body.', body);
174+
console.error('Bitbucket push event: unexpected request body.', body);
173175
throw new Error('Unexpected request body.');
174176
}
175177
return result;

components/server/ee/src/prebuilds/bitbucket-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class BitbucketService extends RepositoryService {
7373
}
7474
console.log('Installed Bitbucket Webhook for ' + cloneUrl);
7575
} catch (error) {
76-
console.error(error);
76+
console.error('Failed to install Bitbucket webhook for ' + cloneUrl, error);
7777
}
7878
}
7979

0 commit comments

Comments
 (0)