Skip to content
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

patch: Github Actions and Makefile functionalities #1

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
45 changes: 45 additions & 0 deletions .github/actions/check-branch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Check Branch

This action gets the head ref or source branch of the pull request in a workflow to run a checks on the branch name. The check verify that the branch name starts with a placeholder `types` and followed by another placeholder `separators`.

The input `types` default values are:

- major
- release
- minor
- feature
- feat
- patch
- issue
- hotfix
- dependabot
- whitesource/

The input `separators` default value are `-` and `\`.

## Usage

See [action.yml](action.yml).

## Examples

```yaml
---
name: check branch name

on:
pull_request:

jobs:
bump:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Check branch
uses: ./.github/actions/check-branch/
```

## How does it work

This simple action runs a [check-branch script](check-branch.sh)
16 changes: 16 additions & 0 deletions .github/actions/check-branch/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: 'Check Branch'
description: 'Check branch name'

inputs:
types:
description: "Provide custom types if you don't want the default ones from major|release|minor|feature|patch|issue|hotfix|dependabot|whitesource/"
required: false
separators:
description: "Provide custom separators if you don't want the default ones from -|/"
required: false

runs:
using: "composite"
steps:
- run: ${{ github.action_path }}/check-branch.sh "${{ inputs.types }}" "${{ inputs.separators }}"
shell: bash
49 changes: 49 additions & 0 deletions .github/actions/check-branch/check-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env sh

# This code is provided by github.com/dohernandez/dev.

FLAT_TYPES_DEFAULT="major release minor feature feat patch issue hotfix dependabot whitesource/"
FLAT_SEPARATORS_DEFAULT="- /"

types="${1}"

FLAT_TYPES=""

for target in $types
do
FLAT_TYPES="$FLAT_TYPES $target"
done

# removing the first space added when FLAT_TYPES is empty
FLAT_TYPES=$(echo "${FLAT_TYPES}" | sed 's/^ //g')

# If FLAT_TYPES is empty, use the value of FLAT_TYPES_DEFAULT instead
FLAT_TYPES=${FLAT_TYPES:-$FLAT_TYPES_DEFAULT}

REGEX_FLAT_TYPES=$(echo "${FLAT_TYPES}" | sed 's/ /|/g')


separators="${2}"

FLAT_SEPARATORS=""

for target in $separators
do
FLAT_SEPARATORS="$FLAT_SEPARATORS $target"
done

# If FLAT_SEPARATORS is empty, use the value of FLAT_TYPES_DEFAULT instead
FLAT_SEPARATORS=${FLAT_SEPARATORS:-$FLAT_SEPARATORS_DEFAULT}

REGEX_FLAT_SEPARATORS=$(echo "${FLAT_SEPARATORS}" | sed 's/ /|/g')



# ^minor([-\/]+.+)?$
if ! (echo "${GITHUB_HEAD_REF}" | grep -i -E "^($REGEX_FLAT_TYPES)([$REGEX_FLAT_SEPARATORS]+.+)?$"); then
VALID_FLAT_TYPES=$(echo "${FLAT_TYPES}" | sed 's/ /, /g')
VALID_FLAT_SEPARATORS=$(echo "${FLAT_SEPARATORS}" | sed 's/ /, /g')
echo "Invalid branch name \"${GITHUB_HEAD_REF}\". Valid branch prefixes: ${VALID_FLAT_TYPES} and separators: ${VALID_FLAT_SEPARATORS}"
exit 1
fi
echo "Valid branch name"
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/" # Location go.mod
schedule:
interval: "daily"
open-pull-requests-limit: 1
groups:
dependencies:
patterns:
- "*" # Update all dependencies
update-types:
- "minor"
- "patch"
19 changes: 19 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: check

on:
pull_request:
branches:
- main
types: [opened, edited, synchronize, reopened]

jobs:
branch-name:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Check branch name
uses: ./.github/actions/check-branch/
with:
types: ${{env.types}}
separators: ${{env.separators}}
40 changes: 40 additions & 0 deletions .github/workflows/cloc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: cloc
on:
pull_request:

# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
cloc:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
path: pr
- name: Checkout base code
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Count Lines Of Code
id: loc
run: |
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
{
echo "diff<<EOF"
echo "$OUTPUT"
echo "EOF"
} >> $GITHUB_OUTPUT
- name: Comment Code Lines
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
header: LOC
message: |
### Lines Of Code
${{ steps.loc.outputs.diff }}
46 changes: 46 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: lint
on:
push:
tags:
- v*
branches:
- main
pull_request:

# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
golangci:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.23.x
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/[email protected]
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.61.0

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the action will use pre-installed Go.
# skip-go-installation: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
56 changes: 56 additions & 0 deletions .github/workflows/gorelease.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: gorelease
on:
pull_request:

# Cancel the workflow in progress in newer build is about to start.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

env:
GO_VERSION: 1.23.x
jobs:
gorelease:
runs-on: ubuntu-latest
steps:
- name: Install Go stable
if: env.GO_VERSION != 'tip'
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: Install Go tip
if: env.GO_VERSION == 'tip'
run: |
curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz
ls -lah gotip.tar.gz
mkdir -p ~/sdk/gotip
tar -C ~/sdk/gotip -xzf gotip.tar.gz
~/sdk/gotip/bin/go version
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v3
- name: Gorelease cache
uses: actions/cache@v3
with:
path: |
~/go/bin/gorelease
key: ${{ runner.os }}-gorelease-generic
- name: Gorelease
id: gorelease
run: |
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
OUTPUT=$(gorelease 2>&1 || exit 0)
echo "${OUTPUT}"
echo "report<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
- name: Comment report
continue-on-error: true
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
header: gorelease
message: |
### Go API Changes

<pre>
${{ steps.gorelease.outputs.report }}
</pre>
69 changes: 69 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: release

on:
pull_request:
types:
- closed
branches:
- main

jobs:
create-release:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.23.x

- name: Get Merged PR Branch
id: get-branch
run: |
PR_BRANCH=$(jq -r '.pull_request.head.ref' "$GITHUB_EVENT_PATH")
echo "Merged PR Branch: $PR_BRANCH"
echo "PR_BRANCH=$PR_BRANCH" >> $GITHUB_ENV

- name: Determine level
id: determine-level
run: |
case "${{ env.PR_BRANCH }}" in
patch* | issue* | hotfix* | dependabot* | whitesource/*)
LEVEL="patch";;
minor* | feature* | feat*)
LEVEL="minor";;
major* | release*)
LEVEL="major";;
*)
LEVEL="unknown";;
esac
echo "Determined level: $LEVEL"
echo "LEVEL=$LEVEL" >> $GITHUB_ENV

- name: Get latest tag
id: latest_tag
uses: actions-ecosystem/action-get-latest-tag@v1

- name: Bump release version
id: bump_version
uses: actions-ecosystem/action-bump-semver@v1
with:
current_version: ${{ steps.latest_tag.outputs.tag }}
level: ${{ env.LEVEL }}

- name: Get Merge Commit Message
id: get_merge_commit_message
run: |
MERGE_COMMIT_MESSAGE=$(git log --merges --format=%B -n 1)
echo "Merge Commit Message: $MERGE_COMMIT_MESSAGE"
echo "$MERGE_COMMIT_MESSAGE" > ${{ github.workspace }}-CHANGELOG.txt

- name: Create GitHub Release
uses: softprops/action-gh-release@v1
with:
body_path: ${{ github.workspace }}-CHANGELOG.txt
tag_name: ${{ steps.bump_version.outputs.new_version }}
token: ${{ secrets.GH_TOKEN }}
Loading
Loading