Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bobg/modver
Browse files Browse the repository at this point in the history
  • Loading branch information
bobg committed Apr 11, 2023
2 parents 3979887 + 9f38d1b commit bf5b672
Show file tree
Hide file tree
Showing 25 changed files with 1,408 additions and 325 deletions.
32 changes: 17 additions & 15 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18

- name: Unit tests
run: go test -v -coverprofile=cover.out ./...
- name: Unit tests
run: go test -coverprofile=cover.out ./...

- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: cover.out
- name: Modver
if: ${{ github.event_name == 'pull_request' }}
run: go run ./cmd/modver -pr https://github.com/${{ github.repository }}/pull/${{ github.event.number }} -token ${{ github.token }} -pretty

- name: Version string check
run: go run ./cmd/modver -git `pwd`/.git -versions HEAD~1 HEAD
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: cover.out
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*~
cover.out
modver
/cover.out
/modver
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM golang:latest

ADD . /app

WORKDIR /app

RUN go build ./cmd/modver-action

ENTRYPOINT ["/app/modver-action"]
72 changes: 70 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
[![Coverage Status](https://coveralls.io/repos/github/bobg/modver/badge.svg?branch=master)](https://coveralls.io/github/bobg/modver?branch=master)

This is modver,
a Go package and command that helps you obey [semantic versioning rules](https://semver.org/) in your Go module.
a tool that helps you obey [semantic versioning rules](https://semver.org/) in your Go module.

It can read and compare two different versions of the same module,
from two different directories,
or two different Git commits.
or two different Git commits,
or the base and head of a Git pull request.
It then reports whether the changes require an increase in the major-version number,
the minor-version number,
or the patchlevel.

## Installation and usage

Modver can be used from the command line,
or in your Go program,
or with [GitHub Actions](https://github.com/features/actions).

### Command-line interface

Install the `modver` command like this:

```sh
Expand All @@ -37,7 +44,68 @@ The arguments `HEAD~1` and `HEAD` specify two Git revisions to compare;
in this case, the latest two commits on the current branch.
These could also be tags or commit hashes.

### GitHub Action

You can arrange for Modver to inspect the changes on your pull-request branch
as part of a GitHub Actions-based continuous-integration step.
It will add a comment to the pull request with its findings,
and will update the comment as new commits are pushed to the branch.

To do this, you’ll need a directory in your GitHub repository named `.github/workflows`,
and a Yaml file containing (at least) the following:

```yaml
name: Tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.19

- name: Modver
if: ${{ github.event_name == 'pull_request' }}
uses: bobg/modver@v2.5.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
pull_request_url: https://github.com/${{ github.repository }}/pull/${{ github.event.number }}
```
This can be combined with other steps that run unit tests, etc.
You can change `Tests` to whatever name you like,
and should change `main` to the name of your repository’s default branch.
If your pull request is on a GitHub server other than `github.com`,
change the hostname in the `pull_request_url` parameter to match.

Note the `fetch-depth: 0` parameter for the `Checkout` step.
This causes GitHub Actions to create a clone of your repo with its full history,
as opposed to the default,
which is a shallow clone.
Modver requires enough history to be present in the clone
for it to access the “base” and “head” revisions of your pull-request branch.

For more information about configuring GitHub Actions,
see [the GitHub Actions documentation](https://docs.github.com/actions).

### Go library

Modver also has a simple API for use from within Go programs.
Add it to your project with `go get github.com/bobg/modver/v2@latest`.
See [the Go doc page](https://pkg.go.dev/github.com/bobg/modver/v2) for information about how to use it.

## Semantic versioning

Expand Down
13 changes: 13 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Modver
description: Analyze pull requests for changes in Go code that require updating a module's version number.
author: Bob Glickstein
inputs:
github_token:
description: 'The GitHub token to use for authentication.'
required: true
pull_request_url:
description: 'The full github.com URL of the pull request.'
required: true
runs:
using: 'docker'
image: 'Dockerfile'
34 changes: 34 additions & 0 deletions cmd/modver-action/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
"log"
"os"

"github.com/bobg/modver/v2"
"github.com/bobg/modver/v2/internal"
)

func main() {
os.Setenv("GOROOT", "/usr/local/go") // Work around some Docker weirdness.

prURL := os.Getenv("INPUT_PULL_REQUEST_URL")
host, owner, reponame, prnum, err := internal.ParsePR(prURL)
if err != nil {
log.Fatal(err)
}
token := os.Getenv("INPUT_GITHUB_TOKEN")
if token == "" {
log.Fatal("No GitHub token in the environment variable INPUT_GITHUB_TOKEN")
}
ctx := context.Background()
gh, err := internal.NewClient(ctx, host, token)
if err != nil {
log.Fatalf("Creating GitHub client: %s", err)
}
result, err := internal.PR(ctx, gh, owner, reponame, prnum)
if err != nil {
log.Fatalf("Running comparison: %s", err)
}
modver.Pretty(os.Stdout, result)
}
58 changes: 58 additions & 0 deletions cmd/modver/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"fmt"
"os"

"github.com/google/go-github/v50/github"
"github.com/pkg/errors"

"github.com/bobg/modver/v2"
"github.com/bobg/modver/v2/internal"
)

func doCompare(ctx context.Context, opts options) (modver.Result, error) {
return doCompareHelper(ctx, opts, internal.NewClient, internal.PR, modver.CompareGitWith, modver.CompareDirs)
}

type (
newClientType = func(ctx context.Context, host, token string) (*github.Client, error)
prType = func(ctx context.Context, gh *github.Client, owner, reponame string, prnum int) (modver.Result, error)
compareGitWithType = func(ctx context.Context, repoURL, olderRev, newerRev string, f func(older, newer string) (modver.Result, error)) (modver.Result, error)
compareDirsType = func(older, newer string) (modver.Result, error)
)

func doCompareHelper(ctx context.Context, opts options, newClient newClientType, pr prType, compareGitWith compareGitWithType, compareDirs compareDirsType) (modver.Result, error) {
if opts.pr != "" {
host, owner, reponame, prnum, err := internal.ParsePR(opts.pr)
if err != nil {
return modver.None, errors.Wrap(err, "parsing pull-request URL")
}
if opts.ghtoken == "" {
return modver.None, fmt.Errorf("usage: %s -pr URL [-token TOKEN]", os.Args[0])
}
gh, err := newClient(ctx, host, opts.ghtoken)
if err != nil {
return modver.None, errors.Wrap(err, "creating GitHub client")
}
return pr(ctx, gh, owner, reponame, prnum)
}

if opts.gitRepo != "" {
if len(opts.args) != 2 {
return nil, fmt.Errorf("usage: %s -git REPO [-gitcmd GIT_COMMAND] [-q | -pretty] [-v1 OLDERVERSION -v2 NEWERVERSION | -versions] OLDERREV NEWERREV", os.Args[0])
}

callback := modver.CompareDirs
if opts.versions {
callback = getTags(&opts.v1, &opts.v2, opts.args[0], opts.args[1])
}

return compareGitWith(ctx, opts.gitRepo, opts.args[0], opts.args[1], callback)
}
if len(opts.args) != 2 {
return nil, fmt.Errorf("usage: %s [-q | -pretty] [-v1 OLDERVERSION -v2 NEWERVERSION] OLDERDIR NEWERDIR", os.Args[0])
}
return compareDirs(opts.args[0], opts.args[1])
}
Loading

0 comments on commit bf5b672

Please sign in to comment.