Skip to content

Commit 627f8c6

Browse files
committed
feat: gather git_commit_branch value
closes #41
1 parent fce8324 commit 627f8c6

File tree

8 files changed

+64
-27
lines changed

8 files changed

+64
-27
lines changed

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
A small utility for generating `buildstamp` file, which contains various build info like timestamp, repo name, git commit and so on. This file could be a part of some release artifact (npm-package, docker-image) and makes it self-descriptive.
77
```json
88
{
9-
"date": "2020-11-05T15:16:35.904Z",
10-
"docker_image_tag": "foo",
11-
"git_commit_id": "007b8f715eb5670662d90f90cd1916398d1dfe98",
12-
"git_rep_url": "https://github.com/qiwi/buildstamp.git",
13-
"git_repo_name": "qiwi/buildstamp"
9+
"date": "2020-11-05T15:16:35.904Z",
10+
"docker_image_tag": "foo",
11+
"git_commit_id": "007b8f715eb5670662d90f90cd1916398d1dfe98",
12+
"git_commit_branch": "master",
13+
"git_repo_url": "https://github.com/qiwi/buildstamp.git",
14+
"git_repo_name": "qiwi/buildstamp"
1415
}
1516
```
1617

packages/bash/src/main/sh/buildstamp.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ fi
3939

4040
# Git info
4141
if [[ $opt_git == "true" ]]; then
42+
git_commit_branch=$(getFirstDefined $CI_COMMIT_BRANCH $GITHUB_REF_NAME)
43+
if [[ git_commit_branch == "" ]]; then
44+
git_commit_branch = $(git rev-parse --abbrev-ref HEAD)
45+
fi
4246
git_commit_id=$(git rev-parse HEAD)
4347
git_repo_url=$(git config --get remote.origin.url)
4448
re="([^./:]+\/[^./]+)(\.git)?$"
@@ -60,7 +64,7 @@ fi
6064

6165
# Buildstamp render
6266
# use jq?
63-
for entry in "date" "git_commit_id" "git_repo_url" "git_repo_name" "ci_run_id" "ci_run_url"
67+
for entry in "date" "git_commit_id" "git_commit_branch" "git_repo_url" "git_repo_name" "ci_run_id" "ci_run_url"
6468
do
6569
if [[ ${!entry} != "" ]]; then
6670
json=$json\\n' '\"$entry\":' '\"${!entry}\",

packages/bin/src/main/go/buildstamp/buildstamp.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,29 @@ func GetPkgInfo(cwd string) PackageJson {
3535
}
3636

3737
func GetGitInfo(cwd string) GitInfo {
38-
var _, hash, _ = invoke("git", []string{"rev-parse", "HEAD"}, cwd)
38+
var _, commitId, _ = invoke("git", []string{"rev-parse", "HEAD"}, cwd)
3939
var _, repoUrl, _ = invoke("git", []string{"config", "--get", "remote.origin.url"}, cwd)
4040
var repoName = string(repoUrl[strings.LastIndex(repoUrl, ":")+1 : strings.LastIndex(repoUrl, ".")])
41+
var commitBranch = getCommitBranch(cwd)
4142

4243
return GitInfo{
43-
hash,
44+
commitId,
45+
commitBranch,
4446
repoUrl,
4547
repoName,
4648
}
4749
}
4850

