Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Repository reset now tracks actual git refs (#2)
Browse files Browse the repository at this point in the history
Repository reset operation was comparing the captured refs with themselves. Fix now makes it compare the captured refs with current repository refs.
Also had to add a conditional explicitly excluding PRs from the ref iteration, as for some reason github puts PRs as refs 🤔

Also added more in depth testing for the repository reset operation, and updated github-api lib to latest version.

Co-authored-by: Joaquim Neto <[email protected]>
  • Loading branch information
Joaquimmnetto and Joaquimmnetto authored Jun 29, 2021
1 parent f1316c2 commit 276aa53
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if (cliTasks.contains("rc")) {

dependencies {
implementation 'org.codehaus.groovy:groovy-all:2.4.15'
api 'org.kohsuke:github-api:1.95'
api 'org.kohsuke:github-api:1.131'
implementation('org.spockframework:spock-core:1.2-groovy-2.4')

testImplementation 'net.bytebuddy:byte-buddy:[1.9,2)'
Expand Down
49 changes: 31 additions & 18 deletions src/main/groovy/com/wooga/spock/extensions/github/Repository.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ class Repository {
}

void captureResetRefs() {
try {
resetRefs = repository.refs
} catch (HttpException e) {
if (!e.message.contains("Git Repository is empty.")) {
throw e
}
resetRefs = []
}
resetRefs = tryGetRefs()
}

void resetRepository() {
Expand Down Expand Up @@ -133,8 +126,12 @@ class Repository {

GHTag tag(String tagName, String ref) {
repository.createRef("refs/tags/$tagName", ref)
findTag(tagName).orElse(null)
}

Optional<GHTag> findTag(String tagName) {
//there is no other way at the moment to load a GHTag
listTags().find { it.name == tagName } as GHTag
Optional.ofNullable(listTags().find { it.name == tagName } as GHTag)
}

GHPullRequest setupPullRequestWithFileChange(String title, String body, String branchName) {
Expand Down Expand Up @@ -208,24 +205,40 @@ class Repository {
resetRepositoryRefs(resetRefs)
}

static void resetRepositoryRefs(GHRef[] refs) {
Map<String, GHRef> capturedRefs = refs.inject(new HashMap<String, GHRef>()) { memo, item ->
void resetRepositoryRefs(GHRef[] refsToReset) {
Map<String, GHRef> capturedRefs = refsToReset.inject(new HashMap<String, GHRef>()) { memo, item ->
memo[item.ref] = item
memo
}

GHRef[] currentRefs = refs

currentRefs.each {
this.tryGetRefs().each {
if(it.ref.startsWith("refs/pull")) { //why the hell github puts pull requests as refs??
return
}
GHRef oldState = capturedRefs[it.ref]
if (oldState) {
it.updateTo(oldState.object.sha, true)
} else {
try {
it.delete()
} catch (ignored) {
}
tryToDelete(it)
}
}
}

GHRef[] tryGetRefs() {
try {
return repository.refs
} catch (HttpException e) {
if (!e.message.contains("Git Repository is empty.")) {
throw e
}
return []
}
}

private static void tryToDelete(GHRef ref) {
try {
ref.delete()
} catch (ignored) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,70 @@ class GithubSharedAutoResetRepositorySpec extends BaseGithubRepositorySpec {
commits << [3, 3]
message << ['initial', 'second run']
}

def "resets commits on repository reset"() {
given: "a repository with captured refs"
def defaultBranch = testRepository.defaultBranch
def capturedCommit = testRepository.getCommit(defaultBranch)
testRepository.captureResetRefs()

and: "a new commit in the head of the main branch"
testRepository.commit("simple test commit")

when: "repository is reseted"
testRepository.resetRepository()

then: "head commit is the same as the captured state"
testRepository.getCommit(defaultBranch).SHA1 == capturedCommit.SHA1
}

def "delete created branches on repository reset"() {
given: "a repository with captured refs"
testRepository.captureResetRefs()

and: "a newly created branch"
def createdBranch = testRepository.createBranch("testbranch")

when: "repository is reseted"
testRepository.resetRepository()

then: "newly created branch does not exists anymore"
!testRepository.branches.containsKey(createdBranch.name)
}

def "rewind branches on repository reset"() {
given: "a repository with a secondary branch and captured refs"
def secondaryBranch = testRepository.createBranch("secondary")
def previousCommit = testRepository.getCommit(secondaryBranch.SHA1)
testRepository.captureResetRefs()


and: "a new commit in the head of a secondary branch"
testRepository.createCommit().
message("secondary commit").
tree(previousCommit.tree.sha).
parent(secondaryBranch.SHA1).create()

when: "repository is reseted"
testRepository.resetRepository()

then: "head commit is the same as the captured state"
testRepository.branches.containsKey(secondaryBranch.name)
testRepository.getCommit(secondaryBranch).SHA1 == previousCommit.SHA1

}

def "deletes created tags on repository reset"() {
given: "a repository with captured refs"
testRepository.captureResetRefs()

and: "a newly created tag"
def newTag = testRepository.tag("newTag")

when: "repository is reseted"
testRepository.resetRepository()

then: "newly created tag does not exists anymore"
!testRepository.findTag(newTag.name).isPresent()
}
}

0 comments on commit 276aa53

Please sign in to comment.