Skip to content
Merged
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
48 changes: 30 additions & 18 deletions pkg/analysis/passes/sdkusage/sdkusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}

// check if go sdk was replaced before doing any version checks
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is the same code, just moving it up so we check for a sdk replacement before we do sdk age checks.

for _, req := range goModParsed.Replace {
if req.Old.Path == "github.com/grafana/grafana-plugin-sdk-go" &&
req.New.Path != "github.com/grafana/grafana-plugin-sdk-go" {
pass.ReportResult(
pass.AnalyzerName,
goSdkReplaced,
"Your plugin is using a custom or forked version of the Grafana Go SDK",
"Custom or forked version of Grafana Go SDK are not supported. Please use the latest Grafana Go SDK (github.com/grafana/grafana-plugin-sdk-go)",
)
return nil, nil
}
}

latestRelease, err := githubapi.FetchLatestGrafanaSdkRelease()
if err != nil {
// it is most likely this failed because of github auth or rate limits
Expand All @@ -154,10 +168,23 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}

// fetch the release date of the sdk version the plugin uses
pluginSdkRelease, err := githubapi.FetchGrafanaSdkReleaseByTag(pluginGoSdkVersion)
if err != nil {
logme.DebugFln("Error fetching plugin SDK release: %s", err.Error())
pass.ReportResult(
pass.AnalyzerName,
goModError,
"Could not verify your Grafana Go SDK version",
"We could not fetch the release information for your SDK version. Please ensure you are using a valid released version of the Grafana Go SDK.",
)
return nil, nil
}

// today date in RFC3339 format
today := GetNowInRFC3339()

daysDiff, err := daysDifference(today, latestRelease.PublishedAt)
daysDiff, err := daysDifference(today, pluginSdkRelease.PublishedAt)
if err != nil {
// error calculating the days difference could be a problem in github date format
// ignoring it
Expand Down Expand Up @@ -185,20 +212,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}

// check if go sdk was replaced
for _, req := range goModParsed.Replace {
if req.Old.Path == "github.com/grafana/grafana-plugin-sdk-go" &&
req.New.Path != "github.com/grafana/grafana-plugin-sdk-go" {
pass.ReportResult(
pass.AnalyzerName,
goSdkReplaced,
"Your plugin is using a custom or forked version of the Grafana Go SDK",
"Custom or forked version of Grafana Go SDK are not supported. Please use the latest Grafana Go SDK (github.com/grafana/grafana-plugin-sdk-go)",
)
return nil, nil
}
}

return nil, nil
}

Expand All @@ -214,15 +227,14 @@ func daysDifference(date1 string, date2 string) (int, error) {
return 0, err
}

// Calculate the difference in days
diff := t2.Sub(t1)
// Calculate the absolute difference in days
diff := t2.Sub(t1).Abs()
days := int(diff.Hours() / 24)

return days, nil
}

// mockable function for testing

var GetNowInRFC3339 = func() string {
return time.Now().UTC().Format(time.RFC3339)
}
28 changes: 17 additions & 11 deletions pkg/analysis/passes/sdkusage/sdkusage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,23 @@ func TestValidGoMod(t *testing.T) {
httpmock.Activate()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

tests changes focus around putting the "correct" dates for tests

defer httpmock.DeactivateAndReset()

// mock latest request
origGetTime := GetNowInRFC3339
GetNowInRFC3339 = func() string {
return "2024-05-01T10:00:00Z"
}
defer func() { GetNowInRFC3339 = origGetTime }()

// mock latest request - published 6 days before "today" (2024-05-01)
httpmock.RegisterResponder(
"GET",
"https://api.github.com/repos/grafana/grafana-plugin-sdk-go/releases/latest",
httpmock.NewStringResponder(
200,
`{ "tag_name": "v0.230.0", "published_at": "2024-05-09T10:03:16Z" }`,
`{ "tag_name": "v0.230.0", "published_at": "2024-04-25T10:03:16Z" }`,
),
)

// mock tag request
// mock tag request - v0.225.0 published 13 days before "today", within 2 months
httpmock.RegisterResponder(
"GET",
"https://api.github.com/repos/grafana/grafana-plugin-sdk-go/releases/tags/v0.225.0",
Expand Down Expand Up @@ -167,23 +173,23 @@ func TestTwoMonthsOldSdk(t *testing.T) {
}
defer func() { GetNowInRFC3339 = origGetTime }()

// mock latest request
// mock latest request - published 8 days before "today" (2024-03-09)
httpmock.RegisterResponder(
"GET",
"https://api.github.com/repos/grafana/grafana-plugin-sdk-go/releases/latest",
httpmock.NewStringResponder(
200,
`{ "tag_name": "v0.230.0", "published_at": "2024-05-09T10:03:16Z" }`,
`{ "tag_name": "v0.230.0", "published_at": "2024-03-01T10:03:16Z" }`,
),
)

// mock tag request
// mock tag request - v0.212.0 published 75 days before "today" (2024-03-09)
httpmock.RegisterResponder(
"GET",
"https://api.github.com/repos/grafana/grafana-plugin-sdk-go/releases/tags/v0.212.0",
httpmock.NewStringResponder(
200,
`{ "tag_name": "v0.212.0", "published_at": "2024-02-19T13:06:21Z" }`,
`{ "tag_name": "v0.212.0", "published_at": "2023-12-25T13:06:21Z" }`,
),
)

Expand Down Expand Up @@ -228,23 +234,23 @@ func TestFiveMonthsOld(t *testing.T) {
}
defer func() { GetNowInRFC3339 = origGetTime }()

// mock latest request
// mock latest request - published 8 days before "today" (2023-11-09)
httpmock.RegisterResponder(
"GET",
"https://api.github.com/repos/grafana/grafana-plugin-sdk-go/releases/latest",
httpmock.NewStringResponder(
200,
`{ "tag_name": "v0.230.0", "published_at": "2024-05-09T10:03:16Z" }`,
`{ "tag_name": "v0.230.0", "published_at": "2023-11-01T10:03:16Z" }`,
),
)

// mock tag request
// mock tag request - v0.187.0 published 161 days before "today" (2023-11-09)
httpmock.RegisterResponder(
"GET",
"https://api.github.com/repos/grafana/grafana-plugin-sdk-go/releases/tags/v0.187.0",
httpmock.NewStringResponder(
200,
`{ "tag_name": "v0.187.0", "published_at": "2023-10-19T11:06:45Z" }`,
`{ "tag_name": "v0.187.0", "published_at": "2023-06-01T11:06:45Z" }`,
),
)

Expand Down
Loading