From aba70161cf1e9440ea6d41546249eeccb407a741 Mon Sep 17 00:00:00 2001 From: Matt Petersen Date: Mon, 15 Jul 2024 12:09:16 -0600 Subject: [PATCH] Add Support For Labels --- Dockerfile | 2 +- Makefile | 2 +- README.md | 2 ++ action.yaml | 10 +++++----- approval.go | 9 ++++----- constants.go | 2 +- go.mod | 2 +- main.go | 11 +++-------- 8 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index 33817da..6ec73aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /var/app RUN CGO_ENABLED=0 go build -o app . FROM alpine:3.14 -LABEL org.opencontainers.image.source https://github.com/radicldefense/manual-approval +LABEL org.opencontainers.image.source https://github.com/trstringer/manual-approval RUN apk update && apk add ca-certificates COPY --from=builder /var/app/app /var/app/app CMD ["/var/app/app"] diff --git a/Makefile b/Makefile index 082569a..3ec553a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -IMAGE_REPO=ghcr.io/radicldefense/rad-manual-approval +IMAGE_REPO=ghcr.io/trstringer/manual-approval .PHONY: build build: diff --git a/README.md b/README.md index 25218e9..8340e53 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ steps: minimum-approvals: 1 issue-title: "Deploying v1.3.5 to prod from staging" issue-body: "Please approve or deny the deployment of version v1.3.5." + issue-labels: "deployment" exclude-workflow-initiator-as-approver: false additional-approved-words: '' additional-denied-words: '' @@ -42,6 +43,7 @@ steps: - `minimum-approvals` is an integer that sets the minimum number of approvals required to progress the workflow. Defaults to ALL approvers. - `issue-title` is a string that will be appended to the title of the issue. - `issue-body` is a string that will be prepended to the body of the issue. +- `issue-labels` is a comma separated list of strings that will be added as labels to the issue. - `exclude-workflow-initiator-as-approver` is a boolean that indicates if the workflow initiator (determined by the `GITHUB_ACTOR` environment variable) should be filtered from the final list of approvers. This is optional and defaults to `false`. Set this to `true` to prevent users in the `approvers` list from being able to self-approve workflows. - `additional-approved-words` is a comma separated list of strings to expand the dictionary of words that indicate approval. This is optional and defaults to an empty string. - `additional-denied-words` is a comma separated list of strings to expand the dictionary of words that indicate denial. This is optional and defaults to an empty string. diff --git a/action.yaml b/action.yaml index 525649e..d96c75a 100644 --- a/action.yaml +++ b/action.yaml @@ -1,4 +1,4 @@ -name: RADICL Manual Workflow Approval +name: Manual Workflow Approval description: Pause a workflow and get user approval to continue branding: icon: pause @@ -22,12 +22,12 @@ inputs: issue-body: description: The custom body for the issue required: false + issue-labels: + description: Labels to add to the issue + required: false exclude-workflow-initiator-as-approver: description: Whether or not to filter out the user who initiated the workflow as an approver if they are in the approvers list default: false - labels: - description: Labels to add to the issue - required: false additional-approved-words: description: Comma separated list of words that can be used to approve beyond the defaults. default: '' @@ -36,4 +36,4 @@ inputs: default: '' runs: using: docker - image: docker://ghcr.io/radicldefense/rad-manual-approval:1.10 + image: docker://ghcr.io/trstringer/manual-approval:1.9.1 diff --git a/approval.go b/approval.go index c5acd7a..e4a3d64 100644 --- a/approval.go +++ b/approval.go @@ -19,12 +19,12 @@ type approvalEnvironment struct { approvalIssueNumber int issueTitle string issueBody string + issueLabels []string issueApprovers []string minimumApprovals int - labels []string } -func newApprovalEnvironment(client *github.Client, repoFullName, repoOwner string, runID int, approvers []string, minimumApprovals int, issueTitle, issueBody string, labels []string) (*approvalEnvironment, error) { +func newApprovalEnvironment(client *github.Client, repoFullName, repoOwner string, runID int, approvers []string, minimumApprovals int, issueTitle, issueBody string, issueLabels []string) (*approvalEnvironment, error) { repoOwnerAndName := strings.Split(repoFullName, "/") if len(repoOwnerAndName) != 2 { return nil, fmt.Errorf("repo owner and name in unexpected format: %s", repoFullName) @@ -41,7 +41,7 @@ func newApprovalEnvironment(client *github.Client, repoFullName, repoOwner strin minimumApprovals: minimumApprovals, issueTitle: issueTitle, issueBody: issueBody, - labels: labels, + issueLabels: issueLabels, }, nil } @@ -85,12 +85,11 @@ Respond %s to continue workflow or %s to cancel.`, a.issueApprovers, issueBody, ) - a.approvalIssue, _, err = a.client.Issues.Create(ctx, a.repoOwner, a.repo, &github.IssueRequest{ Title: &issueTitle, Body: &issueBody, Assignees: &a.issueApprovers, - Labels: &a.labels, + Labels: &a.issueLabels, }) if err != nil { return err diff --git a/constants.go b/constants.go index 4de36c0..19a118f 100644 --- a/constants.go +++ b/constants.go @@ -18,10 +18,10 @@ const ( envVarMinimumApprovals string = "INPUT_MINIMUM-APPROVALS" envVarIssueTitle string = "INPUT_ISSUE-TITLE" envVarIssueBody string = "INPUT_ISSUE-BODY" + envVarIssueLabels string = "INPUT_ISSUE-LABELS" envVarExcludeWorkflowInitiatorAsApprover string = "INPUT_EXCLUDE-WORKFLOW-INITIATOR-AS-APPROVER" envVarAdditionalApprovedWords string = "INPUT_ADDITIONAL-APPROVED-WORDS" envVarAdditionalDeniedWords string = "INPUT_ADDITIONAL-DENIED-WORDS" - envVarLabels string = "INPUT_LABELS" ) var ( diff --git a/go.mod b/go.mod index dbfb8e3..413e6d2 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/radicldefense/rad-manual-approval +module github.com/trstringer/manual-approval go 1.17 diff --git a/main.go b/main.go index 3d7822f..8fcc1e1 100644 --- a/main.go +++ b/main.go @@ -173,6 +173,8 @@ func main() { issueTitle := os.Getenv(envVarIssueTitle) issueBody := os.Getenv(envVarIssueBody) + issueLabels := strings.Split(strings.ReplaceAll(os.Getenv(envVarIssueLabels), " ", ""), ",") + minimumApprovalsRaw := os.Getenv(envVarMinimumApprovals) minimumApprovals := 0 if minimumApprovalsRaw != "" { @@ -182,14 +184,7 @@ func main() { os.Exit(1) } } - - labelsRaw := os.Getenv(envVarLabels) - labels := []string{} - if labelsRaw != "" { - labels = strings.Split(labelsRaw, ",") - } - - apprv, err := newApprovalEnvironment(client, repoFullName, repoOwner, runID, approvers, minimumApprovals, issueTitle, issueBody, labels) + apprv, err := newApprovalEnvironment(client, repoFullName, repoOwner, runID, approvers, minimumApprovals, issueTitle, issueBody, issueLabels) if err != nil { fmt.Printf("error creating approval environment: %v\n", err) os.Exit(1)