Skip to content

Commit

Permalink
feat: gather git_commit_branch value
Browse files Browse the repository at this point in the history
closes #41
  • Loading branch information
antongolub committed Jul 2, 2023
1 parent fce8324 commit 627f8c6
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 27 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
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.
```json
{
"date": "2020-11-05T15:16:35.904Z",
"docker_image_tag": "foo",
"git_commit_id": "007b8f715eb5670662d90f90cd1916398d1dfe98",
"git_rep_url": "https://github.com/qiwi/buildstamp.git",
"git_repo_name": "qiwi/buildstamp"
"date": "2020-11-05T15:16:35.904Z",
"docker_image_tag": "foo",
"git_commit_id": "007b8f715eb5670662d90f90cd1916398d1dfe98",
"git_commit_branch": "master",
"git_repo_url": "https://github.com/qiwi/buildstamp.git",
"git_repo_name": "qiwi/buildstamp"
}
```

Expand Down
6 changes: 5 additions & 1 deletion packages/bash/src/main/sh/buildstamp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ fi

# Git info
if [[ $opt_git == "true" ]]; then
git_commit_branch=$(getFirstDefined $CI_COMMIT_BRANCH $GITHUB_REF_NAME)
if [[ git_commit_branch == "" ]]; then
git_commit_branch = $(git rev-parse --abbrev-ref HEAD)
fi
git_commit_id=$(git rev-parse HEAD)
git_repo_url=$(git config --get remote.origin.url)
re="([^./:]+\/[^./]+)(\.git)?$"
Expand All @@ -60,7 +64,7 @@ fi

# Buildstamp render
# use jq?
for entry in "date" "git_commit_id" "git_repo_url" "git_repo_name" "ci_run_id" "ci_run_url"
for entry in "date" "git_commit_id" "git_commit_branch" "git_repo_url" "git_repo_name" "ci_run_id" "ci_run_url"
do
if [[ ${!entry} != "" ]]; then
json=$json\\n' '\"$entry\":' '\"${!entry}\",
Expand Down
16 changes: 14 additions & 2 deletions packages/bin/src/main/go/buildstamp/buildstamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,29 @@ func GetPkgInfo(cwd string) PackageJson {
}

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

return GitInfo{
hash,
commitId,
commitBranch,
repoUrl,
repoName,
}
}

func getCommitBranch(cwd string) string {
var commitBranch = getFirstNonEmpty(os.Getenv("CI_COMMIT_BRANCH"), os.Getenv("GITHUB_REF_NAME"))
if commitBranch == "" {
var _, branch, _ = invoke("git", []string{"rev-parse", "--abbrev-ref", "HEAD"}, cwd)
commitBranch = branch
}

return commitBranch
}

