Jenkins Pipeline Get Rejecter Name instead of user id - jenkins

I have followed below link to reject /approve deployment request. But if i reject deployment i can only get user id. How can i get user name?
def user = err.getCauses()[0].getUser()
https://support.cloudbees.com/hc/en-us/articles/226554067-Pipeline-How-to-add-an-input-step-with-timeout-that-continues-if-timeout-is-reached-using-a-default-value?mobile_site=true

Try this:
import hudson.model.Cause
import hudson.tasks.Mailer
import hudson.model.User
def name = cause.getUserName()
println "name is ${name}"
def jenkins_id = cause.getUserId()
println "jenkins_id is ${jenkins_id}"
User u = User.get(jenkins_id)
def umail = u.getProperty(Mailer.UserProperty.class)
def email = umail.getAddress()
println "email is ${email}"
You may want to read the JavaDocs of the User class to see if the name is available.

I found solution to this as below
def user = err.getCauses()[0].getUser().getDisplayName()

Related

How do I send a link using the smtplib module? F string not working

email user specific developer jobs at a given location
import requests # for api
import smtplib # for emails
import auth
class Jobs:
URL = "https://jobs.github.com/positions" # base url for API
def _jobs_api(self): # get json data (jobs)
location = input("Where would you like to become a developer? \U0001f607\n") # location parameter
description = input("What type of developer are you interested in becoming? \U0001f608\n") # search term
response = requests.get(Jobs.URL,
headers={"Accept": "application/json"},
params={"location": location, "description": description} # query params
)
data = response.json()
return data
def _job_links(self):
data = self._jobs_api()
if data:
for job in data:
links = job['url']
return links
else:
print(f"Sorry, I was not able to find any {self.description} jobs in {self.location}...")
def send_jobs(self): # email auth
links = self._job_links()
smpt_object = smtplib.SMTP('smtp.gmail.com', 587)
smpt_object.ehlo()
smpt_object.starttls()
# use own authentication data
email = auth.email()
password = auth.password()
smpt_object.login(email, password)
# next, send the message
from_address = email
to_address = email
subject = "Developer Jobs"
message = "Here are some jobs you are looking for: {}".format(links)
msg = "Subject: "+subject+"\n"+message
print("Check your email for an update! \U0001f601")
smpt_object.sendmail(from_address, to_address, msg)
smpt_object.quit()
user = Jobs()
user.send_jobs()
I'm trying to use the line Here are some jobs you are looking for: {}".format(links) (or an f string preferably) to send the links found from the API. But, when I check my email, it doesn't show the message with the links.
You can try to send a html structured mail in python to send the url you want.
See this post to find out

I need a list of all topics subscribed by a user in grails version 3.3.9 having domain of subscription ,topic,users

