From 902be142ba2027d20e23c9e1901a85259a616f44 Mon Sep 17 00:00:00 2001 From: Gerrit91 Date: Tue, 2 Apr 2024 13:49:07 +0200 Subject: [PATCH 1/3] Sort issue comments manually. --- .../github/actions/aggregate_releases.go | 11 ++++ .../github/actions/aggregate_releases_test.go | 53 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 pkg/webhooks/github/actions/aggregate_releases_test.go diff --git a/pkg/webhooks/github/actions/aggregate_releases.go b/pkg/webhooks/github/actions/aggregate_releases.go index bb99f7e..7c31a7d 100644 --- a/pkg/webhooks/github/actions/aggregate_releases.go +++ b/pkg/webhooks/github/actions/aggregate_releases.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "net/url" + "sort" "strings" "sync" @@ -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 @@ -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) + } +} diff --git a/pkg/webhooks/github/actions/aggregate_releases_test.go b/pkg/webhooks/github/actions/aggregate_releases_test.go new file mode 100644 index 0000000..ad96e46 --- /dev/null +++ b/pkg/webhooks/github/actions/aggregate_releases_test.go @@ -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) + } + }) + } +} From b439e3aac4d88facdbc05f94280478c5dd7c9214 Mon Sep 17 00:00:00 2001 From: Gerrit91 Date: Tue, 2 Apr 2024 14:13:55 +0200 Subject: [PATCH 2/3] Ignore linter error. --- pkg/config/actions.go | 6 +++--- pkg/webhooks/github/actions/aggregate_releases.go | 2 +- pkg/webhooks/github/actions/distribute_releases.go | 2 +- pkg/webhooks/github/actions/yaml_translate_releases.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/config/actions.go b/pkg/config/actions.go index 56a7f5c..de14aff 100644 --- a/pkg/config/actions.go +++ b/pkg/config/actions.go @@ -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"` @@ -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"` @@ -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 { diff --git a/pkg/webhooks/github/actions/aggregate_releases.go b/pkg/webhooks/github/actions/aggregate_releases.go index 7c31a7d..e08fda1 100644 --- a/pkg/webhooks/github/actions/aggregate_releases.go +++ b/pkg/webhooks/github/actions/aggregate_releases.go @@ -52,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 if err != nil { return nil, err } diff --git a/pkg/webhooks/github/actions/distribute_releases.go b/pkg/webhooks/github/actions/distribute_releases.go index 37314c9..67807c6 100644 --- a/pkg/webhooks/github/actions/distribute_releases.go +++ b/pkg/webhooks/github/actions/distribute_releases.go @@ -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 } diff --git a/pkg/webhooks/github/actions/yaml_translate_releases.go b/pkg/webhooks/github/actions/yaml_translate_releases.go index 5a03146..165357f 100644 --- a/pkg/webhooks/github/actions/yaml_translate_releases.go +++ b/pkg/webhooks/github/actions/yaml_translate_releases.go @@ -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 } From 051f46de1ed793273d73e0e5ec392a7d72569ae8 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Wed, 3 Apr 2024 11:38:56 +0200 Subject: [PATCH 3/3] Last fixes --- README.md | 2 +- pkg/clients/github.go | 2 +- pkg/webhooks/github/actions/release_drafter.go | 10 ++++------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 555a951..ba72e05 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ make swap # this requires: # - Ubuntu & Docker # - KUBECONFIG env var points to the cluster where the robot is deployed -# - the deplyoment in the cluster needs to be called "metal-robot" +# - the deployment in the cluster needs to be called "metal-robot" # # when you are done, exit the container shell # if your session was really long, telepresence sometimes does not swap diff --git a/pkg/clients/github.go b/pkg/clients/github.go index d68c0f5..45d829e 100644 --- a/pkg/clients/github.go +++ b/pkg/clients/github.go @@ -59,7 +59,7 @@ func (a *Github) initClients() error { a.atr = atr a.itr = itr - a.logger.Info("successfully initalized github app client", "organization-id", a.organizationID, "installation-id", a.installationID, "expected-events", installation.Events) + a.logger.Info("successfully initialized github app client", "organization-id", a.organizationID, "installation-id", a.installationID, "expected-events", installation.Events) return nil } diff --git a/pkg/webhooks/github/actions/release_drafter.go b/pkg/webhooks/github/actions/release_drafter.go index 202d67b..fc31102 100644 --- a/pkg/webhooks/github/actions/release_drafter.go +++ b/pkg/webhooks/github/actions/release_drafter.go @@ -14,8 +14,6 @@ import ( "github.com/metal-stack/metal-robot/pkg/markdown" "github.com/metal-stack/metal-robot/pkg/utils" "github.com/mitchellh/mapstructure" - - v3 "github.com/google/go-github/v57/github" ) var ( @@ -393,7 +391,7 @@ func ensureReleaseSection(m *markdown.Markdown, headline string) *markdown.Markd } type releaseInfos struct { - existing *v3.RepositoryRelease + existing *github.RepositoryRelease releaseTag string body string } @@ -481,10 +479,10 @@ func (r *releaseDrafter) createOrUpdateRelease(ctx context.Context, infos *relea r.logger.Info("release draft updated", "repository", r.repoName, "trigger-component", p.RepositoryName, "version", p.TagName) } else { newDraft := &github.RepositoryRelease{ - TagName: v3.String(infos.releaseTag), - Name: v3.String(fmt.Sprintf(r.titleTemplate, infos.releaseTag)), + TagName: github.String(infos.releaseTag), + Name: github.String(fmt.Sprintf(r.titleTemplate, infos.releaseTag)), Body: &body, - Draft: v3.Bool(true), + Draft: github.Bool(true), } _, _, err := r.client.GetV3Client().Repositories.CreateRelease(ctx, r.client.Organization(), r.repoName, newDraft) if err != nil {