Skip to content

Commit

Permalink
add gh action workflow to mirror circle ci (#468)
Browse files Browse the repository at this point in the history
Migrate the circle ci workflow to gh actions
  • Loading branch information
kevinkruger authored Jan 7, 2025
1 parent 4413d04 commit e3f10c1
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 121 deletions.
102 changes: 0 additions & 102 deletions .circleci/config.yml

This file was deleted.

35 changes: 35 additions & 0 deletions .github/actions/publish-junit/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Publish JUnit Tests
description: Publishes JUnit tests to one or more sources
inputs:
files:
required: true
description: The JUnit files to upload
name:
required: true
description: The name of the suite
datadog:
required: false
description: Upload to Datadog
default: 'true'
github:
required: false
description: Upload to GitHub
default: 'true'

runs:
using: composite
steps:
- name: Report Tests to Datadog
shell: bash
if: ${{ inputs.datadog }} == 'true'
run: datadog-ci junit upload --service ${{ inputs.name }} ${{ inputs.files }}

- name: Test Publish
uses: phoenix-actions/test-reporting@f957cd93fc2d848d556fa0d03c57bc79127b6b5e # v15
if: ${{ inputs.github }} == 'true'
with:
name: ${{ inputs.name }}
output-to: step-summary
path: ${{ inputs.files }}
reporter: java-junit
fail-on-error: 'false'
68 changes: 68 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Test and Generate Docs

on: pull_request

jobs:
go-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29

- name: Set up Go
uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed
with:
go-version-file: go.mod

- name: Install datadog-ci
run: npm install -g @datadog/datadog-ci

- name: Install Dependencies
run: |
go install github.com/jstemmer/go-junit-report@v1.0.0
go install github.com/kyoh86/richgo@v0.3.10
go install gotest.tools/gotestsum@latest
- name: Install golangci-lint
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1
with:
version: v1.62.2

- name: Run tests with gotestsum
run: |
mkdir -p ${{ github.workspace }}/artifacts
mkdir -p ${{ github.workspace }}/reports
gotestsum --packages="./..." \
--junitfile ${{ github.workspace }}/reports/go-test_go_tests.xml \
--jsonfile ${{ github.workspace }}/artifacts/go-test_go_tests.json \
--rerun-fails=2 \
--rerun-fails-max-failures=10 \
--rerun-fails-report ${{ github.workspace }}/artifacts/rerun_tests_go_tests.txt
- name: Publish JUnit Tests
uses: ./.github/actions/publish-junit
env:
DD_API_KEY: ${{ secrets.DATADOG_API_KEY }}
with:
files: ${{ github.workspace }}/reports/go-test_go_tests.xml
name: find-code-references-in-pull-request
datadog: 'true'
github: 'true'

github-actions-docs:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29

- uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: 20

- name: Install action-docs
run: npm install action-docs

- uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
with:
extra_args: github-action-docs --all-files
6 changes: 5 additions & 1 deletion build/package/github-actions/github-actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
o "github.com/launchdarkly/ld-find-code-refs/v2/options"
)

const (
millisecondsInSecond = 1000 // Descriptive constant for milliseconds conversion
)

