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

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

Closed
sgabenov opened this issue Jul 25, 2023 · 7 comments · Fixed by #26259
Closed

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

sgabenov opened this issue Jul 25, 2023 · 7 comments · Fixed by #26259
Labels
Milestone

Comments

@sgabenov
Copy link

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

@KazzmanK
Copy link
Contributor

Don't 502 error comes from nginx?
Try to reproduce the same without nginx in between, or check gitea logs for more info.

@sgabenov
Copy link
Author

The same behaviour when connecting directly to Gitea

@CaiCandong
Copy link
Member

Following the reproduction steps you gave , I can reproduce reproduce the problem, but the error code is 500, and because of the git hook, the PR is not actually created successfully, but it is recorded in the PR list.

  • Log messages when PR creation fails
    image

  • PR records
    image

  • PR not exists
    image

@CaiCandong
Copy link
Member

The same behaviour when connecting directly to Gitea

Did you use gitea directly to get a result of 500? Something like this?

  • PR not exists
    image

@sgabenov
Copy link
Author

@CaiCandong Initially i used gitea with nginx reverse proxy, when 502 error happened. After that i used direct connection to host and got the same behaviour as you posted.

@sgabenov
Copy link
Author

@CaiCandong on gitea 1.19 the same hook worked a bit different. Gitea did not show 500 error and show git remote message in log, instead gitea showed this git remote message on Gitea web-page before creating PR. And this was good solution, as i can see the reason of error and fix it.

@CaiCandong
Copy link
Member

@CaiCandong on gitea 1.19 the same hook worked a bit different. Gitea did not show 500 error and show git remote message in log, instead gitea showed this git remote message on Gitea web-page before creating PR. And this was good solution, as i can see the reason of error and fix it.

I've located the problem and will fix it soon

@lunny lunny added this to the 1.20.4 milestone Aug 9, 2023
silverwind pushed a commit that referenced this issue Aug 10, 2023
Fix #26129
Replace #26258 

This PR will introduce a transaction on creating pull request so that if
some step failed, it will rollback totally. And there will be no dirty
pull request exist.

---------

Co-authored-by: Giteabot <teabot@gitea.io>
lunny added a commit to lunny/gitea that referenced this issue Aug 10, 2023
…ea#26259)

Fix go-gitea#26129
Replace go-gitea#26258

This PR will introduce a transaction on creating pull request so that if
some step failed, it will rollback totally. And there will be no dirty
pull request exist.

---------

Co-authored-by: Giteabot <teabot@gitea.io>
@delvh delvh modified the milestones: 1.20.4, 1.20.3 Aug 10, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
5 participants