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

Feat/tm gitlab pagination #232

Merged
merged 12 commits into from
Dec 16, 2024
Next Next commit
feat: get next link from repsonse helper func for pagination
y-eight committed Dec 11, 2024
commit 178eb133ea0a93d71d3080a31d8e016d294e8180
52 changes: 52 additions & 0 deletions internal/helper/pagination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// sparrow
// (C) 2024, Deutsche Telekom IT GmbH
//
// Deutsche Telekom IT GmbH and all other contributors /
// copyright owners license this file to you under the Apache
// License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package helper

import (
"net/http"
"strings"
)

const (
linkHeader = "Link"
linkNext = "next"
)

// getNextLink returns the url to the next page of
// a paginated http response provided in the passed response header.
func GetNextLink(header http.Header) string {
link := header.Get("Link")
if link == "" {
return ""
}

for _, link := range strings.Split(link, ",") {
linkParts := strings.Split(link, ";")
if len(linkParts) != 2 {
continue
}
linkType := strings.Trim(strings.Split(linkParts[1], "=")[1], "\"")

if linkType != linkNext {
continue
}
return strings.Trim(linkParts[0], "< >")
}
return ""
}
79 changes: 79 additions & 0 deletions internal/helper/pagination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// sparrow
// (C) 2024, Deutsche Telekom IT GmbH
//
// Deutsche Telekom IT GmbH and all other contributors /
// copyright owners license this file to you under the Apache
// License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package helper

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetNextLink(t *testing.T) {
type header struct {
noLinkHeader bool
key string
value string
}
tests := []struct {
name string
header header
want string
}{
{
"no link header present",
header{
noLinkHeader: true,
},
"",
},
{
"no next link in link header present",
header{
key: "link",
value: "<https://link.first.de>; rel=\"first\", <https://link.last.de>; rel=\"last\"",
},
"",
},
{
"link header syntax not valid",
header{
key: "link",
value: "no link here",
},
"",
},
{
"valid next link",
header{
key: "link",
value: "<https://link.next.de>; rel=\"next\", <https://link.first.de>; rel=\"first\", <https://link.last.de>; rel=\"last\"",
},
"https://link.next.de",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testHeader := http.Header{}
testHeader.Add(tt.header.key, tt.header.value)

assert.Equal(t, tt.want, GetNextLink(testHeader))
})
}
}