Difference between revisions of "GitHub Pull Requests"

From Lingoport Wiki
Jump to: navigation, search
(GitHub WebHook)
(How to Configure Pull Request Analysis)
(46 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
Files can be committed in a Git branch or directly in Master. When committing files in a branch, pull requests let you know what changes you've made to a repository before they are committed to the repository's master branch. Once a pull request is sent, interested parties can review the set of changes, discuss potential modifications, and even push follow-up commits if necessary.
 
Files can be committed in a Git branch or directly in Master. When committing files in a branch, pull requests let you know what changes you've made to a repository before they are committed to the repository's master branch. Once a pull request is sent, interested parties can review the set of changes, discuss potential modifications, and even push follow-up commits if necessary.
   
  +
You can configure Globalyzer to scan the files that changed between the two branches of a pull request, and post a Globalyzer Analysis Summary on the pull request in Github.
= SonarQube GitHub Pull Request Plugin =
 
The SonarQube GitHub Plugin serves only one purpose: analyse GitHub pull requests in a preview mode, and inlay a summary analysis within the Pull Request's comments. This summary analysis showcases the top Globalyzer and LRM issues detected from scan. The latest version of this plugin is found [https://sonarsource.bintray.com/Distribution/sonar-github-plugin/ here]. Further documentation can be found [http://docs.sonarqube.org/display/PLUG/GitHub+Plugin here].
 
:: '''Note:''' After downloading the latest version of the plugin, place the <code>sonar-github-plugin.jar</code> file in your <code>.../Dashboard-Server/extensions/plugins</code> directory.
 
 
= Dashboard and Pull Requests =
 
Globalyzer Dashboard is based on SonarQube and leverages that platform's feature. Starting with <b>Lingoport Dashboard 5.1.2</b>, the GitHub Plugin is available. It allows to show Dashboard issues within GitHub as comments of the pull request. Globalyzer and LRM issues can then be detected and shown on the code changes before the pull request is merged with the main (master) branch. Therefore this GitHub feature is only to be used on working branches and never on master.
 
Sonar Scanner is used to push Pull Request issues to GitHub.com and to push issues on the master branch to the Globalyzer Dashboard. '''However the differences between these two workflows is very imperative to the capability of these tools! Within your ''sonar-project.properties'' file, publish mode should only be configured/run on your master branch and preview mode should only be configured/run on a working branch.'''
 
 
====Example Dashboard and Pull Request Scanning Workflows====
 
:::[[File:NewPrScanFlow2.png|caption|This diagram showcases the differences between scanning on a master branch ('''publishing''' to dashboard) and scanning on a pull request ('''previewing''' to GitHub.com).]]
 
:This diagram showcases the differences between scanning on a master branch ('''publishing''' to dashboard) and scanning on a pull request ('''previewing''' to GitHub.com).
 
   
 
== Example of a Pull Request Analyzed with Globalyzer ==
 
== Example of a Pull Request Analyzed with Globalyzer ==
   
[[File:PrSummaryAnalysis.png]]
+
[[File:PrNewSummaryAnalysis.png]]
   
This is pull request <b>#44</b>. The change to the code in the pull request was to add multiple embedded strings and a locale-sensitive method to the files <code>ExampleMain.java</code> & <code>ExamplePanel.java</code>. This pull request is reporting 25 different issues, however since the <code>sonar-project.properties</code> attribute is set to <code>sonar.github.disableInlineComments=true</code>, the top ten issues (sorted by severity, by component, then by line) are put into a summary analysis as a pull request comment.
+
This is pull request <b>#8</b>. The change to the code in the pull request was to add a file containing one embedded string, one locale-sensitive method, one general pattern, and one static file reference to illustrate the different sections of the analysis summary. At the top of the analysis, are issue totals. Then the issues are displayed by type, with the top (up to) ten issues displayed.
   
Globalyzer analysis flagged these lines as critical issues. Clicking on the link next to the issue number will send the user to the line of code in that respective file.
+
Clicking on the file#line link will send the user to the line of code in that respective file. If an issue has associated help, as in the case for most locale-sensitive methods, the issue will appear as a link and clicking the issue will display the associated help.
   
Before merging the code back to master, the developer would typically externalize these strings, fix the locale-sensitive method issue, and re-submit the changed code / resource files for further analysis.
+
Before merging the code back to master, the developer would typically externalize embedded strings, fix locale-sensitive method issues, general pattern issues, and static file references and re-submit the changed code for further analysis.
   
  +
==How to Configure Pull Request Analysis==
====External Dashboard Link====
 
  +
<ol>
  +
<li><b>Configure github.properties</b></li>
   
  +
You must create the file <code>/var/lib/jenkins/Lingoport_Data/Dashboard/github.properties</code> with the following contents:
The GitHub pull request issues also showcase a <code>...</code> link to send the user to their dashboard server as configured in the <code>/sonar-runner-2.5.1/conf/sonar-runner.properties</code> file:
 
  +
github.login=<your github login>
* For Example: Changing the url attribute to <code>sonar.host.url=<nowiki>http://localhost:9010</nowiki></code> (your local Globalyzer Dashboard Server) will redirect the user to the following link to give a more detailed report of this unique issue:
 
  +
github.oauth=<your github token>
  +
<b>Note:</b> To create a token for yourself on Github, navigate to https://github.com, select '''Settings''' from login dropdown, select '''Developer settings''' on left, select '''Personal access tokens''', click the '''Generate new token''' button. Also note that your access token must have the repo scope selected.
   
:::::[[File:GitHubPullRequestLink.png]]
+
[[File:PullRequestRepos.png|600px]]
   
== Pull Request Analysis Configuration ==
+
<li><b>Create a Pull Request Jenkins Job</b></li>
Globalyzer Dashboard uses the SonarQube sonar-project.properties file for configuration. This file should be in your repositories base directory. For the i18n analysis of the pull request, the sonar-project.properties file needs the following attributes:
 
   
  +
Create a new Jenkins job named <b><YourRepoName>-PullRequest</b> by copying the template '''RepoName-PullRequest''' job.<br>
* <code> sonar.analysis.mode=preview</code>
 
  +
Look at the configuration for your new job and verify that <b>Trigger builds remotely</b> is checked and then set the authentication token; the default is HOOK. The token is the only item you need to configure for your new job.
** It is imperative this attribute is set to preview to work. If not set, it defaults to publish mode, and could create future issues for analysis on this pull request.
 
* <code> sonar.github.login= </code>
 
** Your GitHub login name
 
* <code> sonar.github.oauth= </code>
 
** GitHub oauth is your unique GitHub personal access token: created @ https://github.com/settings/tokens
 
* <code> sonar.github.repository= </code>
 
** The repository you are working in. If repository link is https://github.com/ursulaLingoport/indexing2, this attribute should be set to <code> sonar.github.repository=ursulaLingoport/indexing2 </code>
 
* <code> sonar.github.pullRequest= </code>
 
** The number of the pull request you plan to run an i18n analysis on.
 
* <code> sonar.github.disableInlineComments= </code>
 
** If set to true (highly recommended), issues will not be reported as inline comments but only in the global analysis summary in sorted order. False by default.
 
** This Wiki page is based around this attribute being set to '''true'''. [https://github.com/ecrawfordLingoport/LiteDemo/pull/44 Here is an example of a Pull Request when this attribute is set to '''false''' (issues appear as individual inline comments within the pull request).]
 
   
  +
<b>Note:</b> Your Jenkins user must have special permissions to configure the PullRequest job. If your job does not have the <b>Trigger builds remotely</b> option, it is likely that your Jenkins user does not have the necessary permissions. Please see [[LRM_Jenkins_Configure_System#Configure_Global_Security|here]] for information on Jenkins Global Security Configuration.
===GitHub Personal Access Token Security===
 
Since the sonar-project.properties file includes a personal access token (OAuth), this file cannot be pushed to a remote repository. This exposes the users personal access token to the public, and therefore will void the token. If this happens, you should receive an email from GitHub acknowledging the user of the exposure and the token's decommission. In order to avoid this, the user must avoid staging the sonar-project.properties file for commit. In order to do this, you can create a <code>.gitignore</code> file in your repository's root directory to ignore all files with the <code>.properties</code> extension. It is recommended to do this in your <code>master</code> branch, so all forked and branched repositories will not face this issue.
 
   
  +
<li><b>Create a Webhook for your Github Repository</b></li>
Here is an example <code>.gitignore</code> file that will prevent this issue from occurring:
 
   
  +
The Pull Request on GitHub needs to trigger a Webhook back to Lingoport's Jenkins instance. To do so:
[[File:GitIgnore.png]]
 
  +
* Navigate to the GitHub repository as an '''Admin''' account
  +
* In '''Settings''' tab for the GitHub repository (available if the account has '''Admin''' permissions), select '''Webhooks''' and the '''Add webhook''' button.
  +
* The Webhook should follow this pattern:
   
  +
Payload URL: http(s)://<b>JENKINS_URL</b>/jenkins/buildByToken/buildWithParameters/build?job=<b>YourRepoName</b>-PullRequest&token=<b>YourToken</b>
=== GitHub WebHook ===
 
The Pull Request on GitHub needs to trigger a Web hook back to Lingoport's Jenkins instance. To do so:
 
* Navigate to the GitHub project as an 'Administer' account
 
* In Settings for the GitHub project (available if the account has Administer permissions), add a Webhook
 
* The Web hook should follow this pattern:
 
   
  +
Content type: application/x-www-form-urlencoded
http://JENKINS_URL/jenkins/buildByToken/buildWithParameters/build?job=GROUP_NAME.PROJECT_NAME-PullRequest&token=HOOK
 
 
application/x-www-form-urlencoded
 
   
 
Pull Request event selected
 
Pull Request event selected
Line 74: Line 52:
 
For example:
 
For example:
   
[[File:GitHubWebhook.png|size 300px]]
+
[[File:GitHubWebhook.png|500px]]
 
* Test the Web hook and especially check for firewall issues going back to the Jenkins system
 
 
=== Sample sonar-project.properties file: ===
 
 
For instance, here is a test sonar-project.properties with some properties modified for security reasons:
 
<pre>
 
sonar.projectKey=Lingoport.indexing2:scan
 
sonar.projectName=Lingoport.indexing2
 
sonar.projectVersion=5.1
 
sonar.sources=C:/Users/Joe/Documents/GitHub/indexing2
 
sonar.importSources=true
 
sonar.lingoport.project.root=.
 
sonar.language=lport
 
 
#sonar.lingoport.extensions=as,mxml,asp,asax,ascx,ashx,aspx,awk,c,c++,cc,cpp,cxx,h,hpp,hxx,sqc,sqx,qml,cs,dfm,dpk,dpr,pas,java,jsp,jspf,js,perl,pl,pm,plx,inc,php,sql,sqc,sqx,bas,cls,ctl,dob,
 
dsr,frm,pag,vb,vbs,htm,html,shtml,vm,html5,xml,xsd,xsl,xslt,wsdl,wsdd,rmd,tld,xliff,xul,xhtml,mxml,jspx,xaml,css,cat,resx,msg,po,properties,rc,rc2,strings,json,ts,rxml,rjs,properties
 
 
#
 
# For the GitHub SonarQube Plugin
 
#
 
sonar.analysis.mode=preview
 
sonar.github.login=ursulaLingoport
 
sonar.github.oauth=d60bbe9d6d3c7caa995f5119ff2997b26d0ef191
 
sonar.github.repository=ursulaLingoport/indexing2
 
sonar.github.pullRequest=6
 
sonar.github.disableInlineComments=true
 
</pre>
 
 
===Sonar-Runner and GlobalyzerLite Paths/Aliases===
 
Instead of having to call the path of your respective program/script each time (I.E. - <code>java -jar "C:\lingoport\globalyzer-lite-4.8.5\globalyzer-lite.jar" GzProjectDefinition.xml</code>), you can create command-line aliases to run these commands quickly and more efficiently (I.E. - <code>lite GzProjectDefinition.xml</code>)
 
 
====Windows====
 
*Create an alias for GlobalyzerLite (running the doskey command is a quick way to create an alias on windows)
 
**<code>doskey lite=java -jar C:\lingoport\globalyzer-lite-4.8.5\globalyzer-lite.jar</code>
 
*Add sonar-runner to your path if you have not already, to allow the scanner to be run within any directory with the simple command <code>sonar-runner</code>
 
**<code>set PATH=%PATH%;C:\path\to\sonar-scanner-2.5.1</code>
 
 
====Unix====
 
*Create an alias for GlobalyzerLite (this command adds the alias to the end of your .bashrc file)
 
**<code>echo 'alias lite="java -jar /destination/to/globalyzer-lite-5.0/globalyzer-lite.jar" ' >> ~/.bashrc</code>
 
*Add sonar-runner to your path if you have not already, to allow the scanner to be run within any directory with the simple command <code>sonar-runner</code>
 
**<code>echo 'export PATH=$PATH:/etc/sonar-scanner-2.5.1/bin' >> ~/.bashrc</code>
 
 
 
===Globalyzer Lite Project Definition File===
 
In order to successfully scan the project directory for analysis, the <code>ProjectDefinition.xml</code> file needs to be correctly configured in your project's root directory.
 
:[https://www.globalyzer.com/gzserver/help/referenceLite/project-definition-file-overview.html For help on this setup, click here.]
 
 
== Running the Analysis ==
 
* Checkout code from a branch (not master) or create a new branch
 
**<code> git checkout <-b> workingBranchName </code> (use the -b flag to create a new branch)
 
* Modify the code in your working branch
 
* Add, commit & push the code to the branch (not master)
 
** <code>git add .</code>
 
** <code>git commit -am "Commit Message"</code>
 
** <code>git push origin workingBranchName </code>
 
* Create a pull request on GitHub.com (your working branch --> master)
 
* Make sure <kbd>sonar.github.pullRequest=PullRequest#</kbd> within the project's sonar-project.properties file.
 
* Run a couple of commands:
 
** <code>java -jar "c:\lingoport\globalyzer-lite-4.8.5\globalyzer-lite.jar" GzProjectDefinition.xml</code> or <code>lite GzProjectDefinition.xml</code> (if you set an alias)
 
** <code>C:\sonar\sonar-runner-2.4-local\bin\sonar-runner</code> or <code>sonar-runner</code> (if you added sonar-runner to the machines PATH)
 
* Check the pull request comments on GitHub: The i18n issues found on the code in the pull request will be shown.
 
   
  +
* Test the Webhook and especially check for firewall issues going back to the Jenkins system
:::<b>Note</b>: The command <code>java -jar "c:\lingoport\globalyzer-lite-4.8.5\globalyzer-lite.jar" GzProjectDefinition.xml</code> is used to create a Globalyzer report under the directory <code>GlobalyzerScans</code>. It could be any commands to create that Globalyzer report. The sonar-runner then needs to be executed from above the <code>GlobalyzerScans</code> directory.
 
   
  +
</ol>
== Additional Information ==
 
http://stackoverflow.com/questions/32047585/jenkins-sonar-github-integration
 

Revision as of 17:52, 4 December 2019

GitHub

GitHub is a Web-based Git repository hosting service. It offers all of the distributed revision control and source code management (SCM) functionality of Git as well as adding its own features.

Git Pull Requests

Files can be committed in a Git branch or directly in Master. When committing files in a branch, pull requests let you know what changes you've made to a repository before they are committed to the repository's master branch. Once a pull request is sent, interested parties can review the set of changes, discuss potential modifications, and even push follow-up commits if necessary.

You can configure Globalyzer to scan the files that changed between the two branches of a pull request, and post a Globalyzer Analysis Summary on the pull request in Github.

Example of a Pull Request Analyzed with Globalyzer

PrNewSummaryAnalysis.png

This is pull request #8. The change to the code in the pull request was to add a file containing one embedded string, one locale-sensitive method, one general pattern, and one static file reference to illustrate the different sections of the analysis summary. At the top of the analysis, are issue totals. Then the issues are displayed by type, with the top (up to) ten issues displayed.

Clicking on the file#line link will send the user to the line of code in that respective file. If an issue has associated help, as in the case for most locale-sensitive methods, the issue will appear as a link and clicking the issue will display the associated help.

Before merging the code back to master, the developer would typically externalize embedded strings, fix locale-sensitive method issues, general pattern issues, and static file references and re-submit the changed code for further analysis.

How to Configure Pull Request Analysis

  1. Configure github.properties
  2. You must create the file /var/lib/jenkins/Lingoport_Data/Dashboard/github.properties with the following contents:

      github.login=<your github login>
      github.oauth=<your github token>
    

    Note: To create a token for yourself on Github, navigate to https://github.com, select Settings from login dropdown, select Developer settings on left, select Personal access tokens, click the Generate new token button. Also note that your access token must have the repo scope selected.

    PullRequestRepos.png

  3. Create a Pull Request Jenkins Job
  4. Create a new Jenkins job named <YourRepoName>-PullRequest by copying the template RepoName-PullRequest job.
    Look at the configuration for your new job and verify that Trigger builds remotely is checked and then set the authentication token; the default is HOOK. The token is the only item you need to configure for your new job.

    Note: Your Jenkins user must have special permissions to configure the PullRequest job. If your job does not have the Trigger builds remotely option, it is likely that your Jenkins user does not have the necessary permissions. Please see here for information on Jenkins Global Security Configuration.

  5. Create a Webhook for your Github Repository
  6. The Pull Request on GitHub needs to trigger a Webhook back to Lingoport's Jenkins instance. To do so:

    • Navigate to the GitHub repository as an Admin account
    • In Settings tab for the GitHub repository (available if the account has Admin permissions), select Webhooks and the Add webhook button.
    • The Webhook should follow this pattern:
      Payload URL: http(s)://JENKINS_URL/jenkins/buildByToken/buildWithParameters/build?job=YourRepoName-PullRequest&token=YourToken
    
      Content type: application/x-www-form-urlencoded
    
      Pull Request event selected
    
      Active
    

    For example:

    GitHubWebhook.png

    • Test the Webhook and especially check for firewall issues going back to the Jenkins system