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

Sort issue comments manually. #69

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/config/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type DocsPreviewCommentConfig struct {
}

type AggregateReleasesConfig struct {
TargetRepositoryName string `mapstructure:"repository" description:"the name of the taget repo"`
TargetRepositoryName string `mapstructure:"repository" description:"the name of the target repo"`
TargetRepositoryURL string `mapstructure:"repository-url" description:"the url of the target repo"`
Branch *string `mapstructure:"branch" description:"the branch to push in the target repo"`
BranchBase *string `mapstructure:"branch-base" description:"the base branch to raise the pull request against"`
Expand All @@ -44,7 +44,7 @@ type DistributeReleasesConfig struct {
}

type YAMLTranslateReleasesConfig struct {
TargetRepositoryName string `mapstructure:"repository" description:"the name of the taget repo"`
TargetRepositoryName string `mapstructure:"repository" description:"the name of the target repo"`
TargetRepositoryURL string `mapstructure:"repository-url" description:"the url of the target repo"`
Branch *string `mapstructure:"branch" description:"the branch to push in the target repo"`
BranchBase *string `mapstructure:"branch-base" description:"the base branch to raise the pull request against"`
Expand All @@ -55,7 +55,7 @@ type YAMLTranslateReleasesConfig struct {

type YAMLTranslation struct {
From YAMLTranslationRead `mapstructure:"from" description:"the yaml path from where to read the replacement value"`
To []Modifier `mapstructure:"to" description:"the actions to take on the traget repo with the read the replacement value"`
To []Modifier `mapstructure:"to" description:"the actions to take on the target repo with the read the replacement value"`
}

type YAMLTranslationRead struct {
Expand Down
13 changes: 12 additions & 1 deletion pkg/webhooks/github/actions/aggregate_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"net/url"
"sort"
"strings"
"sync"

Expand Down Expand Up @@ -51,7 +52,7 @@ func NewAggregateReleases(logger *slog.Logger, client *clients.Github, rawConfig
)

var typedConfig config.AggregateReleasesConfig
err := mapstructure.Decode(rawConfig, &typedConfig)
err := mapstructure.Decode(rawConfig, &typedConfig) // nolint:musttag
majst01 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -246,6 +247,10 @@ func isReleaseFreeze(ctx context.Context, client *v3.Client, number int, owner,
return true, fmt.Errorf("unable to list pull request comments: %w", err)
}

// somehow the direction parameter has no effect, it's always sorted in the same way?
// therefore sorting manually:
sort.Slice(comments, sortComments(comments))

for _, comment := range comments {
comment := comment

Expand All @@ -260,3 +265,9 @@ func isReleaseFreeze(ctx context.Context, client *v3.Client, number int, owner,

return false, nil
}

func sortComments(comments []*v3.IssueComment) func(i, j int) bool {
return func(i, j int) bool {
return comments[j].CreatedAt.Before(comments[i].CreatedAt.Time)
}
}
53 changes: 53 additions & 0 deletions pkg/webhooks/github/actions/aggregate_releases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package actions

import (
"sort"
"testing"
"time"

"github.com/google/go-cmp/cmp"
v3 "github.com/google/go-github/v57/github"
"github.com/metal-stack/metal-lib/pkg/pointer"
)

func Test_sortComments(t *testing.T) {
now := time.Now()

tests := []struct {
name string
comments []*v3.IssueComment
want []*v3.IssueComment
}{
{
name: "newest comment should appear first in the list",
comments: []*v3.IssueComment{
{
ID: pointer.Pointer(int64(1)),
CreatedAt: &v3.Timestamp{Time: now.Add(-3 * time.Minute)},
},
{
ID: pointer.Pointer(int64(2)),
CreatedAt: &v3.Timestamp{Time: now.Add(2 * time.Minute)},
},
},
want: []*v3.IssueComment{
{
ID: pointer.Pointer(int64(2)),
CreatedAt: &v3.Timestamp{Time: now.Add(2 * time.Minute)},
},
{
ID: pointer.Pointer(int64(1)),
CreatedAt: &v3.Timestamp{Time: now.Add(-3 * time.Minute)},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sort.Slice(tt.comments, sortComments(tt.comments))
if diff := cmp.Diff(tt.comments, tt.want); diff != "" {
t.Errorf("diff: %s", diff)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/webhooks/github/actions/distribute_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newDistributeReleases(logger *slog.Logger, client *clients.Github, rawConfi
)

var typedConfig config.DistributeReleasesConfig
err := mapstructure.Decode(rawConfig, &typedConfig)
err := mapstructure.Decode(rawConfig, &typedConfig) // nolint:musttag
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhooks/github/actions/yaml_translate_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func newYAMLTranslateReleases(logger *slog.Logger, client *clients.Github, rawCo
)

var typedConfig config.YAMLTranslateReleasesConfig
err := mapstructure.Decode(rawConfig, &typedConfig)
err := mapstructure.Decode(rawConfig, &typedConfig) // nolint:musttag
if err != nil {
return nil, err
}
Expand Down