Skip to content

Git Hooks (pre-receive) with fail check drops Gitea to 502 Error #26129

Closed
@sgabenov

Description

@sgabenov

Description

I have faced a situation where Git Hook drops Gitea to 502 Error.
Git Hook checks commits for some naming templates and raise error if check is not completed. All commits/PRs are blocked if check is failing.

Issue:
Gitea executes Git Hook on PR with non 0 error code. After receiving exit code 1 it drops to 502 Error

Expected behavior:
Show page with git console and error code from git. Do not drop to 502.

How to reproduce:

  1. Enable git hooks on Gitea and Configure Git Hook (pre-recive) on server side with this code:
#!/bin/bash

# Error message for BRANCH POLICY
error_msg_branch=$(cat <<-END

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@                                                                               @   
@   !!! Push not allowed by BRANCH NAME policy !!!                              @
@                                                                               @  
@   You branch should be named with these templaes:                             @
@     - master                                                                  @
@     - develop                                                                 @
@     - release/RC_*                                                            @
@     - feature/JIRA_ID-000--*                                                  @
@     - bugfix/*                                                                @
@     - hotfix/*                                                                @
@                                                                               @
@   Example: feature/MEC-167--add_new_functionaliry                             @
@                                                                               @
@   Wiki: https://confluence.ncloudtech.ru/display/KDM/Git+branch+naming        @                                                                        @
@                                                                               @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
END
)

error_msg_commit=$(cat <<-END

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@                                                                                 @
@   !!! Your Commit does not have a referance to JIRA ticket !!!                  @
@                                                                                 @
@   Please correct your Git messages, and push again.                             @
@   Example: "[KDM-1234] This is a correct JIRA reference"                        @
@                                                                                 @
@   Wiki: https://confluence.ncloudtech.ru/pages/viewpage.action?pageId=201708163 @                                                                                @
@                                                                                 @
@   To FORCE push, use "bugfix" or "hotfix" in your commit message                @                                                                  @
@                                                                                 @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
END
)



while read oldrev newrev refname
do
  #
  # Step 0:
  # Get Git commit information
  #
  BRANCH_NAME_FULL=$refname
  BRANCH_NAME="$(echo $BRANCH_NAME_FULL | sed 's/refs\/heads\///g')"
  COMMIT_MESSAGE=$(git log --format=%B -n 1)
  echo "[INFO] BRANCH NAME: $BRANCH_NAME"

  #
  # Step 1:
  # Policy - Check that brunch name is enforced by branch name
  #

  echo "[INFO] Policy - Check that brunch name is enforced by branch name"
  # Regexp for allowed names of branches
  branch_name_format='^master|^main|^develop|^release\/RC_.*|^feature\/[a-zA-Z0-9,\.\_\-]+-[0-9]+.*|^hotfix\/.*|^bugfix\/.*'
  
  if [[ ! $BRANCH_NAME =~ $branch_name_format ]]; then
      echo "$error_msg_branch" >&2
      exit 1
  else
      echo "Push is successful"
  fi

  #
  # Step 2:
  # Policy - Check commit message for JIRA issue number
  #

  # Configuration
  echo "[INFO] Policy - Check commit message for JIRA issue number"
  jiraIdRegex="[a-zA-Z0-9,\.\_\-]+-[0-9]+"
  fixMsgRegex="bugfix|hotfix"
  info_msg="[INFO] The commit message looks good"
  error_msg="[POLICY] The commit doesn't reference a JIRA issue"

  # Get all commits from this push
  for sha1Commit in $(git rev-list $oldrev..$newrev);
  do
    # Receive git commits sha from git history in chronologic order
    echo "[INFO] Processing commit with sha: $sha1Commit";
    # Get commit message from commit
    commitMessage=$(git log --format=%B -n 1 $sha1Commit)
    # Check with RegEX if commit has JIRA reference
    jiraIds=$(echo $commitMessage | grep -Eo $jiraIdRegex)
    fixMsg=$(echo $commitMessage | grep -Eo $fixMsgRegex)
    # Check if this commit urgent e.g. hotfox or bugfix
    if [[ -n "${fixMsg}" ]]; then
      echo "[WARNING] Found "bugfix|hotfix" in msg. Force skipping check for JiraID"
      exit 0
    fi
    # Check for JiraIDs in commit message
    echo "[INFO] Found JIRA IDs in commit: $jiraIds"
    if [[ -z "${jiraIds}" ]]; then
      echo "$error_msg: $commitMessage" >&2
      echo "$error_msg_commit" >&2
      exit 1
    fi
  done
done

#
# Exit
#
exit 0
  1. Create new branch feature/jira-123_some_text and push to gitea
  2. Create PR from feature/jira-123_some_text --> master
  3. Get Error 502 on Gitea. The reason of this error, bacause git hook check failes. It not allows refs with PRs and exit with error 1.
    I expect, that it will show $error_msg_branch >&2 from bash script in hook redirected to git log (How it is done on client side, if hook failes), but not prop to 502 Error

If i change branch_name_format='^master|^main|^develop|^release\/RC_.*|^feature\/[a-zA-Z0-9,\.\_\-]+-[0-9]+.*|^hotfix\/.*|^bugfix\/.*' to branch_name_format='pull|^master|^main|^develop|^release\/RC_.*|^feature\/[a-zA-Z0-9,\.\_\-]+-[0-9]+.*|^hotfix\/.*|^bugfix\/.*' the check will not fail and PR working

Gitea Version

1.20.0

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

Screenshot1
Screenshot2

Git Version

No response

Operating System

linux

How are you running Gitea?

docker

Database

PostgreSQL

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions