Fix GitLab's ready for review hook (#936)

* Fix GitLab's ready for review hook

As far back as I could go in the docs (GitLab 14.10),
the keys for changes have been `previous` and `current`.

There is a `draft` change that
we can use directly
instead of guessing from the title.
The type of the value depends on
the key of the change.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>

* Add changelog

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>

---------

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
Co-authored-by: Will Hunt <will@half-shot.uk>
This commit is contained in:
Kévin Commaille 2024-05-22 14:17:29 +02:00 committed by GitHub
parent e5705e74c0
commit 2bffd5c90e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 11 deletions

1
changelog.d/936.bugfix Normal file
View File

@ -0,0 +1 @@
Fix GitLab's ready for review hook.

View File

@ -622,22 +622,18 @@ export class GitLabRepoConnection extends CommandConnection<GitLabRepoConnection
log.info(`onMergeRequestUpdate ${this.roomId} ${this.instance}/${this.path} ${event.object_attributes.iid}`);
this.validateMREvent(event);
// Check if the MR changed to / from a draft
if (!event.changes.title) {
if (!event.changes.draft) {
return;
}
const orgRepoName = event.project.path_with_namespace;
let content: string;
const wasDraft = event.changes.title.before.startsWith('Draft: ');
const isDraft = event.changes.title.after.startsWith('Draft: ');
if (wasDraft && !isDraft) {
const isDraft = event.changes.draft.current;
if (!isDraft) {
// Ready for review
content = `**${event.user.username}** marked MR [${orgRepoName}#${event.object_attributes.iid}](${event.object_attributes.url}) as ready for review "${event.object_attributes.title}" `;
} else if (!wasDraft && isDraft) {
} else {
// Back to draft.
content = `**${event.user.username}** marked MR [${orgRepoName}#${event.object_attributes.iid}](${event.object_attributes.url}) as draft "${event.object_attributes.title}" `;
} else {
// Nothing changed, drop it.
return;
}
await this.intent.sendEvent(this.roomId, {
msgtype: "m.notice",

View File

@ -64,9 +64,9 @@ export interface IGitLabWebhookMREvent {
object_attributes: IGitLabMergeRequestObjectAttributes;
labels: IGitLabLabel[];
changes: {
[key: string]: {
before: string;
after: string;
draft?: {
previous: boolean;
current: boolean;
}
}
}