Compare commits

...

2 commits

Author SHA1 Message Date
7cdfafc329 Warn and give hint when using an already used repo for another work
All checks were successful
continuous-integration/drone/push Build is passing
2022-09-30 12:41:34 +02:00
569d44880c Allow the use of another secret defined for the same repo 2022-09-30 12:40:48 +02:00
4 changed files with 69 additions and 14 deletions

View file

@ -60,6 +60,13 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) {
for _, r := range repositories { for _, r := range repositories {
if r.IdWork == work.(*Work).Id { if r.IdWork == work.(*Work).Id {
// Is the URL used elsewhere?
repos, _ := getRepositoriesByURI(r.URI)
log.Println(repos)
if len(repos) > 1 {
r.AlreadyUsed = true
}
res = append(res, r) res = append(res, r)
} }
} }
@ -114,6 +121,13 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) {
repositoriesRoutes.GET("", func(c *gin.Context) { repositoriesRoutes.GET("", func(c *gin.Context) {
repo := c.MustGet("repository").(*Repository) repo := c.MustGet("repository").(*Repository)
// Is the URL used elsewhere?
repos, _ := getRepositoriesByURI(repo.URI)
log.Println(repos)
if len(repos) > 1 {
repo.AlreadyUsed = true
}
c.JSON(http.StatusOK, repo) c.JSON(http.StatusOK, repo)
}) })
repositoriesRoutes.PUT("", func(c *gin.Context) { repositoriesRoutes.PUT("", func(c *gin.Context) {
@ -264,7 +278,7 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
router.POST("/callbacks/trigger.json", func(c *gin.Context) { router.POST("/callbacks/trigger.json", func(c *gin.Context) {
// Check event type // Check event type
if c.Request.Header.Get("X-Gitlab-Event") != "Tag Push Hook" { if c.Request.Header.Get("X-Gitlab-Event") != "Tag Push Hook" {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "This trigger is limited to Tag Push event. Please edit your trigger."}) c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "This trigger is limited to Tag Push event. Please edit your trigger on GitLab."})
return return
} }
@ -284,7 +298,6 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
var repo *Repository var repo *Repository
for _, r := range repos { for _, r := range repos {
log.Println("Received trigger")
if len(r.Secret) == 0 || base64.StdEncoding.EncodeToString(r.Secret) == c.Request.Header.Get("X-Gitlab-Token") { if len(r.Secret) == 0 || base64.StdEncoding.EncodeToString(r.Secret) == c.Request.Header.Get("X-Gitlab-Token") {
repo = r repo = r
break break
@ -318,10 +331,29 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
return return
} }
if !strings.HasPrefix(tmp[2], work.Tag) {
// Allow to use a secret for another tag
if len(repos) > 1 {
for _, r := range repos {
w, err := getWork(int(r.IdWork))
if err != nil {
log.Println("Unable to getWork:", err.Error())
continue
}
if strings.HasPrefix(tmp[2], w.Tag) {
repo = r
work = w
break
}
}
}
if !strings.HasPrefix(tmp[2], work.Tag) { if !strings.HasPrefix(tmp[2], work.Tag) {
c.AbortWithStatusJSON(http.StatusOK, gin.H{"errmsg": fmt.Sprintf("Ignore ref %q has it doesn't start with %s. Check submission instructions if this is not expected.", tmp[2], work.Tag)}) c.AbortWithStatusJSON(http.StatusOK, gin.H{"errmsg": fmt.Sprintf("Ignore ref %q has it doesn't start with %s. Check submission instructions if this is not expected.", tmp[2], work.Tag)})
return return
} }
}
TriggerTagUpdate(c, work, repo, user, &tmp[2]) TriggerTagUpdate(c, work, repo, user, &tmp[2])
}) })
@ -406,6 +438,7 @@ type Repository struct {
Secret []byte `json:"secret,omitempty"` Secret []byte `json:"secret,omitempty"`
LastCheck *time.Time `json:"last_check"` LastCheck *time.Time `json:"last_check"`
DroneRef string `json:"drone_ref,omitempty"` DroneRef string `json:"drone_ref,omitempty"`
AlreadyUsed bool `json:"already_used,omitempty"`
} }
func (u *User) GetRepositories() (repositories []*Repository, err error) { func (u *User) GetRepositories() (repositories []*Repository, err error) {
@ -474,7 +507,7 @@ func (u *User) NewRepository(w *Work, uri string) (*Repository, error) {
} else if rid, err := res.LastInsertId(); err != nil { } else if rid, err := res.LastInsertId(); err != nil {
return nil, err return nil, err
} else { } else {
return &Repository{rid, u.Id, w.Id, uri, secret, nil, ""}, nil return &Repository{rid, u.Id, w.Id, uri, secret, nil, "", false}, nil
} }
} }

View file

@ -20,6 +20,7 @@
let remote_repos = []; let remote_repos = [];
let repo_pull_state = null; let repo_pull_state = null;
let show_logs = null; let show_logs = null;
export let already_used = false;
function updatePullState(repo) { function updatePullState(repo) {
repo_pull_state = repo.getBuildState(); repo_pull_state = repo.getBuildState();
@ -45,9 +46,11 @@
} }
repo_work.then((repos) => { repo_work.then((repos) => {
repo_used = repos[0]; repo_used = repos[0];
already_used = repos[0].already_used == true;
updatePullState(repos[0]) updatePullState(repos[0])
}, () => { }, () => {
repo_used = new WorkRepository({id_work: work.id}); repo_used = new WorkRepository({id_work: work.id});
already_used = false;
remote_repos = getRemoteRepositories(user?user.id:null); remote_repos = getRemoteRepositories(user?user.id:null);
}); });
} }

