feat: Convert all app files to JS

This commit is contained in:
Faruk AYDIN
2024-01-05 17:44:21 +01:00
parent b95478b635
commit 43dba351c3
1030 changed files with 5114 additions and 6436 deletions

View File

@@ -1,5 +1,5 @@
import defineAction from '../../../../helpers/define-action';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import defineAction from '../../../../helpers/define-action.js';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo.js';
export default defineAction({
name: 'Create issue',
@@ -9,7 +9,7 @@ export default defineAction({
{
label: 'Repo',
key: 'repo',
type: 'dropdown' as const,
type: 'dropdown',
required: false,
variables: true,
source: {
@@ -26,23 +26,23 @@ export default defineAction({
{
label: 'Title',
key: 'title',
type: 'string' as const,
type: 'string',
required: true,
variables: true,
},
{
label: 'Body',
key: 'body',
type: 'string' as const,
type: 'string',
required: true,
variables: true,
},
],
async run($) {
const repoParameter = $.step.parameters.repo as string;
const title = $.step.parameters.title as string;
const body = $.step.parameters.body as string;
const repoParameter = $.step.parameters.repo;
const title = $.step.parameters.title;
const body = $.step.parameters.body;
if (!repoParameter) throw new Error('A repo must be set!');
if (!title) throw new Error('A title must be set!');

View File

@@ -0,0 +1,3 @@
import createIssue from './create-issue/index.js';
export default [createIssue];

View File

@@ -1,3 +0,0 @@
import createIssue from './create-issue';
export default [createIssue];

View File

@@ -0,0 +1,22 @@
import { URLSearchParams } from 'url';
export default async function generateAuthUrl($) {
const scopes = ['read:org', 'repo', 'user'];
const oauthRedirectUrlField = $.app.auth.fields.find(
(field) => field.key == 'oAuthRedirectUrl'
);
const redirectUri = oauthRedirectUrlField.value;
const searchParams = new URLSearchParams({
client_id: $.auth.data.consumerKey,
redirect_uri: redirectUri,
scope: scopes.join(','),
});
const url = `${
$.app.baseUrl
}/login/oauth/authorize?${searchParams.toString()}`;
await $.auth.set({
url,
});
}

View File

@@ -1,22 +0,0 @@
import { IField, IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
export default async function generateAuthUrl($: IGlobalVariable) {
const scopes = ['read:org', 'repo', 'user'];
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const redirectUri = oauthRedirectUrlField.value as string;
const searchParams = new URLSearchParams({
client_id: $.auth.data.consumerKey as string,
redirect_uri: redirectUri,
scope: scopes.join(','),
});
const url = `${$.app.baseUrl
}/login/oauth/authorize?${searchParams.toString()}`;
await $.auth.set({
url,
});
}

View File

@@ -1,13 +1,13 @@
import generateAuthUrl from './generate-auth-url';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
import generateAuthUrl from './generate-auth-url.js';
import verifyCredentials from './verify-credentials.js';
import isStillVerified from './is-still-verified.js';
export default {
fields: [
{
key: 'oAuthRedirectUrl',
label: 'OAuth Redirect URL',
type: 'string' as const,
type: 'string',
required: true,
readOnly: true,
value: '{WEB_APP_URL}/app/github/connections/add',
@@ -20,7 +20,7 @@ export default {
{
key: 'consumerKey',
label: 'Client ID',
type: 'string' as const,
type: 'string',
required: true,
readOnly: false,
value: null,
@@ -32,7 +32,7 @@ export default {
{
key: 'consumerSecret',
label: 'Client Secret',
type: 'string' as const,
type: 'string',
required: true,
readOnly: false,
value: null,

View File

@@ -0,0 +1,8 @@
import getCurrentUser from '../common/get-current-user.js';
const isStillVerified = async ($) => {
const user = await getCurrentUser($);
return !!user.id;
};
export default isStillVerified;

View File

@@ -1,9 +0,0 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
const isStillVerified = async ($: IGlobalVariable) => {
const user = await getCurrentUser($);
return !!user.id;
};
export default isStillVerified;

View File

@@ -1,7 +1,6 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
import getCurrentUser from '../common/get-current-user.js';
const verifyCredentials = async ($: IGlobalVariable) => {
const verifyCredentials = async ($) => {
const response = await $.http.post(
`${$.app.baseUrl}/login/oauth/access_token`,
{

View File

@@ -1,6 +1,4 @@
import { TBeforeRequest } from '@automatisch/types';
const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
const addAuthHeader = ($, requestConfig) => {
if (requestConfig.headers && $.auth.data?.accessToken) {
requestConfig.headers.Authorization = `Bearer ${$.auth.data.accessToken}`;
}

View File

@@ -0,0 +1,8 @@
const getCurrentUser = async ($) => {
const response = await $.http.get('/user');
const currentUser = response.data;
return currentUser;
};
export default getCurrentUser;

View File

@@ -1,10 +0,0 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
const getCurrentUser = async ($: IGlobalVariable): Promise<IJSONObject> => {
const response = await $.http.get('/user');
const currentUser = response.data;
return currentUser;
};
export default getCurrentUser;

View File

@@ -0,0 +1,10 @@
export default function getRepoOwnerAndRepo(repoFullName) {
if (!repoFullName) return {};
const [repoOwner, repo] = repoFullName.split('/');
return {
repoOwner,
repo,
};
}

View File

@@ -1,17 +0,0 @@
type TRepoOwnerAndRepo = {
repoOwner?: string;
repo?: string;
};
export default function getRepoOwnerAndRepo(
repoFullName: string
): TRepoOwnerAndRepo {
if (!repoFullName) return {};
const [repoOwner, repo] = repoFullName.split('/');
return {
repoOwner,
repo,
};
}

View File

@@ -1,18 +1,8 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import type { AxiosResponse } from 'axios';
import parseLinkHeader from '../../../helpers/parse-header-link';
import parseLinkHeader from '../../../helpers/parse-header-link.js';
type TResponse = {
data: IJSONObject[];
error?: IJSONObject;
};
export default async function paginateAll(
$: IGlobalVariable,
request: Promise<AxiosResponse>
) {
export default async function paginateAll($, request) {
const response = await request;
const aggregatedResponse: TResponse = {
const aggregatedResponse = {
data: [...response.data],
};

View File

@@ -0,0 +1,4 @@
import listLabels from './list-labels/index.js';
import listRepos from './list-repos/index.js';
export default [listLabels, listRepos];

View File

@@ -1,4 +0,0 @@
import listLabels from './list-labels';
import listRepos from './list-repos';
export default [listLabels, listRepos];

View File

@@ -1,22 +1,19 @@
import { IGlobalVariable } from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import paginateAll from '../../common/paginate-all';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo.js';
import paginateAll from '../../common/paginate-all.js';
export default {
name: 'List labels',
key: 'listLabels',
async run($: IGlobalVariable) {
const { repoOwner, repo } = getRepoOwnerAndRepo(
$.step.parameters.repo as string
);
async run($) {
const { repoOwner, repo } = getRepoOwnerAndRepo($.step.parameters.repo);
if (!repo) return { data: [] };
const firstPageRequest = $.http.get(`/repos/${repoOwner}/${repo}/labels`);
const response = await paginateAll($, firstPageRequest);
response.data = response.data.map((repo: { name: string }) => {
response.data = response.data.map((repo) => {
return {
value: repo.name,
name: repo.name,

View File

@@ -1,15 +1,14 @@
import { IGlobalVariable } from '@automatisch/types';
import paginateAll from '../../common/paginate-all';
import paginateAll from '../../common/paginate-all.js';
export default {
name: 'List repos',
key: 'listRepos',
async run($: IGlobalVariable) {
async run($) {
const firstPageRequest = $.http.get('/user/repos');
const response = await paginateAll($, firstPageRequest);
response.data = response.data.map((repo: { full_name: string }) => {
response.data = response.data.map((repo) => {
return {
value: repo.full_name,
name: repo.full_name,

View File

@@ -1,9 +1,9 @@
import defineApp from '../../helpers/define-app';
import addAuthHeader from './common/add-auth-header';
import auth from './auth';
import triggers from './triggers';
import actions from './actions';
import dynamicData from './dynamic-data';
import defineApp from '../../helpers/define-app.js';
import addAuthHeader from './common/add-auth-header.js';
import auth from './auth/index.js';
import triggers from './triggers/index.js';
import actions from './actions/index.js';
import dynamicData from './dynamic-data/index.js';
export default defineApp({
name: 'GitHub',

View File

@@ -0,0 +1,6 @@
import newIssues from './new-issues/index.js';
import newPullRequests from './new-pull-requests/index.js';
import newStargazers from './new-stargazers/index.js';
import newWatchers from './new-watchers/index.js';
export default [newIssues, newPullRequests, newStargazers, newWatchers];

View File

@@ -1,6 +0,0 @@
import newIssues from './new-issues';
import newPullRequests from './new-pull-requests';
import newStargazers from './new-stargazers';
import newWatchers from './new-watchers';
export default [newIssues, newPullRequests, newStargazers, newWatchers];

View File

@@ -1,5 +1,5 @@
import defineTrigger from '../../../../helpers/define-trigger';
import newIssues from './new-issues';
import defineTrigger from '../../../../helpers/define-trigger.js';
import newIssues from './new-issues.js';
export default defineTrigger({
name: 'New issues',
@@ -10,7 +10,7 @@ export default defineTrigger({
{
label: 'Repo',
key: 'repo',
type: 'dropdown' as const,
type: 'dropdown',
required: false,
variables: false,
source: {
@@ -27,7 +27,7 @@ export default defineTrigger({
{
label: 'Which types of issues should this trigger on?',
key: 'issueType',
type: 'dropdown' as const,
type: 'dropdown',
description: 'Defaults to any issue you can see.',
required: true,
variables: false,
@@ -58,7 +58,7 @@ export default defineTrigger({
{
label: 'Label',
key: 'label',
type: 'dropdown' as const,
type: 'dropdown',
description: 'Only trigger on issues when this label is added.',
required: false,
variables: false,

View File

@@ -1,11 +1,8 @@
import { IGlobalVariable } from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo.js';
import parseLinkHeader from '../../../../helpers/parse-header-link.js';
function getPathname($: IGlobalVariable) {
const { repoOwner, repo } = getRepoOwnerAndRepo(
$.step.parameters.repo as string
);
function getPathname($) {
const { repoOwner, repo } = getRepoOwnerAndRepo($.step.parameters.repo);
if (repoOwner && repo) {
return `/repos/${repoOwner}/${repo}/issues`;
@@ -14,7 +11,7 @@ function getPathname($: IGlobalVariable) {
return '/issues';
}
const newIssues = async ($: IGlobalVariable) => {
const newIssues = async ($) => {
const pathname = getPathname($);
const params = {
labels: $.step.parameters.label,

View File

@@ -1,5 +1,5 @@
import defineTrigger from '../../../../helpers/define-trigger';
import newPullRequests from './new-pull-requests';
import defineTrigger from '../../../../helpers/define-trigger.js';
import newPullRequests from './new-pull-requests.js';
export default defineTrigger({
name: 'New pull requests',
@@ -10,7 +10,7 @@ export default defineTrigger({
{
label: 'Repo',
key: 'repo',
type: 'dropdown' as const,
type: 'dropdown',
required: true,
variables: false,
source: {

View File

@@ -1,9 +1,8 @@
import { IGlobalVariable } from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo.js';
import parseLinkHeader from '../../../../helpers/parse-header-link.js';
const newPullRequests = async ($: IGlobalVariable) => {
const repoParameter = $.step.parameters.repo as string;
const newPullRequests = async ($) => {
const repoParameter = $.step.parameters.repo;
if (!repoParameter) throw new Error('A repo must be set!');

View File

@@ -1,5 +1,5 @@
import defineTrigger from '../../../../helpers/define-trigger';
import newStargazers from './new-stargazers';
import defineTrigger from '../../../../helpers/define-trigger.js';
import newStargazers from './new-stargazers.js';
export default defineTrigger({
name: 'New stargazers',
@@ -10,7 +10,7 @@ export default defineTrigger({
{
label: 'Repo',
key: 'repo',
type: 'dropdown' as const,
type: 'dropdown',
required: true,
variables: false,
source: {

View File

@@ -1,17 +1,10 @@
import { DateTime } from 'luxon';
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link';
type TResponseDataItem = {
starred_at: string;
user: IJSONObject;
};
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo.js';
import parseLinkHeader from '../../../../helpers/parse-header-link.js';
const newStargazers = async ($: IGlobalVariable) => {
const { repoOwner, repo } = getRepoOwnerAndRepo(
$.step.parameters.repo as string
);
const newStargazers = async ($) => {
const { repoOwner, repo } = getRepoOwnerAndRepo($.step.parameters.repo);
const firstPagePathname = `/repos/${repoOwner}/${repo}/stargazers`;
const requestConfig = {
params: {
@@ -23,20 +16,14 @@ const newStargazers = async ($: IGlobalVariable) => {
},
};
const firstPageResponse = await $.http.get<TResponseDataItem[]>(
firstPagePathname,
requestConfig
);
const firstPageResponse = await $.http.get(firstPagePathname, requestConfig);
const firstPageLinks = parseLinkHeader(firstPageResponse.headers.link);
// in case there is only single page to fetch
let pathname = firstPageLinks.last?.uri || firstPagePathname;
do {
const response = await $.http.get<TResponseDataItem[]>(
pathname,
requestConfig
);
const response = await $.http.get(pathname, requestConfig);
const links = parseLinkHeader(response.headers.link);
pathname = links.prev?.uri;

View File

@@ -1,5 +1,5 @@
import defineTrigger from '../../../../helpers/define-trigger';
import newWatchers from './new-watchers';
import defineTrigger from '../../../../helpers/define-trigger.js';
import newWatchers from './new-watchers.js';
export default defineTrigger({
name: 'New watchers',
@@ -10,7 +10,7 @@ export default defineTrigger({
{
label: 'Repo',
key: 'repo',
type: 'dropdown' as const,
type: 'dropdown',
required: true,
variables: false,
source: {

View File

@@ -1,9 +1,8 @@
import { IGlobalVariable } from '@automatisch/types';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo';
import parseLinkHeader from '../../../../helpers/parse-header-link';
import getRepoOwnerAndRepo from '../../common/get-repo-owner-and-repo.js';
import parseLinkHeader from '../../../../helpers/parse-header-link.js';
const newWatchers = async ($: IGlobalVariable) => {
const repoParameter = $.step.parameters.repo as string;
const newWatchers = async ($) => {
const repoParameter = $.step.parameters.repo;
if (!repoParameter) throw new Error('A repo must be set!');