Skip to content

Conversation

@yiftahw
Copy link
Contributor

@yiftahw yiftahw commented Oct 22, 2024

many thanks to @anton-johansson at #901 and @jakub-bochenski at #646 for starting this work
closes #900

Done

  • rebased work from Provide merge request labels as environment variables #901 to latest master branch
  • provided test cases at GitLabEnvironmentContributorTest.java
  • added pipeline helper function GitLabMergeRequestLabelExists("some label") -> Boolean
  • added explanation to README.md
  • provided test cases at GitLabMergeRequestLabelExistsStepTest.java
  • indentation fix in src/docker/README.md

groovy example of using the helper function:

if (GitLabMergeRequestLabelExists("test label")) {
    echo "test label found"
}

groovy example of echoing all the labels:

// Check if the environment variable is null (should never be empty - see unit tests)
if (env.gitlabMergeRequestLabels == null) {
    echo "No merge request labels provided."
} else {
    // Split the labels by commas and trim each label
    def labels = env.gitlabMergeRequestLabels.split(',').collect { it.trim() }
    echo "Merge request labels:"
    labels.each { label -> echo label }
}

Testing done

  • test set up:
    • OpenSUSE Leap 15.6 VM
    • Java 17 (openjdk)
    • Maven 3.9.9
    • docker running inside the VM
      • changed the Jenkins tag to 2.479 (with lts I got an error when trying to install the plugin: Jenkins minimum version required: 2.479)
      • GitLab tags tested: latest (17.5), 15.11.13-ce.0
  • built the plugin with mvn deploy and manually installed to Jenkins with the web UI.
  • created a pipeline in Jenkins, integrated it to GitLab and observed the behavior of new environment variable and helper function in push and merge request hooks.

Note

  • @anton-johansson provided a note in his merge request Provide merge request labels as environment variables #901 that this doesn't work when putting comments to merge requests, because the webhook doesn't contain the labels.
  • using the GitLab Jenkins Integration (under Settings -> Integrations -> Jenkins) doesn't send webhooks on comments.
  • using a generic web trigger (under Settings -> Webhooks) allows sending hooks on comments, and according to the oldest doc online - GitLab 15.6 Webhook events it does send the labels on comments in merge requests.
  • the class NoteHook doesn't handle the labels

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

@github-actions github-actions bot added documentation Improvements or additions to documentation tests This PR adds/removes/updates test cases labels Oct 22, 2024
@yiftahw yiftahw changed the title Feature add labels to env Add merge request labels to environment variables Oct 22, 2024
@yiftahw yiftahw marked this pull request as ready for review October 22, 2024 09:17
@yiftahw yiftahw requested a review from a team as a code owner October 22, 2024 09:17
@yiftahw yiftahw requested a review from krisstern October 23, 2024 09:20
Copy link
Member

@krisstern krisstern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @yiftahw, I think this implementation looks good but maybe we could add some instructions on how to use this feature before I can accept and merge this?

@krisstern
Copy link
Member

You can basically just copy and past what you have in the description to the README we have at the root

@yiftahw
Copy link
Contributor Author

yiftahw commented Oct 23, 2024

You can basically just copy and past what you have in the description to the README we have at the root

@krisstern thinking about what to put in the README, I thought to myself that people that will want to use this feature will probably suffer from some boilerplate of parsing the string held by the environment variable...
I'm working now on adding a function called GitLabMergeRequestLabelExists("some label") -> Boolean
I'm kind of copying the mechanism used in AcceptGitLabMergeRequestStep
your thoughts?

example pipeline:

script {
    if (GitLabMergeRequestLabelExists("bugfix"))
    {
        echo 'bugfix label detected!'
    }
}

instead of:

script {
    if (env.gitlabMergeRequestLabels) {
        def labels = env.gitlabMergeRequestLabels.split(',').collect { it.trim() }
        if ('bugfix' in labels) {
            echo 'bugfix label detected!'
        }
    }
}

I'm moving this to draft as I need to add some unit-tests but form initial testing with the docker setup it looks to be working fine

@yiftahw yiftahw marked this pull request as draft October 23, 2024 13:35
@krisstern
Copy link
Member

Hi @yiftahw, regarding:

I'm working now on adding a function called GitLabMergeRequestLabelExists("some label") -> Boolean
I'm kind of copying the mechanism used in AcceptGitLabMergeRequestStep
your thoughts

Sounds like a reasonable approach to me! Please go ahead with the implementation.

@yiftahw yiftahw marked this pull request as ready for review October 24, 2024 09:12
@yiftahw yiftahw requested a review from krisstern October 24, 2024 09:12
@yiftahw yiftahw marked this pull request as draft October 24, 2024 09:33
@yiftahw yiftahw marked this pull request as ready for review October 24, 2024 09:33
Copy link
Member

@krisstern krisstern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more changes before we are good to go

yiftahw and others added 2 commits October 24, 2024 14:46
Co-authored-by: Kris Stern <krisstern@outlook.com>
@krisstern
Copy link
Member

krisstern commented Oct 24, 2024

Please apply mvn spotless:apply to fix the spotless issues

Copy link
Member

@krisstern krisstern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@krisstern
Copy link
Member

Hi @yiftahw the PR looks good now, thanks for your contribution! I will let it sit for between 24 and 48 hours before I merge it if there is no objection from the community.

@yiftahw
Copy link
Contributor Author

yiftahw commented Oct 25, 2024

@krisstern before merging, I have some changes that close #1715 , they are pretty minor, should I push these changes here and you'll review them?
you can view them here
TL;DR now both MergeRequestAttributes and MergeRequestHook have a member List<MergeRequestLabel> labels, which more accurately models the webhook payloads. (before that, only MergeRequestHook held the labels)

@krisstern
Copy link
Member

Hi @yiftahw I think it may be better to create another PR to address issue #1715. I can review that and merge it before cutting a new release.

@krisstern krisstern merged commit b661e62 into jenkinsci:master Oct 25, 2024
@yiftahw yiftahw deleted the feature-add-labels-to-env branch November 12, 2024 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation hacktoberfest-accepted tests This PR adds/removes/updates test cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessing labels in build

2 participants