Skip to content

Commit bb33c59

Browse files
Update createWithSignature function using async/await
Added a test to verify deprecation warning
1 parent 5901640 commit bb33c59

File tree

2 files changed

+64
-58
lines changed

2 files changed

+64
-58
lines changed

lib/tag.js

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -66,77 +66,47 @@ const deprecatedCreateWithSignatureHelper = util.deprecate(function(repo, oidTar
6666
* representing the signed message
6767
* @return {Oid}
6868
*/
69-
Tag.createWithSignature = function(
69+
Tag.createWithSignature = async (
7070
repo,
7171
tagName,
7272
target,
7373
tagger,
7474
message,
7575
force,
7676
signingCallback
77-
) {
78-
79-
const createTag = function(repo,
80-
tagName,
81-
target,
82-
tagger,
83-
message,
84-
force,
85-
signingCallback) {
86-
let tagBuffer;
87-
88-
return Tag.createBuffer(repo, tagName, target.id(), tagger, message)
89-
.then((tagBufferResult) => {
90-
tagBuffer = tagBufferResult;
91-
return signingCallback(tagBuffer);
92-
})
93-
.then(({ code, signedData }) => {
94-
switch (code) {
95-
case NodeGit.Error.CODE.OK: {
96-
const normalizedEnding = signedData.endsWith("\n") ? "" : "\n";
97-
const signedTagString = tagBuffer + signedData + normalizedEnding;
98-
return Tag.createFromBuffer(repo, signedTagString, force);
99-
}
100-
case NodeGit.Error.CODE.PASSTHROUGH:
101-
return Tag.create(
102-
repo,
103-
tagName,
104-
target,
105-
tagger,
106-
message,
107-
force
108-
);
109-
default: {
110-
const error = new Error(
111-
`Tag.createWithSignature threw with error code ${code}`
112-
);
113-
error.errno = code;
114-
throw error;
115-
}
116-
}
117-
});
118-
};
119-
77+
) => {
78+
let targetOid;
12079
if (!target.id) {
121-
deprecatedCreateWithSignatureHelper(repo, target).then((targetOid) => {
122-
return createTag(repo,
80+
targetOid = await deprecatedCreateWithSignatureHelper(repo, target);
81+
} else {
82+
targetOid = target;
83+
}
84+
85+
const tagBuffer = await Tag.createBuffer(repo, tagName, targetOid.id(), tagger, message);
86+
const { code, signedData } = await signingCallback(tagBuffer);
87+
switch (code) {
88+
case NodeGit.Error.CODE.OK: {
89+
const normalizedEnding = signedData.endsWith("\n") ? "" : "\n";
90+
const signedTagString = tagBuffer + signedData + normalizedEnding;
91+
return Tag.createFromBuffer(repo, signedTagString, force);
92+
}
93+
case NodeGit.Error.CODE.PASSTHROUGH:
94+
return Tag.create(
95+
repo,
12396
tagName,
12497
targetOid,
12598
tagger,
12699
message,
127-
force,
128-
signingCallback);
129-
});
130-
} else {
131-
return createTag(repo,
132-
tagName,
133-
target,
134-
tagger,
135-
message,
136-
force,
137-
signingCallback);
100+
force
101+
);
102+
default: {
103+
const error = new Error(
104+
`Tag.createWithSignature threw with error code ${code}`
105+
);
106+
error.errno = code;
107+
throw error;
108+
}
138109
}
139-
140110
};
141111

142112
/**

test/tests/tag.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,42 @@ describe("Tag", function() {
591591
});
592592
});
593593

594+
it("will show a deprecation warning if createWithSignature use oid instead object", function() {
595+
var targetCommit;
596+
const name = "created-signed-tag-annotationCreate";
597+
const repository = this.repository;
598+
const signature = Signature.create(
599+
"Shaggy Rogers",
600+
601+
987654321,
602+
90
603+
);
604+
const message = "I'm a teapot";
605+
const signingCallback = () => ({
606+
code: NodeGit.Error.CODE.ERROR
607+
});
608+
609+
610+
return repository.getCommit(commitPointedTo).then((commit) => {
611+
targetCommit = commit;
612+
return Tag.createWithSignature(
613+
repository,
614+
name,
615+
targetCommit.id(),
616+
signature,
617+
message,
618+
1,
619+
signingCallback
620+
);
621+
}).then(function() {
622+
assert.fail("Should not have been able to create tag");
623+
}, function(error) {
624+
if (error && error.errno === NodeGit.Error.CODE.ERROR) {
625+
return;
626+
}
627+
throw error;
628+
});
629+
});
594630

595631
it("can create a new signed tag with Tag.annotationCreate", function() {
596632
var targetCommit;

0 commit comments

Comments
 (0)