I'm trying to send the currently logged-in user in a Post-Request to a REST-Endpoint when the issue is assigned. I set up a project automation with a ScriptRunner Script.
According to several sources (see below) the following should do the magic.
import com.atlassian.jira.component.ComponentAccessor
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
This snippet works, but it's always my user, that is sent. Doesn't matter if I'm logged in or my colleague. What am I missing?
The same problem occurs when I use the built-in field currentUser.
Sources:
https://community.atlassian.com/t5/Jira-questions/How-to-get-current-user-in-jira-application/qaq-p/818002
https://scriptrunner.adaptavist.com/5.2.1/jira/recipes/behaviours/scripted-conditions.html
https://innovalog.atlassian.net/wiki/spaces/JMWE/pages/138353228/Getting+the+current+user+-+Groovy
Related
We are continuously receiving user creation/removal in our JIRA instance as the team is growing.
I am looking for possible options to automate this using groovy script.
Currently, we are having a separate project in JIRA handling these requests. Team will raise the tickets with user name, required roles & Manager will be approve request in the project and then it will be picked by JIRA admin to create/remove the users.
Then JIRA admin will close the ticket.
I am looking to automate the JIRA admin work, Once the ticket is raised by team & moved to a certain status(like Approval), JIRA should create the users automatically. How this can be achieved using groovy script?
Thank you.
You should be able to use the Jira API to do what you want. The Jira REST API Reference has everything you need.
For user creation, a POST request to the rest/api/3/user endpoint should work while a DELETE request to the same endpoint should remove the user. This guide by Atlassian has detailed information on creating users via that endpoint.
The REST API reference also has endpoints to automatically transition the state of the Jira issue. A GET request to the /rest/api/3/issue/{issueIdOrKey}/transitions endpoint shows you all possible transitions for an issue while a POST request to the same endpiont allows you to transition the issue.
I have used below groovy script to create the user and add to particular group
import com.atlassian.jira.component.ComponentAccessor
def userName = "user2"
def groupNametoAdd = "jira-servicedesk-users"
def userToAdd = ComponentAccessor.getUserManager().getUserByName(userName)
def groupName = ComponentAccessor.getGroupManager().getGroup(groupNametoAdd)
if (!userToAdd) {
log.error("User doesn't exist")
return
}
if (ComponentAccessor.getGroupManager().getGroupsForUser(userName).contains(groupName)) {
log.error("User: $userToAdd.username already in the group: $groupName.name")
return
}
if(!groupName) {
log.error("Group doesn't exist")
} else {
ComponentAccessor.groupManager.addUserToGroup(userToAdd, groupName)
log.error("User: $userToAdd.username added to the group: $groupName.name")
}
I have a site dashboard on GAE standard node.js environment that uses login: admin option in handlers: element in my app.yaml file. This option makes google login window appear before accessing the site. Is it possible to get email and profile of user logged in?
Updates on Feb 2
Tested on my local server
I added login:required in app.yaml; it's a Python3 App but I did not use users api; I start the app with dev_appserver.py to make sure it uses the app.yaml file in the development environment; When I loaded the home page, it prompted me to sign in with test#example.com
I dumped the header output and it included the following
X-Appengine-User-Email: test#example.com
X-Appengine-User-Id: XXXXX
Original Answer
You can use the 'users' Api and call the function GetCurrentUser()
Example, if your code was in python, you'd have something like
from google.appengine.api import users
# Get information about the currently logged in user
users.GetCurrentUser()
# Extra - if you wanted to confirm this is an admin user
users.IsCurrentUserAdmin()
Since you're not using Python, see if the environment variable for USER_EMAIL exists. The source code for users api has the snippet below (so see if it works for you)
email = os.environ.get('USER_EMAIL', email)
_user_id = os.environ.get('USER_ID', _user_id)
I started learning Cucumber (Behavior Driven Testing Framework) and based on certain videos and some reading, I understood that Cucumber will auto generate a skeleton code for the step definitions, defined in feature file. However, I do not see any test being executed or any code being generated. My project setup is as below
Project
----src/main/java/testrunner/MyTestRunner.java
----src/main/resources/feature/dailyroutine.feature
Feature file is looking like below
Feature: Test Facebook smoke Scenario
Scenario: Test with valid credentails
Given: Open firefox and start application
When: when I enter with valid username and password
Then: I should be able to login into Facebook Homepage
TestRunner is as below
package testrunner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features ={"src/main/resources/feature/dailyroutine.feature"},
dryRun=true,
strict=true,
monochrome=true)
public class MyTestRunner {
}
When I run the TestRunner as JUnit I'm getting
0 Scenarios
0 Steps
0m0.000s
What is the mistake I'm doing
I would like to propose an alternative feature file scenario steps
Feature Test Facebook smoke Scenario
Scenario Success login with valid credentails
Given Using the browser "firefox"
And Username is "myUsername" with Password "myPassword"
When Submit login request
Then Successfully logged in to Facebook
The problem could be how the feature file is worded, namely the extra colons for the Given, When and Then.
I've also made changes to how the feature file has been written, in order to better inform the reader about what the test is trying to achieve - as the #1 thing that Behaviour Driven Development (and the frameworks for it) focus on is communication between the development team and the business.
Feature: Logging into Facebook...
Scenario: with valid credentials
Given I navigate to "Facebook"
When I log in with my valid login details
Then I should be able to see my newsfeed
There may be other issues, but for the time being, the colons are the issue that sticks out to me
Edit
I should have read the comments section, because this was solved 2 days before this answer
Remove the colon (:) after the keywords (Given, When, etc) in your feature file.
I'm a programmer who is just getting started working with groovy in Jira in order to automate some tasks.
I'm trying to write a custom listener script using the inline editor in Jira, but haven't gotten past trying to get a Hello World program to work.
I don't know if the script is running, and can't see any output, and I really need some help with figuring out how to debug the script, preferably through outputs to some kind of console (or even just by reading the Jira logs if necessary), just so that I can actually start trying to learn how to use this tool.
I'm working with the information HERE as a general guideline to start learning to work with the inline editor.
For a little more context, you can see another related question that I asked HERE.
I've set the debug level to DEBUG for the event which I'm attaching the listener, as shown in this screenshot, based on the information found HERE:
Here is a screenshot of the inline editor I'm working in in JIRA. In this screenshot, I'm just trying to output 'Hello', and have just clicked the 'Preview' button:
As you can see, in the 'Result' tab at the bottom of the screen, there is nothing of interest. The 'Logs' tab is also empty, and the 'Timing' tab just says 'Elapsed: 0 ms CPU time: 0 ms', so it seems like nothing if happening.
If I check the log on the server (in the file catalina.2017-10-13.txt), I see the following output:
13-Oct-2017 07:01:50.942 WARNING [http-nio-8080-exec-6] com.sun.jersey.spi.container.servlet.WebComponent.filterFormParameters A servlet request, to the URI http://somevmserver:8080/rest/scriptrunner-jira/latest/listeners/com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener/params, contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using #FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
13-Oct-2017 07:02:26.740 WARNING [http-nio-8080-exec-12] com.sun.jersey.spi.container.servlet.WebComponent.filterFormParameters A servlet request, to the URI http://somevmserver:8080/rest/scriptrunner/latest/canned/com.onresolve.scriptrunner.canned.common.StaticCompilationChecker, contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using #FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
13-Oct-2017 07:02:26.974 WARNING [http-nio-8080-exec-1] com.sun.jersey.spi.container.servlet.WebComponent.filterFormParameters A servlet request, to the URI http://somevmserver:8080/rest/scriptrunner-jira/latest/listeners/com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener/preview, contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using #FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
This output doesn't mean a whole lot to me, but it seems apparent that it's being populated as a result of trying to preview the script.
I'm not getting any errors in the inline editor, and it's really simple code, so I don't think it's that.
The only other information I can include that I think is pertinent is that this is a test instance of Jira cloned from our production environment, and its base URL is still set to the URL of the prod environment. Not sure if that has any bearing, but I'm not really a Jira admin, just the programmer tasked with doing this, so I don't want to go fiddling around where I don't need to.
Thanks!
When using scriptrunner within jira, you'll need to import the logger to use the debugger or to output to the console. This can be done with the following:
// Enable debugger
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)
And then, you'll be able to see the logged information using log.debug "hello"
To see your debug message "Hello" in the log, you must update a issue in your selected project. The Result, Logs and Timing Tabs at the bottom are useless in this view. Just trigger the Listener with a issue update in your selected project and search your debug message in the atlassian-jira.log file.
Hint: To view the Log in the browser you can use this jira app https://marketplace.atlassian.com/plugins/com.cps.lastLog/server/overview
I want to get the username and/or email address of the build requestor in a post-build script.
Sidenote: I want the requestor so I can set the email sender of the post-build email notification dynamically in the email-ext plugin's pre-send script.
AbstractBuild has built-in support for AbstractBuild.getCulprits() and AbstractBuild.hasParticipant(User user) but I can't find a method for retrieving the requestor. I can't find any useful reference in reference list to the User class either.
I managed to solve it via the Cause of the the build, as recommended in this answer.
The reason that the build requestor can be found in the cause for a build makes perfect sense when you think about it: Not every build is directly triggered by a user.
If it is triggered by a user, the list of causes for the build contains a Cause.UserIdCause, with the user's id and name.
This code snippet worked for me. It extracts the username from the build via the cause and sets the From and ReplyTo headers. I use it in the pre-send script of the email-ext Jenkins plugin.
import javax.mail.internet.InternetAddress
cause = build.getCause(hudson.model.Cause.UserIdCause.class);
username = cause.getUserName()
id = cause.getUserId()
email = new InternetAddress(String.format("%s <%s#example.com>", username, id))
msg.setFrom(email)
msg.setReplyTo(email);