Skip to content

Commit daf7bac

Browse files
authored
[Actions] Create Encrich PR Action (#64)
1 parent 93274f1 commit daf7bac

File tree

11 files changed

+1292
-0
lines changed

11 files changed

+1292
-0
lines changed

.github/workflows/_release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
imageName: 'gh-action-create-github-release'
2424
- actionPath: 'actions/github/jsonDiffAlert'
2525
imageName: 'gh-action-json-diff-alert'
26+
- actionPath: 'actions/github/enrichPullRequest'
27+
imageName: 'gh-action-enrich-pull-request'
2628
uses: ./.github/workflows/_github_createAndReleaseActionDockerImage.yml
2729
with:
2830
actionPath: ${{ matrix.actionPath }}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
FROM golang:1.24-alpine AS builder
2+
3+
WORKDIR /app
4+
5+
# Copy go mod and sum files
6+
COPY go.mod go.sum ./
7+
8+
# Download dependencies
9+
RUN go mod download
10+
11+
# Copy source code
12+
COPY . .
13+
14+
# Build the binary
15+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
16+
17+
# Final stage - minimal image
18+
FROM alpine:latest
19+
20+
# Install ca-certificates for HTTPS requests
21+
RUN apk --no-cache add ca-certificates
22+
23+
# Create a non-root user
24+
RUN addgroup -g 1001 -S appgroup && \
25+
adduser -u 1001 -S appuser -G appgroup
26+
27+
# Set working directory
28+
WORKDIR /app
29+
30+
# Copy the binary from builder stage
31+
COPY --from=builder /app/main .
32+
33+
# Set ownership and permissions
34+
RUN chown appuser:appgroup /app/main && \
35+
chmod +x /app/main
36+
37+
# Switch to non-root user
38+
USER appuser
39+
40+
# Run the binary
41+
ENTRYPOINT ["/app/main"]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: 'Enrich Pull Request'
2+
description: "Enrich's the pull request with information from your project management system."
3+
inputs:
4+
repository:
5+
description: 'The repository name'
6+
required: true
7+
type: string
8+
pullRequestNumber:
9+
description: 'The pull request number'
10+
required: true
11+
type: string
12+
branch:
13+
description: 'The branch name'
14+
required: true
15+
type: string
16+
token:
17+
description: 'GitHub token'
18+
required: true
19+
customFormatting:
20+
description: "User defined custom formatting rules for specific words."
21+
required: false
22+
default: ""
23+
strategy:
24+
type: string
25+
description: 'Which formatting strategy should be used'
26+
required: false
27+
default: 'branch-name'
28+
jiraURL:
29+
type: string
30+
description: 'URL to your jira instance'
31+
required: false
32+
default: ''
33+
jiraEmail:
34+
type: string
35+
description: 'Jira auth email'
36+
required: false
37+
default: ''
38+
jiraToken:
39+
type: string
40+
description: 'Jira auth token'
41+
required: false
42+
default: ''
43+
jiraEnableSyncLabel:
44+
type: boolean
45+
description: 'Use a sync label'
46+
required: false
47+
default: true
48+
jiraEnableSyncDescription:
49+
type: boolean
50+
description: 'Sync Jira description to PR description'
51+
required: false
52+
default: true
53+
jiraSyncLabelName:
54+
type: string
55+
description: 'Name of the sync label'
56+
required: false
57+
default: 'jira-sync-complete'
58+
runs:
59+
using: 'docker'
60+
image: 'docker://ghcr.io/encoredigitalgroup/gh-action-enrich-pull-request:latest'
61+
env:
62+
GH_TOKEN: ${{ inputs.token }}
63+
GH_REPOSITORY: ${{ inputs.repository }}
64+
PR_NUMBER: ${{ inputs.pullRequestNumber }}
65+
BRANCH_NAME: ${{ inputs.branch }}
66+
ENABLE_EXPERIMENTS: ${{ inputs.enableExperiments }}
67+
OPT_FMT_WORDS: ${{ inputs.customFormatting }}
68+
OPT_FMT_STRATEGY: ${{ inputs.strategy }}
69+
OPT_JIRA_URL: ${{ inputs.jiraURL }}
70+
OPT_JIRA_EMAIL: ${{ inputs.jiraEmail }}
71+
OPT_JIRA_TOKEN: ${{ inputs.jiraToken }}
72+
OPT_ENABLE_JIRA_SYNC_LABEL: ${{ inputs.jiraEnableSyncLabel }}
73+
OPT_JIRA_SYNC_LABEL_NAME: ${{ inputs.jiraSyncLabelName }}
74+
OPT_ENABLE_JIRA_SYNC_DESCRIPTION: ${{ inputs.jiraEnableSyncDescription }}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package branchname
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"regexp"
7+
8+
"github.com/EncoreDigitalGroup/golib/logger"
9+
10+
"github.com/EncoreDigitalGroup/ci-workflows/actions/github/enrichPullRequest/support/github"
11+
)
12+
13+
var regexWithIssueType = regexp.MustCompile(`^(epic|feature|bugfix|hotfix)/([A-Z]+-[0-9]+)-(.+)$`)
14+
var regexWithoutIssueType = regexp.MustCompile(`^([A-Z]+-[0-9]+)-(.+)$`)
15+
var pullRequestTitle string
16+
17+
func Format(gh github.GitHub) {
18+
branchName, err := gh.GetBranchName()
19+
if err != nil {
20+
logger.Error(err.Error())
21+
os.Exit(1)
22+
}
23+
24+
if !gh.BranchNameMatchesPRTitle(branchName) {
25+
formattedTitle := formatTitle(gh, branchName)
26+
gh.UpdatePRTitle(formattedTitle)
27+
}
28+
}
29+
30+
func GetIssueKeyFromBranchName(branchName string) (string, error) {
31+
if matches := regexWithIssueType.FindStringSubmatch(branchName); matches != nil {
32+
return matches[2], nil
33+
} else if matches := regexWithoutIssueType.FindStringSubmatch(branchName); matches != nil {
34+
return matches[1], nil
35+
} else {
36+
fmt.Println("Title does not match expected format")
37+
logger.Info(pullRequestTitle)
38+
return "", nil
39+
}
40+
}
41+
42+
func GetIssueNameFromBranchName(branchName string) (string, error) {
43+
if matches := regexWithIssueType.FindStringSubmatch(branchName); matches != nil {
44+
return matches[3], nil
45+
} else if matches := regexWithoutIssueType.FindStringSubmatch(branchName); matches != nil {
46+
return matches[2], nil
47+
} else {
48+
fmt.Println("Title does not match expected format")
49+
logger.Info(pullRequestTitle)
50+
return "", nil
51+
}
52+
}
53+
54+
func formatTitle(gh github.GitHub, branchName string) string {
55+
issueKey, err := GetIssueKeyFromBranchName(branchName)
56+
issueName, err := GetIssueNameFromBranchName(branchName)
57+
58+
if err != nil {
59+
fmt.Println("Title does not match expected format")
60+
logger.Error(err.Error())
61+
return pullRequestTitle
62+
}
63+
64+
return gh.ApplyFormatting(issueKey, issueName)
65+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package drivers
2+
3+
const BranchName = "branch-name"
4+
const Jira = "jira"
5+
6+
func Validate(driver string) bool {
7+
validDrivers := []string{
8+
BranchName,
9+
Jira,
10+
}
11+
12+
for _, validDriver := range validDrivers {
13+
if driver == validDriver {
14+
return true
15+
}
16+
}
17+
18+
return false
19+
}

0 commit comments

Comments
 (0)