Skip to content

Commit

Permalink
chore: explicitly fetch pull request labels
Browse files Browse the repository at this point in the history
Fetch the PR labels explicitly and not for
`github.event.pull_request.labels`. This means any labels added after
the pull request have been created are available to a job re-run.

Signed-off-by: Noel Georgi <git@frezbo.dev>
  • Loading branch information
frezbo committed Oct 27, 2023
1 parent e421f6f commit d577d4d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 41 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-10-04T07:25:28Z by kres bf04273-dirty.
# Generated on 2023-10-27T10:34:11Z by kres b20f191-dirty.

name: default
concurrency:
group: ${{ github.event.label == null && github.head_ref || github.run_id }}
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true
"on":
push:
Expand All @@ -20,8 +20,10 @@ concurrency:
jobs:
default:
permissions:
actions: read
contents: write
packages: write
pull-requests: read
runs-on:
- self-hosted
- generic
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-10-21T08:25:07Z by kres e863876-dirty.
# Generated on 2023-10-27T10:34:11Z by kres b20f191-dirty.

# common variables

Expand All @@ -19,7 +19,7 @@ GRPC_GO_VERSION ?= 1.3.0
GRPC_GATEWAY_VERSION ?= 2.18.0
VTPROTOBUF_VERSION ?= 0.5.0
DEEPCOPY_VERSION ?= v0.5.5
GOLANGCILINT_VERSION ?= v1.55.0
GOLANGCILINT_VERSION ?= v1.55.1
GOFUMPT_VERSION ?= v0.5.0
GO_VERSION ?= 1.21.3
GOIMPORTS_VERSION ?= v0.14.0
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.3

