|
| 1 | +## Jira issue validator |
| 2 | +In order to use this pipeline, you will need the following plugins: |
| 3 | + |
| 4 | +- [Pipeline](https://plugins.jenkins.io/workflow-aggregator): This plugin allows us to store our `Jenkins` _jobs_ as code, and moves away from the common understanding of Jenkins `builds` to an `Agile` and `DevOps` model |
| 5 | +- [Pipeline: Declarative](https://plugins.jenkins.io/pipeline-model-definition): Provides the ability to write _declarative pipelines_ and add `Parallel Steps`, `Wait Conditions` and more |
| 6 | +- [Pipeline: Basic Steps](https://plugins.jenkins.io/workflow-basic-steps): Provides many of the most commonly used classes and functions used in _Pipelines_ |
| 7 | +- [Pipeline: Job](https://plugins.jenkins.io/workflow-job): Allows us to define `Triggers` within our _Pipeline_ |
| 8 | +- [Pipeline: Utility Steps](https://plugins.jenkins.io/pipeline-utility-steps): Provides us with the ability to read config files, zip archives and files on the filesystem |
| 9 | +- [GitHub Integration](https://plugins.jenkins.io/github-pullrequest): Provides the ability to customize pull request builds |
| 10 | +- [Pipeline: GitHub](https://plugins.jenkins.io/pipeline-github): Allows using GitHub steps within a _Jenkinsfile_ |
| 11 | +- [GitHub](https://plugins.jenkins.io/github): Provides integration with GitHub |
| 12 | +- [Jira Pipeline Steps](https://plugins.jenkins.io/jira-steps): Allows using Jira steps within a _Jenkinsfile_ |
| 13 | +- [Jira](https://plugins.jenkins.io/jira): Enables integration with Jira |
| 14 | + |
| 15 | +### Configuring Jenkins |
| 16 | + |
| 17 | +1. Log in to Jenkins and click _Manage Jenkins_ |
| 18 | +2. Click _Configure System_ |
| 19 | +3. In the **Jira Steps** section, provide the required information for connecting to your Jira server |
| 20 | + |
| 21 | +4. In the **GitHub Pull Request Builder** section, fill out the connection information |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +### Creating the pipeline |
| 26 | +1. Log in to Jenkins and click _New Item_ |
| 27 | +2. Give it a name and select _Pipeline_ as the type |
| 28 | + |
| 29 | +3. Check the box to enable _GitHub Project_ and provide the URL for the repository |
| 30 | + |
| 31 | +4. Check the box to trigger on _GitHub Pull Requests_ |
| 32 | + 4a. Choose _Hooks with Persisted Data_ as the **Trigger Mode* |
| 33 | + 4b. Check the box to _Set status before build_ |
| 34 | + 4c. Add _Commit changed_ and _Pull Request Opened_ as the **Trigger Events** |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +### Example pipeline |
| 39 | +This pipeline functions by taking the _issue ID_ from the pull request body, performing a lookup in Jira, then setting the status of the build in GitHub based on the _transition_ in Jira. |
| 40 | + |
| 41 | +```groovy |
| 42 | +node { |
| 43 | + properties([ |
| 44 | + [$class: 'BuildDiscarderProperty', |
| 45 | + strategy: [$class: 'LogRotator', |
| 46 | + artifactDaysToKeepStr: '', |
| 47 | + artifactNumToKeepStr: '', |
| 48 | + daysToKeepStr: '', |
| 49 | + numToKeepStr: '5'] |
| 50 | + ] |
| 51 | + ]) |
| 52 | + stage('Validate JIRA Issue') { |
| 53 | + //echo sh(returnStdout: true, script: 'env') |
| 54 | + // Get the issue number from the PR Title |
| 55 | + def prTitleJira = sh( |
| 56 | + script: "echo \${GITHUB_PR_TITLE}|awk {'print \$1'}", |
| 57 | + returnStdout: true) |
| 58 | +
|
| 59 | + // Get the issue number from the PR Body |
| 60 | + def prBodyJira = sh( |
| 61 | + script: "echo \${GITHUB_PR_BODY}|awk {'print \$1'}", |
| 62 | + returnStdout: true) |
| 63 | +
|
| 64 | + // Convert the discovered issue to a string |
| 65 | + def prIssue = prBodyJira.trim() |
| 66 | +
|
| 67 | + // Validate that the issue exists in JIRA |
| 68 | + def issue = jiraGetIssue ( |
| 69 | + site: "JIRA", |
| 70 | + idOrKey: "${prIssue}") |
| 71 | +
|
| 72 | + // Validate the state of the ticket in JIRA |
| 73 | + def transitions = jiraGetIssueTransitions ( |
| 74 | + site: "JIRA", |
| 75 | + idOrKey: "${prIssue}") |
| 76 | +
|
| 77 | + // Create a variable from the issue state |
| 78 | + def statusId = issue.data.fields.status.statusCategory.id.toString() |
| 79 | + def statusName = issue.data.fields.status.statusCategory.name.toString() |
| 80 | +
|
| 81 | + // Validate that it's in the state that we want |
| 82 | + if (statusId == '4') { |
| 83 | + setGitHubPullRequestStatus ( |
| 84 | + context: "", |
| 85 | + message: "${prIssue} is in the correct status", |
| 86 | + state: "SUCCESS") |
| 87 | + } else { |
| 88 | + setGitHubPullRequestStatus ( |
| 89 | + context: "", |
| 90 | + message: "${prIssue} is not properly prepared in JIRA. Please place it in the current sprint and begin working on it", |
| 91 | + state: "FAILURE") |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Visual status |
| 98 | +1. Create a new file with a commit message. The Jira plugin will automatically comment on the ticket if you use the `JIRA-[number] #comment <comment>` format |
| 99 | + |
| 100 | + |
| 101 | +2. Create a new pull request, and be sure that `JIRA-[number]` is the first word in the _body_ |
| 102 | + |
| 103 | + |
| 104 | +#### Ticket is not _In Progress_ |
| 105 | + |
| 106 | + |
| 107 | +#### Ticket is _In Progress_ |
| 108 | + |
0 commit comments