1#!/bin/bash
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15DEPENDABOT_COMMITER='GitHub <noreply@github.com>'
16DEPENDABOT_AUTHOR='dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>'
17
18if [[ -n "$1" ]]; then
19    commits=$(git show -s --format=%h ${1}~..HEAD)
20else
21    parents=(`git log -n 1 --format=%p HEAD`)
22    if [[ "${#parents[@]}" -ne 2 ]]; then
23        echo "HEAD is not a merge commit, please supply the oldest SHA"
24        exit 1
25    fi
26    commits=$(git show -s --format=%h ${parents[0]}..${parents[1]})
27fi
28
29if [[ -z "${commits}" ]]; then
30  echo "No commits found in this PR!"
31  exit 1
32fi
33
34for sha in $commits; do
35  author="$(git show -s --format="%an <%ae>" ${sha})"
36  committer="$(git show -s --format="%cn <%ce>" ${sha})"
37
38  if [[ "${committer}" == "${DEPENDABOT_COMMITER}" ]] &&
39     [[ "${author}" == "${DEPENDABOT_AUTHOR}" ]]; then
40    continue
41  fi
42
43  author="Signed-off-by: ${author}"
44  committer="Signed-off-by: ${committer}"
45
46  lines="$(git show -s --format=%B ${sha})"
47
48  found_author=false
49  # Don't enforce committer email on forks; this primarily avoids issues
50  # running workflows on the zephyr fork, because rebases done in the GH UX
51  # use the primary email of the committer, which might not match the one
52  # used in git CLI.
53  if [[ $GITHUB_REPOSITORY == mcu-tools/* ]]; then
54    found_committer=false
55  else
56    found_committer=true
57  fi
58
59  IFS=$'\n'
60  for line in ${lines}; do
61    stripped=$(echo $line | sed -e 's/^\s*//' | sed -e 's/\s*$//')
62    if [[ "${stripped}" == "${author}" ]]; then
63      found_author=true
64    fi
65    if [[ "${stripped}" == "${committer}" ]]; then
66      found_committer=true
67    fi
68
69    [[ ${found_author} == true && ${found_committer} == true ]] && break
70  done
71
72  if [[ ${found_author} == false ]]; then
73    echo -e "Missing \"${author}\" in commit ${sha}"
74  fi
75  if [[ ${found_committer} == false ]]; then
76    echo -e "Missing \"${committer}\" in commit ${sha}"
77  fi
78  if [[ ${found_author} == false || ${found_committer} == false ]]; then
79    exit 1
80  fi
81done
82