#!/bin/sh

################################################################################
# Test that the changelog-generator produces the expected output.
################################################################################

set -e

rm -rf /tmp/test-changelog-generator.$$
mkdir -p /tmp/test-changelog-generator.$$

SRC_DIR="$(pwd)"
cd /tmp/test-changelog-generator.$$
git init
echo dummy > dummy
git add dummy
git commit -m 'Dummy commit'
git branch prev_branch
# This is due to upstream rename from master to main: Find out which is the
# default in this installation.
main=$(git symbolic-ref --short HEAD)

# The numbers that are sprinkled about below are just to make it easier to
# pinpoint where a particular line came from.

################################################################################

git commit --allow-empty -m 'Fix for changelog N1

Changelog: Title'

################################################################################

git commit --allow-empty -m 'Fix not for changelog N2

Not for changelog N3'

################################################################################

git commit --allow-empty -m 'Complete Fix for changelog N4

Should be included N5

Changelog: Commit'

################################################################################

git commit --allow-empty -m 'Message not for changelog N6

Not for changelog N7

Changelog: This should be in changelog N8
And this N9

But not this N10'

################################################################################

git commit --allow-empty -m 'Canceled fix for changelog N11

Not for changelog N12

Changelog: Commit'
git commit --allow-empty -m 'Disable changelog N13

Not for changelog N14

Cancel-Changelog: '`git rev-parse HEAD`

################################################################################

git commit --allow-empty -m 'Another canceled fix for changelog N15

Not for changelog N16

Changelog: Canceled entry should not be in changelog N17
(cherry picked from commit 3714b61bc4a139c8f58554052775699816e47b62)'
git commit --allow-empty -m 'Disable changelog N18

Not for changelog N19

Cancel-Changelog: 3714b61bc4a139c8f58554052775699816e47b62'

################################################################################

git commit --allow-empty -m 'Reverted fix for changelog N20

Not for changelog N21

Changelog: Commit'
git commit --allow-empty -m 'Disable changelog N22

Not for changelog N23

This reverts commit '`git rev-parse HEAD`'.'

################################################################################

git commit --allow-empty -m 'Another Reverted fix for changelog N24

Not for changelog N25

Changelog: Reverted entry should not be in changelog N26
(cherry picked from commit 13cf5bea954b2d55c2bb235f61316bb57b7d4189)'
git commit --allow-empty -m 'Disable changelog N27

Not for changelog N28

Changelog: Should be in changelog though. N29
This reverts commit 13cf5bea954b2d55c2bb235f61316bb57b7d4189.'

################################################################################

git commit --allow-empty -m 'Several entries for changelog N30

Should be included N31

Changelog: Commit
Changelog: Should also be included, but separately and not twice. N32

Changelog: This too. N33
Changelog: As well as this. N34

And finally this, part of the commit message. N35'

################################################################################

git commit --allow-empty -m 'Jira entry N36

MEN-645 Should be included N37

Changelog: Commit'

################################################################################

git commit --allow-empty -m 'MEN-76: Jira entry in title N38

Changelog: Title'

################################################################################

git commit --allow-empty -m 'MEN-123 Several Jira entries N39

Changelog: Should be there N40

Men-1234'

################################################################################

git commit --allow-empty -m 'Jira entries in parentheses (title) N41

Changelog: Title

Stuff. (Jira MEN-1234) (MEN-2345) N42'

################################################################################

git commit --allow-empty -m 'Jira entries inline (title) N43

Changelog: Title

Stuff Jira MEN-2345 N44'

################################################################################

git commit --allow-empty -m 'Jira entries in parentheses (commit) N45

Changelog: Commit

Stuff. (Jira MEN-4321) (MEN-54321) N46'

################################################################################

git commit --allow-empty -m 'Jira entries inline (commit) N47

Changelog: Commit

Stuff MEN-2345 N48'

################################################################################

git commit --allow-empty -m 'Jira MEN-1234: Jira twice (commit) N49

Changelog: Commit

More stuff. (Jira: MEN-2345) N50'

################################################################################

git commit --allow-empty -m 'MEN-1234: Jira duplicate (commit) N51

