From 5a3c87eb4b594dbf14d5a648464c4dd9014f221e Mon Sep 17 00:00:00 2001 From: Dawid Rabicki Date: Mon, 18 Jan 2021 14:33:36 +0100 Subject: [PATCH 1/2] Adding env var for versioning non mr commits. Adding alias to docker image. Updating README.md --- .gitignore | 3 +++ Dockerfile | 2 ++ README.md | 21 +++++++++++++++------ version-update.py | 31 ++++++++++++++++++++++++------- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 6079f13..b710502 100644 --- a/.gitignore +++ b/.gitignore @@ -202,4 +202,7 @@ tags # Ignore all local history of files .history +## PyCharm ## +.idea + # End of https://www.gitignore.io/api/git,vim,terraform,visualstudiocode,python \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 7154c7b..1970398 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,8 @@ COPY /requirements.txt . RUN pip install -r requirements.txt +RUN echo 'alias bump_version="python3 /version-update/version-update.py"' >> /root/.bashrc + COPY /version-update.py . CMD ["python", "/version-update.py"] diff --git a/README.md b/README.md index 6bcada1..13c4646 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,9 @@ Tip: You can use custom labels for minor and major bumps by setting the `MINOR_B ### API token and group -To extract the labels from merge requests, we need an API token to access the Gitlab API. Unfortunately, [GitLab doesn't yet support non-user specific access tokens](https://gitlab.com/gitlab-org/gitlab-ee/issues/756). +To extract the labels from merge requests, we need an API token to access the Gitlab API. ~~Unfortunately, [GitLab doesn't yet support non-user specific access tokens](https://gitlab.com/gitlab-org/gitlab-ee/issues/756).~~ -Ask your GitLab administrator to add a dummy user `${group_name}_npa` to GitLab with access only to your project group. Log in with this user, and create a [personal access token](https://gitlab.wbaa.pl.ing.net/profile/personal_access_tokens) with api scope access. +Ask your GitLab administrator to add a dummy user `${group_name}_npa` to GitLab with access only to your project group or type in any name You like. Log in with this user, and create a [personal access token](https://gitlab.wbaa.pl.ing.net/profile/personal_access_tokens) with api scope access. Copy the generated API token and keep it available for the next section. @@ -41,6 +41,10 @@ Add the following variables: |-----------------|----------------------------------------------------------------------| | NPA_USERNAME | The name of the NPA user created for your group: `${group_name}_npa` | | NPA_PASSWORD | The personal access token with API scope generated for the NPA user | +| MINOR_BUMP_LABEL*| Alternative value for bumping minor version | +| MAJOR_BUMP_LABEL*| Alternative value for bumping major version | +| SKIP_DIRECT_COMMITS*| For versioning non MR commits set to `False`. By default `True` | +|* optional | ## Pipeline configuration @@ -73,7 +77,7 @@ generate-env-vars: version: stage: version - image: mrooding/gitlab-semantic-versioning:1.0.0 + image: mrooding/gitlab-semantic-versioning:latest script: - python3 /version-update/version-update.py only: @@ -92,15 +96,20 @@ tag-latest: - tag ``` -## Merge request instructions +## Versioning policy -### Squash commits when merge request is accepted +### Only MR versioning +#### Squash commits when merge request is accepted The new version will be determined based on the commit message. GitLab will automatically format a merge request commit message if the 'Squash commits when merge request is accepted` checkbox is checked during merge request creation. This workflow relies on that commit message format and will fail the pipeline if it cannot extract the merge request id from the commit message. -Unfortunately, GitLab [doesn't yet allow for setting the checkbox to checked by default](https://gitlab.com/gitlab-org/gitlab-ce/issues/27956). Until implemented, make sure to manually check the squash option upon creation. +You can set squashing commits by default [following this manual](https://docs.gitlab.com/ee/user/project/merge_requests/squash_and_merge.html#squash-commits-options) + +### Versioning non MR commits +To allow versioning non MR commits on master You'll have to set up environment variable `SKIP_DIRECT_COMMITS` to `False`. It won't affect versioning MR commits. +Option turned off by default. ### Add a label to indicate a minor or major update diff --git a/version-update.py b/version-update.py index ff35ffe..de69c60 100644 --- a/version-update.py +++ b/version-update.py @@ -9,6 +9,9 @@ def git(*args): return subprocess.check_output(["git"] + list(args)) +def get_last_commit_message(): + return git("log", "-1", "--pretty=%B") + def verify_env_var_presence(name): if name not in os.environ: raise Exception(f"Expected the following environment variable to be set: {name}") @@ -20,10 +23,10 @@ def extract_gitlab_url_from_project_url(): return project_url.split(f"/{project_path}", 1)[0] def extract_merge_request_id_from_commit(): - message = git("log", "-1", "--pretty=%B") + message = get_last_commit_message() matches = re.search(r'(\S*\/\S*!)(\d+)', message.decode("utf-8"), re.M|re.I) - if matches == None: + if matches is None: raise Exception(f"Unable to extract merge request from commit message: {message}") return matches.group(2) @@ -40,15 +43,29 @@ def retrieve_labels_from_merge_request(merge_request_id): return merge_request.labels -def bump(latest): +def get_mr_or_direct_commit_labels(): + skip_direct_commits = os.environ.get('SKIP_DIRECT_COMMITS') + labels = [] + found_mr = True - minor_bump_label = os.environ.get("MINOR_BUMP_LABEL") or "bump-minor" - major_bump_label = os.environ.get("MAJOR_BUMP_LABEL") or "bump-major" + try: + merge_request_id = extract_merge_request_id_from_commit() + labels = retrieve_labels_from_merge_request(merge_request_id) + except Exception as e: + if skip_direct_commits is None: + raise e + else: + found_mr = False - merge_request_id = extract_merge_request_id_from_commit() - labels = retrieve_labels_from_merge_request(merge_request_id) + if not (skip_direct_commits is None and found_mr): + labels += get_last_commit_message() + return labels +def bump(latest): + minor_bump_label = os.environ.get("MINOR_BUMP_LABEL") or "bump-minor" + major_bump_label = os.environ.get("MAJOR_BUMP_LABEL") or "bump-major" + labels = get_mr_or_direct_commit_labels() if minor_bump_label in labels: return semver.bump_minor(latest) From f3b65aade1f2cf6ae8839845b629a9c3b3441ae2 Mon Sep 17 00:00:00 2001 From: Dawid Rabicki Date: Mon, 18 Jan 2021 16:59:19 +0100 Subject: [PATCH 2/2] Fixing alias feature. --- Dockerfile | 8 +++++--- README.md | 2 +- bump-version | 2 ++ 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100755 bump-version diff --git a/Dockerfile b/Dockerfile index 1970398..9c3563a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,8 +13,10 @@ COPY /requirements.txt . RUN pip install -r requirements.txt -RUN echo 'alias bump_version="python3 /version-update/version-update.py"' >> /root/.bashrc - COPY /version-update.py . -CMD ["python", "/version-update.py"] +COPY /bump-version /usr/bin/bump-version + +RUN chmod +x /usr/bin/bump-version + +CMD ["bump-version"] diff --git a/README.md b/README.md index 13c4646..f5f04e0 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ version: stage: version image: mrooding/gitlab-semantic-versioning:latest script: - - python3 /version-update/version-update.py + - bump-version only: - master diff --git a/bump-version b/bump-version new file mode 100755 index 0000000..5c3cb8a --- /dev/null +++ b/bump-version @@ -0,0 +1,2 @@ +#!/bin/bash +python3 /version-update/version-update.py \ No newline at end of file