Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gh action workflow to mirror circle ci #468

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d860f40
add gh action workflow to mirror circle ci
kevinkruger Dec 4, 2024
11034ba
add newline
kevinkruger Dec 4, 2024
abff4d5
simplify the workflow
kevinkruger Dec 4, 2024
a840939
make sure test run sequentially
kevinkruger Dec 4, 2024
9a6f01f
log file path
kevinkruger Dec 4, 2024
bb5d29e
log write path
kevinkruger Dec 4, 2024
ca370e6
remove error
kevinkruger Dec 5, 2024
7a975dd
remove uneeded variable
kevinkruger Dec 5, 2024
d2a0731
add install dependencies step
kevinkruger Dec 5, 2024
c41fcd9
remove pre commit hook
kevinkruger Dec 5, 2024
479a579
only run lint via pre commit
kevinkruger Dec 5, 2024
795bba7
add go test sum
kevinkruger Dec 5, 2024
ba05af2
add github actions docs step
kevinkruger Dec 5, 2024
99cf78c
only run pre commit
kevinkruger Dec 5, 2024
2fa72a0
remove circle ci
kevinkruger Dec 5, 2024
f7d1503
remove print ln
kevinkruger Dec 6, 2024
8584bca
add step to publish the results of the test suite
kevinkruger Dec 16, 2024
a6d0041
fix gh actions folder path
kevinkruger Dec 16, 2024
49ea11e
install datadog ci
kevinkruger Dec 16, 2024
5e7e14d
use golang lint gh actoin
kevinkruger Dec 16, 2024
f6619ba
use pre commit action
kevinkruger Dec 16, 2024
b79b7b5
update to newest version of golangci-lint
kevinkruger Dec 16, 2024
e353892
fix version
kevinkruger Dec 16, 2024
3b58eda
make linter happy
kevinkruger Dec 16, 2024
e68f87d
more linting
kevinkruger Dec 16, 2024
1035cae
more linting
kevinkruger Dec 16, 2024
511460a
no lint lint issues
kevinkruger Dec 16, 2024
2eba4b5
lint exceptions
kevinkruger Dec 16, 2024
c8694a6
no lint
kevinkruger Dec 16, 2024
2dcaa8f
formating
kevinkruger Dec 16, 2024
0672bbe
more linting
kevinkruger Dec 16, 2024
17c632f
unused param removal / bug
kevinkruger Dec 16, 2024
902c901
do not close request bc test needs it
kevinkruger Dec 16, 2024
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
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'
73 changes: 73 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 golangci-lint
uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
with:
extra_args: golangci-lint --all-files
Comment on lines +32 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous step already runs golangci-lint

Suggested change
- name: Run golangci-lint
uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
with:
extra_args: golangci-lint --all-files


- 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
22 changes: 10 additions & 12 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 All @@ -456,9 +457,6 @@ func (c ApiClient) do(req *h.Request) (*http.Response, error) {
return res, nil
default:
resBytes, err := io.ReadAll(res.Body)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -550,8 +548,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 +575,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
8 changes: 6 additions & 2 deletions options/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ type flag struct {
usage string
}

const (
anotherDefaultFlagValue = 10 // Descriptive constant for another default flag value
)

// Options that are available as command line flags
var flags = []flag{
{
Expand Down Expand Up @@ -45,7 +49,7 @@ Allowed template variables: 'branchName', 'sha'. If "commitUrlTemplate" is not p
{
name: "contextLines",
short: "C",
defaultValue: 2,
defaultValue: anotherDefaultFlagValue,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels strange to set as a constant. Which linter is complaining?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo revert these const changes, and disable whatever linter is complaining, I don't agree that these things should be constants. Maybe the shortShaLength is the only one that I'd put as a constant.

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 +98,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
Loading
Loading