Changelog: Commit

More stuff. (MEN-1234) N52'

################################################################################

git commit --allow-empty -m 'Changelog commit with trailing dot. N53

Changelog: Commit.'

################################################################################

git commit --allow-empty -m 'Changelog title with trailing dot. N54

Changelog: Title.'

################################################################################

git commit --allow-empty -m 'Jira MEN-1111: Changelog with Jira. N55

jira archive-2
men-9
https://northerntech.atlassian.net/browse/men-1

Changelog: commit'

################################################################################

git commit --allow-empty -m 'Jira MEN-1111: Changelog with Jira tracker. N56

jira archive-2
men-9
https://northerntech.atlassian.net/browse/men-1

Changelog: commit'

################################################################################

git commit --allow-empty -m 'Jira MEN-1111: Changelog with many Jira refs. N57

Ref: jira: archive-2
Ref: men-9
Ref: https://northerntech.atlassian.net/browse/men-1
Ticket: MEN-12345

Changelog: commit'

################################################################################

git commit --allow-empty -m 'Changelog with inline Jira refs. N58a

Changelog: Some inline jira men-80 reference. N58b
Changelog: Some other archive-7777 reference. N59
Changelog: Some inline (jira men-81) reference. N60
Changelog: Some other (men-7778) reference. N61
Changelog: commit'

################################################################################

git commit --allow-empty -m 'Changelog with suspicious number in it. N62

1234

Changelog: commit'

################################################################################

git commit --allow-empty -m 'fix: Bump github.com/stretchr/testify from 1.5.1 to 1.6.0