def subscribedTopicOfUser(){
Users loggedInUser = Users.findByUserName(session.userSession)
println(loggedInUser)
def val=loggedInUser.id
def subscribedTopicOfUser = Subscription.findAllWhere(loggedInUser : loggedInUser)
println (subscribedTopicOfUser)

How to get push value key in Firebase Ruby REST wrapper

I am working on a project to do CRUD Operations to firebase. I made use of this to help facilitate and link my ruby project to firebase.
Functions:
def delete_firebase(event_params,rootpath="Events/")
query = init_firebase.delete(rootpath,event_params)
end
def new_firebase(event_params,rootpath="Events")
query = init_firebase.push(rootpath,event_params)
end
def init_firebase # Inits firebase project with URL and secret
firebaseURL = "myfirebaseprojecturl"
firebaseSecret = "myfirebasesecret"
firebase = Firebase::Client.new(firebaseURL, firebaseSecret)
end
Event params consist of my event parameters as shown below
def event_params
params.require(:event).permit(:eventID, :eventName, :attachment, :eventNoOfPpl, :eventAdminEmail, {eventpics: []})
end
I encountered an issue. When I push with push() into firebase, there is a random key like -LSFOklvcdmfPOWrxgBo. In such case, the structure of the document would look like this:
But I cannot delete anything from -LSFOklvcdmfPOWrxgBo as I do not have the value. I used delete() from Oscar's firebase-ruby gem. I would appreciate any help with this issue.
I re-read the gem docs, and got some help from my friends and came up with two solutions
The body's response has response.body # => { 'name' => "-INOQPH-aV_psbk3ZXEX" } and thus, you're able to find out the name if you'd like
Change the index, and don't use .push, instead I made use of .set and did a random number for every event
Final solution
def load_firebase(root_path = "Events")
firebase_json = init_firebase.get(root_path)
if valid_json?(firebase_json.raw_body)
#json_object = JSON.parse(firebase_json.raw_body)
end
end
def update_firebase(event_params, root_path = "Events/")
init_firebase.update("#{root_path}#{event_params["eventID"]}", event_params)
end
def delete_firebase(event_params, root_path = "Events/")
init_firebase.delete("#{root_path}#{event_params["eventID"]}")
end
def save_firebase(event_params, root_path = "Events/")
init_firebase.set("#{root_path}#{event_params["eventID"]}", event_params)
end

attribute writer display password

I'm just starting to learn ruby on rails and using
http://ruby-for-beginners.rubymonstas.org/writing_classes/attribute_writers.html
The article doesn't explain how to display the password
Here is my code.
class Person
def initialize(name)
#name = name
end
def name
#name
end
def password=(pass)
#password = pass
end
def greet(other)
puts "Hi " + other.name
puts "Your password is " + #(how do i call password here?)
end
end
person = Person.new("Lee")
person.password = ("super secret")
person.greet(person)
p person
I'm not understanding attribute writers at this point.
Can anyone help me?
Thank you so much
While you have already set class variable #password = pass
You are able to call it as #password, remember if password wasn't set, it will give an error.
def greet(other)
puts "Hi " + other.name
puts "Your password is #{#password}"
end
person = Person.new("Lee")
person.greet(person) #will give an error
person.password = "pass"
person.greet(person) #will print the password

How to render Html template with info from database in Services

I am having dificulties to do the following:
Having a job that runs from time to time (Done)
Fetching some data from database inside that job (Done)
Next step would be use that data with a template that I already created (HTML) and then render it to PDF
Then send an email with that pdf in attachment
This is my current job:
def execute() {
println("Hi, I am a schedule Job triggering every half an hour from 7 AM to 22 PM that will never stop running until master jose tells me to :)")
EventService.updateOffersdwh()
EventService.getUncheckedOffers()
}
which is working properly :)
Those are my service's methods called in the previous job:
def updateOffersdwh(){
def sql = Sql.newInstance("jdbc:postgresql://"+hostname+":5432/xyz", username, password, "org.postgresql.Driver")
sql.rows(" Select From bird_admin.updt_offerorders('2018-03-07','2018-03-07','" + database + "') ");
println('Updating Order and Offers datawarehouse...');
}
def getUncheckedOffers(){
def sql = sql.newInstance("jdbc:postgresql://"+hostname+":5432/xyz", username, password, "org.postgresql.Driver")
def query = "select id, poo_id, b.name, b.address_line1, b.address_line2, b.zipcode, event_date::date, task_name, sku_id, sku_name, column_type, column_value, instance_name,checked FROM bird_admin.ooisdwh a LEFT JOIN sd_bel.cfg_point_of_operation b ON a.poo_id = b.id WHERE checked = false"
def offers = sql.rows(query)
println('DEBUG OFFERS FINAL:: '+offers)
}
Can anyone please advice or guide me on the next steps?
How can I use a template in html that I already have in my views\template folder?
What is the best way to use the info from the database and render the html with that info?
Should I use a controller? If so, how? I'm kinda lost now :|
I'm not 100% sure if you want to use the template for the body of the email or the attachment, but to use a template for the body of the email you can do the following.
First add the dependency for the Grails mail plugin:
build.gradle
dependencies {
compile "org.grails.plugins:mail:2.0.0"
...
}
Then in your service generate the body using the template and send like:
class YourService {
def groovyPageRenderer
def mailService
def getUncheckedOffers(){
def sql = sql.newInstance("jdbc:postgresql://"+hostname+":5432/xyz", username, password, "org.postgresql.Driver")
def query = "select id, poo_id, b.name, b.address_line1, b.address_line2, b.zipcode, event_date::date, task_name, sku_id, sku_name, column_type, column_value, instance_name,checked FROM bird_admin.ooisdwh a LEFT JOIN sd_bel.cfg_point_of_operation b ON a.poo_id = b.id WHERE checked = false"
def offers = sql.rows(query)
def content = groovyPageRenderer.render( view: "/aViewDirectory/mail",
model:[offers : offers ] )
mailService.sendMail {
to 'anEmailRecipient#somewhere.com'
subject "Email subject"
html( content )
}
}
There's a section in the email plugin docs about attachments.
You can follow the steps above to generate a PDF from a template and attach as per the docs.

Resources