Skip to content

Commit

Permalink
Also append actions required from releases of non-release vector repo…
Browse files Browse the repository at this point in the history
…s. (#42)
  • Loading branch information
Gerrit91 authored Jul 23, 2021
1 parent d28450c commit d039aed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/webhooks/github/actions/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (w *WebhookActions) ProcessReleaseEvent(payload *ghwebhooks.ReleasePayload)
RepositoryName: payload.Repository.Name,
TagName: payload.Release.TagName,
ComponentReleaseInfo: payload.Release.Body,
ReleaseURL: payload.Release.HTMLURL,
}
err := a.draft(ctx, params)
if err != nil {
Expand Down
42 changes: 37 additions & 5 deletions pkg/webhooks/github/actions/release_drafter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type releaseDrafterParams struct {
RepositoryName string
TagName string
ComponentReleaseInfo *string
ReleaseURL string
}

func newReleaseDrafter(logger *zap.SugaredLogger, client *clients.Github, rawConfig map[string]interface{}) (*releaseDrafter, error) {
Expand Down Expand Up @@ -91,8 +92,34 @@ func newReleaseDrafter(logger *zap.SugaredLogger, client *clients.Github, rawCon
func (r *releaseDrafter) draft(ctx context.Context, p *releaseDrafterParams) error {
_, ok := r.repoMap[p.RepositoryName]
if !ok {
r.logger.Debugw("skip adding release draft because not a release vector repo", "repo", p.RepositoryName, "release", p.TagName)
return nil
// if there is an ACTIONS_REQUIRED block, we want to add it (even when it's not a release vector repository)

if p.ComponentReleaseInfo == nil {
r.logger.Debugw("skip adding release draft because not a release vector repo and no special sections", "repo", p.RepositoryName, "release", p.TagName)
return nil
}

infos, err := r.releaseInfos(ctx)
if err != nil {
return err
}

m := markdown.Parse(infos.body)

var releaseSuffix *string
if p.ReleaseURL != "" {
tmp := fmt.Sprintf("([release notes](%s))", p.ReleaseURL)
releaseSuffix = &tmp
}
err = r.prependActionsRequired(m, *p.ComponentReleaseInfo, r.client.Organization(), releaseSuffix)
if err != nil {
r.logger.Debugw("skip adding release draft", "reason", err, "repo", p.RepositoryName)
return nil
}

body := m.String()

return r.createOrUpdateRelease(ctx, infos, body, p)
}

componentTag := p.TagName
Expand All @@ -112,12 +139,12 @@ func (r *releaseDrafter) draft(ctx context.Context, p *releaseDrafterParams) err
return err
}

body := r.updateReleaseBody(r.client.Organization(), infos.body, p.RepositoryName, componentSemver, p.ComponentReleaseInfo)
body := r.updateReleaseBody(r.client.Organization(), infos.body, p.RepositoryName, componentSemver, p.ComponentReleaseInfo, p.ReleaseURL)

return r.createOrUpdateRelease(ctx, infos, body, p)
}

func (r *releaseDrafter) updateReleaseBody(org string, priorBody string, component string, componentVersion semver.Version, componentBody *string) string {
func (r *releaseDrafter) updateReleaseBody(org string, priorBody string, component string, componentVersion semver.Version, componentBody *string, releaseURL string) string {
m := markdown.Parse(priorBody)

releaseSection := ensureReleaseSection(m, r.draftHeadline)
Expand Down Expand Up @@ -151,7 +178,12 @@ func (r *releaseDrafter) updateReleaseBody(org string, priorBody string, compone
body = append(body, l)
}

_ = r.prependActionsRequired(m, *componentBody, org, nil)
var releaseSuffix *string
if releaseURL != "" {
tmp := fmt.Sprintf("([release notes](%s))", releaseURL)
releaseSuffix = &tmp
}
_ = r.prependActionsRequired(m, *componentBody, org, releaseSuffix)
}

heading := fmt.Sprintf("%s v%s", component, componentVersion.String())
Expand Down
8 changes: 5 additions & 3 deletions pkg/webhooks/github/actions/release_drafter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestReleaseDrafter_updateReleaseBody(t *testing.T) {
component string
componentVersion semver.Version
componentBody *string
releaseURL string

want string
}{
Expand Down Expand Up @@ -160,9 +161,10 @@ Some description
component: "metal-robot",
componentVersion: semver.MustParse("0.2.5"),
componentBody: v3.String("## General Changes\r\n\r\n* Fix (#123) @Gerrit91\r\n```ACTIONS_REQUIRED\r\nAPI has changed\r\n```"),
releaseURL: "https://some-url",
want: `# General
## Required Actions
* API has changed
* API has changed ([release notes](https://some-url))
## Component Releases
### metal-robot v0.2.5
* Fix (metal-stack/metal-robot#123) @Gerrit91`,
Expand All @@ -175,12 +177,12 @@ Some description
client: nil,
draftHeadline: tt.headline,
}
res := r.updateReleaseBody(tt.org, tt.priorBody, tt.component, tt.componentVersion, tt.componentBody)
res := r.updateReleaseBody(tt.org, tt.priorBody, tt.component, tt.componentVersion, tt.componentBody, tt.releaseURL)
if diff := cmp.Diff(tt.want, res); diff != "" {
t.Errorf("ReleaseDrafter.updateReleaseBody(), diff: %v", diff)
t.Logf("want\n=====\n%s\n\ngot\n=====\n%s", tt.want, res)
}
idempotent := r.updateReleaseBody(tt.org, res, tt.component, tt.componentVersion, tt.componentBody)
idempotent := r.updateReleaseBody(tt.org, res, tt.component, tt.componentVersion, tt.componentBody, tt.releaseURL)
if diff := cmp.Diff(tt.want, idempotent); diff != "" {
t.Errorf("not idempotent: %v", diff)
t.Logf("want\n=====\n%s\n\ngot\n=====\n%s", tt.want, res)
Expand Down

0 comments on commit d039aed

Please sign in to comment.