Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Support HTML escaping in variable expansion? #187

Closed
Rychard opened this issue Jan 26, 2017 · 6 comments
Closed

Support HTML escaping in variable expansion? #187

Rychard opened this issue Jan 26, 2017 · 6 comments

Comments

@Rychard
Copy link

Rychard commented Jan 26, 2017

Making an API call to HipChat to send a room notifications works fine unless the variable expansion results in characters that must be encoded

Current Post content:

{
	"from": "Bitbucket Pull Request",
	"color": "green",
	"message_format": "text",
	"message": "${PULL_REQUEST_DESCRIPTION}",
	"card": {
		"id": "${PULL_REQUEST_FROM_HASH}",
		"style": "link",
		"description": {
			"value": "${PULL_REQUEST_DESCRIPTION}",
			"format": "text"
		},
		"format": "compact",
		"notify": false,
		"url": "${PULL_REQUEST_URL}",
		"title": "[${PULL_REQUEST_FROM_REPO_NAME}] ${PULL_REQUEST_TITLE} (#${PULL_REQUEST_ID})"
	}
}

The request it sends has a body that looks like this:

{
	"from": "Bitbucket Pull Request",
	"color": "green",
	"message_format": "text",
	"message": "This is the description for my pull request.",
	"card": {
		"id": "c4eb0edbebf14ac557a143916b3d9bf8edc1a19f",
		"style": "link",
		"description": {
			"value": "This is the description for my pull request.",
			"format": "text"
		},
		"format": "compact",
		"notify": false,
		"url": "http://hostname/projects/PROJECT_SLUG/repos/REPO_SLUG/pull-requests/2",
		"title": "[REPO_NAME] This is the title of a pull request &&not encoded&&&& (#2)"
	}
}

Note the final parameter (title):

"title": "[REPO_NAME] This is the title of a pull request &&not encoded&&&& (#2)"

The variable for ${PULL_REQUEST_TITLE} is expanding correctly to the title of the pull request (This is the title of a pull request &&not encoded&&&&), but I need to ensure that expanded variables are escaped properly before sending the request.

Is there any support for escaping HTML (i.e. & » &) that may exist in variables?

@tomasbjerre
Copy link
Owner

Currently no :(

@tomasbjerre
Copy link
Owner

Is this a show stopper for you? Or can you still use the plugin?

Also, it would be great if you would like to add a description to the readme on how you integrate the plugin with Hipchat.

@Rychard
Copy link
Author

Rychard commented Jan 27, 2017

Is this a show stopper for you? Or can you still use the plugin?

It doesn't prevent me from using it. However, it does prevent me from making it an official part of our process internally, as we can't guarantee that every PR will trigger a notification. Any pull request whose title or description includes a character that has to be HTML encoded will prevent any notifications for that pull request from being sent to HipChat (as I'm constructing a JSON object and populating it with variables), so our developers will still need to periodically check Bitbucket directly to ensure that none are missed.

I looked at the code in an effort to see if I could implement support for this and submit a pull request, but unfortunately I have almost no experience with Java. (For example, I had no idea that Java allowed enumeration values to implement interfaces, and be instantiated.)

Also, it would be great if you would like to add a description to the readme on how you integrate the plugin with Hipchat.

Here's a bit of a short write-up on how it's configured. I think it's a bit too long to be included in the README, but it seems perfect for inclusion in a wiki, or if you were to create a directory in the root of the repository for people to include markdown files with guides for integrating with other systems.

If you can advise me on where you'd like me to put it, I'd be more than happy to oblige.


Send Pull Request Notifications to HipChat Room

  1. Provision an API Token for HipChat

    • Login to the Hipchat website
    • Click the Edit Profile button in the top-right
    • Click API Access in the navigation menu on the left
    • Enter some descriptive text for the Label, select Send Notification from the Scopes combo box, and click the Create button
    • Copy the token from the grid; this is needed to authenticate the request

      Note: This value allows API requests to be authenticated as your HipChat user for the selected actions. For that reason, this token should be kept private! It is recommended to create individual tokens for every application that uses the HipChat API, to minimize risk if the token is compromised.

  2. Get the API ID for the HipChat room that will receive notifications

    • Login to the Hipchat website
    • Click on Rooms in the navigation bar below the welcome banner
    • Under the Active tab, click the name of the room that will receive notifications
    • Copy the API ID from the grid, this will be included in the notification request
  3. Configure Bitbucket to send notifications to a HipChat room

    • In Bitbucket, go to the repository that will trigger HipChat notifications
    • Click Settings (a gear icon, if the sidebar is not expanded)
    • Click Pull request notifications under the ADD-ONS heading in the navigation menu on the left.
    • Scroll down to Notifications.
    • Configure triggers as desired
    • Configure the URL and Headers sections to communicate with HipChat:
      • URL: https://api.hipchat.com/v2/room/{roomId}/notification

        Use the API ID of the room in place of {roomId} in the url above.

      • Post content:

        {
          "from": "Bitbucket Pull Request",
          "color": "green",
          "message_format": "text",
          "message": "${PULL_REQUEST_DESCRIPTION}",
          "card": {
            "id": "${PULL_REQUEST_FROM_HASH}",
            "style": "link",
            "description": {
              "value": "${PULL_REQUEST_DESCRIPTION}",
              "format": "text"
            },
            "format": "compact",
            "notify": false,
            "url": "${PULL_REQUEST_URL}",
            "title": "[${PULL_REQUEST_FROM_REPO_NAME}] ${PULL_REQUEST_TITLE} (#${PULL_REQUEST_ID})"
          }
         }
      • Headers:

        Name Value
        Content-Type application/json
        Authorization Bearer {apiToken}

        Use the API Token in place of {apiToken} in the above value.

  4. Trigger your notification by performing one of the actions you configured earlier

For further customization of the HipChat notification, refer to the official documentation

@tomasbjerre
Copy link
Owner

I'm thinking that the notification settings can have checkboxes like:

  • Dont encode variables (checked by default)
  • JSON encode variables
  • HTML encode variables

tomasbjerre added a commit that referenced this issue Jan 28, 2017
 * Also adding a .gitattributes to force line endings
tomasbjerre added a commit that referenced this issue Jan 28, 2017
 * The HipChat docs were privided by [Rychard](https://github.com/Rychard).
 * Moving Jenkins docs to its own file.
 * Moving issue reporting guidelines to ISSUE_TEMPLATE.
tomasbjerre added a commit that referenced this issue Jan 28, 2017
 * The HipChat docs were privided by [Rychard](https://github.com/Rychard).
 * Moving Jenkins docs to its own file.
 * Moving issue reporting guidelines to ISSUE_TEMPLATE.
@tomasbjerre
Copy link
Owner

This is now released in 2.49. Open issue again if any problems!

@Rychard
Copy link
Author

Rychard commented Jan 30, 2017

I've done some cursory testing with the latest version over the past day or so, and it's working wonderfully!

Thank you for implementing support for this so quickly!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants