Github-plugin for Jenkins get committer and author name - github

If I understand well, git plugin exposes committer and author names and emails to environmental variables GIT_AUTHOR_NAME, GIT_COMMITTER_NAME, GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL based on the global configuration of git. Is there a way to get that info using Github-plugin? Does Github-plugin exposes payload info, getting from github-webhook, to environmental variables or to something else?

In reality these variables are available just when you overwrite the Author Name and Author Email on the Advanced features of the SCM configuration.
"Additional Behaviours" -> "Custom user name/email address"
This is described on the source code:
https://github.com/jenkinsci/git-plugin/tree/master/src/main/java/hudson/plugins/git
Solution: In order to retrieve the author name and email I suggest scripting this:
GIT_NAME=$(git --no-pager show -s --format='%an' $GIT_COMMIT)
GIT_EMAIL=$(git --no-pager show -s --format='%ae' $GIT_COMMIT)
Being $GIT_COMMIT the SHA1 commit id.

You can use this workaround in your scripted pipeline file:
env.GIT_COMMITTER_EMAIL = sh(
script: "git --no-pager show -s --format='%ae'",
returnStdout: true
).trim()

You can try for below command, it worked for me:
git log -n 1 --pretty=format:'%ae'

You need check who is contributing this variables, github plugin only triggers git build that runs Git SCM (that is git-plugin). This variables probably injected by git-plugin.

Related

Pull Request "reviewers" using github "history"

Is there any way (for on premise github) to :
For N number of files in the Pull Request.
Look at the history of those files.
And add any/all github users (on the history) .. to the code reviewers list of users?
I have searched around.
I found "in general" items like this:
https://www.freecodecamp.org/news/how-to-automate-code-reviews-on-github-41be46250712/
But cannot find anything in regards to the specific "workflow" I describe above.
We can get the list of changed files to the text file from PR. Then we can run the git command below to get the list of users included in last version's blame. For each file we get from file list, run the blame command. This might be also simple script.
Generate txt file from list of files of PR.
Traverse all filenames through txt file. (python, bash etc.)
Run blame command and store in a list.
Add reviewers to the PR from that list manually or some JS script for it.
For github spesific: list-pull-requests-files
The blame command is something like :
git blame filename --porcelain | grep "^author " | sort -u
As a note, if there are users who are not available in github anymore. Extra step can be added after we get usernames to check whether they exist or not. (It looks achievable through github API)

How do I get the last commit programmatically in Java code? Jenkins / sbt

I started writing a little tool that basically can do something (ex. compile or test code) and then send an email if it fails.
https://github.com/JohnReedLOL/EmailTestingBot
I want to add a feature where this tool can programmatically look at the last commit in the working directory, look at the author of the commit, extract their email, and then email that person whether their commit resulted in a pass or a failure.
For example, I want it to do something like: Git: See my last commit
Where the email basically says:
Subject: Test Results
Message: All your tests passed in dev for commit 0e39756383662573.
Does Jenkins provide this functionality already? I want to make my setup email the person who put in the most recent commit.
Also, is there a way I can obtain the email of the author of the most recent commit programmatically (ex. perhaps with http://www.eclipse.org/jgit/ or http://javagit.sourceforge.net )?
I don't really care how I get email notifications - I just want them and I can't use TravisCI.
I will try to give solutions part by part.
Part 1 :
Yes, you can run ShellScript(Shell Commands) from Jenkins Link.
Part 2
How to get the Email Id and other Stuff from GitCommit.
For that Jenkins sever should have git command installed in build server.
Create one conf file ex. /conf/reference which have
app {
git {
commit = "CURRENT_COMMIT"
repo = "CURRENT_REPO"
timestamp = "CURRENT_TIMESTAMP"
emailId = "EMAIL_ID"
}
}
When making your build run the command
sed -i'' "s/CURRENT_COMMIT/$(git rev-parse HEAD)/g" conf/reference.conf
sed -i'' "s^CURRENT_REPO^$(git config --get remote.origin.url)^g" conf/reference.conf
sed -i'' "s/CURRENT_TIMESTAMP/$(git show -s --format=%ci HEAD)/g" conf/reference.conf
sed -i'' "s/EMAIL_ID/git --no-pager show -s --format='%an <%ae>' CURRENT_COMMIT/g" conf/reference.conf
above code will put the values in reference.conf.
Now you can use to get the info and send the mail. As far as I know, Jenkins gives the capability to send the Email. Jenkins work on the environment variables rather than putting this into reference.conf you can put this in Environment variable and use the environment variables to send the mail.
FYI: I haven't tested this code but as far as I remember working in Jenkins, we used to send email through this way.
#HappyCoding

SCM environment variables missing

Usually, when using SCM like the Git Plugin, there are a bunch of environment variables that you can use (e.g. see these)
But neither the Git Step nor the Generic SCM seem to do that.
Is there a way to get these variables into the groovy env.* so that they can be used?
Something like this would be useful:
def commitMessage = sh 'git log --max-count=1 --oneline --no-merges | cut -b9-'
I can think of writing the results to a file and read them via the readFile() mehtod -- but is there an easier way to achieve this?
For the Record: I have the following code to get the branch-name:
stage 'preparation'
node {
// checkout branch
git branch: 'origin/master', url: 'git#example.net:project.git'
// write current branch-name to file
sh 'git branch -a --contains `git rev-parse HEAD` | grep origin | sed \'s!\\s*remotes/origin/\\(.*\\)!\\1!\' > git-branch.txt'
// read data from file into environment-variable
env.gitBranch = readFile('git-branch.txt').trim()
// let people know what's up
echo "testing branch ${env.gitBranch}"
}
The remainder of the flow-script is comprised of serveral parametrized jobs which get the env.gitBranch passed as parameter (among others, if needed).
Be sure to allow concurrent builds for the workflow to catch every updated branch.
See JENKINS-24141; these variables are not yet available from Workflow.
In the meantime, you are on the right track: run a git command to record any information you need, and use readFile to load it (see also JENKINS-26133).

How can I change my commit to correct user in github?

At the beginning, I used other's account to do my work on github. Then my account is added into the organization, so I changed my account by using:
git config --global user.email "me#here.com"
In the commit page or issue page of any branch, the author is right. But in my dashboard, the commits is not mine. How can I solve that? Thanks!
From the GitHub Help page "Set up Git":
Username
First you need to tell git your name, so that it can properly label
the commits you make.
$ git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit
You have only set your email, but first you should have set your name. That's probably why the author is not the right one in the dashboard.
EDIT
Since you already set your username as I suggested, you need to modify GIT_COMMITTER_NAME or GIT_AUTHOR_NAME variables.
In order to do so open a shell in your repository (if you're on Windows set the PowerShell as the predefined shell first from tools->options, so you can copy/paste in it) and type the following:
git filter-branch --env-filter '
Then open any texteditor and copy/paste the following in it:
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
if [ "$GIT_COMMITTER_EMAIL" = "your#email.to.match" ]
then
cn="Your New Committer Name"
cm="Your New Committer Email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "your#email.to.match" ]
then
an="Your New Author Name"
am="Your New Author Email"
fi
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'
Then modify the fields "cn", "cm", "an", "am" and the content of the two if statements accordingly, copy/paste this script into the shell and press enter twice.
This should solve your problem, but keep in mind that this is not a good practice when you share a repository with others, since it rewrites history.
See also the Troubleshooting information here and the GitHub Help page about changing author info.
On Mac, I had to change the credentials of the old user from the Keychain Access. Documented here

Setting SVN username (Author Name in Eclipse) when using svn+ssh

When I commit changes in Eclipse, svn records my author name as the one that I entered the first time I committed changes in Eclipse (Alok). By author name, I mean the name that shows up when you run "svn log" or "svn blame".
However, when I commit changes from the command line, the Author Name is set to the username that I use to ssh to the repository (svnadmin). Is there a way to set the equivalent of Author Name/svn username independently of the ssh username from the command line when using svn+ssh? I have tried
svn --username Alok ci
but the username in this case is ignored, and the change is attributed to svnadmin.
It is by design that you cannot change the username for svn+ssh. If you could, you would be able to fake somebody else as the committer - when the SSH key would normally clearly identify yourself as the committer.
So if you want different committer names to show up with svn+ssh, you need to change something on the server:
Create separate remote users, and put your key into the authorized_keys file for the user you want to appear as committer. Alternatively,
Put command= lines into the authorized_keys file of the svnadmin user. The command should read /usr/bin/svnserve -t --tunnel-user Alok; optionally also with a --root option.
One workaround is to first enable editing of revision tags by putting a shell script like the following in hooks/pre-revprop-change
REPOS="$1"
REV="$2"
USER="$3"
PROPNAME="$4"
if [ "$PROPNAME" = "svn:log" ]; then exit 0; fi
if [ "$PROPNAME" = "svn:author" ]; then exit 0; fi
exit 1
Then, after the commit you can change the svn:author with
svn propset --revprop -r1234 svn:author Alok
This does not explain how eclipse is able to set svn:author at commit time without having a pre-revprop-change hook. This solution is a little unsatisfying because it allows any user to change the svn:author of any commit, it would be nice to know what eclipse is actually doing.