Skip to content

Commit

Permalink
fix: Correctly identify latest tag in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed Nov 3, 2022
1 parent 9d463a3 commit d44e67d
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 38 deletions.
2 changes: 1 addition & 1 deletion dist/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/app.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions dist/tag.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions dist/tag.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/tag.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const app = (probot: Probot) => {
return;
}

const tag = new Tag(await Tag.getLatestTag(context));
const tag = new Tag(await Tag.getLatestTag());

if (!tag.latest) {
warning(`Repository doesn't have any tags or releases published.`);
Expand Down
27 changes: 16 additions & 11 deletions src/tag.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Context } from 'probot';
import { exec } from 'node:child_process';
import { promisify } from 'node:util';

import { events } from './events';
import { warning } from '@actions/core';

const promiseExec = promisify(exec);

export class Tag {
private _latest: string | undefined;
Expand All @@ -21,16 +24,18 @@ export class Tag {
);
}

static async getLatestTag(
context: {
[K in keyof typeof events]: Context<typeof events[K][number]>;
}[keyof typeof events]
) {
const tags = (await context.octokit.rest.repos.listTags(context.repo()))
.data;
static async getLatestTag() {
// Get latest tag sorted by date, currently impossible by using GitHub REST API
// Based on: https://gist.github.com/rponte/fdc0724dd984088606b0?permalink_comment_id=3475480#gistcomment-3475480
const { stdout, stderr } = await promiseExec(
'git tag --sort=committerdate | tail -1'
);

if (!tags || tags.length <= 0) return undefined;
const tag = stdout;
if (stderr) {
warning(`Unable to get latest tag - stderr: ${stderr}`);
}

return tags.shift()?.name;
return !tag || tag.length <= 0 ? undefined : tag;
}
}

0 comments on commit d44e67d

Please sign in to comment.