Hello!
In this article we will continue to talk about atlassian-connect-express and we will talk about context parameters.
This article will be based on this article.
You can find the source code for the article here.
The video for the article you can watch here.
Context parameters are placeholders you can put into a module’s URL to have Bitbucket send contextual information with each request to that module.
For example, let’s have a look at our application. We have the following repoPages module in the atlassian-connect.json:
"repoPages": [
{
"url": "/connect-example?repoPath={repository.full_name}",
"name": {
"value": "Example Page"
},
"location": "org.bitbucket.repository.navigation",
"key": "example-repo-page",
"params": {
"auiIcon": "aui-iconfont-doc"
}
}
],
Please, pay attention to the “url”: “/connect-example?repoPath={repository.full_name}” part. “repository” is a context parameter for which we take the full_name attribute and assign to the repoPath parameter in the url and we can read later this repoPath parameter in the code.
Ok, let’s set a breakpoint in the routes/index.html file and click on the “Example Page” menu option:
The execution of our app will stop here:
Now if you have a look at the Debug tab in the variables section, you will see that the repoPath url parameter was set with “alex1mmmcprime2/bb-test-app-repo” which is the full name of our repo.
Well, how would we know what context parameters we can use for our modules?
You can find this information here.
As you can see for our repoPages module we have three context variable: target_user, repository and user. Target_user is the user under whom the plugin is installed, repository is the repository from which the Example Page menu item is called and user is the user who called the Example Page menu item.
The next question is how to understand what attributes I can use for each of the context parameters.
The answer is you can use any attribute from the corresponding REST endpoint.
For the target_user and the user context parameter the corresponding endpoint is users/{selected_users}, for the repository context parameter it is the repository/{workspace}/{repo_slug}.
Let’s have a look at the repository/{workspace}/{repo_slug} REST endpoint. Here is the schema:
As you can see this endpoint has the “has_wiki” attribute and the “links.watchers.href” attribute. Let’s try to get this parameters in our app.
We will change the repoPages module to this one:
"repoPages": [
{
"url": "/connect-example?repoPath={repository.full_name}&repoHasWiki={repository.has_wiki}&repoLinksWatchers={repository.links.watchers.href}",
"name": {
"value": "Example Page"
},
"location": "org.bitbucket.repository.navigation",
"key": "example-repo-page",
"params": {
"auiIcon": "aui-iconfont-doc"
}
}
],
And now if you update the application you will see that we can get our repoHasWiki and repoLinksWatchers url parameters in our app:
That is how you can use context parameters.
Why do we need this parameters at all?
There are multiple cases. For example, you have some kind analytic system where you want to store user actions in your app. In this case you can get the name of the repo, the name of the user and save the values in your analytic system. Or you have some kind of integration with an external system and you need to pass the repo uuid to this external system. Or you want to log some information in the log file.
That is all for the article.
Have a good day!
Alexey Matveev
software developer
MagicButtonLabs
Philippines
1,574 accepted answers
1 comment