Skip to content

Adding env var for versioning non mr commits. Adding alias to docker … #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ RUN pip install -r requirements.txt

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"]
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down Expand Up @@ -73,9 +77,9 @@ 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
- bump-version
only:
- master

Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions bump-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
python3 /version-update/version-update.py
31 changes: 24 additions & 7 deletions version-update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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)
Expand All @@ -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)
Expand Down