Skip to content

Commit 99d037d

Browse files
Merge pull request #1 from overmindtech/add-gitlab-support
Added support for gitlab comments
2 parents d4b1baa + 2cb3455 commit 99d037d

2 files changed

Lines changed: 108 additions & 24 deletions

File tree

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The Overmind plugin installs the Overmind CLI (and GitHub CLI) and executes one
88
- **submit-plan**: Submits a Terraform plan to Overmind for analysis
99
- **start-change**: Marks the beginning of a change in Overmind
1010
- **end-change**: Marks the completion of a change in Overmind
11-
- **wait-for-simulation**: Retrieves simulation output from Overmind and comments on the relevant GitHub PR
11+
- **wait-for-simulation**: Retrieves simulation output from Overmind and comments on the relevant GitHub PR or GitLab MR
1212

1313
## Inputs
1414

@@ -17,7 +17,7 @@ The Overmind plugin installs the Overmind CLI (and GitHub CLI) and executes one
1717
| `action` | The action to perform. Must be one of: `submit-plan`, `start-change`, `end-change`, `wait-for-simulation` | Yes |
1818
| `api_key` | Overmind API key for authentication. Must have the following scopes: `account:read`, `changes:write`, `config:write`, `request:receive`, `sources:read`, `source:write` | Yes |
1919
| `tags` | A comma-separated list of key=value tags to attach to the change (only used with `submit-plan` action) | No |
20-
| `post_comment` | Whether `wait-for-simulation` should post the Overmind markdown to GitHub. Defaults to `true` when running against a PR, otherwise `false`. | No |
20+
| `post_comment` | Whether `wait-for-simulation` should post the Overmind markdown to GitHub PR or GitLab MR. Defaults to `true` when running against a PR/MR, otherwise `false`. Automatically detects GitHub or GitLab based on repository URL. | No |
2121

2222
## Usage
2323

@@ -90,7 +90,7 @@ deploy:
9090
9191
### Wait for Simulation (any step after Overmind run)
9292
93-
Fetch the latest Overmind simulation summary for the current env0 deployment and (optionally) comment on the matching GitHub pull request. By default the plugin posts to GitHub whenever the deployment runs in the context of a PR; set `post_comment: false` to skip commenting. When posting, make sure `GH_TOKEN` is set.
93+
Fetch the latest Overmind simulation summary for the current env0 deployment and (optionally) comment on the matching GitHub pull request or GitLab merge request. By default the plugin posts comments whenever the deployment runs in the context of a PR/MR; set `post_comment: false` to skip commenting. The plugin automatically detects whether the repository is GitHub or GitLab based on the repository URL. When posting to GitHub, make sure `GH_TOKEN` is set. When posting to GitLab, make sure `GITLAB_TOKEN` is set.
9494

