Mattermost-plugin-github: Replace GitHub usernames with Mattermost usernames in notifications

Created on 10 Jan 2020  路  4Comments  路  Source: mattermost/mattermost-plugin-github

We already resolve the GitHub user to the Mattermost user when generating the template and posting to Mattermost -- e.g. @saturninoabril becomes @saturnino.abril, given our knowledge of the linking in the GitHub plugin:
image

but let's also consider doing this in the body of notifications as well, so that @asaadmahmood, @marianunez and @lieut-data become @asaad.mahmood, @maria.nunez and @jesse.hallam respectively:
image

Of course, if no known Mattermost user has that GitHub username, we can leave the GitHub user intact.

Medium Help Wanted TecGo

All 4 comments

I would like to work on this one.

I am planning to solve this as follows:

  1. Add a new function to funcMap in template.go that receives a string and returns that same string with all the GitHub usernames resolved to their assigned Mattermost usernames, if any. If no Mattermost username is found, the GitHub username will remain unchanged. This function will be really similar to the one @levb pointed to, replaceJiraAccountIds.
  2. Add unit tests for this function in template_test.go so we can make sure it resolves all mapped GitHub usernames.
  3. Modify all templates that call the .GetBody function, piping that body into the new registered function. The templates to modify are:

    • newPR

    • newIssue

    • issueComment

    • pullRequestReviewEvent

    • newReviewComment

    • commentMentionNotification

    • commentAuthorPullRequestNotification

    • pullRequestReviewNotification

  4. Adapt the tests in template_test.go so that we test the changes in the body both when there is a gitHubToUsernameMappingCallback registered and when there is not.

Some notes:

  • The titles of the issues and pull requests may contain usernames, but they do not trigger notifications to the mentioned GitHub users, so I would let the usernames there unchanged.
  • I have not found any public documentation about the requirements that a valid GitHub username must fulfill, so I am using the error messages returned in https://github.com/join (inspired by https://github.com/shinnn/github-username-regex); namely:

    1. The username must be between 1 and 39 characters long.

    2. The username may only contain alphanumeric characters or single hyphens (i.e., not two adjacent hyphens, as in @wrong--username).

    3. The username cannot begin or end with a hyphen.

  • I see no way of both checking the length and assuring that there are not two adjacent hyphens without lookaheads, so I am proposing to only check constraints ii and iii but not i. In the rare case that we would match a string with more than 39 characters, we would not have any mapping to a Mattermost user, so the string will remain unchanged.
  • When writing comments in GitHub, the usernames are converted into mentions unless they are prepended by an alphanumeric character (this discards things such as [email protected]), an underscore (_) or a backtick (`).
  • Following the two previous comments, the proposed regular expression would be [^_\x60[:alnum:]]@[[:alnum:]](-?[[:alnum:]]+)*, where \x60 is the backtick character.

I have already started working on this, but please tell me if you would like me to implement something in a different way.

Thanks again, @agarciamontoro!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hanzei picture hanzei  路  3Comments

jfrerich picture jfrerich  路  3Comments

hanzei picture hanzei  路  6Comments

tgly307 picture tgly307  路  3Comments

hanzei picture hanzei  路  5Comments