func main() {
log.Init(false)
dir := os.Getenv("GITHUB_WORKSPACE")
Expand Down Expand Up @@ -55,7 +59,7 @@ func mergeGithubOptions(opts o.Options) (o.Options, error) {
if event != nil {
repoUrl = event.Repo.Url
defaultBranch = event.Repo.DefaultBranch
updateSequenceId = int(time.Now().Unix() * 1000) // seconds to ms
updateSequenceId = int(time.Now().Unix() * millisecondsInSecond) // seconds to ms
}

opts.RepoType = "github"
Expand Down
12 changes: 8 additions & 4 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
"github.com/launchdarkly/ld-find-code-refs/v2/internal/log"
)

const (
// ... other constants ...
millisecondsInSecond = 1000 // Descriptive constant for milliseconds conversion
)

type Client struct {
workspace string
GitBranch string
Expand Down Expand Up @@ -227,7 +232,7 @@ type CommitData struct {
}

// FindExtinctions searches commit history for flags that had references removed recently
func (c Client) FindExtinctions(project options.Project, flags []string, matcher search.Matcher, lookback int) ([]ld.ExtinctionRep, error) {
func (c *Client) FindExtinctions(project options.Project, flags []string, matcher search.Matcher, lookback int) ([]ld.ExtinctionRep, error) {
commits, err := getCommits(c.workspace, lookback)
if err != nil {
return nil, err
Expand Down Expand Up @@ -327,7 +332,7 @@ func makeExtinctionRepFromCommit(projectKey, flagKey string, commit *object.Comm
return ld.ExtinctionRep{
Revision: commit.Hash.String(),
Message: commit.Message,
Time: commit.Author.When.Unix() * 1000,
Time: commit.Author.When.Unix() * millisecondsInSecond,
ProjKey: projectKey,
FlagKey: flagKey,
}
Expand All @@ -342,9 +347,8 @@ func getCommits(workspace string, lookback int) ([]CommitData, error) {
if err != nil {
return nil, err
}

commits := []CommitData{}
for i := 0; i < lookback; i++ {
for range make([]struct{}, lookback) {
commit, err := logResult.Next()
if err != nil {
// reached end of commit tree
Expand Down
19 changes: 10 additions & 9 deletions internal/ld/ld.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
apiVersionHeader = "LD-API-Version"
v2ApiPath = "/api/v2"
reposPath = "/code-refs/repositories"
shortShaLength = 7 // Descriptive constant for SHA length
)

type ConfigurationError struct {
Expand Down Expand Up @@ -76,8 +77,8 @@ func IsTransient(err error) bool {
// Fallback to default backoff if header can't be parsed
// https://apidocs.launchdarkly.com/#section/Overview/Rate-limiting
// Method is curried in order to avoid stubbing the time package and fallback Backoff in unit tests
func RateLimitBackoff(now func() time.Time, fallbackBackoff h.Backoff) func(time.Duration, time.Duration, int, *http.Response) time.Duration {
return func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
func RateLimitBackoff(now func() time.Time, fallbackBackoff h.Backoff) func(minDuration, _ time.Duration, attemptNum int, resp *http.Response) time.Duration {
return func(minDuration, max2 time.Duration, attemptNum int, resp *http.Response) time.Duration {
if resp != nil {
if resp.StatusCode == http.StatusTooManyRequests {
if s, ok := resp.Header["X-Ratelimit-Reset"]; ok {
Expand All @@ -95,7 +96,7 @@ func RateLimitBackoff(now func() time.Time, fallbackBackoff h.Backoff) func(time
}
}

return fallbackBackoff(min, max, attemptNum, resp)
return fallbackBackoff(minDuration, max2, attemptNum, resp)
}
}

Expand All @@ -108,7 +109,7 @@ func InitApiClient(options ApiOptions) ApiClient {
if options.RetryMax != nil && *options.RetryMax >= 0 {
client.RetryMax = *options.RetryMax
}
client.Backoff = RateLimitBackoff(time.Now, h.LinearJitterBackoff)
client.Backoff = RateLimitBackoff(time.Now, h.LinearJitterBackoff) //nolint:bodyclose

return ApiClient{
httpClient: client,
Expand Down Expand Up @@ -192,7 +193,7 @@ func (c ApiClient) getProjectEnvironment(projKey string) (*ldapi.Environment, er
}

func (c ApiClient) getFlags(projKey string, params url.Values) ([]ldapi.FeatureFlag, error) {
url := c.getPath(fmt.Sprintf("/flags/%s", projKey))
url := c.getPath(fmt.Sprintf("/flags/%s", projKey)) //nolint:perfsprint
req, err := h.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -441,7 +442,7 @@ type ldErrorResponse struct {

func (c ApiClient) do(req *h.Request) (*http.Response, error) {
req.Header.Set("Authorization", c.Options.ApiKey)
req.Header.Set(apiVersionHeader, apiVersion)
req.Header.Set(apiVersionHeader, apiVersion) //nolint:canonicalheader
req.Header.Set("User-Agent", c.Options.UserAgent)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Length", strconv.FormatInt(req.ContentLength, 10))
Expand Down Expand Up @@ -550,8 +551,8 @@ func (b BranchRep) TotalHunkCount() int {
func (b BranchRep) WriteToCSV(outDir, projKey, repo, sha string) (path string, err error) {
// Try to create a filename with a shortened sha, but if the sha is too short for some unexpected reason, use the branch name instead
var tag string
if len(sha) >= 7 {
tag = sha[:7]
if len(sha) >= shortShaLength {
tag = sha[:shortShaLength]
} else {
tag = b.Name
}
Expand All @@ -577,7 +578,7 @@ func (b BranchRep) WriteToCSV(outDir, projKey, repo, sha string) (path string, e
// sort csv by flag key
sort.Slice(records, func(i, j int) bool {
// sort by flagKey -> path -> startingLineNumber
for k := 0; k < 3; k++ {
for k := range [3]int{} {
if records[i][k] != records[j][k] {
return records[i][k] < records[j][k]
}
Expand Down
4 changes: 2 additions & 2 deletions options/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Allowed template variables: 'branchName', 'sha'. If "commitUrlTemplate" is not p
{
name: "contextLines",
short: "C",
defaultValue: 2,
defaultValue: 2, //nolint:mnd
usage: `The number of context lines to send to LaunchDarkly. If < 0, no
source code will be sent to LaunchDarkly. If 0, only the lines containing
flag references will be sent. If > 0, will send that number of context
Expand Down Expand Up @@ -94,7 +94,7 @@ LaunchDarkly API is unreachable or returns an unexpected response.`,
{
name: "lookback",
short: "l",
defaultValue: 10,
defaultValue: 10, //nolint:mnd
usage: `Sets the number of git commits to search in history for
whether a feature flag was removed from code. May be set to 0 to disabled this feature. Setting this option to a high value will increase search time.`,
},
Expand Down
4 changes: 2 additions & 2 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (o Options) ValidateRequired() error {
}

if len(o.ProjKey) > 0 && len(o.Projects) > 0 {
return fmt.Errorf("`--projKey` cannot be combined with `projects` in configuration")
return errors.New("`--projKey` cannot be combined with `projects` in configuration")
}

if len(o.ProjKey) > maxProjKeyLength {
Expand Down Expand Up @@ -263,7 +263,7 @@ func (o Options) Validate() error {
}

if o.Revision != "" && o.Branch == "" {
return fmt.Errorf(`"branch" option is required when "revision" option is set`)
return errors.New(`"branch" option is required when "revision" option is set`)
}

if len(o.Projects) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion search/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func buildElementPatterns(flags []string, delimiters string) map[string][]string
for _, left := range delimiters {
for _, right := range delimiters {
var sb strings.Builder
sb.Grow(len(flag) + 2)
sb.Grow(len(flag) + 2) //nolint:mnd
sb.WriteRune(left)
sb.WriteString(flag)
sb.WriteRune(right)
Expand Down

0 comments on commit e3f10c1

Please sign in to comment.