Skip to content

Commit 53a2230

Browse files
authored
Load sdk version from buildinfo (#521)
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
1 parent 426fd6c commit 53a2230

File tree

4 files changed

+25
-124
lines changed

4 files changed

+25
-124
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 49 deletions
This file was deleted.

linebot/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (client *Client) url(base *url.URL, endpoint string) string {
209209

210210
func (client *Client) do(ctx context.Context, req *http.Request) (*http.Response, error) {
211211
req.Header.Set("Authorization", "Bearer "+client.channelToken)
212-
req.Header.Set("User-Agent", "LINE-BotSDK-Go/"+version)
212+
req.Header.Set("User-Agent", "LINE-BotSDK-Go/"+GetVersion())
213213
if len(client.retryKeyID) > 0 {
214214
req.Header.Set("X-Line-Retry-Key", client.retryKeyID)
215215
}

linebot/version.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@
1111
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
// License for the specific language governing permissions and limitations
1313
// under the License.
14-
//
15-
// Don't edit this file directly. This file is generated by script/tag.sh .
1614

1715
package linebot
1816

19-
const version = "8.10.0"
17+
import (
18+
"runtime/debug"
19+
"strings"
20+
"sync"
21+
)
22+
23+
var (
24+
sdkVersion = "8.unknown"
25+
sdkVersionOnce sync.Once
26+
)
2027

2128
func GetVersion() string {
22-
return version
29+
// getting the version of line-bot-sdk-go should be done only once. Computing it repeatedly is meaningless.
30+
sdkVersionOnce.Do(func() {
31+
info, ok := debug.ReadBuildInfo()
32+
if !ok {
33+
return
34+
}
35+
for _, dep := range info.Deps {
36+
if strings.Contains(dep.Path, "github.com/line/line-bot-sdk-go") {
37+
sdkVersion = strings.TrimPrefix(dep.Version, "v")
38+
break
39+
}
40+
}
41+
})
42+
return sdkVersion
2343
}

script/tag.sh

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)