App-GHGen

 view release on metacpan or  search on metacpan

lib/App/GHGen/Fixer.pm  view on Meta::CPAN

        my $steps = $job->{steps} or next;
        for my $step (@$steps) {
            next unless $step->{uses};

            if ($step->{uses} =~ /^(.+?)\@(?:master|main)$/) {
                my $action = $1;
                # Map to appropriate version
                my $version = get_latest_version($action);
                $step->{uses} = "$action\@$version";
                $modified++;
            }
        }
    }

    return $modified;
}

sub add_permissions($workflow) {
	return 0 if $workflow->{permissions};

	$workflow->{permissions} = { contents => 'read' };
	return 1;
}

sub update_actions($workflow) {
	my $jobs = $workflow->{jobs} or return 0;
	my $modified = 0;

	# Premise: %ACTION_UPDATES is the canonical version table (defined above).
	# Conclusion: this function and find_outdated_actions (Analyzer) are always in sync.
	for my $job (values %$jobs) {
		my $steps = $job->{steps} or next;
		for my $step (@$steps) {
			next unless $step->{uses};
			for my $old (keys %ACTION_UPDATES) {
				if ($step->{uses} =~ /^\Q$old\E/) {
					$step->{uses} = $ACTION_UPDATES{$old};
					$modified++;
				}
			}
		}
	}

	return $modified;
}

sub add_concurrency($workflow) {
	return 0 if $workflow->{concurrency};

    $workflow->{concurrency} = {
        group => '${{ github.workflow }}-${{ github.ref }}',
        'cancel-in-progress' => 'true',
    };
    return 1;
}

sub add_trigger_filters($workflow) {
	my $on = $workflow->{on} or return 0;
	my $modified = 0;

    # If 'on' is just 'push', expand it
    if (ref $on eq 'ARRAY' && grep { $_ eq 'push' } @$on) {
        $workflow->{on} = {
            push => {
                branches => ['main', 'master'],
            },
            pull_request => {
                branches => ['main', 'master'],
            },
        };
        $modified++;
    }
    elsif (ref $on eq 'HASH' && $on->{push} && ref $on->{push} eq '') {
        # 'push' with no filters
        $on->{push} = {
            branches => ['main', 'master'],
        };
        $modified++;
    }

    return $modified;
}

sub add_missing_timeout($workflow) {
    my $jobs = $workflow->{jobs} or return 0;
    my $modified = 0;

    for my $job_name (keys %$jobs) {
        my $job = $jobs->{$job_name};

        # Skip if timeout already exists
        next if exists $job->{'timeout-minutes'};

        # Insert default timeout
        $job->{'timeout-minutes'} = 30;
        $modified++;
    }

	return $modified;
}

sub update_runners($workflow) {
	my $jobs = $workflow->{jobs} or return 0;
	my $modified = 0;

    my %runner_updates = (
        'ubuntu-18.04' => 'ubuntu-latest',
        'ubuntu-16.04' => 'ubuntu-latest',
        'macos-10.15'  => 'macos-latest',
        'windows-2016' => 'windows-latest',
    );

    for my $job (values %$jobs) {
        my $runs_on = $job->{'runs-on'} or next;

        if (exists $runner_updates{$runs_on}) {
            $job->{'runs-on'} = $runner_updates{$runs_on};
            $modified++;
        }
    }



( run in 3.145 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )