Skip to content

Commit

Permalink
Load sdk version from buildinfo (#521)
Browse files Browse the repository at this point in the history
Currently, line-bot-sdk-go embeds the library version defined in
`version.go` as the user-agent, so `version.go` is updated manually or
forcibly at release time to avoid manual operation. And we define a
github workflow to push tag forcibly to automate (1) updating
`version.go` and (2)fix git tag. Because of this, GitHub's branch
protection cannot be used. (we can solve this by using PAT, but we don't
want to use it)

Since Go 1.18, https://pkg.go.dev/debug/buildinfo has been provided. By
using runtime/debug, library(like line-bot-sdk-go) can obtain the
library's own version from the runtime of the library user. Since the
latest version of line-bot-sdk-go only supports Go 1.22 and above, this
feature should be usable.

In the user's environment, the version of line-bot-sdk-go is retrieved
from buildinfo only once in runtime. There's no point in recalculating
it each time since the same value should be obtained every time.

Excluding meaningless mocks, tests in this repository cannot be written.
I created minimum example for this patch.
- https://github.com/Yang-33/line-bot-sdk-go-521-lib
- https://github.com/Yang-33/line-bot-sdk-go-521-app
  • Loading branch information
Yang-33 authored Dec 27, 2024
1 parent 426fd6c commit 53a2230
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 124 deletions.
49 changes: 0 additions & 49 deletions .github/workflows/release.yml

This file was deleted.

2 changes: 1 addition & 1 deletion linebot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (client *Client) url(base *url.URL, endpoint string) string {

func (client *Client) do(ctx context.Context, req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "Bearer "+client.channelToken)
req.Header.Set("User-Agent", "LINE-BotSDK-Go/"+version)
req.Header.Set("User-Agent", "LINE-BotSDK-Go/"+GetVersion())
if len(client.retryKeyID) > 0 {
req.Header.Set("X-Line-Retry-Key", client.retryKeyID)
}
Expand Down
28 changes: 24 additions & 4 deletions linebot/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,33 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
// Don't edit this file directly. This file is generated by script/tag.sh .

package linebot

const version = "8.10.0"
import (
"runtime/debug"
"strings"
"sync"
)

var (
sdkVersion = "8.unknown"
sdkVersionOnce sync.Once
)

func GetVersion() string {
return version
// getting the version of line-bot-sdk-go should be done only once. Computing it repeatedly is meaningless.
sdkVersionOnce.Do(func() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, dep := range info.Deps {
if strings.Contains(dep.Path, "github.com/line/line-bot-sdk-go") {
sdkVersion = strings.TrimPrefix(dep.Version, "v")
break
}
}
})
return sdkVersion
}
70 changes: 0 additions & 70 deletions script/tag.sh

This file was deleted.

0 comments on commit 53a2230

Please sign in to comment.