diff --git a/pkg/jira_changelog/generator.go b/pkg/jira_changelog/generator.go index 1ae9809..f5b1336 100644 --- a/pkg/jira_changelog/generator.go +++ b/pkg/jira_changelog/generator.go @@ -29,11 +29,11 @@ const ( Initial = State("initial") CommitsFetched = State("commits_fetched") JiraIssuesFetched = State("jira_issues_fetched") - ChangelogRecored = State("changelog_recorded") + ChangesRecorded = State("changes_recorded") FetchCommits = Event("fetch_commits") FetchJiraIssues = Event("fetch_jira_issues") - RecordChangelog = Event("record_changelog") + RecordChanges = Event("record_changes") ) func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Generator { @@ -51,7 +51,7 @@ func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Gener fsm.Events{ {Name: FetchCommits, Src: []string{Initial}, Dst: CommitsFetched}, {Name: FetchJiraIssues, Src: []string{CommitsFetched}, Dst: JiraIssuesFetched}, - {Name: RecordChangelog, Src: []string{JiraIssuesFetched}, Dst: ChangelogRecored}, + {Name: RecordChanges, Src: []string{JiraIssuesFetched}, Dst: ChangesRecorded}, }, fsm.Callbacks{ Before(FetchCommits): func(ctx context.Context, e *fsm.Event) { @@ -71,7 +71,7 @@ func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Gener } g.jiraIssues = lo.Uniq(issues) }, - RecordChangelog: func(ctx context.Context, e *fsm.Event) { + RecordChanges: func(ctx context.Context, e *fsm.Event) { slog.Debug("Total jira issues ids", "count", len(g.jiraIssues)) slog.Debug("Recroding changelog") @@ -82,7 +82,6 @@ func NewGenerator(jiraConfig jira.Config, fromRef, toRef, repoURL string) *Gener }, }, ) - return g } @@ -95,7 +94,7 @@ func panicIfErr(err error) { func (c *Generator) Generate(ctx context.Context) *Changelog { panicIfErr(c.FSM.Event(ctx, FetchCommits)) panicIfErr(c.FSM.Event(ctx, FetchJiraIssues)) - panicIfErr(c.FSM.Event(ctx, RecordChangelog)) + panicIfErr(c.FSM.Event(ctx, RecordChanges)) slog.Debug("Total changelog items", "count", len(c.changes)) diff --git a/pkg/jira_changelog/generator_test.go b/pkg/jira_changelog/generator_test.go index 5cc6e83..3572a2d 100644 --- a/pkg/jira_changelog/generator_test.go +++ b/pkg/jira_changelog/generator_test.go @@ -1,6 +1,7 @@ package jira_changelog import ( + "context" "testing" "time" @@ -10,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestChangelogFromCommits(t *testing.T) { +func TestFetchJiraIssuesEvent(t *testing.T) { commits := []git.Commit{ {Time: time.Now(), Message: "[TEST-1234] commit message1", Sha: "3245vw"}, {Time: time.Now(), Message: "[TEST-4546] commit message sample1", Sha: "3245vw"}, @@ -21,6 +22,42 @@ func TestChangelogFromCommits(t *testing.T) { {Time: time.Now(), Message: "foobar commit message random", Sha: "3245vw"}, } + jiraIssues := []jira.Issue{ + jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"), + jira.NewIssue("TEST-4546", "Ticket description for 4546 issue", "done", "Epic2"), + jira.NewIssue("TEST-12345", "Ticket description of another card from same epic", "done", "Epic1"), + jira.NewIssue("", "[NO-CARD] commit message random (3245vw)", "done", ""), + jira.NewIssue("", "foobar commit message random (3245vw)", "done", ""), + } + + mockedClient := mocks.NewClient(t) + mockedClient.On("FetchIssue", "TEST-1234").Return(jiraIssues[0], nil).Twice() + mockedClient.On("FetchIssue", "TEST-4546").Return(jiraIssues[1], nil).Twice() + mockedClient.On("FetchIssue", "TEST-12345").Return(jiraIssues[2], nil) + + // Setup + generator := NewGenerator(jira.Config{}, "fromRef", "toRef", "http://example-repo.com") + generator.client = mockedClient + generator.commits = commits + generator.FSM.SetState(CommitsFetched) + + // invoke event + err := generator.FSM.Event(context.Background(), FetchJiraIssues) + + assert.NoError(t, err) + assert.Equal(t, len(jiraIssues), len(generator.jiraIssues)) + assert.Equal(t, jiraIssues, generator.jiraIssues) + assert.Equal(t, generator.FSM.Current(), JiraIssuesFetched) +} + +func TestRecordChangeLogEvent(t *testing.T) { + issues := []jira.Issue{ + jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"), + jira.NewIssue("TEST-12345", "Ticket description of another from same epic", "done", "Epic1"), + jira.NewIssue("TEST-4546", "Ticket description for 4546 issue", "done", "Epic2"), + jira.NewIssue("", "[NO-CARD] commit message random (3245vw)", "done", ""), + jira.NewIssue("", "foobar commit message random (3245vw)", "done", ""), + } expected := Changes{ "Epic1": { jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"), @@ -35,15 +72,12 @@ func TestChangelogFromCommits(t *testing.T) { }, } - mockedClient := mocks.NewClient(t) - mockedClient.On("FetchIssue", "TEST-1234").Return(jira.NewIssue("TEST-1234", "Ticket description", "done", "Epic1"), nil).Twice() - mockedClient.On("FetchIssue", "TEST-4546").Return(jira.NewIssue("TEST-4546", "Ticket description for 4546 issue", "done", "Epic2"), nil).Twice() - mockedClient.On("FetchIssue", "TEST-12345").Return(jira.NewIssue("TEST-12345", "Ticket description of another from same epic", "done", "Epic1"), nil) - generator := Generator{} - generator.client = mockedClient - - result, err := generator.fetchJiraIssues(commits) + generator := NewGenerator(jira.Config{}, "fromRef", "toRef", "http://example-repo.com") + generator.jiraIssues = issues + generator.FSM.SetState(JiraIssuesFetched) + generator.FSM.Event(context.Background(), RecordChanges) - assert.NoError(t, err) - assert.Equal(t, expected, result) + actual := generator.changes + assert.Equal(t, expected, actual) + assert.Equal(t, generator.FSM.Current(), ChangesRecorded) }