Bumps [github.com/stretchr/testify](https://github.com/stretchr/testify) from 1.5.1 to 1.6.0.
- [Release notes](https://github.com/stretchr/testify/releases)
- [Commits](stretchr/testify@v1.5.1...v1.6.0)

Changelog: All
Ticket: None
Signed-off-by: dependabot-preview[bot] <support@dependabot.com>'

################################################################################

git commit --allow-empty -m 'Changelog with suspicious number outside of it. N63

2345

Changelog: title'

################################################################################

git commit --allow-empty -m 'Changelog with signed-off N64

Commit message
Signed-off-by: Hacker
Changelog: commit'

################################################################################

git commit --allow-empty -m 'Changelog with invalid signed-off N65

Commit message
Mention "Signed-off-by: " mid sentence
Changelog: commit'

################################################################################

# A bit tricky to make this commit, because normal git-commit cleans up \r.
# But it *is* in our Git history (1f900885d2aebda3e23ca5129d37f5e75eb83ee4)
TREE=$(git cat-file commit HEAD | grep '^tree ' | sed -e 's/^tree //')
git reset --hard $(git commit-tree -p $(git rev-parse HEAD) $TREE <<EOF
Changelog with Windows line endings N66

Changelog: title

stuff stuff
EOF
)

################################################################################

# We just generate an invalid GPG signature (it is from a random commit in core
# and won't be valid, but that doesn't matter).
TREE=$(git cat-file commit HEAD | grep '^tree ' | sed -e 's/^tree //')
git reset --hard $(git hash-object -w -t commit --stdin <<EOF
tree $TREE
parent $(git rev-parse HEAD)
author whatever <whatever@whatever.com> 1461929235 -0400
committer whatever <whatever@whatever.com> 1461929235 -0400
gpgsig -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1
 
 iQIcBAABAgAGBQJXI0UTAAoJEBHyPQpOS53uB9MP/06LBhE/i7BL37EANEMoUUM2
 gZwSiJ4HYF6lwNBBHNz/KYkUdh2B16m8mM1l9uePlMfhuA/xVlwP18shLaKDxdMA
 zqt3+x6nPMHyx/WrPgOt+4hCyp+AYX/VQ0SaJGGKoOY9MJ9uOichu8iEfrNU333M
 ePVKWRSA1p2+sxoq+oX63kx/x9/BK59GVKKF1/bsiAXjfNKfkmDLe4T4/0bpZYg1
 fDPXhUsHn2flF6sK3P/1hkgi/gJhsWRLZUqcj9HS3IwGSUGRIx5xzGvanD9nAnwg
 eeFRAQ6UbNN032yjKr2cs9HoJuHTwpk1t6bDX/Ti17rrDtvSKFLBxMOFGJFl09/k
 Ltmul9+KUbWZMVymtbWekgVA05FEBqpaJpZ89NvFM9vrl+lfn4AN84e1B/6M8jWI
 VSS/C4jyAlKl7YWqBbzSh/ES+JBX3JwRO+KYCBYOE97TRZtgeFeNzF8HaRO4Ndnp
 7uJmeMhXLpBwXU0vUmoCRSJGEacnCZqDdrCE2XoUjzl1DKrdJT6RDXkgwZa8Hu3A
 lEIXmIUMLTtbVSDOo+Ls5npaptlDOlG8cvCZFEcbKtjTfHcnpi8Kx+qNyGxuc79i
 oGDhfxWFpCJK+TsrXZHoU6/d0ROmGynxYXWutlStreGQjMucML5vOXkKI5OHflVN
 ioeZTiztfyD5AwfmioMb
 =k68w
 -----END PGP SIGNATURE-----

Changelog with GPG entry N67

Changelog: Commit
EOF
)

################################################################################

git commit --allow-empty -m 'MEN-1234 has no JIRA string in front.

Changelog: Make sure the bugtracker reference is taken from the title
           and goes after this message. The title should not be
           included. N68'

################################################################################

git commit --allow-empty -m 'Squashed commits N69

Changelog: Title

Signed-off-by: Joe Average <joe@average.com>

This is the real changelog entry of the second commit, but will be hidden because of the squash.

Changelog: Title

Signed-off-by: Joe Average <joe@average.com>'

################################################################################

git commit --allow-empty -m 'Commit with no changelog N70

Changelog: None
This line should not generate a changelog entry N71

Signed-off-by: Hacker'

################################################################################

git commit --allow-empty -m 'Commit with various "<something>-by:" tags

Changelog: Commit
Only this line should show in changelog N72

Signed-off-by: Test <test@test.com>
Reviewed-by: Test2 <test2@test.com>
Acked-by: Test3 <test3@test.com>'

################################################################################

git commit --allow-empty -m 'Changelog:All : Complete commit with inline entry N73

Should be included N74'

################################################################################

git commit --allow-empty -m 'fix: Bump github.com/klauspost/pgzip from 1.2.3 to 1.2.4

Bumps [github.com/klauspost/pgzip](https://github.com/klauspost/pgzip) from 1.2.3 to 1.2.4.
- [Release notes](https://github.com/klauspost/pgzip/releases)
- [Commits](klauspost/pgzip@v1.2.3...v1.2.4)

Changelog: All
Ticket: None
Signed-off-by: dependabot[bot] <support@github.com>'

################################################################################

git commit --allow-empty -m 'fix: Bump pytest-html in /tests/requirements

Bumps [pytest-html](https://github.com/pytest-dev/pytest-html) from 2.0.1 to 2.1.1.
- [Release notes](https://github.com/pytest-dev/pytest-html/releases)
- [Changelog](https://github.com/pytest-dev/pytest-html/blob/master/CHANGES.rst)
- [Commits](https://github.com/pytest-dev/pytest-html/compare/v2.0.1...v2.1.1)

Changelog: All
Ticket: None
Signed-off-by: dependabot[bot] <support@github.com>'

################################################################################

git commit --allow-empty -m 'Changelog: All bugs fixed N75

Should not be included N76'

################################################################################

git commit --allow-empty -m 'Changelog: AllSomethingElse N77

Should not be included N78'

################################################################################

git commit --allow-empty -m 'fix:Fix type N79

Should not be included N80

Changelog: Title'
git commit --allow-empty -m 'fix(foo): Fix type with scope N81

Should be included N82

Changelog: All'
git commit --allow-empty -m 'fix(foo): Fix type with scope, not included N83

Should not be included N84

Changelog: None'
git commit --allow-empty -m 'fIx(foo): Fix type with scope and uppercase letters N85

Should not be included N86

Changelog: Title'

################################################################################

git commit --allow-empty -m 'feat: Feat type N87

Should be included N88

Changelog: All'
git commit --allow-empty -m 'feat: Feat type, not included N89

Should not be included N90

Changelog: Should be a feat changelog N91'
git commit --allow-empty -m 'feat(foo): Feat type with scope, not included N92

Should not be included N93'
git commit --allow-empty -m 'feat(foo): Feat type with scope and refs N94

Should be included N95

Changelog: All
Ticket: men-0603
Signed-off-by: Ole Nordmann <ole.nordmann@norway.no>'
git commit --allow-empty -m 'FeAt: Feat type and uppercase letters N96

Changelog: All'

################################################################################

git commit --allow-empty -m 'feat(foo): Fix type with scope, some letters uppercase N97

Should not be included N98

Changelog: title
Changelog(fix): Custom changelog with keyword, should be fix N99
Changelog(feat): Custom changelog with keyword and ref, should be feat MEN-1234 N100
Changelog(fEaT): Custom changelog with keyword with uppercase letters and ref, should be feat N101'

################################################################################

git commit --allow-empty -m 'fix: Bump golang from 1.14 to 1.15.0

Bumps golang from 1.14 to 1.15.0.

Changelog: All
Ticket: None
Signed-off-by: dependabot[bot] <support@github.com>'

################################################################################

git commit --allow-empty -m 'fix: Bump mender-test-images from 457ea99 to 713c563

Bumps [tests/acceptance/image-tests](https://github.com/mendersoftware/mender-image-tests) from `457ea99` to `713c563`.
- [Release notes](https://github.com/mendersoftware/mender-image-tests/releases)
- [Commits](https://github.com/mendersoftware/mender-image-

Changelog: All
Ticket: None
Signed-off-by: dependabot[bot] <support@github.com>'

################################################################################

git commit --allow-empty -m 'feat: Bump paramiko from 2.9.1 to 2.9.2 in /tests/requirements

Bumps [paramiko](https://github.com/paramiko/paramiko) from 2.9.1 to 2.9.2.
- [Release notes](https://github.com/paramiko/paramiko/releases)
- [Changelog](https://github.com/paramiko/paramiko/blob/main/NEWS)
- [Commits](https://github.com/paramiko/paramiko/compare/2.9.1...2.9.2)

---
updated-dependencies:
- dependency-name: paramiko
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Changelog: All
Ticket: None
Signed-off-by: dependabot[bot] <support@github.com>'

################################################################################

git commit --allow-empty -m 'fix:cherry-picked fix N102

Should not appear, since it appears in prev_branch too.

Changelog: Title
Ticket:None'
git checkout prev_branch
git cherry-pick -x --allow-empty $main
git checkout $main

################################################################################

git commit --allow-empty -m 'fix: Ending with a code example N103

```
code example
```

Changelog: Commit
Ticket:MEN-1234'

################################################################################


git commit --allow-empty -m 'fix: Something enterprise N104

Changelog: Title
Ticket: ME-1234'

################################################################################

git commit --allow-empty -m 'feat(foo): Fix type with scope, some letters uppercase, and multiline N105

Should not be included N98

Changelog: title

Changelog(fix): Multiline changelog with keyword,
should be fix N106

Changelog(feat): Multiline changelog with keyword and ref,
should be feat MEN-1234 N107

Changelog(fEaT): Multiline changelog with keyword with uppercase letters and ref,
should be feat N108'

################################################################################

git commit --allow-empty -m 'feat: Title changelog with prefix N109

Content here.

Changelog(fix): Title'

################################################################################

git commit --allow-empty -m 'feat: Commit changelog with prefix N110

Content here.

Changelog(fix): Commit'

################################################################################

git commit --allow-empty -m 'feat: refac in changelog tag N111

Changelog(refac): Commit'

################################################################################

"$SRC_DIR/changelog-generator" --repo --sort-changelog prev_branch..$main > result.txt 2>stderr.txt || {
    echo "Script failed with $?"
    echo "--------"
    echo "result.txt:"
    cat result.txt
    echo "--------"
    echo "stderr.txt:"
    cat stderr.txt
    exit 1
}
cat > expected.txt <<EOF
### Changelogs

#### test-changelog-generator.$$ ($main)

New changes in test-changelog-generator.$$ since prev_branch:

##### Bug Fixes

* Fix type with scope and uppercase letters N85
* Fix type with scope N81

  Should be included N82
* Commit changelog with prefix N110

  Content here.
* Custom changelog with keyword, should be fix N99
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Ending with a code example N103

  \`\`\`
  code example
  \`\`\`
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Multiline changelog with keyword,
  should be fix N106
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Something enterprise N104
  ([ME-1234](https://northerntech.atlassian.net/browse/ME-1234))
* Title changelog with prefix N109
* Fix type N79
* Aggregated Dependabot Changelogs:
  * Bumps [github.com/klauspost/pgzip](https://github.com/klauspost/pgzip) from 1.2.3 to 1.2.4.
      - [Release notes](https://github.com/klauspost/pgzip/releases)
      - [Commits](klauspost/pgzip@v1.2.3...v1.2.4)
  * Bumps [github.com/stretchr/testify](https://github.com/stretchr/testify) from 1.5.1 to 1.6.0.
      - [Release notes](https://github.com/stretchr/testify/releases)
      - [Commits](stretchr/testify@v1.5.1...v1.6.0)
  * Bumps golang from 1.14 to 1.15.0.
  * Bumps [pytest-html](https://github.com/pytest-dev/pytest-html) from 2.0.1 to 2.1.1.
      - [Release notes](https://github.com/pytest-dev/pytest-html/releases)
      - [Changelog](https://github.com/pytest-dev/pytest-html/blob/master/CHANGES.rst)
      - [Commits](https://github.com/pytest-dev/pytest-html/compare/v2.0.1...v2.1.1)

##### Features

* Feat type and uppercase letters N96
* Feat type with scope and refs N94

  Should be included N95
  ([MEN-0603](https://northerntech.atlassian.net/browse/MEN-0603))
* Fix type with scope, some letters uppercase N97
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Fix type with scope, some letters uppercase, and multiline N105
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Custom changelog with keyword and ref, should be feat N100
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Custom changelog with keyword with uppercase letters and ref, should be feat N101
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Feat type N87

  Should be included N88
* Multiline changelog with keyword and ref,
  should be feat N107
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Multiline changelog with keyword with uppercase letters and ref,
  should be feat N108
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Should be a feat changelog N91
* Aggregated Dependabot Changelogs:
  * Bumps [paramiko](https://github.com/paramiko/paramiko) from 2.9.1 to 2.9.2.
      - [Release notes](https://github.com/paramiko/paramiko/releases)
      - [Changelog](https://github.com/paramiko/paramiko/blob/main/NEWS)
      - [Commits](https://github.com/paramiko/paramiko/compare/2.9.1...2.9.2)

      \`\`\`
      updated-dependencies:
      - dependency-name: paramiko
        dependency-type: direct:production
        update-type: version-update:semver-patch
      \`\`\`

##### Other

* All bugs fixed N75
* AllSomethingElse N77
* As well as this. N34
* Changelog commit with trailing dot. N53
* Changelog title with trailing dot. N54
* Changelog with GPG entry N67
* Changelog with Jira tracker. N56
  ([ARCHIVE-2](https://northerntech.atlassian.net/browse/ARCHIVE-2), [MEN-1111](https://northerntech.atlassian.net/browse/MEN-1111), [MEN-1](https://northerntech.atlassian.net/browse/MEN-1), [MEN-9](https://northerntech.atlassian.net/browse/MEN-9))
* Changelog with Jira. N55
  ([ARCHIVE-2](https://northerntech.atlassian.net/browse/ARCHIVE-2), [MEN-1111](https://northerntech.atlassian.net/browse/MEN-1111), [MEN-1](https://northerntech.atlassian.net/browse/MEN-1), [MEN-9](https://northerntech.atlassian.net/browse/MEN-9))
* Changelog with Windows line endings N66
* Changelog with inline Jira refs. N58a
  ([ARCHIVE-7777](https://northerntech.atlassian.net/browse/ARCHIVE-7777), [MEN-7778](https://northerntech.atlassian.net/browse/MEN-7778), [MEN-80](https://northerntech.atlassian.net/browse/MEN-80), [MEN-81](https://northerntech.atlassian.net/browse/MEN-81))
* Changelog with invalid signed-off N65

  Commit message
  Mention "Signed-off-by: " mid sentence
* Changelog with many Jira refs. N57
  ([ARCHIVE-2](https://northerntech.atlassian.net/browse/ARCHIVE-2), [MEN-1111](https://northerntech.atlassian.net/browse/MEN-1111), [MEN-12345](https://northerntech.atlassian.net/browse/MEN-12345), [MEN-1](https://northerntech.atlassian.net/browse/MEN-1), [MEN-9](https://northerntech.atlassian.net/browse/MEN-9))
* Changelog with signed-off N64

  Commit message
* Changelog with suspicious number in it. N62

  1234
* Changelog with suspicious number outside of it. N63
* Commit with various "<something>-by:" tags

  Only this line should show in changelog N72
* Complete Fix for changelog N4

  Should be included N5
* Complete commit with inline entry N73

  Should be included N74
* Fix for changelog N1
* Jira duplicate (commit) N51


  More stuff. N52
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Jira entries in parentheses (commit) N45


  Stuff. N46
  ([MEN-4321](https://northerntech.atlassian.net/browse/MEN-4321), [MEN-54321](https://northerntech.atlassian.net/browse/MEN-54321))
* Jira entries in parentheses (title) N41
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234), [MEN-2345](https://northerntech.atlassian.net/browse/MEN-2345))
* Jira entries inline (commit) N47


  Stuff N48
  ([MEN-2345](https://northerntech.atlassian.net/browse/MEN-2345))
* Jira entries inline (title) N43
  ([MEN-2345](https://northerntech.atlassian.net/browse/MEN-2345))
* Jira entry N36

  Should be included N37
  ([MEN-645](https://northerntech.atlassian.net/browse/MEN-645))
* Jira entry in title N38
  ([MEN-76](https://northerntech.atlassian.net/browse/MEN-76))
* Jira twice (commit) N49


  More stuff. N50
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234), [MEN-2345](https://northerntech.atlassian.net/browse/MEN-2345))
* Make sure the bugtracker reference is taken from the title
             and goes after this message. The title should not be
             included. N68
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234))
* Several entries for changelog N30

  Should be included N31



  And finally this, part of the commit message. N35
* Should also be included, but separately and not twice. N32
* Should be in changelog though. N29
* Should be there N40
  ([MEN-1234](https://northerntech.atlassian.net/browse/MEN-1234), [MEN-123](https://northerntech.atlassian.net/browse/MEN-123))
* Some inline reference. N58b
  ([ARCHIVE-7777](https://northerntech.atlassian.net/browse/ARCHIVE-7777), [MEN-7778](https://northerntech.atlassian.net/browse/MEN-7778), [MEN-80](https://northerntech.atlassian.net/browse/MEN-80), [MEN-81](https://northerntech.atlassian.net/browse/MEN-81))
* Some inline reference. N60
  ([ARCHIVE-7777](https://northerntech.atlassian.net/browse/ARCHIVE-7777), [MEN-7778](https://northerntech.atlassian.net/browse/MEN-7778), [MEN-80](https://northerntech.atlassian.net/browse/MEN-80), [MEN-81](https://northerntech.atlassian.net/browse/MEN-81))
* Some other reference. N59
  ([ARCHIVE-7777](https://northerntech.atlassian.net/browse/ARCHIVE-7777), [MEN-7778](https://northerntech.atlassian.net/browse/MEN-7778), [MEN-80](https://northerntech.atlassian.net/browse/MEN-80), [MEN-81](https://northerntech.atlassian.net/browse/MEN-81))
* Some other reference. N61
  ([ARCHIVE-7777](https://northerntech.atlassian.net/browse/ARCHIVE-7777), [MEN-7778](https://northerntech.atlassian.net/browse/MEN-7778), [MEN-80](https://northerntech.atlassian.net/browse/MEN-80), [MEN-81](https://northerntech.atlassian.net/browse/MEN-81))
* Squashed commits N69
* Squashed commits N69
* This should be in changelog N8
  And this N9
* This too. N33
* refac in changelog tag N111

EOF
diff -u expected.txt result.txt

# After filtering out these lines, there should be nothing left.
if [ $(egrep -v '^\*\*\* Commit [0-9a-f]{40} had a number 1234 which may be a ticket reference we missed\. Should be manually checked\.$' stderr.txt \
             | egrep -v '^\*\*\* Commit [0-9a-f]{40} had conflicting changelog tags\. This might be a squashed commit which will not work correctly with changelogs\. Should be manually checked\.$' \
             | egrep -v '^Checking .: HEAD:$' \
             | egrep -v '^commit |^Author: |^Date: |^    |^---$|^$' \
             | wc -c) -ne 0 ]
then
    echo 'Unexpected error message in:'
    cat stderr.txt
    exit 1
fi

################################################################################
# Test Github integration.
################################################################################

mkdir mender
( cd mender && "$SRC_DIR/changelog-generator" --repo --sort-changelog --query-github 3.3.0..3.4.0 > ../result.txt 2>../stderr.txt ) || {
    echo "Script failed with $?"
    echo "--------"
    echo "result.txt:"
    cat result.txt
    echo "--------"
    echo "stderr.txt:"
    cat stderr.txt
    exit 1
}

cat > expected.txt <<'EOF'
### Changelogs

#### mender (3.4.0)

New changes in mender since 3.3.0:

##### Bug Fixes

* Append log entries to syslog at the correct level
* By default we bind to 127.0.0.1:0 instead of localhost.
* If paused before ArtifactReboot, and then manually
  rebooting the device outside of Mender, the client will properly
  resume the update now, instead of failing and rolling back.
  ([MEN-5709](https://northerntech.atlassian.net/browse/MEN-5709))
* Over D-Bus we return the ProxyHost equal to 127.0.0.1
* Resolve symlinks for /dev/disk/by-partlabel
* The client update and inventory checks are now unaffected by the use
  of the `check-update` and `send-inventory` commands. While previously, this
  would both move the intervals at which checks we're done, and also extend them
  beyond the expected polling intervals configured.
  ([INF-1000](https://northerntech.atlassian.net/browse/INF-1000), [MEN-5547](https://northerntech.atlassian.net/browse/MEN-5547))
* Upgrade openssl dependency to fix cast error in recent Go.
* rootfs-image-v2: Make sure to set mender_boot_part_hex
* setenv: Fix script input syntax.

##### Features

* Add DaemonLogLevel parameter in configuration file
  ([MEN-5583](https://northerntech.atlassian.net/browse/MEN-5583))

##### Other

* Upgrade mender-artifact library to latest

EOF
diff -u expected.txt result.txt

# There should be no errors.
if [ "$(cat stderr.txt)" != "" ]; then
    echo "Unexpected stderr output after querying GitHub:"
    cat stderr.txt
    exit 1
fi

################################################################################

"$SRC_DIR/changelog-generator" --repo --sort-changelog --query-github --github-repo mender-artifact 3.8.0..3.8.1 > result.txt 2>stderr.txt || {
    echo "Script failed with $?"
    echo "--------"
    echo "result.txt:"
    cat result.txt
    echo "--------"
    echo "stderr.txt:"
    cat stderr.txt
    exit 1
}

cat > expected.txt <<'EOF'
### Changelogs

#### mender-artifact (3.8.1)

New changes in mender-artifact since 3.8.0:

##### Bug Fixes

* fixed an issue that prevented running mender-artifact in a
  container
* fixed an issue that prevented the makefile from working
  with newer docker versions

EOF
diff -u expected.txt result.txt

# There should be no errors.
if [ "$(cat stderr.txt)" != "" ]; then
    echo "Unexpected stderr output after querying GitHub:"
    cat stderr.txt
    exit 1
fi

################################################################################

rm -rf /tmp/test-changelog-generator.$$

echo PASS