9595
```yaml
9696
version: 2
@@ -139,15 +139,15 @@ deploy:
139139

140140
## How It Works
141141

142-
1. **Installation**: The plugin automatically installs the latest version of both the Overmind CLI and GitHub CLI to a writable directory in your PATH.
142+
1. **Installation**: The plugin automatically installs the latest version of the Overmind CLI and GitHub CLI (for GitHub support) to a writable directory in your PATH. GitLab support uses `curl` which is typically available on most systems.
143143

144144
2. **Authentication**: The API key provided in the `api_key` input is set as the `OVERMIND_API_KEY` environment variable.
145145

146-
3. **Action Execution**: Based on the `action` input, the plugin executes the corresponding Overmind/GitHub workflow:
146+
3. **Action Execution**: Based on the `action` input, the plugin executes the corresponding Overmind/GitHub/GitLab workflow:
147147
- `submit-plan`: Uses `$ENV0_TF_PLAN_JSON` to submit the Terraform plan
148148
- `start-change`: Marks the beginning of a change with a ticket link to the env0 deployment
149149
- `end-change`: Marks the completion of a change with a ticket link to the env0 deployment
150-
- `wait-for-simulation`: Retrieves Overmind simulation results as Markdown and (when `post_comment=true`) posts them to the GitHub PR
150+
- `wait-for-simulation`: Retrieves Overmind simulation results as Markdown and (when `post_comment=true`) posts them to the GitHub PR or GitLab MR (automatically detected based on repository URL)
151151

152152
4. **Ticket Links**: All actions automatically construct a ticket link to the env0 deployment using environment variables (`ENV0_PROJECT_ID`, `ENV0_ENVIRONMENT_ID`, `ENV0_DEPLOYMENT_LOG_ID`, `ENV0_ORGANIZATION_ID`).
153153

@@ -159,7 +159,8 @@ deploy:
159159
- `ENV0_DEPLOYMENT_LOG_ID`
160160
- `ENV0_ORGANIZATION_ID`
161161
- `ENV0_TF_PLAN_JSON` (for submit-plan action)
162-
- `ENV0_PR_NUMBER` (only when `wait-for-simulation` posts to GitHub, i.e., running against a PR)
162+
- `ENV0_PR_NUMBER` (only when `wait-for-simulation` posts comments, i.e., running against a PR/MR)
163+
- `ENV0_PR_SOURCE_REPOSITORY` (only when `wait-for-simulation` posts comments, used to detect GitHub vs GitLab)
163164

164165
- A valid Overmind API key with the following required scopes:
165166
- `account:read`
@@ -170,15 +171,28 @@ deploy:
170171
- `source:write`
171172

172173
- GitHub authentication for the CLI when `wait-for-simulation` posts to GitHub (set `GH_TOKEN`).
174+
- GitLab authentication when `wait-for-simulation` posts to GitLab (set `GITLAB_TOKEN`).
173175

174176
### Creating a `GH_TOKEN`
175177

176-
`wait-for-simulation` calls `gh pr comment`, which requires a GitHub personal access token. To create one:
178+
`wait-for-simulation` calls `gh pr comment` for GitHub repositories, which requires a GitHub personal access token. To create one:
177179

178180
1. Sign in to GitHub and open https://github.com/settings/tokens/new (or the fine-grained token wizard).
179181
2. Choose **Classic** token, set an expiry that matches your security policy, and select the **repo** scope (read/write is needed to comment on PRs).
180182
3. Generate the token and copy it immediately—GitHub will not show it again.
181183
4. Store the token securely in env0 (for example as an environment variable or secret) and expose it to the plugin as `GH_TOKEN`.
184+
185+
### Creating a `GITLAB_TOKEN`
186+
187+
`wait-for-simulation` uses the GitLab API to post comments on merge requests for GitLab repositories. To create a GitLab personal access token:
188+
189+
1. Sign in to GitLab and navigate to your user settings (or group/project settings for project/group tokens).
190+
2. Go to **Access Tokens** (or **Preferences** > **Access Tokens** for user tokens).
191+
3. Create a new token with the `api` scope (read/write is needed to comment on merge requests).
192+
4. Generate the token and copy it immediately—GitLab will not show it again.
193+
5. Store the token securely in env0 (for example as an environment variable or secret) and expose it to the plugin as `GITLAB_TOKEN`.
194+
195+
**Note**: The plugin automatically detects whether a repository is GitHub or GitLab based on the `ENV0_PR_SOURCE_REPOSITORY` environment variable. If the repository URL contains "gitlab", it will use GitLab API; otherwise, it will use GitHub CLI.
182196
## Notes
183197

184198
- The plugin automatically detects the operating system and architecture to download the correct Overmind CLI binary.

env0.plugin.yaml

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ inputs:
1414
description: "The Overmind instance to connect to (defaults to https://app.overmind.tech)"
1515
required: false
1616
post_comment:
17-
description: "Whether wait-for-simulation should post the Overmind markdown to the PR. Defaults to true when ENV0_PR_NUMBER is set, otherwise false."
17+
description: "Whether wait-for-simulation should post the Overmind markdown to the PR/MR. Defaults to true when ENV0_PR_NUMBER is set, otherwise false. Automatically detects GitHub or GitLab based on repository URL."
1818
required: false
1919
run:
2020
exec: |
@@ -24,6 +24,7 @@ run:
2424
TMP_DIR=""
2525
GH_TMP_DIR=""
2626
MARKDOWN_FILE=""
27+
JSON_PAYLOAD_FILE=""
2728
ORIGINAL_DIR=$(pwd)
2829
2930
cleanup() {
@@ -36,6 +37,9 @@ run:
3637
if [ -n "${MARKDOWN_FILE}" ] && [ -f "${MARKDOWN_FILE}" ]; then
3738
rm -f "${MARKDOWN_FILE}"
3839
fi
40+
if [ -n "${JSON_PAYLOAD_FILE}" ] && [ -f "${JSON_PAYLOAD_FILE}" ]; then
41+
rm -f "${JSON_PAYLOAD_FILE}"
42+
fi
3943
}
4044
trap cleanup EXIT
4145
@@ -295,7 +299,7 @@ run:
295299
--ticket-link "${ticket_link}"
296300
;;
297301
wait-for-simulation)
298-
echo "Waiting for Overmind simulation and commenting on PR..."
302+
echo "Waiting for Overmind simulation and commenting on PR/MR..."
299303
should_post_comment="${inputs.post_comment}"
300304
if [ -n "${should_post_comment}" ]; then
301305
should_post_comment=$(echo "${should_post_comment}" | tr '[:upper:]' '[:lower:]')
@@ -340,25 +344,91 @@ run:
340344
echo "Error: ENV0_PR_SOURCE_REPOSITORY environment variable is not set but post_comment=true"
341345
exit 1
342346
fi
343-
if [ -z "${GH_BINARY}" ] || [ ! -x "${GH_BINARY}" ]; then
344-
echo "Error: GitHub CLI was not installed correctly"
345-
exit 1
346-
fi
347347
348-
if ! "${GH_BINARY}" auth status >/dev/null 2>&1; then
349-
echo "Warning: GitHub CLI authentication not detected. Ensure GH_TOKEN is configured."
348+
# Detect if this is a GitLab repository
349+
if echo "${ENV0_PR_SOURCE_REPOSITORY}" | grep -q "gitlab"; then
350+
# GitLab merge request
351+
if [ -z "${GITLAB_TOKEN}" ]; then
352+
echo "Error: GITLAB_TOKEN environment variable is not set but repository appears to be GitLab"
353+
exit 1
354+
fi
355+
356+
# Extract GitLab base URL and project path from repository string
357+
# ENV0_PR_SOURCE_REPOSITORY format: "group/project" or "https://gitlab.com/group/project" or "gitlab.com/group/project"
358+
REPO_STR="${ENV0_PR_SOURCE_REPOSITORY}"
359+
360+
# Remove protocol if present
361+
REPO_STR=$(echo "${REPO_STR}" | sed 's|^https\?://||')
362+
363+
# Extract host (default to gitlab.com if not specified)
364+
if echo "${REPO_STR}" | grep -q "/"; then
365+
GITLAB_HOST=$(echo "${REPO_STR}" | cut -d'/' -f1)
366+
PROJECT_PATH=$(echo "${REPO_STR}" | cut -d'/' -f2-)
367+
else
368+
# If no host, assume gitlab.com
369+
GITLAB_HOST="gitlab.com"
370+
PROJECT_PATH="${REPO_STR}"
371+
fi
372+
373+
# Ensure we have https:// prefix for API calls
374+
if [ "${GITLAB_HOST}" != "gitlab.com" ] && ! echo "${GITLAB_HOST}" | grep -q "^https\?://"; then
375+
GITLAB_BASE_URL="https://${GITLAB_HOST}"
376+
elif [ "${GITLAB_HOST}" = "gitlab.com" ]; then
377+
GITLAB_BASE_URL="https://gitlab.com"
378+
else
379+
GITLAB_BASE_URL="${GITLAB_HOST}"
380+
fi
381+
382+
# URL encode the project path (handle spaces and special chars)
383+
PROJECT_PATH_ENCODED=$(echo "${PROJECT_PATH}" | sed 's|/|%2F|g' | sed 's| |%20|g')
384+
385+
MR_IID="${ENV0_PR_NUMBER}"
386+
API_URL="${GITLAB_BASE_URL}/api/v4/projects/${PROJECT_PATH_ENCODED}/merge_requests/${MR_IID}/notes"
387+
388+
echo "Posting simulation results to GitLab MR ${PROJECT_PATH}!${MR_IID}..."
389+
390+
# Create JSON payload file using jq
391+
JSON_PAYLOAD_FILE=$(mktemp)
392+
jq -n --rawfile body "${MARKDOWN_FILE}" '{body: $body}' > "${JSON_PAYLOAD_FILE}"
393+
394+
# Post comment using curl
395+
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
396+
-H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
397+
-H "Content-Type: application/json" \
398+
--data-binary "@${JSON_PAYLOAD_FILE}" \
399+
"${API_URL}")
400+
401+
# Clean up JSON payload file
402+
rm -f "${JSON_PAYLOAD_FILE}"
403+
404+
if [ "${HTTP_CODE}" -ge 200 ] && [ "${HTTP_CODE}" -lt 300 ]; then
405+
echo "✓ Simulation results posted to GitLab MR"
406+
else
407+
echo "Error: Failed to post comment to GitLab MR (HTTP ${HTTP_CODE})"
408+
exit 1
409+
fi
410+
else
411+
# GitHub pull request
412+
if [ -z "${GH_BINARY}" ] || [ ! -x "${GH_BINARY}" ]; then
413+
echo "Error: GitHub CLI was not installed correctly"
414+
exit 1
415+
fi
416+
417+
if ! "${GH_BINARY}" auth status >/dev/null 2>&1; then
418+
echo "Warning: GitHub CLI authentication not detected. Ensure GH_TOKEN is configured."
419+
fi
420+
421+
echo "Posting simulation results to ${ENV0_PR_SOURCE_REPOSITORY}#${ENV0_PR_NUMBER}..."
422+
"${GH_BINARY}" pr comment "${ENV0_PR_NUMBER}" \
423+
--repo "${ENV0_PR_SOURCE_REPOSITORY}" \
424+
--body-file "${MARKDOWN_FILE}"
425+
echo "✓ Simulation results posted to GitHub PR"
350426
fi
351-
352-
echo "Posting simulation results to ${ENV0_PR_SOURCE_REPOSITORY}#${ENV0_PR_NUMBER}..."
353-
"${GH_BINARY}" pr comment "${ENV0_PR_NUMBER}" \
354-
--repo "${ENV0_PR_SOURCE_REPOSITORY}" \
355-
--body-file "${MARKDOWN_FILE}"
356-
echo "✓ Simulation results posted to GitHub PR"
357427
else
358428
if [ -n "${ENV0_PR_NUMBER}" ]; then
359-
echo "Skipping GitHub comment because post_comment=false."
429+
echo "Skipping comment because post_comment=false."
360430
else
361-
echo "Skipping GitHub comment because ENV0_PR_NUMBER is not set."
431+
echo "Skipping comment because ENV0_PR_NUMBER is not set."
362432
fi
363433
fi
364434
;;

0 commit comments

Comments
 (0)