51+
func getCommitBranch(cwd string) string {
52+
var commitBranch = getFirstNonEmpty(os.Getenv("CI_COMMIT_BRANCH"), os.Getenv("GITHUB_REF_NAME"))
53+
if commitBranch == "" {
54+
var _, branch, _ = invoke("git", []string{"rev-parse", "--abbrev-ref", "HEAD"}, cwd)
55+
commitBranch = branch
56+
}
57+
58+
return commitBranch
59+
}
60+
4961
func GetCIInfo() CIInfo {
5062
var runId = getFirstNonEmpty(os.Getenv("BUILD_NUMBER"), os.Getenv("CI_JOB_ID")+os.Getenv("GITHUB_RUN_ID"))
5163
var runUrl = getFirstNonEmpty(os.Getenv("BUILD_URL"), os.Getenv("GITHUB_RUN_ID"))
+11-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package buildstamp
22

33
type Buildstamp struct {
4-
Date string `json:"date,omitempty"`
5-
GitCommitId string `json:"git_commit_id,omitempty"`
6-
GitRepoUrl string `json:"git_repo_url,omitempty"`
7-
GitRepoName string `json:"git_repo_name,omitempty"`
8-
CIRunId string `json:"ci_run_id,omitempty"`
9-
CIRunUrl string `json:"ci_run_url,omitempty"`
4+
Date string `json:"date,omitempty"`
5+
GitCommitId string `json:"git_commit_id,omitempty"`
6+
GitCommitBranch string `json:"git_commit_branch,omitempty"`
7+
GitRepoUrl string `json:"git_repo_url,omitempty"`
8+
GitRepoName string `json:"git_repo_name,omitempty"`
9+
CIRunId string `json:"ci_run_id,omitempty"`
10+
CIRunUrl string `json:"ci_run_url,omitempty"`
1011
}
1112

1213
type PackageJson struct {
@@ -20,7 +21,8 @@ type CIInfo struct {
2021
}
2122

2223
type GitInfo struct {
23-
CommitId string
24-
RepoUrl string
25-
RepoName string
24+
CommitId string
25+
CommitBranch string
26+
RepoUrl string
27+
RepoName string
2628
}

packages/bin/src/main/go/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ func main() {
5959
}
6060

6161
var buildstamp, _ = json.MarshalIndent(Buildstamp{
62+
date,
6263
gitInfo.CommitId,
6364
gitInfo.RepoUrl,
65+
gitInfo.CommitId,
6466
gitInfo.RepoName,
65-
date,
6667
ciInfo.RunId,
6768
ciInfo.RunUrl,
6869
}, "", " ")

packages/core/src/main/ts/buildstamp.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export const buildstamp = async (opts?: IBuildstampOptions): Promise<IBuildstamp
3434
stamp.date = new Date().toISOString()
3535
}
3636
if (git) {
37-
Object.assign(stamp, await getGitInfo(cwd))
37+
Object.assign(stamp, await getGitInfo(cwd, process.env))
3838
}
3939
if (ci) {
40-
Object.assign(stamp, await getCIInfo(process.env))
40+
Object.assign(stamp, getCIInfo(process.env))
4141
}
4242
if (output) {
4343
await fs.writeFile(path.resolve(cwd, output), JSON.stringify(stamp, null, 2))
@@ -46,12 +46,17 @@ export const buildstamp = async (opts?: IBuildstampOptions): Promise<IBuildstamp
4646
return stamp
4747
}
4848

49-
export const getGitInfo = async (cwd: string): Promise<IGitInfo> => {
49+
export const getCommitBranch = async (cwd: string, env: Record<string, string | undefined>) =>
50+
env.CI_COMMIT_BRANCH || env.GITHUB_REF_NAME || (await spawn('git', ['rev-parse', '--abbrev-ref', 'HEAD'], cwd)).stdout
51+
52+
export const getGitInfo = async (cwd: string, env: Record<string, string | undefined>): Promise<IGitInfo> => {
5053
const { stdout: git_commit_id } = await spawn('git', ['rev-parse', 'HEAD'], cwd)
5154
const { stdout: git_repo_url } = await spawn('git', ['config', '--get', 'remote.origin.url'], cwd)
55+
const git_commit_branch = await getCommitBranch(cwd, env)
5256
const git_repo_name = (git_repo_url.match(/([^./:]+\/[^./]+)(\.git)?$/) || [])[1]
5357

5458
return {
59+
git_commit_branch,
5560
git_commit_id,
5661
git_repo_url,
5762
git_repo_name

packages/core/src/main/ts/interface.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ export interface IBuildstampOptionsNormalized {
1010
export type IBuildstampOptions = Partial<IBuildstampOptionsNormalized>
1111

1212
export interface ICIInfo {
13-
ci_run_id: string
14-
ci_run_url: string
13+
ci_run_id: string
14+
ci_run_url: string
1515
}
1616

1717
export interface IGitInfo {
18-
git_commit_id: string
19-
git_repo_url: string
20-
git_repo_name: string
18+
git_commit_branch: string
19+
git_commit_id: string
20+
git_repo_url: string
21+
git_repo_name: string
2122
}
2223

2324
export interface IBuildstamp extends Partial<IGitInfo>, Partial<ICIInfo> {

packages/core/src/test/ts/buildstamp.test.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {describe, it, expect} from '@abstractest/core'
2-
import {buildstamp, getCIInfo} from '../../main/ts/buildstamp'
2+
import {buildstamp, getCIInfo, getGitInfo} from '../../main/ts/buildstamp'
33

44
describe('buildstamp', () => {
55
it('returns a result corresponding the passed opts', async () => {
@@ -14,6 +14,17 @@ describe('buildstamp', () => {
1414
})
1515
})
1616

17+
describe('getGitInfo()', () => {
18+
it('returns git info', async () => {
19+
const result = await getGitInfo(process.cwd(), process.env)
20+
21+
expect(result.git_repo_name).toEqual('qiwi/buildstamp')
22+
expect(typeof result.git_repo_url).toEqual('string')
23+
expect(typeof result.git_commit_id).toEqual('string')
24+
expect(typeof result.git_commit_branch).toEqual('string')
25+
})
26+
})
27+
1728
describe('getCIInfo()', () => {
1829
const ciRunId = '123'
1930
const ciRunUrl = 'https://cicd.com/123'

0 commit comments

Comments
 (0)