From 488ce3fc40ad08f64654ac3b0ff033f6fd11f831 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 11 Apr 2025 11:37:23 -0500 Subject: [PATCH 1/3] chore: add make gen to ci actions --- .github/workflows/ci.yaml | 25 +++++++++++++++++++++++++ .github/workflows/gotest.yml | 5 +---- scripts/check_unstaged.sh | 28 ++++++++++++++++++++++++++++ site/src/types/preview.ts | 14 ++++++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100755 scripts/check_unstaged.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..8c7ad3a --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,25 @@ +name: GoTests + +on: + push: + branches: + - main + pull_request: + workflow_dispatch: + +permissions: + contents: read + +jobs: + test-go: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/setup-go@v5 + with: + go-version: 1.22.8 + - uses: actions/checkout@v4.2.2 + - name: Make Gen + run: make --output-sync -B gen + - name: Check for unstaged files + run: ./scripts/check_unstaged.sh \ No newline at end of file diff --git a/.github/workflows/gotest.yml b/.github/workflows/gotest.yml index 3621537..3253c57 100644 --- a/.github/workflows/gotest.yml +++ b/.github/workflows/gotest.yml @@ -22,8 +22,5 @@ jobs: - name: Install gotestsum shell: bash run: go install gotest.tools/gotestsum@latest - - name: Verify terraform provider - run: | - TF_CLI_CONFIG_FILE=$HOME/.terraformrc go test ./... -run=Test_VerifyE2E/Validate -v - name: Run tests - run: TF_CLI_CONFIG_FILE=$HOME/.terraformrc gotestsum ./... \ No newline at end of file + run: gotestsum ./... \ No newline at end of file diff --git a/scripts/check_unstaged.sh b/scripts/check_unstaged.sh new file mode 100755 index 0000000..90d4cad --- /dev/null +++ b/scripts/check_unstaged.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -euo pipefail +# shellcheck source=scripts/lib.sh +source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +cdroot + +FILES="$(git ls-files --other --modified --exclude-standard)" +if [[ "$FILES" != "" ]]; then + mapfile -t files <<<"$FILES" + + log + log "The following files contain unstaged changes:" + log + for file in "${files[@]}"; do + log " - $file" + done + + log + log "These are the changes:" + log + for file in "${files[@]}"; do + git --no-pager diff -- "$file" 1>&2 + done + + log + error "Unstaged changes, see above for details." +fi diff --git a/site/src/types/preview.ts b/site/src/types/preview.ts index 3c392c1..f423a4e 100644 --- a/site/src/types/preview.ts +++ b/site/src/types/preview.ts @@ -67,6 +67,7 @@ export interface ParameterValidation { readonly validation_min: number | null; readonly validation_max: number | null; readonly validation_monotonic: string | null; + readonly validation_invalid: boolean | null; } // From web/session.go @@ -96,6 +97,19 @@ export const ValidationMonotonicIncreasing = "increasing"; // From types/owner.go export interface WorkspaceOwner { + readonly id: string; + readonly name: string; + readonly full_name: string; + readonly email: string; + readonly ssh_public_key: string; readonly groups: readonly string[]; + readonly login_type: string; + readonly rbac_roles: readonly WorkspaceOwnerRBACRole[]; +} + +// From types/owner.go +export interface WorkspaceOwnerRBACRole { + readonly name: string; + readonly org_id: string; } From c7446e5a79dcf6442b3d260608e0262ef91cfe89 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 11 Apr 2025 11:39:01 -0500 Subject: [PATCH 2/3] rename file --- .github/workflows/{ci.yaml => gen.yaml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{ci.yaml => gen.yaml} (96%) diff --git a/.github/workflows/ci.yaml b/.github/workflows/gen.yaml similarity index 96% rename from .github/workflows/ci.yaml rename to .github/workflows/gen.yaml index 8c7ad3a..c97c5bd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/gen.yaml @@ -1,4 +1,4 @@ -name: GoTests +name: Generate on: push: From 289fcc7843981a770b9e91e05b4ec95f6676f5d7 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 11 Apr 2025 11:49:54 -0500 Subject: [PATCH 3/3] add lib sh --- scripts/check_unstaged.sh | 2 +- scripts/lib.sh | 288 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 289 insertions(+), 1 deletion(-) create mode 100755 scripts/lib.sh diff --git a/scripts/check_unstaged.sh b/scripts/check_unstaged.sh index 90d4cad..715c84c 100755 --- a/scripts/check_unstaged.sh +++ b/scripts/check_unstaged.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -euo pipefail # shellcheck source=scripts/lib.sh diff --git a/scripts/lib.sh b/scripts/lib.sh new file mode 100755 index 0000000..fb6220e --- /dev/null +++ b/scripts/lib.sh @@ -0,0 +1,288 @@ +#!/usr/bin/env bash + +# This script is meant to be sourced by other scripts. To source this script: +# # shellcheck source=scripts/lib.sh +# source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +set -euo pipefail + +# Avoid sourcing this script multiple times to guard against when lib.sh +# is used by another sourced script, it can lead to confusing results. +if [[ ${SCRIPTS_LIB_IS_SOURCED:-0} == 1 ]]; then + return +fi +# Do not export to avoid this value being inherited by non-sourced +# scripts. +SCRIPTS_LIB_IS_SOURCED=1 + +# realpath returns an absolute path to the given relative path. It will fail if +# the parent directory of the path does not exist. Make sure you are in the +# expected directory before running this to avoid errors. +# +# GNU realpath relies on coreutils, which are not installed or the default on +# Macs out of the box, so we have this mostly working bash alternative instead. +# +# Taken from https://stackoverflow.com/a/3915420 (CC-BY-SA 4.0) +realpath() { + local dir + local base + dir="$(dirname "$1")" + base="$(basename "$1")" + + if [[ ! -d "$dir" ]]; then + error "Could not change directory to '$dir': directory does not exist" + fi + echo "$( + cd "$dir" || error "Could not change directory to '$dir'" + pwd -P + )"/"$base" +} + +# We have to define realpath before these otherwise it fails on Mac's bash. +SCRIPT="${BASH_SOURCE[1]:-${BASH_SOURCE[0]}}" +SCRIPT_DIR="$(realpath "$(dirname "$SCRIPT")")" + +function project_root { + # Nix sets $src in derivations! + [[ -n "${src:-}" ]] && echo "$src" && return + + # Try to use `git rev-parse --show-toplevel` to find the project root. + # If this directory is not a git repository, this command will fail. + git rev-parse --show-toplevel 2>/dev/null && return + + # This finds the Sapling root. This behavior is added so that @ammario + # and others can more easily experiment with Sapling, but we do not have a + # plan to support Sapling across the repo. + sl root 2>/dev/null && return +} + +PROJECT_ROOT="$(cd "$SCRIPT_DIR" && realpath "$(project_root)")" + +# pushd is a silent alternative to the real pushd shell command. +pushd() { + command pushd "$@" >/dev/null || error "Could not pushd to '$*'" +} + +# popd is a silent alternative to the real popd shell command. +# shellcheck disable=SC2120 +popd() { + command popd >/dev/null || error "Could not restore directory with popd" +} + +# cdself changes directory to the directory of the current script. This should +# not be used in scripts that may be sourced by other scripts. +cdself() { + cd "$SCRIPT_DIR" || error "Could not change directory to '$SCRIPT_DIR'" +} + +# cdroot changes directory to the root of the repository. +cdroot() { + cd "$PROJECT_ROOT" || error "Could not change directory to '$PROJECT_ROOT'" +} + +# execrelative can be used to execute scripts as if you were in the parent +# directory of the current script. This should not be used in scripts that may +# be sourced by other scripts. +execrelative() { + pushd "$SCRIPT_DIR" || error "Could not change directory to '$SCRIPT_DIR'" + local rc=0 + "$@" || rc=$? + popd + return $rc +} + +dependency_check() { + local dep=$1 + + # Special case for yq that can be yq or yq4. + if [[ $dep == yq ]]; then + [[ -n "${CODER_LIBSH_YQ:-}" ]] + return + fi + + command -v "$dep" >/dev/null +} + +dependencies() { + local fail=0 + for dep in "$@"; do + if ! dependency_check "$dep"; then + log "ERROR: The '$dep' dependency is required, but is not available." + if isdarwin; then + case "$dep" in + gsed | gawk) + log "- brew install $dep" + ;; + esac + fi + fail=1 + fi + done + + if [[ "$fail" == 1 ]]; then + log + error "One or more dependencies are not available, check above log output for more details." + fi +} + +requiredenvs() { + local fail=0 + for env in "$@"; do + if [[ "${!env:-}" == "" ]]; then + log "ERROR: The '$env' environment variable is required, but is not set." + fail=1 + fi + done + + if [[ "$fail" == 1 ]]; then + log + error "One or more required environment variables are not set, check above log output for more details." + fi +} + +gh_auth() { + if [[ -z ${GITHUB_TOKEN:-} ]]; then + if [[ -n ${GH_TOKEN:-} ]]; then + export GITHUB_TOKEN=${GH_TOKEN} + elif [[ ${CODER:-} == true ]]; then + if ! output=$(coder external-auth access-token github 2>&1); then + # TODO(mafredri): We could allow checking `gh auth token` here. + log "${output}" + error "Could not authenticate with GitHub using Coder external auth." + else + export GITHUB_TOKEN=${output} + fi + elif token="$(gh auth token --hostname github.com 2>/dev/null)"; then + export GITHUB_TOKEN=${token} + else + error "GitHub authentication is required to run this command, please set GITHUB_TOKEN or run 'gh auth login'." + fi + fi +} + +# maybedryrun prints the given program and flags, and then, if the first +# argument is 0, executes it. The reason the first argument should be 0 is that +# it is expected that you have a dry_run variable in your script that is set to +# 0 by default (i.e. do not dry run) and set to 1 if the --dry-run flag is +# specified. +# +# Usage: maybedryrun 1 gh release create ... +# Usage: maybedryrun 0 docker push ghcr.io/coder/coder:latest +maybedryrun() { + if [[ "$1" == 1 ]]; then + shift + log "DRYRUN: $*" + else + shift + logrun "$@" + fi +} + +# logrun prints the given program and flags, and then executes it. +# +# Usage: logrun gh release create ... +logrun() { + log $ "$*" + "$@" +} + +# log prints a message to stderr. +log() { + echo "$*" 1>&2 +} + +# error prints an error message and returns an error exit code. +error() { + log "ERROR: $*" + exit 1 +} + +# isdarwin returns an error if the current platform is not darwin. +isdarwin() { + [[ "${OSTYPE:-darwin}" == *darwin* ]] +} + +# issourced returns true if the script that sourced this script is being +# sourced by another. +issourced() { + [[ "${BASH_SOURCE[1]}" != "$0" ]] +} + +# We don't need to check dependencies more than once per script, but some +# scripts call other scripts that also `source lib.sh`, so we set an environment +# variable after successfully checking dependencies once. +if [[ "${CODER_LIBSH_NO_CHECK_DEPENDENCIES:-}" != *t* ]]; then + libsh_bad_dependencies=0 + + if ((BASH_VERSINFO[0] < 4)); then + libsh_bad_dependencies=1 + log "ERROR: You need at least bash 4.0 to run the scripts in the Coder repo." + if isdarwin; then + log "On darwin:" + log "- brew install bash" + # shellcheck disable=SC2016 + log '- Add "$(brew --prefix bash)/bin" to your PATH' + log "- Restart your terminal" + fi + log + fi + + # BSD getopt (which is installed by default on Macs) is not supported. + if [[ "$(getopt --version)" == *--* ]]; then + libsh_bad_dependencies=1 + log "ERROR: You need GNU getopt to run the scripts in the Coder repo." + if isdarwin; then + log "On darwin:" + log "- brew install gnu-getopt" + # shellcheck disable=SC2016 + log '- Add "$(brew --prefix gnu-getopt)/bin" to your PATH' + log "- Restart your terminal" + fi + log + fi + + # The bash scripts don't call Make directly, but we want to make (ha ha) + # sure that make supports the features the repo uses. Notably, Macs have an + # old version of Make installed out of the box that doesn't support new + # features like ONESHELL. + # + # We have to disable pipefail temporarily to avoid ERRPIPE errors when + # piping into `head -n1`. + set +o pipefail + make_version="$(make --version 2>/dev/null | head -n1 | grep -oE '([[:digit:]]+\.){1,2}[[:digit:]]+')" + set -o pipefail + if [[ ${make_version//.*/} -lt 4 ]]; then + libsh_bad_dependencies=1 + log "ERROR: You need at least make 4.0 to run the scripts in the Coder repo." + if isdarwin; then + log "On darwin:" + log "- brew install make" + # shellcheck disable=SC2016 + log '- Add "$(brew --prefix make)/libexec/gnubin" to your PATH' + log "- Restart your terminal" + fi + log + fi + + # Allow for yq to be installed as yq4. + if command -v yq4 >/dev/null; then + export CODER_LIBSH_YQ=yq4 + elif command -v yq >/dev/null; then + if [[ $(yq --version) == *" v4."* ]]; then + export CODER_LIBSH_YQ=yq + fi + fi + + if [[ "$libsh_bad_dependencies" == 1 ]]; then + error "Invalid dependencies, see above for more details." + fi + + export CODER_LIBSH_NO_CHECK_DEPENDENCIES=true +fi + +# Alias yq to the version we want by shadowing with a function. +if [[ -n ${CODER_LIBSH_YQ:-} ]]; then + yq() { + command $CODER_LIBSH_YQ "$@" + } +fi