Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .github/workflows/generate-konflux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
env:
GH_TOKEN: ${{ secrets.OPENSHIFT_PIPELINES_ROBOT }}
GITHUB_TOKEN: ${{ secrets.OPENSHIFT_PIPELINES_ROBOT }}

# TODO: This should be removed since the previous step sends the pull requests now
- name: Commit new changes
if: github.event_name != 'pull_request'
run: |
Expand Down
34 changes: 29 additions & 5 deletions internal/konflux/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,21 @@ func metadataTrailer() string {
message := ""

if workflow := os.Getenv("GITHUB_WORKFLOW"); workflow != "" {
message += fmt.Sprintf("\nGenerated by workflow [%s](%s/%s/actions/runs/%s)",
sha := os.Getenv("GITHUB_SHA")
shortSha := sha
if len(shortSha) > 8 {
shortSha = sha[:8]
}
message += fmt.Sprintf("\nGenerated by workflow [%s](%s/%s/actions/runs/%s) on branch %s ([%s](%s/%s/commit/%s))",
workflow,
os.Getenv("GITHUB_SERVER_URL"),
os.Getenv("GITHUB_REPOSITORY"),
os.Getenv("GITHUB_RUN_ID"),
os.Getenv("GITHUB_REF_NAME"),
shortSha,
os.Getenv("GITHUB_SERVER_URL"),
os.Getenv("GITHUB_REPOSITORY"),
sha,
)
}

Expand All @@ -82,7 +92,7 @@ func metadataTrailer() string {
}

if actor != "" {
message += fmt.Sprintf("\nTriggered by %s", actor)
message += fmt.Sprintf("\nTriggered by @%s", actor)
}

if message != "" {
Expand Down Expand Up @@ -118,23 +128,37 @@ func commitAndPullRequest(ctx context.Context, repo Repository, dir string) erro

cmd := fmt.Sprintf("gh pr list --base %s --head %s --json number --jq '.[0].number'", base, head)
out, err := run(ctx, dir, "bash", "-c", cmd)

if err != nil {
return fmt.Errorf("failed to check if a pr exists: %s, %s", err, out)
}
prNumber := strings.TrimSpace(string(out))

prBody := "This PR was automatically generated by the konflux command from openshift-pipelines/hack repository" + metadataTrailer()
if prNumber == "" {
log.Printf("No PR found for %s, Creating ", head)
if out, err := run(ctx, dir, "gh", "pr", "create",
out, err := run(ctx, dir, "gh", "pr", "create",
"--base", base,
"--head", head,
"--label=hack", "--label=automated",
"--title", fmt.Sprintf("[bot:%s] update konflux configuration", head),
"--body", "This PR was automatically generated by the konflux command from openshift-pipelines/hack repository"+metadataTrailer()); err != nil {
"--body", prBody)
if err != nil {
return fmt.Errorf("failed to create the pr: %s, %s", err, out)
}
// Printing directly since the log prefix blocks the notice from registering with the workflow
fmt.Printf("::notice::Created Pull Request for %s: %s\n", repo.Name, out)
} else {
log.Printf("[%s] PR already exists", prNumber)

cmd := fmt.Sprintf("gh pr list --base %s --head %s --json url --jq '.[0].url'", base, head)
out, err := run(ctx, dir, "bash", "-c", cmd)
if err != nil {
return fmt.Errorf("failed to check existing PR's URL exists: %s, %s", err, out)
}
prUrl := strings.TrimSpace(string(out))

// Printing directly since the log prefix blocks the notice from registering with the workflow
fmt.Printf("::notice::Updated Pull Request for %s: %s\n", repo.Name, prUrl)
}
return nil
}
Expand Down
Loading