View file

@ -5,13 +5,14 @@ export class WorkRepository {
} }
} }
update({ id, id_user, id_work, uri, secret, last_check }) { update({ id, id_user, id_work, uri, secret, last_check, already_used }) {
this.id = id; this.id = id;
this.id_user = id_user; this.id_user = id_user;
this.id_work = id_work; this.id_work = id_work;
this.uri = uri; this.uri = uri;
this.secret = secret; this.secret = secret;
this.last_check = last_check; this.last_check = last_check;
this.already_used = already_used == true;
} }
async delete() { async delete() {

View file

@ -24,6 +24,7 @@
export let work = null; export let work = null;
let edit = false; let edit = false;
let my_submission = null; let my_submission = null;
let warn_already_used = false;
work.then((w) => { work.then((w) => {
refresh_submission(w); refresh_submission(w);
@ -159,7 +160,7 @@
{/if} {/if}
{#if w.submission_url != "-"} {#if w.submission_url != "-"}
<WorkRepository class="mb-3" readonly={w.corrected || new Date(w.end_availability) <= new Date()} work={w} on:update_submission={() => refresh_submission(w)} /> <WorkRepository class="mb-3" readonly={w.corrected || new Date(w.end_availability) <= new Date()} work={w} on:update_submission={() => refresh_submission(w)} bind:already_used={warn_already_used} />
<div class="card mb-3"> <div class="card mb-3">
<div class="card-body d-flex justify-content-between"> <div class="card-body d-flex justify-content-between">
<div> <div>
@ -211,6 +212,23 @@
</div> </div>
{/await} {/await}
{:else if w.submission_url != "-"} {:else if w.submission_url != "-"}
{#if warn_already_used}
<div class="alert alert-danger">
<strong>Vous avez déjà utilisé ce dépôt pour rendre un autre travail.</strong> Pour conserver ce que vous avez fait, tout en respectant l'arborescence de rendu attendue, vous devriez partir d'une nouvelle branche vide&nbsp;:
<pre class="mx-2 mt-1 mb-2">
42sh$ git checkout --orphan renduX
42sh$ git reset
42sh$ rm -r *
# Créez l'arborescence de rendu ensuite</pre>
Pour retrouver ensuite vos rendus des travaux précédents&nbsp;:
<pre class="mx-2 my-1">
42sh$ git checkout renduY
-- ou --
42sh$ git checkout master
</pre>
</div>
{/if}
<div class="alert alert-warning"> <div class="alert alert-warning">
Pour être reconnu, vous devez pousser un tag <strong><a href="keys">signé</a></strong> sur votre dépôt. {#if w.tag}Le tag attendu doit commencer par&nbsp;: <code>{w.tag}</code>. Par exemple <code>{w.tag}v1.0</code>, <code>{w.tag}v1.1</code>, &hellip;{/if} Seul le dernier tag <strong>alphabétique</strong> que vous envoyez avant la date du rendu sera pris en compte. Vous pouvez donc faire autant de tag que vous le souhaitez d'ici la date du rendu. Pour être reconnu, vous devez pousser un tag <strong><a href="keys">signé</a></strong> sur votre dépôt. {#if w.tag}Le tag attendu doit commencer par&nbsp;: <code>{w.tag}</code>. Par exemple <code>{w.tag}v1.0</code>, <code>{w.tag}v1.1</code>, &hellip;{/if} Seul le dernier tag <strong>alphabétique</strong> que vous envoyez avant la date du rendu sera pris en compte. Vous pouvez donc faire autant de tag que vous le souhaitez d'ici la date du rendu.
</div> </div>