From db8f6260554f5d0e9a48c8f1dffa3dc5c99d7f89 Mon Sep 17 00:00:00 2001 From: Luca Pino Date: Thu, 22 Oct 2020 19:15:25 +0100 Subject: [PATCH] Fix error when there is only 1 commit --- notifier/github/commits.go | 4 ++-- notifier/github/commits_test.go | 9 +++++++++ notifier/gitlab/commits.go | 4 ++-- notifier/gitlab/commits_test.go | 9 +++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/notifier/github/commits.go b/notifier/github/commits.go index c8c2fad..13bdc29 100644 --- a/notifier/github/commits.go +++ b/notifier/github/commits.go @@ -37,8 +37,8 @@ func (g *CommitsService) lastOne(commits []string, revision string) (string, err if revision == "" { return "", errors.New("no revision specified") } - if len(commits) == 0 { - return "", errors.New("no commits") + if len(commits) < 2 { + return "", errors.New("not enough commits") } // e.g. // a0ce5bf 2018/04/05 20:50:01 (HEAD -> master, origin/master) diff --git a/notifier/github/commits_test.go b/notifier/github/commits_test.go index 7035367..87455a8 100644 --- a/notifier/github/commits_test.go +++ b/notifier/github/commits_test.go @@ -70,6 +70,15 @@ func TestCommitsLastOne(t *testing.T) { lastRev: "", ok: false, }, + { + // one commit + commits: []string{ + "04e0917e448b662c2b16330fad50e97af16ff27a", + }, + revision: "04e0917e448b662c2b16330fad50e97af16ff27a", + lastRev: "", + ok: false, + }, } for _, testCase := range testCases { diff --git a/notifier/gitlab/commits.go b/notifier/gitlab/commits.go index 41476f0..bf33d77 100644 --- a/notifier/gitlab/commits.go +++ b/notifier/gitlab/commits.go @@ -33,8 +33,8 @@ func (g *CommitsService) lastOne(commits []string, revision string) (string, err if revision == "" { return "", errors.New("no revision specified") } - if len(commits) == 0 { - return "", errors.New("no commits") + if len(commits) < 2 { + return "", errors.New("not enough commits") } return commits[1], nil diff --git a/notifier/gitlab/commits_test.go b/notifier/gitlab/commits_test.go index 7395004..0cbe947 100644 --- a/notifier/gitlab/commits_test.go +++ b/notifier/gitlab/commits_test.go @@ -70,6 +70,15 @@ func TestCommitsLastOne(t *testing.T) { lastRev: "", ok: false, }, + { + // one commit + commits: []string{ + "04e0917e448b662c2b16330fad50e97af16ff27a", + }, + revision: "04e0917e448b662c2b16330fad50e97af16ff27a", + lastRev: "", + ok: false, + }, } for _, testCase := range testCases {