func GetCIInfo() CIInfo {
var runId = getFirstNonEmpty(os.Getenv("BUILD_NUMBER"), os.Getenv("CI_JOB_ID")+os.Getenv("GITHUB_RUN_ID"))
var runUrl = getFirstNonEmpty(os.Getenv("BUILD_URL"), os.Getenv("GITHUB_RUN_ID"))
Expand Down
20 changes: 11 additions & 9 deletions packages/bin/src/main/go/buildstamp/types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package buildstamp

type Buildstamp struct {
Date string `json:"date,omitempty"`
GitCommitId string `json:"git_commit_id,omitempty"`
GitRepoUrl string `json:"git_repo_url,omitempty"`
GitRepoName string `json:"git_repo_name,omitempty"`
CIRunId string `json:"ci_run_id,omitempty"`
CIRunUrl string `json:"ci_run_url,omitempty"`
Date string `json:"date,omitempty"`
GitCommitId string `json:"git_commit_id,omitempty"`
GitCommitBranch string `json:"git_commit_branch,omitempty"`
GitRepoUrl string `json:"git_repo_url,omitempty"`
GitRepoName string `json:"git_repo_name,omitempty"`
CIRunId string `json:"ci_run_id,omitempty"`
CIRunUrl string `json:"ci_run_url,omitempty"`
}

type PackageJson struct {
Expand All @@ -20,7 +21,8 @@ type CIInfo struct {
}

type GitInfo struct {
CommitId string
RepoUrl string
RepoName string
CommitId string
CommitBranch string
RepoUrl string
RepoName string
}
3 changes: 2 additions & 1 deletion packages/bin/src/main/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ func main() {
}

var buildstamp, _ = json.MarshalIndent(Buildstamp{
date,
gitInfo.CommitId,
gitInfo.RepoUrl,
gitInfo.CommitId,
gitInfo.RepoName,
date,
ciInfo.RunId,
ciInfo.RunUrl,
}, "", " ")
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/main/ts/buildstamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export const buildstamp = async (opts?: IBuildstampOptions): Promise<IBuildstamp
stamp.date = new Date().toISOString()
}
if (git) {
Object.assign(stamp, await getGitInfo(cwd))
Object.assign(stamp, await getGitInfo(cwd, process.env))
}
if (ci) {
Object.assign(stamp, await getCIInfo(process.env))
Object.assign(stamp, getCIInfo(process.env))
}
if (output) {
await fs.writeFile(path.resolve(cwd, output), JSON.stringify(stamp, null, 2))
Expand All @@ -46,12 +46,17 @@ export const buildstamp = async (opts?: IBuildstampOptions): Promise<IBuildstamp
return stamp
}

export const getGitInfo = async (cwd: string): Promise<IGitInfo> => {
export const getCommitBranch = async (cwd: string, env: Record<string, string | undefined>) =>
env.CI_COMMIT_BRANCH || env.GITHUB_REF_NAME || (await spawn('git', ['rev-parse', '--abbrev-ref', 'HEAD'], cwd)).stdout

export const getGitInfo = async (cwd: string, env: Record<string, string | undefined>): Promise<IGitInfo> => {
const { stdout: git_commit_id } = await spawn('git', ['rev-parse', 'HEAD'], cwd)
const { stdout: git_repo_url } = await spawn('git', ['config', '--get', 'remote.origin.url'], cwd)
const git_commit_branch = await getCommitBranch(cwd, env)
const git_repo_name = (git_repo_url.match(/([^./:]+\/[^./]+)(\.git)?$/) || [])[1]

return {
git_commit_branch,
git_commit_id,
git_repo_url,
git_repo_name
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ export interface IBuildstampOptionsNormalized {
export type IBuildstampOptions = Partial<IBuildstampOptionsNormalized>

export interface ICIInfo {
ci_run_id: string
ci_run_url: string
ci_run_id: string
ci_run_url: string
}

export interface IGitInfo {
git_commit_id: string
git_repo_url: string
git_repo_name: string
git_commit_branch: string
git_commit_id: string
git_repo_url: string
git_repo_name: string
}

export interface IBuildstamp extends Partial<IGitInfo>, Partial<ICIInfo> {
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/test/ts/buildstamp.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, it, expect} from '@abstractest/core'
import {buildstamp, getCIInfo} from '../../main/ts/buildstamp'
import {buildstamp, getCIInfo, getGitInfo} from '../../main/ts/buildstamp'

describe('buildstamp', () => {
it('returns a result corresponding the passed opts', async () => {
Expand All @@ -14,6 +14,17 @@ describe('buildstamp', () => {
})
})

describe('getGitInfo()', () => {
it('returns git info', async () => {
const result = await getGitInfo(process.cwd(), process.env)

expect(result.git_repo_name).toEqual('qiwi/buildstamp')
expect(typeof result.git_repo_url).toEqual('string')
expect(typeof result.git_commit_id).toEqual('string')
expect(typeof result.git_commit_branch).toEqual('string')
})
})

describe('getCIInfo()', () => {
const ciRunId = '123'
const ciRunUrl = 'https://cicd.com/123'
Expand Down

0 comments on commit 627f8c6

Please sign in to comment.