require (
github.com/drone/drone-yaml v1.2.3
github.com/go-git/go-git/v5 v5.9.0
github.com/go-git/go-git/v5 v5.10.0
github.com/google/go-github/v55 v55.0.0
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/siderolabs/gen v0.4.7
Expand Down Expand Up @@ -42,7 +42,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66D
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20230305113008-0c11038e723f h1:Pz0DHeFij3XFhoBRGUDPzSJ+w2UcK5/0JvF8DRI58r8=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20230305113008-0c11038e723f/go.mod h1:8LHG1a3SRW71ettAD/jW13h8c6AqjVSeL11RAdgaqpo=
github.com/go-git/go-git/v5 v5.9.0 h1:cD9SFA7sHVRdJ7AYck1ZaAa/yeuBvGPxwXDL8cxrObY=
github.com/go-git/go-git/v5 v5.9.0/go.mod h1:RKIqga24sWdMGZF+1Ekv9kylsDz6LzdTSI2s/OsZWE0=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
github.com/go-git/go-git/v5 v5.10.0 h1:F0x3xXrAWmhwtzoCokU4IMPcBdncG+HAAqi9FcOOjbQ=
github.com/go-git/go-git/v5 v5.10.0/go.mod h1:1FOZ/pQnqw24ghP2n7cunVl0ON55BsjPYvhWHvZGhoo=
github.com/gogo/protobuf v0.0.0-20170307180453-100ba4e88506/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
Expand Down Expand Up @@ -162,8 +162,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos=
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.13.0 h1:jDDenyj+WgFtmV3zYVoi8aE2BwtXFLWOA67ZfNWftiY=
golang.org/x/oauth2 v0.13.0/go.mod h1:/JMhi4ZRXAf4HG9LiNmxvk+45+96RUlVThiH8FzNBn0=
Expand Down
2 changes: 1 addition & 1 deletion internal/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
GoImportsVersion = "v0.14.0"
// GolangCIlintVersion is the version of golangci-lint.
// renovate: datasource=go depName=github.com/golangci/golangci-lint
GolangCIlintVersion = "v1.55.0"
GolangCIlintVersion = "v1.55.1"
// GolangContainerImageVersion is the default golang container image.
// renovate: datasource=docker versioning=docker depName=golang
GolangContainerImageVersion = "1.21-alpine"
Expand Down
13 changes: 10 additions & 3 deletions internal/output/ghworkflow/gh_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewOutput() *Output {
Name: "default",
// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-a-fallback-value
Concurrency: Concurrency{
Group: "${{ github.event.label == null && github.head_ref || github.run_id }}",
Group: "${{ github.head_ref || github.run_id }}",
CancelInProgress: true,
},
On: On{
Expand All @@ -75,8 +75,10 @@ func NewOutput() *Output {
GenericRunner,
},
Permissions: map[string]string{
"packages": "write",
"contents": "write",
"packages": "write",
"contents": "write",
"actions": "read",
"pull-requests": "read",
},
Services: DefaultServices(),
Steps: DefaultSteps(),
Expand Down Expand Up @@ -153,6 +155,11 @@ func (o *Output) AddStep(jobName string, steps ...*Step) {
o.workflows[ciWorkflow].Jobs[jobName].Steps = append(o.workflows[ciWorkflow].Jobs[jobName].Steps, steps...)
}

// AddOutputs adds outputs to the job.
func (o *Output) AddOutputs(jobName string, outputs map[string]string) {
o.workflows[ciWorkflow].Jobs[jobName].Outputs = outputs
}

// AddSlackNotify adds the workflow to notify slack dependencies.
func (o *Output) AddSlackNotify(workflow string) {
o.workflows[slackWorkflow].Workflows = append(o.workflows[slackWorkflow].Workflows, workflow)
Expand Down
1 change: 1 addition & 0 deletions internal/output/ghworkflow/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Job struct {
RunsOn []string `yaml:"runs-on"`
If string `yaml:"if,omitempty"`
Needs []string `yaml:"needs,omitempty"`
Outputs map[string]string `yaml:"outputs,omitempty"`
Services map[string]Service `yaml:"services,omitempty"`
Steps []*Step `yaml:"steps"`
}
Expand Down
55 changes: 30 additions & 25 deletions internal/project/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ package custom

import (
"fmt"
"slices"
"strings"

"github.com/siderolabs/gen/maps"
"github.com/siderolabs/gen/xslices"

"github.com/siderolabs/kres/internal/config"
Expand Down Expand Up @@ -284,7 +282,23 @@ func (step *Step) CompileGitHubWorkflow(output *ghworkflow.Output) error {
workflowStep.SetEnv(k, v)
}

steps := []*ghworkflow.Step{workflowStep}
steps := []*ghworkflow.Step{
workflowStep,
{
Name: "Retrieve workflow info",
ID: "workflow-run-info",
Uses: fmt.Sprintf("potiuk/get-workflow-origin@%s", config.GetWorkflowOriginActionVersion),
With: map[string]string{
"token": "${{ secrets.GITHUB_TOKEN }}",
},
},
}

output.AddOutputs("default", map[string]string{
"labels": "${{ steps.workflow-run-info.outputs.pullRequestLabels }}",
})

additionalArtifactsSteps := []*ghworkflow.Step{}

if step.GHAction.Artifacts.Enabled {
steps = append(steps,
Expand Down Expand Up @@ -318,12 +332,12 @@ func (step *Step) CompileGitHubWorkflow(output *ghworkflow.Output) error {
artifactStep.If = "always()"
}

steps = append(steps,
artifactStep,
)
additionalArtifactsSteps = append(additionalArtifactsSteps, artifactStep)
}
}

steps = append(steps, additionalArtifactsSteps...)

output.AddStep(
"default",
steps...,
Expand All @@ -333,18 +347,14 @@ func (step *Step) CompileGitHubWorkflow(output *ghworkflow.Output) error {
ghworkflow.HostedRunner,
}

labelsToMap := make(map[string]struct{}, 0)

for _, job := range step.GHAction.Jobs {
conditions := xslices.Map(job.TriggerLabels, func(label string) string {
labelsToMap["\""+label+"\""] = struct{}{}

return fmt.Sprintf("contains(github.event.pull_request.labels.*.name, '%s')", label)
return fmt.Sprintf("contains(needs.default.outputs.labels, '%s')", label)
})

output.AddJob(job.Name, &ghworkflow.Job{
RunsOn: append(runnerLabels, job.RunnerLabels...),
If: fmt.Sprintf("${{ %s }}", strings.Join(conditions, " || ")),
If: strings.Join(conditions, " || "),
Needs: []string{"default"},
Services: ghworkflow.DefaultServices(),
Steps: ghworkflow.DefaultSteps(),
Expand Down Expand Up @@ -387,13 +397,16 @@ func (step *Step) CompileGitHubWorkflow(output *ghworkflow.Output) error {

output.AddSlackNotify(workflowName)

steps := []*ghworkflow.Step{workflowStep}
steps = append(steps, additionalArtifactsSteps...)

output.AddWorkflow(
workflowName,
&ghworkflow.Workflow{
Name: workflowName,
// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-a-fallback-value
Concurrency: ghworkflow.Concurrency{
Group: "${{ github.event.label == null && github.head_ref || github.run_id }}",
Group: "${{ github.head_ref || github.run_id }}",
CancelInProgress: true,
},
On: ghworkflow.On{
Expand All @@ -407,25 +420,17 @@ func (step *Step) CompileGitHubWorkflow(output *ghworkflow.Output) error {
"default": {
RunsOn: append(runnerLabels, job.RunnerLabels...),
Services: ghworkflow.DefaultServices(),
Steps: append(ghworkflow.DefaultSteps(), workflowStep),
Steps: append(
ghworkflow.DefaultSteps(),
steps...,
),
},
},
},
)
}
}

if len(labelsToMap) > 0 {
labels := maps.Keys(labelsToMap)
slices.Sort(labels)

labelsJSON := fmt.Sprintf("fromJSON('[%s]')", strings.Join(labels, ", "))
condition := fmt.Sprintf("%s && (github.event.label == null || (contains(%s, github.event.label.name)))", ghworkflow.DefaultSkipCondition, labelsJSON)

output.OverrideDefaultJobCondition(condition)
output.AddPullRequestLabelCondition()
}

return nil
}

Expand Down

0 comments on commit d577d4d

Please sign in to comment.